KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > deferredparsing > DynamicClassLoader


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 David
15  * Li.
16  *
17  * The Enhydra Application Server and portions created by Lutris
18  * Technologies, Inc. are Copyright Lutris Technologies, Inc. All
19  * Rights Reserved.
20  *
21  * Contributor(s):
22  * David Li
23  *
24  * $Id: DynamicClassLoader.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
25  */

26 package org.enhydra.xml.xmlc.deferredparsing;
27
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.bcel.classfile.JavaClass;
33 import org.enhydra.xml.xmlc.StreamXMLCLogger;
34 import org.enhydra.xml.xmlc.XMLCLogger;
35
36 public class DynamicClassLoader extends ClassLoader JavaDoc
37 {
38     private ClassLoader JavaDoc delegateLoader = null;
39     private XMLCLogger logger = null;
40     private Map JavaDoc classMap;
41
42     DynamicClassLoader(ClassLoader JavaDoc parent) {
43     this(parent, null);
44     }
45     
46     DynamicClassLoader(ClassLoader JavaDoc parent, XMLCLogger logger) {
47     super(parent);
48     if (logger != null) {
49         this.logger = logger;
50     } else {
51         this.logger = new StreamXMLCLogger();
52     }
53     classMap = Collections.synchronizedMap(new HashMap JavaDoc());
54     }
55
56     /**
57      * Add a delegate be used in class loading.
58      * @param delegate the loader to be used as delegate
59      */

60     public void setDelegate (ClassLoader JavaDoc delegate) {
61     delegateLoader = delegate;
62     }
63
64     /**
65      * load class,
66      */

67     public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
68     Class JavaDoc clazz = null;
69     ClassLoader JavaDoc loader = null;
70
71     try {
72         if (delegateLoader != null) {
73         loader = delegateLoader;
74         } else {
75         loader = getParent();
76         }
77         clazz = loader.loadClass(className);
78     } catch (ClassNotFoundException JavaDoc e) {
79         if (loader != getParent()) {
80         clazz = getParent().loadClass(className);
81         } else {
82         throw e;
83         }
84     }
85
86     return clazz;
87     }
88
89     /**
90      * Define a class in this loader using the given JavaClass
91      * @param clazz JavaClass to be defined this
92      */

93     private Class JavaDoc defineClass (JavaClass clazz) {
94     byte[] buf = clazz.getBytes();
95     return defineClass(null, buf, 0, buf.length);
96     }
97
98     /**
99      * Generate a class based on the class name
100      * @param className class name for the class to be generated
101      */

102     Class JavaDoc createClass(String JavaDoc className, String JavaDoc path) {
103
104     if (logger.debugEnabled()) {
105         logger.logDebug(">>>Request to create class " + className + " from document " + path);
106     }
107
108     Class JavaDoc clazz = (Class JavaDoc)classMap.get(className);
109     if (clazz == null) {
110         synchronized(classMap) {
111         clazz = (Class JavaDoc)classMap.get(className);
112         if (clazz == null) {
113             DynamicMLCreator dhc = new DynamicMLCreator(className, path);
114             JavaClass javaClass = dhc.create();
115             clazz = defineClass(javaClass);
116         
117             classMap.put(className, clazz);
118         }
119         }
120     } else {
121         if (logger.debugEnabled()) {
122         logger.logDebug(">>>Got class " + className + "from cache");
123         }
124     }
125
126     return clazz;
127     }
128
129 }
130
Popular Tags