001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.exec.impl;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.ByteArrayOutputStream;
021    import java.io.File;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.util.List;
025    
026    import org.apache.camel.component.exec.ExecCommand;
027    import org.apache.camel.component.exec.ExecCommandExecutor;
028    import org.apache.camel.component.exec.ExecEndpoint;
029    import org.apache.camel.component.exec.ExecException;
030    import org.apache.camel.component.exec.ExecResult;
031    import org.apache.commons.exec.CommandLine;
032    import org.apache.commons.exec.DefaultExecutor;
033    import org.apache.commons.exec.ExecuteException;
034    import org.apache.commons.exec.ExecuteWatchdog;
035    import org.apache.commons.exec.PumpStreamHandler;
036    import org.apache.commons.exec.ShutdownHookProcessDestroyer;
037    import org.apache.commons.io.IOUtils;
038    import org.apache.commons.logging.Log;
039    import org.apache.commons.logging.LogFactory;
040    
041    import static org.apache.camel.util.ObjectHelper.notNull;
042    
043    /**
044     * Executes the command utilizing the <a
045     * href="http://commons.apache.org/exec/">Apache Commons exec library</a>. Adds
046     * a shutdown hook for every executed process.
047     */
048    public class DefaultExecCommandExecutor implements ExecCommandExecutor {
049    
050        private static final Log LOG = LogFactory.getLog(DefaultExecCommandExecutor.class);
051    
052        public ExecResult execute(ExecCommand command) {
053            notNull(command, "command");
054    
055            ByteArrayOutputStream out = new ByteArrayOutputStream();
056            ByteArrayOutputStream err = new ByteArrayOutputStream();
057    
058            DefaultExecutor executor = prepareDefaultExecutor(command);
059            // handle error and output of the process and write them to the given
060            // out stream
061            PumpStreamHandler handler = new PumpStreamHandler(out, err, command.getInput());
062            executor.setStreamHandler(handler);
063    
064            CommandLine cl = toCommandLine(command);
065    
066            try {
067                int exitValue = executor.execute(cl);
068                // if the size is zero, we have no output, so construct the result
069                // with null (required by ExecResult)
070                InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
071                InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
072                ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
073                return result;
074    
075            } catch (ExecuteException ee) {
076                LOG.error("ExecException while executing command: " + command.toString() + " - " + ee.getMessage());
077                throw new ExecException("Failed to execute command " + command, ee);
078            } catch (IOException ioe) {
079                // invalid working dir
080                LOG.error("IOException while executing command: " + command.toString() + " - " + ioe.getMessage());
081                throw new ExecException("Unable to execute command " + command, ioe);
082            } finally {
083                // the inputStream must be closed after the execution
084                IOUtils.closeQuietly(command.getInput());
085            }
086        }
087    
088        protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
089            DefaultExecutor executor = new DefaultExecutor();
090            executor.setExitValues(null);
091    
092            if (execCommand.getWorkingDir() != null) {
093                executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
094            }
095            if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
096                executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
097            }
098            executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
099            return executor;
100        }
101    
102        /**
103         * Transforms an {@link ExecCommand} to a {@link CommandLine}. No quoting fo
104         * the arguments is used.
105         * 
106         * @param execCommand a not-null <code>ExecCommand</code> instance.
107         * @return a {@link CommandLine} object.
108         */
109        protected CommandLine toCommandLine(ExecCommand execCommand) {
110            notNull(execCommand, "execCommand");
111            CommandLine cl = new CommandLine(execCommand.getExecutable());
112            List<String> args = execCommand.getArgs();
113            for (String arg : args) {
114                // do not handle quoting here, it is already quoted
115                cl.addArgument(arg, false);
116            }
117            return cl;
118        }
119    }