View Javadoc
1   package org.apache.maven.shared.release;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.PrintStream;
24  
25  /**
26   * @author Edwin Punzalan
27   */
28  public class ReleaseResult
29  {
30      /** The result of the release. */
31      public static final int UNDEFINED = -1;
32      /** The release was successful. */
33      public static final int SUCCESS = 0;
34      /** The release failed. */
35      public static final int ERROR = 1;
36  
37      private StringBuilder stdOut = new StringBuilder();
38  
39      private int resultCode = UNDEFINED;
40  
41      private long startTime;
42  
43      private long endTime;
44  
45      private static final String LS = System.getProperty( "line.separator" );
46  
47      /**
48       * Append Info message to the output.
49       * @param message the message to append
50       */
51      public void appendInfo( String message )
52      {
53          stdOut.append( "[INFO] " ).append( message ).append( LS );
54      }
55      /**
56       * Append warning message to the output.
57       * @param message the message to append
58       */
59      public void appendWarn( String message )
60      {
61          stdOut.append( "[WARN] " ).append( message ).append( LS );
62      }
63  
64      /**
65       * Append debug message to the output.
66       * @param message the message to append
67       */
68      public void appendDebug( String message )
69      {
70          stdOut.append( "[DEBUG] " ).append( message ).append( LS );
71      }
72  
73      /**
74       * Append error message to the output.
75       * @param message the message to append
76       * @param e the exception to append
77       */
78      public void appendDebug( String message, Exception e )
79      {
80          appendDebug( message );
81  
82          stdOut.append( getStackTrace( e ) ).append( LS );
83      }
84  
85      /**
86       * Append error message to the output.
87       *
88       * @param message the message to append
89       */
90      public void appendError( String message )
91      {
92          stdOut.append( "[ERROR] " ).append( message ).append( LS );
93  
94          setResultCode( ERROR );
95      }
96  
97      /**
98       * Append error exception to the output
99       *
100      * @param e the exception to append
101      */
102     public void appendError( Exception e )
103     {
104         appendError( getStackTrace( e ) );
105     }
106 
107     /**
108      * Append stack trace to the output
109      *
110      * @param message the message to append
111      * @param e the exception to append
112      */
113     public void appendError( String message, Exception e )
114     {
115         appendError( message );
116 
117         stdOut.append( getStackTrace( e ) ).append( LS );
118     }
119 
120     /**
121      * Append message to the output.
122      *
123      * @param message the message to append
124      */
125     public void appendOutput( String message )
126     {
127         stdOut.append( message );
128     }
129 
130     public String getOutput()
131     {
132         return stdOut.toString();
133     }
134 
135     public int getResultCode()
136     {
137         return resultCode;
138     }
139 
140     public void setResultCode( int resultCode )
141     {
142         this.resultCode = resultCode;
143     }
144 
145     public long getStartTime()
146     {
147         return startTime;
148     }
149 
150     public void setStartTime( long startTime )
151     {
152         this.startTime = startTime;
153     }
154 
155     public long getEndTime()
156     {
157         return endTime;
158     }
159 
160     public void setEndTime( long endTime )
161     {
162         this.endTime = endTime;
163     }
164 
165     private String getStackTrace( Exception e )
166     {
167         ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
168 
169         PrintStream stream = new PrintStream( byteStream );
170 
171         e.printStackTrace( stream );
172 
173         stream.flush();
174 
175         return byteStream.toString();
176     }
177 }