001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.lang;
006:
007: import com.tc.config.schema.setup.ConfigurationSetupException;
008: import com.tc.exception.DatabaseException;
009: import com.tc.exception.ExceptionHelperImpl;
010: import com.tc.exception.MortbayMultiExceptionHelper;
011: import com.tc.exception.RuntimeExceptionHelper;
012: import com.tc.logging.TCLogger;
013: import com.tc.util.TCDataFileLockingException;
014: import com.tc.util.startuplock.FileNotCreatedException;
015: import com.tc.util.startuplock.LocationNotCreatedException;
016:
017: import java.net.BindException;
018:
019: /**
020: * Handle throwables appropriately by printing messages to the logger, etc. Deal with
021: * nasty problems that can occur as the VM shuts down.
022: */
023: public class ThrowableHandler {
024: //XXX: The dispatching in this class is retarded, but I wanted to move as much of the exception handling into a single
025: //place first, then come up with fancy ways of dealing with them. --Orion 03/20/2006
026:
027: private final TCLogger logger;
028: private final ExceptionHelperImpl helper;
029:
030: /**
031: * Construct a new ThrowableHandler with a logger
032: * @param logger Logger
033: */
034: public ThrowableHandler(TCLogger logger) {
035: this .logger = logger;
036: helper = new ExceptionHelperImpl();
037: helper.addHelper(new RuntimeExceptionHelper());
038: helper.addHelper(new MortbayMultiExceptionHelper());
039: }
040:
041: /**
042: * Handle throwable occurring on thread
043: * @param thread Thread receiving Throwable
044: * @param t Throwable
045: */
046: public void handleThrowable(final Thread thread, final Throwable t) {
047: final Throwable proximateCause = helper.getProximateCause(t);
048: final Throwable ultimateCause = helper.getUltimateCause(t);
049: if (proximateCause instanceof ConfigurationSetupException) {
050: handleStartupException((ConfigurationSetupException) proximateCause);
051: } else if (ultimateCause instanceof BindException) {
052: logger.error(ultimateCause);
053: handleStartupException(
054: (Exception) ultimateCause,
055: ". Please make sure the server isn't already running or choose a different port.");
056: } else if (ultimateCause instanceof DatabaseException) {
057: handleStartupException((Exception) proximateCause);
058: } else if (ultimateCause instanceof LocationNotCreatedException) {
059: handleStartupException((Exception) ultimateCause);
060: } else if (ultimateCause instanceof FileNotCreatedException) {
061: handleStartupException((Exception) ultimateCause);
062: } else if (ultimateCause instanceof TCDataFileLockingException) {
063: handleStartupException((Exception) ultimateCause);
064: } else {
065: handleDefaultException(thread, proximateCause);
066: }
067: }
068:
069: private void handleDefaultException(Thread thread,
070: Throwable throwable) {
071: // We need to make SURE that our stacktrace gets printed, when using just the logger sometimes the VM exits
072: // before the stacktrace prints
073: throwable.printStackTrace(System.err);
074: System.err.flush();
075: logger
076: .error(
077: "Thread:"
078: + thread
079: + " got an uncaught exception. About to sleep then exit.",
080: throwable);
081: try {
082: // Give our logger a chance to print the stacktrace before the VM exits
083: Thread.sleep(3000);
084: } catch (InterruptedException ie) {
085: // When you suck you just suck and nothing will help you
086: }
087: System.exit(1);
088: }
089:
090: private void handleStartupException(Exception e) {
091: handleStartupException(e, "");
092: }
093:
094: private void handleStartupException(Exception e, String extraMessage) {
095: System.err.println("");
096: System.err.println("");
097: System.err.println("Fatal Terracotta startup exception:");
098: System.err.println("");
099: System.err.println(" " + e.getMessage() + extraMessage);
100: System.err.println("");
101: System.err.println("Server startup failed.");
102: System.exit(2);
103: }
104:
105: }
|