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("control panel at: http:
047 sb.append("manage/-/server/external-services");
048
049 throw new IllegalStateException(sb.toString());
050 }
051
052 LinkedList<String> arguments = new LinkedList<String>();
053
054 arguments.add(_commandPath);
055 arguments.add("-dBATCH");
056 arguments.add("-dSAFER");
057 arguments.add("-dNOPAUSE");
058 arguments.add("-dNOPROMPT");
059 arguments.add("-sFONTPATH" + _globalSearchPath);
060 arguments.addAll(commandArguments);
061
062 if (_log.isInfoEnabled()) {
063 StringBundler sb = new StringBundler(arguments.size() * 2);
064
065 for (String argument : arguments) {
066 sb.append(argument);
067 sb.append(StringPool.SPACE);
068 }
069
070 _log.info("Excecuting command '" + sb.toString() + "'");
071 }
072
073 return ProcessUtil.execute(
074 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, arguments);
075 }
076
077 public boolean isEnabled() {
078 return ImageMagickUtil.isEnabled();
079 }
080
081 public void reset() {
082 if (isEnabled()) {
083 try {
084 _globalSearchPath = ImageMagickUtil.getGlobalSearchPath();
085
086 _commandPath = getCommandPath();
087 }
088 catch (Exception e) {
089 _log.warn(e, e);
090 }
091 }
092 }
093
094 protected String getCommandPath() throws Exception {
095 String commandPath = null;
096
097 if (OSDetector.isWindows()) {
098 commandPath = getCommandPathWindows();
099 }
100 else {
101 commandPath = getCommandPathUnix();
102 }
103
104 if (commandPath == null) {
105 throw new FileNotFoundException(
106 "Unable to find Ghostscript command");
107 }
108
109 return commandPath;
110 }
111
112 protected String getCommandPathUnix() throws Exception {
113 String[] dirNames = _globalSearchPath.split(File.pathSeparator);
114
115 for (String dirName : dirNames) {
116 File file = new File(dirName, _GHOSTSCRIPT_COMMAND_UNIX);
117
118 if (file.exists()) {
119 return file.getCanonicalPath();
120 }
121 }
122
123 return null;
124 }
125
126 protected String getCommandPathWindows() throws Exception {
127 String[] dirNames = _globalSearchPath.split(File.pathSeparator);
128
129 for (String dirName : dirNames) {
130 for (String command : _GHOSTSCRIPT_COMMAND_WINDOWS) {
131 File file = new File(dirName, command + ".exe");
132
133 if (!file.exists()) {
134 file = new File(dirName, command + ".cmd");
135
136 if (!file.exists()) {
137 file = new File(dirName, command + ".bat");
138
139 if (!file.exists()) {
140 continue;
141 }
142 }
143 }
144
145 return file.getCanonicalPath();
146 }
147 }
148
149 return null;
150 }
151
152 private static final String _GHOSTSCRIPT_COMMAND_UNIX = "gs";
153
154 private static final String[] _GHOSTSCRIPT_COMMAND_WINDOWS = {
155 "gswin32c", "gswin64c"
156 };
157
158 private static Log _log = LogFactoryUtil.getLog(GhostscriptImpl.class);
159
160 private String _commandPath;
161 private String _globalSearchPath;
162
163 }