Revision: 1190
Author: ge0ffrey
Date: 2006-05-28 05:44:34 -0700 (Sun, 28 May 2006)
ViewCVS: http://svn.sourceforge.net/spring-rich-c/?rev=1190&view=rev
Log Message:
-----------
first of a couple of commits for the exceptionhandling (not finished yet)
feed-back welcome on dev list
Added Paths:
-----------
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandler.java
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerDelegate.java
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/exceptionhandling/
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerTests.java
Added:
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandler.java
===================================================================
---
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandler.java
(rev 0)
+++
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandler.java
2006-05-28 12:44:34 UTC (rev 1190)
@@ -0,0 +1,58 @@
+package org.springframework.richclient.exceptionhandling;
+
+import java.util.List;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.util.Assert;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+
+/**
+ * An exception handler that selects an appropriate exception handler from a
list
+ * based on the class of the thrown exception and delegates the handling of
the exception to it.
+ *
+ * This class works very similar to catch statements:
+ * the first delegate which can handle the exception will handle it.
+ * For example, consider 3 delegates for the following classes in this order:
+ * NullPointerException (1), RuntimeException (2), IllegalArgumentException
(3).
+ * A thrown IllegalArgumentException will be handled by the (2) handler. The
(3) handler is useless.
+ *
+ * @author Geoffrey De Smet
+ */
+public class DelegatingExceptionHandler implements
Thread.UncaughtExceptionHandler, InitializingBean {
+
+ private final transient Log logger = LogFactory.getLog(getClass());
+
+ private List<DelegatingExceptionHandlerDelegate> delegateList;
+
+ /**
+ * Sets the list of delegates.
+ * This is not a map because the order is important
+ * and delegate selection is not a simple key based selector.
+ * @param delegateList a list of DelegatingExceptionHandlerDelegate
+ */
+ public void setDelegateList(List<DelegatingExceptionHandlerDelegate>
delegateList) {
+ this.delegateList = delegateList;
+ }
+
+ public void afterPropertiesSet() throws Exception {
+ Assert.notEmpty(delegateList, "The delegate list must contains at
least one entry.");
+ }
+
+ /**
+ * Delegates the throwable to the appropriate delegate exception handler.
+ * @param thread the thread in which the throwable occurred
+ * @param throwable the thrown throwable
+ */
+ public void uncaughtException(Thread thread, Throwable throwable) {
+ for (DelegatingExceptionHandlerDelegate delegate : delegateList) {
+ if (delegate.hasAppropriateHandler(throwable)) {
+ Thread.UncaughtExceptionHandler exceptionHandler =
delegate.getExceptionHandler();
+ exceptionHandler.uncaughtException(thread, throwable);
+ return;
+ }
+ }
+ // A silent exception handler should be configured if it needs to be
silent
+ logger.error("No exception handler found for throwable", throwable);
+ }
+
+}
Added:
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerDelegate.java
===================================================================
---
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerDelegate.java
(rev 0)
+++
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerDelegate.java
2006-05-28 12:44:34 UTC (rev 1190)
@@ -0,0 +1,48 @@
+package org.springframework.richclient.exceptionhandling;
+
+/**
+ * A couple of a throwable class and its exception handler.
+ * @author Geoffrey De Smet
+ */
+public class DelegatingExceptionHandlerDelegate {
+
+ private Class throwableClass;
+ private Thread.UncaughtExceptionHandler exceptionHandler;
+
+ public DelegatingExceptionHandlerDelegate() {
+ }
+
+ public DelegatingExceptionHandlerDelegate(Class throwableClass,
+ Thread.UncaughtExceptionHandler
exceptionHandler) {
+ this.throwableClass = throwableClass;
+ this.exceptionHandler = exceptionHandler;
+ }
+
+ public Class getThrowableClass() {
+ return throwableClass;
+ }
+
+ public void setThrowableClass(Class throwableClass) {
+ this.throwableClass = throwableClass;
+ }
+
+ public Thread.UncaughtExceptionHandler getExceptionHandler() {
+ return exceptionHandler;
+ }
+
+ public void setExceptionHandler(Thread.UncaughtExceptionHandler
exceptionHandler) {
+ this.exceptionHandler = exceptionHandler;
+ }
+
+
+ /**
+ * Returns true if thrownTrowable is an instance of throwableClass.
+ *
+ * @param thrownTrowable the thrown exception or error.
+ * @return true if thrownTrowable is an instance of throwableClass
+ */
+ public boolean hasAppropriateHandler(Throwable thrownTrowable) {
+ return throwableClass.isInstance(thrownTrowable);
+ }
+
+}
Added:
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerTests.java
===================================================================
---
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerTests.java
(rev 0)
+++
trunk/spring-richclient/tiger/src/test/java/org/springframework/richclient/exceptionhandling/DelegatingExceptionHandlerTests.java
2006-05-28 12:44:34 UTC (rev 1190)
@@ -0,0 +1,52 @@
+package org.springframework.richclient.exceptionhandling;
+
+import junit.framework.TestCase;
+import java.util.List;
+import java.util.LinkedList;
+
+/**
+ * @author Geoffrey De Smet
+ */
+public class DelegatingExceptionHandlerTests extends TestCase {
+
+ public void testDelegation() {
+ DelegatingExceptionHandler delegatingExceptionHandler = new
DelegatingExceptionHandler();
+ List<DelegatingExceptionHandlerDelegate> delegateList = new
LinkedList<DelegatingExceptionHandlerDelegate>();
+ ExceptionHandlerCounter illegalArgumentCounter = new
ExceptionHandlerCounter();
+ delegateList.add(new
DelegatingExceptionHandlerDelegate(IllegalArgumentException.class,
illegalArgumentCounter));
+ ExceptionHandlerCounter nullPointerCounter = new
ExceptionHandlerCounter();
+ delegateList.add(new
DelegatingExceptionHandlerDelegate(NullPointerException.class,
nullPointerCounter));
+ ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
+ delegateList.add(new
DelegatingExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
+ delegatingExceptionHandler.setDelegateList(delegateList);
+
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new NullPointerException());
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new RuntimeException());
+ // NumberFormatException extends IllegalArgumentException
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new NumberFormatException());
+ // IllegalStateException extends RuntimeException
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new IllegalStateException());
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new IllegalArgumentException());
+ delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
new NullPointerException());
+
+ assertEquals(2, illegalArgumentCounter.getCounter());
+ assertEquals(2, nullPointerCounter.getCounter());
+ assertEquals(2, runtimeCounter.getCounter());
+ }
+
+
+ public static class ExceptionHandlerCounter implements
Thread.UncaughtExceptionHandler {
+
+ private int counter = 0;
+
+ public int getCounter() {
+ return counter;
+ }
+
+ public void uncaughtException(Thread t, Throwable e) {
+ counter++;
+ }
+
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs