001    package org.springframework.richclient.exceptionhandling.delegation;
002    
003    import java.lang.Thread.UncaughtExceptionHandler;
004    
005    /**
006     * Superclass for delegate implementations
007     * 
008     * @author Geoffrey De Smet
009     * @since 0.3.0
010     */
011    public abstract class AbstractExceptionHandlerDelegate implements ExceptionHandlerDelegate {
012    
013        protected Thread.UncaughtExceptionHandler exceptionHandler;
014        protected ExceptionPurger exceptionPurger = null;
015        protected boolean purgeOnAppropriateCheck = true;
016        protected boolean purgeOnHandling = true;
017        
018        public AbstractExceptionHandlerDelegate() {
019        }
020        
021        public AbstractExceptionHandlerDelegate(UncaughtExceptionHandler exceptionHandler) {
022            this.exceptionHandler = exceptionHandler;
023        }
024    
025        public void setExceptionHandler(Thread.UncaughtExceptionHandler exceptionHandler) {
026            this.exceptionHandler = exceptionHandler;
027        }
028        
029        /**
030         * If set the throwable will first be purged before doing the approriate check or handling it.
031         * @param exceptionPurger
032         */
033        public void setExceptionPurger(ExceptionPurger exceptionPurger) {
034            this.exceptionPurger = exceptionPurger;
035        }
036    
037        public void setPurgeOnAppropriateCheck(boolean purgeOnAppropriateCheck) {
038            this.purgeOnAppropriateCheck = purgeOnAppropriateCheck;
039        }
040        
041        public void setPurgeOnHandling(boolean purgeOnHandling) {
042            this.purgeOnHandling = purgeOnHandling;
043        }
044    
045    
046        /**
047         * {@inheritDoc}
048         */
049        public final boolean hasAppropriateHandler(Throwable throwable) {
050            if (exceptionPurger != null && purgeOnAppropriateCheck) {
051                throwable = exceptionPurger.purge(throwable);
052            }
053            return hasAppropriateHandlerPurged(throwable);
054        }
055        
056    
057        public abstract boolean hasAppropriateHandlerPurged(Throwable throwable);
058    
059        /**
060         * {@inheritDoc}
061         */
062        public final void uncaughtException(Thread thread, Throwable throwable) {
063            if (exceptionPurger != null && purgeOnHandling) {
064                throwable = exceptionPurger.purge(throwable);
065            }
066            uncaughtExceptionPurged(thread, throwable);
067        }
068    
069        public void uncaughtExceptionPurged(Thread thread, Throwable throwable) {
070            exceptionHandler.uncaughtException(thread, throwable);
071        }
072        
073    }