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.graphicsmagick.org/">Graphics Magick Command Utilities</a>. 23 */ 24 public class GraphicsMagickCommand extends AbstractMagickCommand { 25 26 /** 27 * System property name for Graphics Magick command executable. 28 * The default value is {@link #DEFAULT_EXECUTABLE}. 29 */ 30 public static final String PROP_EXECUTABLE = "org.onehippo.forge.gallerymagick.core.command.gm"; 31 32 /** 33 * Default Graphics Magick command executable. 34 */ 35 public static final String DEFAULT_EXECUTABLE = "gm"; 36 37 /** 38 * Constructor with an {@code executable} and a {@code subCommand}. 39 * If {@code executable} is null, it tries to find it from a system property keyed by {@link #PROP_EXECUTABLE}. 40 * If not found from the system property, it uses the default Graphics Magick executable, {@link #DEFAULT_EXECUTABLE}. 41 * @param executable executable of Graphics Magick command 42 * @param subCommand sub-command of <code>gm</code> command 43 */ 44 public GraphicsMagickCommand(final String executable, final String subCommand) { 45 super(StringUtils.defaultIfBlank(getExecutableFromSystemProperty(), DEFAULT_EXECUTABLE), 46 StringUtils.defaultIfBlank(subCommand, DEFAULT_SUBCOMMAND_CONVERT)); 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 @Override 53 protected CommandLine createCommandLine() { 54 CommandLine cmdLine = new CommandLine(getExecutable()); 55 56 cmdLine.addArgument(getSubCommand()); 57 58 for (String argument : getArguments()) { 59 cmdLine.addArgument(argument); 60 } 61 62 return cmdLine; 63 } 64 65 /** 66 * Finds the Graphics Magick command executable by finding it from the system property, {@link #PROP_EXECUTABLE}, 67 * and returns it if found. 68 * @return Graphics Magick command executable 69 */ 70 static String getExecutableFromSystemProperty() { 71 String executable = null; 72 final String value = System.getProperty(PROP_EXECUTABLE); 73 74 if (StringUtils.isNotBlank(value)) { 75 executable = value; 76 } 77 78 return executable; 79 } 80 81 }