001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    import com.liferay.portal.kernel.util.StringPool;
025    
026    import java.io.File;
027    import java.io.FileNotFoundException;
028    
029    import java.util.LinkedList;
030    import java.util.List;
031    import java.util.concurrent.Future;
032    
033    /**
034     * @author Ivica Cardic
035     */
036    public class GhostscriptImpl implements Ghostscript {
037    
038            @Override
039            public Future<?> execute(List<String> commandArguments) throws Exception {
040                    if (!isEnabled()) {
041                            StringBundler sb = new StringBundler(6);
042    
043                            sb.append("Cannot execute the Ghostscript command. Please ");
044                            sb.append("install ImageMagick and Ghostscript and enable ");
045                            sb.append("ImageMagick in portal-ext.properties or in the Server ");
046                            sb.append("Administration section of the Control Panel at: ");
047                            sb.append("http://<server>/group/control_panel/manage/-/server/");
048                            sb.append("external-services");
049    
050                            throw new IllegalStateException(sb.toString());
051                    }
052    
053                    LinkedList<String> arguments = new LinkedList<>();
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            @Override
079            public boolean isEnabled() {
080                    return ImageMagickUtil.isEnabled();
081            }
082    
083            @Override
084            public void reset() {
085                    if (isEnabled()) {
086                            try {
087                                    _globalSearchPath = ImageMagickUtil.getGlobalSearchPath();
088    
089                                    _commandPath = getCommandPath();
090                            }
091                            catch (Exception e) {
092                                    _log.error(e, e);
093                            }
094                    }
095            }
096    
097            protected String getCommandPath() throws Exception {
098                    String commandPath = null;
099    
100                    if (OSDetector.isWindows()) {
101                            commandPath = getCommandPathWindows();
102                    }
103                    else {
104                            commandPath = getCommandPathUnix();
105                    }
106    
107                    if (commandPath == null) {
108                            StringBundler sb = new StringBundler(4);
109    
110                            sb.append("Unable to find the Ghostscript command. Please verify ");
111                            sb.append("the path specified in the Server Administration ");
112                            sb.append("control panel at: http://<server>/group/control_panel/");
113                            sb.append("manage/-/server/external-services");
114    
115                            throw new FileNotFoundException(sb.toString());
116                    }
117    
118                    return commandPath;
119            }
120    
121            protected String getCommandPathUnix() throws Exception {
122                    String[] dirNames = _globalSearchPath.split(File.pathSeparator);
123    
124                    for (String dirName : dirNames) {
125                            File file = new File(dirName, _GHOSTSCRIPT_COMMAND_UNIX);
126    
127                            if (file.exists()) {
128                                    return file.getCanonicalPath();
129                            }
130                    }
131    
132                    return null;
133            }
134    
135            protected String getCommandPathWindows() throws Exception {
136                    String[] dirNames = _globalSearchPath.split(File.pathSeparator);
137    
138                    for (String dirName : dirNames) {
139                            for (String command : _GHOSTSCRIPT_COMMAND_WINDOWS) {
140                                    File file = new File(dirName, command + ".exe");
141    
142                                    if (!file.exists()) {
143                                            file = new File(dirName, command + ".cmd");
144    
145                                            if (!file.exists()) {
146                                                    file = new File(dirName, command + ".bat");
147    
148                                                    if (!file.exists()) {
149                                                            continue;
150                                                    }
151                                            }
152                                    }
153    
154                                    return file.getCanonicalPath();
155                            }
156                    }
157    
158                    return null;
159            }
160    
161            private static final String _GHOSTSCRIPT_COMMAND_UNIX = "gs";
162    
163            private static final String[] _GHOSTSCRIPT_COMMAND_WINDOWS = {
164                    "gswin32c", "gswin64c"
165            };
166    
167            private static final Log _log = LogFactoryUtil.getLog(
168                    GhostscriptImpl.class);
169    
170            private String _commandPath;
171            private String _globalSearchPath;
172    
173    }