View Javadoc

1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   // this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  // this list of conditions and the following disclaimer in the documentation  
12  // and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  // nor the names of its contributors may be used to endorse or promote products  
15  // derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.external;
30  
31  import java.io.BufferedInputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.IOException;
35  import java.io.UnsupportedEncodingException;
36  
37  import org.objectledge.utils.StringUtils;
38  
39  /***
40   * Represents the result of script execution.
41   *
42   * @author <a href="rafal@caltha.pl">RafaƂ Krzewski</a>
43   * @version $Id: ExecutionResult.java,v 1.1 2006/03/24 14:27:37 rafal Exp $
44   */
45  public class ExecutionResult
46  {
47      private static final int BUFFER_SIZE = 65536;
48      private final int exitValue;
49      private final byte[] output;
50      private final byte[] error;
51  
52      /***
53       * Creates a new ExecutionResult instance.
54       *
55       * @param exitValue the exit value of the process.
56       * @param captured output stream the process, or null if not available.
57       * @param captured error stream the process, or null if not available.
58       */
59      public ExecutionResult(int exitValue, byte[] output, byte[] error)
60      {
61          this.exitValue = exitValue;
62          this.output = output;
63          this.error = error;        
64      }
65      
66      /***
67       * Creates a new ExecutionResult instance.
68       *
69       * @param exitValue process exit value.
70       */
71      public ExecutionResult(int exitValue)
72      {
73          this.exitValue = exitValue;
74          this.output = null;
75          this.error = null;
76      }
77  
78      /***
79       * Returns the process exit value.
80       * 
81       * @return the process exit value.
82       */
83      public int getExitValue()
84      {
85          return exitValue;
86      }
87      
88      /***
89       * Returns the data captured from process output stream as bytes.
90       * 
91       * @return the data captured from process output stream as bytes, or null if not available.
92       */
93      public byte[] getOutputBytes()
94      {
95          return output;
96      }
97      
98      /***
99       * Returns the data captured from process output stream as a string.
100      * 
101      * @param charset output stream encoding.
102      * @return the data captured from process output stream as a string, or null if not available.
103      * @throws UnsupportedEncodingException if the encoding is not supported by the VM.
104      */
105     public String getOutput(String charset) throws UnsupportedEncodingException
106     {
107         if(output == null)
108         {
109             return null;
110         }
111         return new String(output, charset);
112     }
113     
114     /***
115      * Returns the data captured from process output stream as an UTF-8 string.
116      * 
117      * @return the data captured from process output stream as an UTF-8 string, or null if not available.
118      */
119     public String getOutput()
120     {
121         return StringUtils.fromUTF8(output);
122     }
123     
124     /***
125      * Returns the data captured from process error stream as bytes.
126      * 
127      * @return the data captured from process error stream as bytes, or null if not available.
128      */
129     public byte[] getErrorBytes()
130     {
131         return error;
132     }
133     
134     /***
135      * Returns the data captured from process error stream as a string.
136      * 
137      * @param charset output stream encoding.
138      * @return the data captured from process error stream as a string, or null if not available.
139      * @throws UnsupportedEncodingException if the encoding is not supported by the VM.
140      */
141     public String getError(String charset) throws UnsupportedEncodingException
142     {
143         if(error == null)
144         {
145             return null;
146         }
147         return new String(error, charset);
148     }
149 
150     /***
151      * Returns the data captured from process error stream as an UTF-8 string.
152      * 
153      * @return the data captured from process error stream as an UTF-8 string, or null if not available.
154      */
155     public String getError()
156     {
157         return StringUtils.fromUTF8(error);
158     }
159 }