001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.service;
017    
018    import java.util.List;
019    
020    import org.codehaus.plexus.util.cli.CommandLineException;
021    import org.codehaus.plexus.util.cli.CommandLineUtils;
022    import org.codehaus.plexus.util.cli.Commandline;
023    import org.codehaus.plexus.util.cli.StreamConsumer;
024    import org.kuali.common.util.CollectionUtils;
025    import org.kuali.common.util.LoggerLevel;
026    import org.kuali.common.util.LoggingStreamConsumer;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    public class DefaultExecService implements ExecService {
031    
032            private static final Logger logger = LoggerFactory.getLogger(DefaultExecService.class);
033    
034            @Override
035            public int execute(ExecContext context) {
036                    Commandline cl = getCommandLine(context);
037                    return execute(context, cl);
038            }
039    
040            @Override
041            public int execute(String executable, List<String> args) {
042                    DefaultExecContext context = new DefaultExecContext();
043                    context.setExecutable(executable);
044                    context.setArgs(args);
045                    return execute(context);
046            }
047    
048            protected int executeAndValidate(String executable, List<String> args) {
049                    int exitValue = execute(executable, args);
050                    validateExitValue(exitValue);
051                    return exitValue;
052            }
053    
054            protected int executeAndValidate(ExecContext context) {
055                    int exitValue = execute(context);
056                    validateExitValue(exitValue);
057                    return exitValue;
058            }
059    
060            protected int execute(ExecContext context, Commandline cl) {
061                    try {
062                            logger.debug("[{}]", cl);
063                            StreamConsumer stdout = getStreamConsumer(context.getStandardOutConsumer(), logger, LoggerLevel.INFO);
064                            StreamConsumer stderr = getStreamConsumer(context.getStandardErrConsumer(), logger, LoggerLevel.WARN);
065                            return CommandLineUtils.executeCommandLine(cl, context.getInput(), stdout, stderr, context.getTimeoutInSeconds());
066                    } catch (CommandLineException e) {
067                            throw new IllegalStateException(e);
068                    }
069            }
070    
071            protected StreamConsumer getStreamConsumer(StreamConsumer provided, Logger logger, LoggerLevel level) {
072                    if (provided != null) {
073                            return provided;
074                    } else {
075                            return new LoggingStreamConsumer(logger, level);
076                    }
077            }
078    
079            protected void validateExitValue(int exitValue) {
080                    if (exitValue != 0) {
081                            throw new IllegalStateException("Non-zero exit value - " + exitValue);
082                    }
083            }
084    
085            protected Commandline getCommandLine(ExecContext context) {
086                    Commandline cl = new Commandline();
087                    cl.setExecutable(context.getExecutable());
088                    if (context.isAddSystemEnvironment()) {
089                            try {
090                                    cl.addSystemEnvironment();
091                            } catch (Exception e) {
092                                    throw new IllegalStateException(e);
093                            }
094                    }
095                    if (context.getArgs() != null) {
096                            cl.addArguments(CollectionUtils.toStringArray(context.getArgs()));
097                    }
098                    if (context.getWorkingDirectory() != null) {
099                            cl.setWorkingDirectory(context.getWorkingDirectory());
100                    }
101                    return cl;
102            }
103    
104    }