001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.platform.commandline.executor.api;
022
023import java.io.Serializable;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
027
028/**
029 * Wraps result of the command-line execution:
030 * <ul>
031 * <li>executed command line,
032 * <li>output buffer,
033 * <li>return code,
034 * <li>java Exception.
035 * </ul>
036 *
037 * @author tiry
038 */
039public class ExecResult implements Serializable {
040
041    private static final long serialVersionUID = 1L;
042
043    protected final String commandLine;
044
045    protected final List<String> output;
046
047    protected final long execTime;
048
049    protected boolean success;
050
051    protected final CommandException error;
052
053    protected int returnCode;
054
055    public ExecResult(String commandLine, List<String> output, long execTime, int returnCode) {
056        this.commandLine = commandLine;
057        this.execTime = execTime;
058        this.output = output;
059        this.returnCode = returnCode;
060        success = returnCode == 0;
061        if (!success) {
062            error = new CommandException(String.format("Error code %d return by command: %s\n%s", returnCode,
063                    commandLine, StringUtils.join(output, "\n  ")));
064        } else {
065            error = null;
066        }
067    }
068
069    public ExecResult(String commandLine, Exception error) {
070        this.commandLine = commandLine;
071        execTime = 0;
072        output = null;
073        returnCode = 1;
074        success = false;
075        this.error = new CommandException(String.format("Error while running command: %s", commandLine), error);
076    }
077
078    public List<String> getOutput() {
079        return output;
080    }
081
082    public long getExecTime() {
083        return execTime;
084    }
085
086    public boolean isSuccessful() {
087        return success;
088    }
089
090    /**
091     * Rather rely on {@link #isSuccessful()} to check for the execution success. Note however that since 5.7.3, the
092     * {@link #getError()} method cannot return null even if the execution failed (it was not the case before).
093     *
094     * @return CommandException attached to the result, optionally wrapping the root cause.
095     */
096    public CommandException getError() {
097        return error;
098    }
099
100    public int getReturnCode() {
101        return returnCode;
102    }
103
104    /**
105     * @since 5.5
106     * @return the executed command line
107     */
108    public String getCommandLine() {
109        return commandLine;
110    }
111
112}