001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.commandline.executor.api; 020 021import java.io.Serializable; 022import java.util.List; 023 024import org.apache.commons.lang.StringUtils; 025 026/** 027 * Wraps result of the command-line execution: 028 * <ul> 029 * <li>executed command line, 030 * <li>output buffer, 031 * <li>return code, 032 * <li>java Exception. 033 * </ul> 034 * 035 * @author tiry 036 */ 037public class ExecResult implements Serializable { 038 039 private static final long serialVersionUID = 1L; 040 041 protected final String commandLine; 042 043 protected final List<String> output; 044 045 protected final long execTime; 046 047 protected boolean success; 048 049 protected final CommandException error; 050 051 protected int returnCode; 052 053 public ExecResult(String commandLine, List<String> output, long execTime, int returnCode) { 054 this.commandLine = commandLine; 055 this.execTime = execTime; 056 this.output = output; 057 this.returnCode = returnCode; 058 success = returnCode == 0; 059 if (!success) { 060 error = new CommandException(String.format("Error code %d return by command: %s\n%s", returnCode, 061 commandLine, StringUtils.join(output, "\n "))); 062 } else { 063 error = null; 064 } 065 } 066 067 public ExecResult(String commandLine, Exception error) { 068 this.commandLine = commandLine; 069 execTime = 0; 070 output = null; 071 returnCode = 1; 072 success = false; 073 this.error = new CommandException(String.format("Error while running command: %s", commandLine), error); 074 } 075 076 public List<String> getOutput() { 077 return output; 078 } 079 080 public long getExecTime() { 081 return execTime; 082 } 083 084 public boolean isSuccessful() { 085 return success; 086 } 087 088 /** 089 * Rather rely on {@link #isSuccessful()} to check for the execution success. Note however that since 5.7.3, the 090 * {@link #getError()} method cannot return null even if the execution failed (it was not the case before). 091 * 092 * @return CommandException attached to the result, optionally wrapping the root cause. 093 */ 094 public CommandException getError() { 095 return error; 096 } 097 098 public int getReturnCode() { 099 return returnCode; 100 } 101 102 /** 103 * @since 5.5 104 * @return the executed command line 105 */ 106 public String getCommandLine() { 107 return commandLine; 108 } 109 110}