1 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 ; 16 17 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 thread, final Throwable t) { 32 final Throwable proximateCause = helper.getProximateCause(t); 33 final Throwable ultimateCause = helper.getUltimateCause(t); 34 if (proximateCause instanceof ConfigurationSetupException) { 35 handleStartupException((ConfigurationSetupException) proximateCause); 36 } else if (ultimateCause instanceof BindException ) { 37 logger.error(ultimateCause); 38 handleStartupException((Exception )ultimateCause, ". Please make sure the server isn't already running or choose a different port."); 39 } else if (ultimateCause instanceof DatabaseException) { 40 handleStartupException((Exception )proximateCause); 41 } else if (ultimateCause instanceof LocationNotCreatedException) { 42 handleStartupException((Exception )ultimateCause); 43 } else if (ultimateCause instanceof FileNotCreatedException) { 44 handleStartupException((Exception )ultimateCause); 45 } else { 46 handleDefaultException(thread, proximateCause); 47 } 48 } 49 50 private void handleDefaultException(Thread thread, Throwable throwable) { 51 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 Thread.sleep(3000); 59 } catch (InterruptedException ie) { 60 } 62 System.exit(1); 63 } 64 65 private void handleStartupException(Exception e) { 66 handleStartupException(e, ""); 67 } 68 69 private void handleStartupException(Exception e, String 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 |