KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > exception > ExceptionDelegateFactory


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.exception;
19
20 // Imports for apache commons logger
21
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 /**
28  * <p>This is the Carbon Factory for building new exception delegates for use
29  * in Carbon based exceptions. This allows for the configuration of a new
30  * Exception Delegate on a system-wide basis, or the default use of the
31  * standard <code>DefaultExceptionDelegateImpl</code> delegate.</p>
32  *
33  *
34  * Copyright 2002 Sapient
35  * @since carbon 1.0
36  * @author Greg Hinkle, April 2002
37  * @version $Revision: 1.12 $($Author: dvoet $ / $Date: 2003/05/05 21:21:21 $)
38  * @stereotype singleton
39  */

40 public class ExceptionDelegateFactory {
41
42     /** The handle to Apache-commons logger */
43     private Log log = LogFactory.getLog(this.getClass());
44
45     /**
46      * The system property string which can contain the fully-qualified class
47      * name of the class to be used as the system's standard exception delegate.
48      */

49     private static final String JavaDoc DELEGATE_PROPERTY =
50         "carbon.exception.Delegate";
51
52     /**
53      * <p>This is a singleton so we have a private constructor to prevent
54      * impropper instantiation.</p>
55      */

56     private ExceptionDelegateFactory() {
57     }
58
59
60     /**
61      * Creates an exception delegate.
62      *
63      * @param exceptionSource the exception source being delegated
64      * @param severity the severity of the exception
65      * @param delegatee the delegatee
66      * @param message the error message of the exception
67      * @param cause a cause of the exception
68      * @return the new delegate
69      */

70     public ExceptionDelegate createDelegate(
71         Class JavaDoc exceptionSource,
72         SeverityEnum severity,
73         Throwable JavaDoc delegatee,
74         String JavaDoc message,
75         Throwable JavaDoc 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     /**
89      * Creates a new empty exception delegate.
90      *
91      * @return the new delegate
92      */

93     protected ExceptionDelegate newDelegate() {
94         String JavaDoc className = BootStrapper.getInstance()
95             .getDeploymentProperty(ExceptionDelegateFactory.DELEGATE_PROPERTY);
96
97         ExceptionDelegate delegate = null;
98         if (className != null) {
99
100             try {
101                 Class JavaDoc delegateClass =
102                     Class.forName(className, true,
103                                             this.getClass().getClassLoader());
104
105                 delegate = (ExceptionDelegate) delegateClass.newInstance();
106             } catch (InstantiationException JavaDoc 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 JavaDoc iae) {
114                 if (log.isFatalEnabled()) {
115                     log.fatal("Could not access configured exception delegate ["
116                         + className + "]: "
117                         + ExceptionUtility.printStackTracesToString(iae));
118                 }
119             } catch (ClassNotFoundException JavaDoc 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     // ******** static class members follow ********
137
// Note: For sub-classable singletons, use a static
138
// initializer that utilizes a configuration based
139
// class reference
140

141
142     /** References the single valid instance of this singleton */
143     private static final ExceptionDelegateFactory INSTANCE =
144                                                  new ExceptionDelegateFactory();
145
146     /**
147      * <p>
148      * Static factory method for getting a reference to the
149      * <code>ExceptionDelegateFactory</code>.
150      * </p>
151      *
152      * @return ExceptionDelegateFactory
153      */

154     public static ExceptionDelegateFactory getInstance() {
155         return ExceptionDelegateFactory.INSTANCE;
156     }
157
158 }
159
Popular Tags