View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.util;
20  
21  import java.io.OutputStream;
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  
26  /**
27   *  Utility class for dealing with serious failures.
28   *
29   *  @author <a href="mailto:meling@item.ntnu.no">Hein Meling</a>
30   *  @since Jgroup 1.1
31   */
32  public class Abort
33    extends Error
34  {
35  
36    /////////////////////////////////////////////////////////////////////////////
37    // Private Fields
38    /////////////////////////////////////////////////////////////////////////////
39  
40    private String message;
41  
42    private Throwable exception;
43  
44    /////////////////////////////////////////////////////////////////////////////
45    // Constructors
46    /////////////////////////////////////////////////////////////////////////////
47  
48    public Abort()
49    {
50      this(null, null);
51    }
52  
53    public Abort(String message)
54    {
55      this(message, null);
56    }
57  
58    public Abort(Throwable exception)
59    {
60      this(null, exception);
61    }
62  
63    public Abort(String message, Throwable exception)
64    {
65      this.message = message;
66      this.exception = exception;
67    }
68  
69    /////////////////////////////////////////////////////////////////////////////
70    // Static Methods
71    /////////////////////////////////////////////////////////////////////////////
72  
73    public static Abort exit()
74    {
75      return exit(null, null, 1);
76    }
77  
78    public static Abort exit(int code)
79    {
80      return exit(null, null, code);
81    }
82  
83    public static Abort exit(String error)
84    {
85      return exit(error, null, 1);
86    }
87  
88    public static Abort exit(String error, int code)
89    {
90      return exit(error, null, code);
91    }
92  
93    public static Abort exit(Throwable exception)
94    {
95      return exit(null, exception, 1);
96    }
97  
98    public static Abort exit(Throwable exception, int code)
99    {
100     return exit(null, exception, code);
101   }
102 
103   public static Abort exit(String error, Throwable exception)
104   {
105     return exit(error, exception, 1);
106   }
107 
108   public static Abort exit(String error, Throwable exception, int code)
109   {
110     if (error != null)
111       System.err.println(error);
112     if (exception != null)
113       exception.printStackTrace();
114     System.exit(code);
115     return new Abort("System.exit(" + code + ")");
116   }
117 
118   public static String stackTrace(Throwable exception)
119   {
120     StringWriter writer = new StringWriter();
121     printStackTrace(exception, writer);
122     return writer.toString();
123   }
124 
125   public static void printStackTrace(Throwable exception, StringBuffer buf)
126   {
127     buf.append(stackTrace(exception));
128   }
129 
130   public static void printStackTrace(Throwable exception, OutputStream stream)
131   {
132     PrintWriter printer = new PrintWriter(stream);
133     exception.printStackTrace(printer);
134   }
135 
136   public static void printStackTrace(Throwable exception, Writer writer)
137   {
138     PrintWriter printer = new PrintWriter(writer);
139     exception.printStackTrace(printer);
140   }
141 
142   /**
143    *  Prints the given usage message on stderr and exits with zero error code.
144    *
145    *  @param message
146    *    The usage message to print.
147    */
148   public static void usage(String message)
149   {
150     usage(message, 0);
151   }
152 
153   /**
154    *  Prints the given usage message on stderr and exits with the given error code.
155    *
156    *  @param message
157    *    The usage message to print.
158    *  @param errorcode
159    *    The error code for exit.
160    */
161   public static void usage(String message, int errorcode)
162   {
163     System.err.println(message);
164     System.exit(errorcode);
165   }
166 
167   /////////////////////////////////////////////////////////////////////////////
168   // Object Methods
169   /////////////////////////////////////////////////////////////////////////////
170 
171   /**
172    *  Returns a string representation of this object
173    */
174   public String toString() 
175   {
176     StringWriter writer = new StringWriter();
177     if (exception != null)
178       printStackTrace(exception, writer);
179     StringBuffer buf = writer.getBuffer();
180     buf.append(getClass().getName());
181     if (message != null) {
182       buf.append(": ");
183       buf.append(message);
184     }
185     return buf.toString();
186   }
187 
188 } // END Abort class