KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > XMLCStdFactory


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XMLCStdFactory.java,v 1.1.1.1 2003/03/10 16:36:19 taweili Exp $
22  */

23
24 package org.enhydra.xml.xmlc;
25
26 /**
27  * XMLC standard factory class. Used for creating instances of
28  * XMLC-generated classes. This is a default factory used when
29  * no special support, such as auto-recompilation is required.
30  */

31 public class XMLCStdFactory implements XMLCFactory {
32     /**
33      * Class loader that is used to load the first reference to a class.
34      */

35     private ClassLoader JavaDoc fDefaultClassLoader;
36
37     /**
38      * Logging interface.
39      */

40     private XMLCLogger fLogger;
41
42     /**
43      * Constructor.
44      * @param classLoader Classloader used to load classes when a class name
45      * is specified. If null, the system classload is used.
46      * @param logger XMLC logger or null for no logging.
47      */

48     public XMLCStdFactory(ClassLoader JavaDoc classLoader,
49                           XMLCLogger logger) {
50         fDefaultClassLoader = classLoader;
51         if (logger != null) {
52             fLogger = logger;
53         } else {
54             fLogger = new StreamXMLCLogger(); // all will be disabled.
55
}
56     }
57
58     /**
59      * Handle all errors, logging if enabled. Also handles logging
60      * error output of the compiler.
61      *
62      * @param className Class being loaded.
63      * @param except Can be any error or exception.
64      * @return Returns runtime exceptions to be rethrown. Could throw
65      * everything, but catch blocks appear to fall through to the compiler so
66      * we would have to put in dummy returns.
67      */

68     protected Error JavaDoc handleError(String JavaDoc className,
69                               Throwable JavaDoc except) {
70 // ole fLogger.logError("Factory creation of an XMLC class failed: \""
71
// + className + "\"", except);
72

73         // Errors just continue on.
74
if (except instanceof Error JavaDoc) {
75             return (Error JavaDoc)except;
76         }
77         
78         // All other exceptions get wrapped.
79
return new XMLCError("Couldn't load XMLC class", except);
80     }
81
82     /**
83      * Do actualy work of creating a new object.
84      */

85     protected XMLObject doCreate(Class JavaDoc xmlcBasedClass)
86             throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
87         if (xmlcBasedClass.isInterface()) {
88             // Convert interface to class name.
89
return doCreate(xmlcBasedClass.getName() + "Impl",
90                             fDefaultClassLoader);
91         } else {
92             if (!XMLObject.class.isAssignableFrom(xmlcBasedClass)) {
93                 throw new XMLCError("class \"" + xmlcBasedClass.getName()
94                                     + "\" does not implement \""
95                                     + XMLObject.class.getName() + "\"");
96             }
97             return (XMLObject)(xmlcBasedClass.newInstance());
98         }
99     }
100
101     /**
102      * Do actualy work of creating a new object given a name.
103      */

104     protected XMLObject doCreate(String JavaDoc xmlcClassName,
105                                ClassLoader JavaDoc classLoader)
106             throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
107         if (classLoader != null) {
108             return doCreate(classLoader.loadClass(xmlcClassName));
109         } else {
110             return doCreate(Class.forName(xmlcClassName));
111         }
112     }
113
114     /**
115      * Create an instance of a XMLC-generated class or a class derived from
116      * one, given either a class or an interface object, using the associated
117      * classloader
118      */

119     public XMLObject create(String JavaDoc xmlcClassName) {
120         try {
121             return doCreate(xmlcClassName, fDefaultClassLoader);
122         } catch (Throwable JavaDoc except) {
123             throw handleError(xmlcClassName, except);
124         }
125     }
126
127     /**
128      * Create an instance of a XMLC-generated class or a class derived
129      * from one, given the name of the class, using the associated
130      * classloader
131      */

132     public XMLObject create(Class JavaDoc xmlcBasedClass) {
133         try {
134             if (xmlcBasedClass.isInterface()) {
135                 // Convert interface to class name.
136
return doCreate(xmlcBasedClass.getName() + "Impl",
137                                 xmlcBasedClass.getClassLoader());
138             } else {
139                 return doCreate(xmlcBasedClass);
140             }
141         } catch (Throwable JavaDoc except) {
142             throw handleError(xmlcBasedClass.getName(), except);
143         }
144     }
145
146     /** Get the default class loader */
147     protected ClassLoader JavaDoc getDefaultClassLoader() {
148     return fDefaultClassLoader;
149     }
150     /**
151      * Get the logger.
152      */

153     public XMLCLogger getLogger() {
154         return fLogger;
155     }
156
157 }
158
Popular Tags