View Javadoc

1   /*
2    * Copyright 2016-2016 Hippo B.V. (http://www.onehippo.com)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.onehippo.forge.gallerymagick.core.command;
17  
18  import org.apache.commons.exec.CommandLine;
19  import org.apache.commons.lang.StringUtils;
20  
21  /**
22   * Encapsulation of <a href="http://www.imagemagick.org/">Image Magick Command Utilities</a>.
23   */
24  public class ImageMagickCommand extends AbstractMagickCommand {
25  
26      /**
27       * System property name for Image Magick command executable, convert.
28       */
29      public static final String PROP_EXECUTABLE_CONVERT = "org.onehippo.forge.gallerymagick.core.command.im.convert";
30  
31      /**
32       * System property name for Image Magick command executable, identify.
33       */
34      public static final String PROP_EXECUTABLE_IDENTIFY = "org.onehippo.forge.gallerymagick.core.command.im.identify";
35  
36      /**
37       * Constructor with an {@code executable} and a {@code subCommand}.
38       * If {@code executable} is null, it tries to find it from a system property keyed by {@link #PROP_EXECUTABLE_CONVERT}.
39       * If not found from the system property, it uses the default Image Magick convert command executable, {@link #DEFAULT_SUBCOMMAND_CONVERT}.
40       * @param executable (optional) executable of ImageMagick command
41       * @param subCommand sub-command
42       */
43      public ImageMagickCommand(final String executable, final String subCommand) {
44          super(StringUtils.defaultIfBlank(getExecutableFromSystemProperty(subCommand), executable),
45                  StringUtils.defaultIfBlank(subCommand, DEFAULT_SUBCOMMAND_CONVERT));
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      protected CommandLine createCommandLine() {
53          String executable = getExecutable();
54  
55          if (StringUtils.isBlank(executable)) {
56              executable = getSubCommand();
57          }
58  
59          CommandLine cmdLine = new CommandLine(executable);
60  
61          for (String argument : getArguments()) {
62              cmdLine.addArgument(argument);
63          }
64  
65          return cmdLine;
66      }
67  
68      /**
69       * Finds the Image Magick command executable by finding it from the system property, {@link #PROP_EXECUTABLE_CONVERT},
70       * and returns it if found.
71       * @return Image Magick command executable
72       */
73      static String getExecutableFromSystemProperty(final String subCommand) {
74          String executable = null;
75  
76          if (StringUtils.equals("convert", subCommand)) {
77              final String value = System.getProperty(PROP_EXECUTABLE_CONVERT);
78              if (StringUtils.isNotBlank(value)) {
79                  executable = value;
80              }
81          } else if (StringUtils.equals("identify", subCommand)) {
82              final String value = System.getProperty(PROP_EXECUTABLE_IDENTIFY);
83              if (StringUtils.isNotBlank(value)) {
84                  executable = value;
85              }
86          }
87  
88          return executable;
89      }
90  
91  }