KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > lang > ThrowableHandler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.lang;
5
6 import com.tc.config.schema.setup.ConfigurationSetupException;
7 import com.tc.exception.DatabaseException;
8 import com.tc.exception.ExceptionHelperImpl;
9 import com.tc.exception.MortbayMultiExceptionHelper;
10 import com.tc.exception.RuntimeExceptionHelper;
11 import com.tc.logging.TCLogger;
12 import com.tc.util.startuplock.FileNotCreatedException;
13 import com.tc.util.startuplock.LocationNotCreatedException;
14
15 import java.net.BindException JavaDoc;
16
17 // XXX: The dispatching in this class is retarded, but I wanted to move as much of the exception handling into a single
18
// place first, then come up with fancy ways of dealing with them. --Orion 03/20/2006
19
public class ThrowableHandler {
20
21   private final TCLogger logger;
22   private final ExceptionHelperImpl helper;
23
24   public ThrowableHandler(TCLogger logger) {
25     this.logger = logger;
26     helper = new ExceptionHelperImpl();
27     helper.addHelper(new RuntimeExceptionHelper());
28     helper.addHelper(new MortbayMultiExceptionHelper());
29   }
30
31   public void handleThrowable(final Thread JavaDoc thread, final Throwable JavaDoc t) {
32     final Throwable JavaDoc proximateCause = helper.getProximateCause(t);
33     final Throwable JavaDoc ultimateCause = helper.getUltimateCause(t);
34     if (proximateCause instanceof ConfigurationSetupException) {
35       handleStartupException((ConfigurationSetupException) proximateCause);
36     } else if (ultimateCause instanceof BindException JavaDoc) {
37       logger.error(ultimateCause);
38       handleStartupException((Exception JavaDoc)ultimateCause, ". Please make sure the server isn't already running or choose a different port.");
39     } else if (ultimateCause instanceof DatabaseException) {
40       handleStartupException((Exception JavaDoc)proximateCause);
41     } else if (ultimateCause instanceof LocationNotCreatedException) {
42       handleStartupException((Exception JavaDoc)ultimateCause);
43     } else if (ultimateCause instanceof FileNotCreatedException) {
44       handleStartupException((Exception JavaDoc)ultimateCause);
45     } else {
46       handleDefaultException(thread, proximateCause);
47     }
48   }
49
50   private void handleDefaultException(Thread JavaDoc thread, Throwable JavaDoc throwable) {
51     // We need to make SURE that our stacktrace gets printed, when using just the logger sometimes the VM exits
52
// before the stacktrace prints
53
throwable.printStackTrace(System.err);
54     System.err.flush();
55     logger.error("Thread:" + thread + " got an uncaught exception. About to sleep then exit.", throwable);
56     try {
57       // Give our logger a chance to print the stacktrace before the VM exits
58
Thread.sleep(3000);
59     } catch (InterruptedException JavaDoc ie) {
60       // When you suck you just suck and nothing will help you
61
}
62     System.exit(1);
63   }
64
65   private void handleStartupException(Exception JavaDoc e) {
66     handleStartupException(e, "");
67   }
68   
69   private void handleStartupException(Exception JavaDoc e, String JavaDoc extraMessage) {
70     System.err.println("");
71     System.err.println("");
72     System.err.println("Fatal Terracotta startup exception:");
73     System.err.println("");
74     System.err.println(" " + e.getMessage() + extraMessage);
75     System.err.println("");
76     System.err.println("Server startup failed.");
77     System.exit(2);
78   }
79
80 }
81
Popular Tags