1 5 package com.nightlabs.rcp.exceptionhandler; 6 7 import java.util.HashMap ; 8 9 import org.apache.commons.lang.exception.ExceptionUtils; 10 import org.apache.log4j.Logger; 11 import org.eclipse.swt.widgets.Display; 12 13 import com.nightlabs.rcp.extensionpoint.EPProcessorException; 14 15 24 public class ExceptionHandlerRegistry { 25 public static final Logger LOGGER = Logger.getLogger(ExceptionHandlerRegistry.class); 26 27 private HashMap exceptionHandlers = new HashMap (); 28 29 private Object synchronizedObject = new Object (); 32 33 protected HashMap getExceptionHandlers(){ 34 return exceptionHandlers; 35 } 36 37 public void addExceptionHandler(String targetType, IExceptionHandler handler){ 38 synchronized(synchronizedObject){ 39 ensureProcessingDone(); 40 if (exceptionHandlers.containsKey(targetType)) 41 throw new DuplicateHandlerRegistryException("An exceptionHandler was already defined for "+targetType); 42 exceptionHandlers.put(targetType,handler); 43 } 44 } 45 46 50 protected IExceptionHandler getExceptionHandler(String targetType){ 51 synchronized(synchronizedObject){ 52 ensureProcessingDone(); 53 if (exceptionHandlers.containsKey(targetType)) 54 return (IExceptionHandler)exceptionHandlers.get(targetType); 55 else 56 return null; 57 } 58 } 59 60 61 65 public void removeExceptionHandler(String targetType){ 66 synchronized(synchronizedObject){ 67 if (exceptionHandlers.containsKey(targetType)) 68 exceptionHandlers.remove(targetType); 69 } 70 } 71 72 76 protected IExceptionHandler getExceptionHandler(Class targetType){ 77 return getExceptionHandler(targetType.getName()); 78 } 79 80 85 protected boolean haveHandler(Class targetType){ 86 return exceptionHandlers.containsKey(targetType.getName()); 87 } 88 89 private ExceptionHandlerEPProcessor epProcessor = null; 91 92 protected void ensureProcessingDone() { 93 synchronized(synchronizedObject){ 94 if (epProcessor == null){ 95 try { 96 epProcessor = new ExceptionHandlerEPProcessor(); 97 epProcessor.process(); 98 } catch (EPProcessorException e) { 99 e.printStackTrace(); 101 } 102 } 103 } 104 } 105 106 public ExceptionHandlerSearchResult getTopLevelCauseHandler(Throwable x) 107 { 108 ExceptionHandlerSearchResult handler = null; 109 Throwable cause = x.getCause(); 110 if(cause != null) 111 handler = getTopLevelCauseHandler(cause); 112 if((handler == null) || (cause == null)) { 113 if (haveHandler(x.getClass())) { 114 IExceptionHandler eHandler = getExceptionHandler(x.getClass()); 115 handler = new ExceptionHandlerSearchResult(); 116 handler.setHandler(eHandler); 117 handler.setTriggerException(x); 118 } 119 } 120 return handler; 121 } 122 123 127 public static class ExceptionHandlerSearchResult { 128 private Throwable triggerException; 129 private IExceptionHandler handler; 130 131 public IExceptionHandler getHandler() { 132 return handler; 133 } 134 public void setHandler(IExceptionHandler handler) { 135 this.handler = handler; 136 } 137 public Throwable getTriggerException() { 138 return triggerException; 139 } 140 public void setTriggerException(Throwable triggerException) { 141 this.triggerException = triggerException; 142 } 143 } 144 145 153 public ExceptionHandlerSearchResult searchHandler(Throwable exception){ 154 ensureProcessingDone(); 156 157 ExceptionHandlerSearchResult rootCauseResult = getTopLevelCauseHandler(exception); 158 if (rootCauseResult != null) 159 return rootCauseResult; 160 161 Class classRun = exception.getClass(); 162 Throwable exceptionRun = exception; 163 while (exceptionRun != null) { 164 165 classRun = exceptionRun.getClass(); 166 while ( (!haveHandler(classRun)) && (!classRun.equals(Throwable .class)) ) { 167 classRun = classRun.getSuperclass(); 168 } 169 170 if (!classRun.equals(Throwable .class)) 171 if (haveHandler(classRun)) 172 break; 173 174 exceptionRun = ExceptionUtils.getCause(exceptionRun); 176 } 177 178 ExceptionHandlerSearchResult result = new ExceptionHandlerSearchResult(); 179 result.setHandler(getExceptionHandler(classRun)); 180 if (exceptionRun == null) 181 exceptionRun = exception; 182 result.setTriggerException(exceptionRun); 183 return result; 185 } 186 187 194 public static void asyncHandleException(Throwable exception) 195 { 196 getSharedInstance().handleException(Thread.currentThread(), exception, true); 197 } 198 199 206 public static boolean asyncHandleException(Thread thread, Throwable exception) 207 { 208 return getSharedInstance().handleException(thread, exception, true); 209 } 210 211 217 public static boolean syncHandleException(Throwable exception) 218 { 219 return getSharedInstance().handleException(Thread.currentThread(), exception, false); 220 } 221 222 228 public static boolean syncHandleException(Thread thread, Throwable exception) 229 { 230 return getSharedInstance().handleException(thread, exception, false); 231 } 232 233 private boolean handleException(final Thread thread, final Throwable exception, boolean async) 234 { 235 Throwable handlingException = null; 237 final ExceptionHandlerSearchResult handlerSearch = getSharedInstance().searchHandler(exception); 238 if (handlerSearch.getHandler() != null){ 239 try { 240 Runnable runnable = new Runnable (){ 241 public void run(){ 242 try { 243 handlerSearch.getHandler().handleException( 244 thread,exception, handlerSearch.getTriggerException()); 245 } catch(Throwable x) { 246 LOGGER.fatal("Exception occured while handling exception on GUI thread!", x); 247 } 248 } 249 }; 250 251 if (async) 252 Display.getDefault().asyncExec(runnable); 253 else 254 Display.getDefault().syncExec(runnable); 255 256 } catch (Throwable ex) { 257 LOGGER.fatal("Exception occured while handling exception on causing thread!", ex); 258 } 259 return true; 260 } 261 else { 262 LOGGER.fatal("Did not find an ExceptionHandler for this Throwable!", exception); 263 return false; 264 } 265 } 266 267 268 private static ExceptionHandlerRegistry sharedInstance; 269 270 public static ExceptionHandlerRegistry getSharedInstance() { 271 if (sharedInstance == null) { 272 sharedInstance = new ExceptionHandlerRegistry(); 273 } 274 return sharedInstance; 275 } 276 } | Popular Tags |