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