001
014
015 package com.liferay.portal.image;
016
017 import com.liferay.portal.kernel.image.Ghostscript;
018 import com.liferay.portal.kernel.image.ImageMagickUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.process.ProcessUtil;
022 import com.liferay.portal.kernel.util.OSDetector;
023 import com.liferay.portal.kernel.util.StringBundler;
024
025 import java.io.File;
026 import java.io.FileNotFoundException;
027
028 import java.util.LinkedList;
029 import java.util.List;
030 import java.util.concurrent.Future;
031
032 import jodd.util.StringPool;
033
034
037 public class GhostscriptImpl implements Ghostscript {
038
039 public Future<?> execute(List<String> commandArguments) throws Exception {
040 if (!isEnabled()) {
041 StringBundler sb = new StringBundler(5);
042
043 sb.append("Cannot execute the Ghostscript command. Please install");
044 sb.append("ImageMagick and Ghostscript and enable ImageMagick in ");
045 sb.append("portal-ext.properties or in the Server Administration ");
046 sb.append("section of the Control Panel at: ");
047 sb.append("http:
048 sb.append("external-services");
049
050 throw new IllegalStateException(sb.toString());
051 }
052
053 LinkedList<String> arguments = new LinkedList<String>();
054
055 arguments.add(_commandPath);
056 arguments.add("-dBATCH");
057 arguments.add("-dSAFER");
058 arguments.add("-dNOPAUSE");
059 arguments.add("-dNOPROMPT");
060 arguments.add("-sFONTPATH" + _globalSearchPath);
061 arguments.addAll(commandArguments);
062
063 if (_log.isInfoEnabled()) {
064 StringBundler sb = new StringBundler(arguments.size() * 2);
065
066 for (String argument : arguments) {
067 sb.append(argument);
068 sb.append(StringPool.SPACE);
069 }
070
071 _log.info("Excecuting command '" + sb.toString() + "'");
072 }
073
074 return ProcessUtil.execute(
075 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, arguments);
076 }
077
078 public boolean isEnabled() {
079 return ImageMagickUtil.isEnabled();
080 }
081
082 public void reset() {
083 if (isEnabled()) {
084 try {
085 _globalSearchPath = ImageMagickUtil.getGlobalSearchPath();
086
087 _commandPath = getCommandPath();
088 }
089 catch (Exception e) {
090 _log.error(e, e);
091 }
092 }
093 }
094
095 protected String getCommandPath() throws Exception {
096 String commandPath = null;
097
098 if (OSDetector.isWindows()) {
099 commandPath = getCommandPathWindows();
100 }
101 else {
102 commandPath = getCommandPathUnix();
103 }
104
105 if (commandPath == null) {
106 StringBundler sb = new StringBundler(4);
107
108 sb.append("Unable to find the Ghostscript command. Please verify ");
109 sb.append("the path specified in the Server Administration ");
110 sb.append("control panel at: http:
111 sb.append("manage/-/server/external-services");
112
113 throw new FileNotFoundException(sb.toString());
114 }
115
116 return commandPath;
117 }
118
119 protected String getCommandPathUnix() throws Exception {
120 String[] dirNames = _globalSearchPath.split(File.pathSeparator);
121
122 for (String dirName : dirNames) {
123 File file = new File(dirName, _GHOSTSCRIPT_COMMAND_UNIX);
124
125 if (file.exists()) {
126 return file.getCanonicalPath();
127 }
128 }
129
130 return null;
131 }
132
133 protected String getCommandPathWindows() throws Exception {
134 String[] dirNames = _globalSearchPath.split(File.pathSeparator);
135
136 for (String dirName : dirNames) {
137 for (String command : _GHOSTSCRIPT_COMMAND_WINDOWS) {
138 File file = new File(dirName, command + ".exe");
139
140 if (!file.exists()) {
141 file = new File(dirName, command + ".cmd");
142
143 if (!file.exists()) {
144 file = new File(dirName, command + ".bat");
145
146 if (!file.exists()) {
147 continue;
148 }
149 }
150 }
151
152 return file.getCanonicalPath();
153 }
154 }
155
156 return null;
157 }
158
159 private static final String _GHOSTSCRIPT_COMMAND_UNIX = "gs";
160
161 private static final String[] _GHOSTSCRIPT_COMMAND_WINDOWS = {
162 "gswin32c", "gswin64c"
163 };
164
165 private static Log _log = LogFactoryUtil.getLog(GhostscriptImpl.class);
166
167 private String _commandPath;
168 private String _globalSearchPath;
169
170 }