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