KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > server > util > ClassLoaderChain


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.server.util;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.security.SecureClassLoader JavaDoc;
33
34 /**
35  * Represents a classloader chain. A ClassLoader chain is a linked list of
36  * classloaders. All class requests is searched across the classloader chain in
37  * a horizontal manner. A classloader in this chain can either exist within
38  * the scope of this chain only or could be shared across multiple chains.
39  *
40  * @author Harsha RA, Sivakumar Thyagarajan
41  */

42 public class ClassLoaderChain extends SecureClassLoader JavaDoc {
43     private List JavaDoc<ClassLoader JavaDoc> classLoaderList = new ArrayList JavaDoc<ClassLoader JavaDoc>();
44     private ClassLoader JavaDoc parentCL = null;
45     private String JavaDoc nameOfCL;
46     
47     public ClassLoaderChain(){
48         super();
49     }
50     
51     public ClassLoaderChain(ClassLoader JavaDoc parent){
52         super(parent);
53         this.parentCL = parent;
54     }
55     
56     public void setName(String JavaDoc n) {
57         this.nameOfCL = n;
58     }
59
60     public String JavaDoc getName() {
61         return this.nameOfCL;
62     }
63
64
65     public void addToList(ClassLoader JavaDoc cl) {
66         this.classLoaderList.add(cl);
67     }
68
69     protected Class JavaDoc<?> findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
70         throw new ClassNotFoundException JavaDoc(name);
71     }
72     
73     /**
74      * Special loadClass method that attempts to a load a class in the chain,
75      * but skips the classLoader mentioned as "toSkip" to prevent infinite
76      * recursion, while going through the chain.
77      * @param name The binary name of the class
78      * @param toSkip ClassLoader component in the chain to skip while attempting
79      * to load a class.
80      * @return The resulting <code>Class</code> object
81      * @throws ClassNotFoundException If the class was not found in the other
82      * components of the chain.
83      */

84     protected Class JavaDoc loadClass(String JavaDoc name, ClassLoader JavaDoc toSkip)
85                                         throws ClassNotFoundException JavaDoc {
86         //Iterate through all components in the chain.
87
for (Iterator JavaDoc<ClassLoader JavaDoc> iter = classLoaderList.iterator();
88                                                         iter.hasNext();) {
89             ClassLoader JavaDoc element = iter.next();
90             
91             if(element.equals(toSkip)) {
92                 continue;
93             }
94             
95             Class JavaDoc clz = null;
96             try {
97                 //Attempt to load the class from a component in the chain.
98
clz = element.loadClass(name);
99                 if (clz != null) {
100                     return clz;
101                 }
102             } catch (ClassNotFoundException JavaDoc e) {
103                 //Ignore if a component in the chain fails to load the class.
104
//e.printStackTrace();
105
}
106         }
107         //All components in the chain failed to find the class.
108
throw new ClassNotFoundException JavaDoc(name);
109     }
110     
111
112     /**
113      * Loads the class with the specified binary name.
114      * The traditional loadClass has been overridden to check if component
115      * classloaders in the chain could find the class.
116      */

117     @Override JavaDoc
118     public Class JavaDoc<?> loadClass(String JavaDoc className,boolean resolve)
119                                         throws ClassNotFoundException JavaDoc {
120         //System.out.println(className + " attempted in Chain" + name );
121
Class JavaDoc c = findLoadedClass(className);
122         if( c != null ) {
123             return c;
124         }
125         //XXX: What happens if parentCL is a chain.
126
if( parentCL instanceof ClassLoaderChain ) {
127             //warning message
128
throw new RuntimeException JavaDoc("ClassLoader " + this.toString()
129                         + " parent is a chain " + parentCL.toString() );
130         }
131         if (parentCL != null) {
132             try {
133                 //Delegate to parent of chain first.!
134
c = parentCL.loadClass(className);
135                 if (c != null) {
136                     if (resolve) {
137                         resolveClass(c);
138                     }
139                 }
140             } catch(ClassNotFoundException JavaDoc e) {
141                 //ignore
142
}
143             if(c != null) {
144                 //System.out.println("parent of " + this + "loaded " + className);
145
return c;
146             }
147         }
148
149         //Traverse through all classloaders in the chain to find class
150
for (ClassLoader JavaDoc element : classLoaderList) {
151             if(element == null)
152                 continue;
153             
154             Class JavaDoc clz = null;
155             try {
156                 clz = element.loadClass(className);
157                 if (clz != null) {
158                     if(resolve) {
159                         resolveClass(clz);
160                     }
161                     return clz;
162                 }
163                 //System.out.println(className + "was loaded by"+clz.getClassLoader());
164
} catch (ClassNotFoundException JavaDoc e) {
165                 //e.printStackTrace();
166
}
167         }
168         //All components in the chain failed to find the class.
169
throw new ClassNotFoundException JavaDoc(className);
170     }
171     
172     @Override JavaDoc
173     protected URL JavaDoc findResource(String JavaDoc name) {
174         for(ClassLoader JavaDoc cl:classLoaderList) {
175             URL JavaDoc res = cl.getResource(name);
176             if (res != null) return res;
177         }
178         return null;
179     }
180     
181     @Override JavaDoc
182     protected Enumeration JavaDoc<URL JavaDoc> findResources(String JavaDoc name) throws IOException JavaDoc {
183         for(ClassLoader JavaDoc cl:classLoaderList) {
184             Enumeration JavaDoc<URL JavaDoc> res = cl.getResources(name);
185             List JavaDoc<URL JavaDoc> al = new ArrayList JavaDoc<URL JavaDoc>();
186             while(res.hasMoreElements()) {
187                 al.add(res.nextElement());
188             }
189             if (al.size() != 0) {
190                 return ((new java.util.Vector JavaDoc<URL JavaDoc>(al)).elements());
191             }
192         }
193         return ((new java.util.Vector JavaDoc<URL JavaDoc>()).elements());
194     }
195     
196     @Override JavaDoc
197     public String JavaDoc toString() {
198         String JavaDoc s = this.nameOfCL + " parentCL :: " + this.parentCL +
199                                                 " constituent CLs :: \n";
200         for(ClassLoader JavaDoc cl:this.classLoaderList) {
201             String JavaDoc nameofCL1 = null;
202             if (cl instanceof ASURLClassLoader) {
203                 //As of now limiting this to just get the name.
204
nameofCL1 = ((ASURLClassLoader)cl).getName();
205             } else if (cl instanceof ClassLoaderChain) {
206                 nameofCL1 = ((ClassLoaderChain)cl).getName();
207             }
208             s += " :: " + nameofCL1;
209         }
210         s += "\n";
211         return s;
212     }
213 }
214
215
216
Popular Tags