1 17 18 package org.sape.carbon.core.exception; 19 20 import org.sape.carbon.core.bootstrap.BootStrapper; 22 import org.sape.carbon.core.util.classify.SeverityEnum; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 40 public class ExceptionDelegateFactory { 41 42 43 private Log log = LogFactory.getLog(this.getClass()); 44 45 49 private static final String DELEGATE_PROPERTY = 50 "carbon.exception.Delegate"; 51 52 56 private ExceptionDelegateFactory() { 57 } 58 59 60 70 public ExceptionDelegate createDelegate( 71 Class exceptionSource, 72 SeverityEnum severity, 73 Throwable delegatee, 74 String message, 75 Throwable cause) { 76 77 ExceptionDelegate delegate = newDelegate(); 78 79 delegate.setExceptionSource(exceptionSource); 80 delegate.setSeverity(severity); 81 delegate.setDelegatee(delegatee); 82 delegate.setMessage(message); 83 delegate.setCause(cause); 84 85 return delegate; 86 } 87 88 93 protected ExceptionDelegate newDelegate() { 94 String className = BootStrapper.getInstance() 95 .getDeploymentProperty(ExceptionDelegateFactory.DELEGATE_PROPERTY); 96 97 ExceptionDelegate delegate = null; 98 if (className != null) { 99 100 try { 101 Class delegateClass = 102 Class.forName(className, true, 103 this.getClass().getClassLoader()); 104 105 delegate = (ExceptionDelegate) delegateClass.newInstance(); 106 } catch (InstantiationException ie) { 107 if (log.isFatalEnabled()) { 108 log.fatal( 109 "Could not instantiate configured exception delegate [" 110 + className + "]: " 111 + ExceptionUtility.printStackTracesToString(ie)); 112 } 113 } catch (IllegalAccessException iae) { 114 if (log.isFatalEnabled()) { 115 log.fatal("Could not access configured exception delegate [" 116 + className + "]: " 117 + ExceptionUtility.printStackTracesToString(iae)); 118 } 119 } catch (ClassNotFoundException cnfe) { 120 if (log.isFatalEnabled()) { 121 log.fatal("Could not find configured exception delegate [" 122 + className + "]: " 123 + ExceptionUtility.printStackTracesToString(cnfe)); 124 } 125 } 126 } 127 128 if (delegate == null) { 129 delegate = new DefaultExceptionDelegateImpl(); 130 } 131 return delegate; 132 } 133 134 135 136 141 142 143 private static final ExceptionDelegateFactory INSTANCE = 144 new ExceptionDelegateFactory(); 145 146 154 public static ExceptionDelegateFactory getInstance() { 155 return ExceptionDelegateFactory.INSTANCE; 156 } 157 158 } 159 | Popular Tags |