001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2003 Tino Schwarze
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.installer;
023
024/**
025 * This class describes the result of the compilation.
026 * 
027 * This class is here because error handling is not straight-forward with regard to compilation.
028 * 
029 * The error condition consists of an error message, the full command line which failed to execute
030 * plus it's stdout and stderr. The reason for this class to exist is that there are three possible
031 * reactions to the error (chosen by the user).
032 * <ol>
033 * <li>abort</li>
034 * <li>ignore (continue anyway)</li>
035 * <li>reconfigure</li>
036 * </ol>
037 * 
038 * @author Tino Schwarze
039 */
040public class CompileResult
041{
042
043    // -------- public constants ---------------
044    // arbitrary values
045    public final static int SUCCESS = 42;
046
047    public final static int FAILED = 23;
048
049    public final static int ACTION_ABORT = 27;
050
051    public final static int ACTION_CONTINUE = 39;
052
053    public final static int ACTION_RECONFIGURE = 31;
054
055    // -------- private variables ---------------
056    // we're optimistic...
057    private int status = SUCCESS;
058
059    // here we're pessimistic
060    private int action = ACTION_ABORT;
061
062    /** the error message */
063    private String message = null;
064
065    /** the command line */
066    private String[] cmdline = null;
067
068    /** the stdout of the command */
069    private String stdout = null;
070
071    /** the stderr of the command */
072    private String stderr = null;
073
074    /** constructor, create a new successful result */
075    public CompileResult()
076    {
077        this.status = SUCCESS;
078        this.action = ACTION_CONTINUE;
079    }
080
081    /**
082     * creates a new CompileResult with status FAILED
083     * 
084     * @param message description of the exception
085     * @param cmdline full command line of failed command
086     * @param stdout standard output of failed command
087     * @param stderr standard error of failed command
088     */
089    public CompileResult(String message, String[] cmdline, String stdout, String stderr)
090    {
091        this.message = message;
092        this.status = FAILED;
093        this.cmdline = cmdline;
094        this.stdout = stdout;
095        this.stderr = stderr;
096    }
097
098    public void setStatus(int status)
099    {
100        if ((status == SUCCESS) || (status == FAILED))
101        {
102            this.status = status;
103        }
104    }
105
106    public int getStatus()
107    {
108        return this.status;
109    }
110
111    public void setAction(int action)
112    {
113        if ((action == ACTION_ABORT) || (action == ACTION_CONTINUE)
114                || (action == ACTION_RECONFIGURE))
115        {
116            this.action = action;
117        }
118
119    }
120
121    public int getAction()
122    {
123        return this.action;
124    }
125
126    /** check for success (convenience function) */
127    public boolean isSuccess()
128    {
129        return (this.status == SUCCESS);
130    }
131
132    /** check whether to abort (convenience function) */
133    public boolean isAbort()
134    {
135        return ((this.status == FAILED) && (this.action == ACTION_ABORT));
136    }
137
138    /**
139     * check whether to continue (convenience function)
140     * 
141     * @return true if status is SUCCESS or action is CONTINUE
142     */
143    public boolean isContinue()
144    {
145        return ((this.status == SUCCESS) || (this.action == ACTION_CONTINUE));
146    }
147
148    /** check whether to reconfigure (convenience function) */
149    public boolean isReconfigure()
150    {
151        return ((this.status == FAILED) && (this.action == ACTION_RECONFIGURE));
152    }
153
154    /**
155     * return error message
156     * 
157     * @return the error message describing the action that failed (might be null)
158     */
159    public String getMessage()
160    {
161        return this.message;
162    }
163
164    /**
165     * get command line of failed command as a string
166     * 
167     * @return command line of failed command
168     */
169    public String getCmdline()
170    {
171        StringBuffer sb = new StringBuffer();
172        for (int i = 0; i < this.cmdline.length; ++i)
173        {
174            if (sb.length() > 0) sb.append(' ');
175            sb.append(this.cmdline[i]);
176        }
177        return sb.toString();
178    }
179
180    /**
181     * get command line of failed command as an array of strings
182     * 
183     * @return command line of failed command
184     */
185    public String[] getCmdlineArray()
186    {
187        return this.cmdline;
188    }
189
190    public String getStdout()
191    {
192        return this.stdout;
193    }
194
195    public String getStderr()
196    {
197        return this.stderr;
198    }
199
200}