KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.server.PELaunch;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29
30 /**
31  * <code>ASURLClassLoader</code> represents a individual component
32  * in the <code>ASClassLoaderChain</code>.
33  *
34  * Since it is a subclass of <code>URLClassLoader</code> it can contain a
35  * search path of one or more URLs
36  *
37  * @author Harsha RA, Sivakumar Thyagarajan
38  */

39
40 public class ASURLClassLoader extends URLClassLoader JavaDoc {
41     /** List of URLs that this classloader checks for a class */
42     private URL JavaDoc[] classLoaderList = new URL JavaDoc[]{};
43     /** Name of this classloader - primarily used in the string
44         representation of the classloader - for debugging */

45     private String JavaDoc nameOfCL = null;
46     /** The classloader chain this classloader is a component of */
47     private ClassLoader JavaDoc parentChain = null;
48     /** toString() representation of parent - to prevent recursion */
49     private String JavaDoc parentToString = null;
50 /*
51     public ASURLClassLoader(URL[] urls) {
52         super(urls); //Parent: System ClassLoader.
53         this.classLoaderList = urls;
54     }
55 */

56
57     public ASURLClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
58         //The parent of the classloader chain component
59
//is either the SystemClassLoader (if shared chain is null!)
60
//or the Shared Chain.
61
//super(urls, PELaunch.getSharedChain());
62
super(urls, PELaunch.getSharedChain()!=null ?PELaunch.getSharedChain()
63                                                                                              : ClassLoader.getSystemClassLoader());
64         this.parentChain = parent;
65         
66         if(this.parentChain instanceof ClassLoaderChain) {
67             //Computed earlier to prevent recursion in toString of this
68
//class
69
this.parentToString =
70                          ((ClassLoaderChain)this.parentChain).getName();
71         } else {
72             this.parentToString = this.parentChain.toString();
73         }
74         this.classLoaderList = urls;
75     }
76     
77     public void setName(String JavaDoc n) {
78         this.nameOfCL = n;
79     }
80     
81     public String JavaDoc getName() {
82         return this.nameOfCL;
83     }
84     
85    /**
86     * The loading strategy is:
87     * - check self first and
88     * - then delegate to parent chain if one exists. This is done to prevent recursion
89     * while referring to the chain. One issue with this approach is that when class load
90     * requests come via this component classloader, it might override a duplicate class in
91     * a peer present earlier in the chain.
92     */

93     @Override JavaDoc
94     public Class JavaDoc<?> loadClass(String JavaDoc name, boolean resolve)
95                                             throws ClassNotFoundException JavaDoc {
96         try {
97             //Hands over to URLClassLoader.loadClass. First attempt to delegate
98
//to parent and then check for the class in the
99
//<code>classLoaderList</code>.
100
Class JavaDoc cls = super.loadClass(name, resolve);
101             if (cls != null) {
102                 return cls;
103             }
104             //Class not in this chain component.
105
throw new ClassNotFoundException JavaDoc(name);
106         } catch(ClassNotFoundException JavaDoc e) {
107             //If the parent is a ClassLoaderChain, check in the ClassLoaderChain
108
//ignoring this classloader component instance. Failure to do this
109
//would result in an infinte recursive call.
110
if ((parentChain != null) &&
111                              (parentChain instanceof ClassLoaderChain)) {
112                 ClassLoaderChain chain = (ClassLoaderChain)parentChain;
113                 //Ask parent chain to load class, skipping self
114
Class JavaDoc c = chain.loadClass(name,this);
115                 //Resolve if required.
116
if (c != null) {
117                     if(resolve) {
118                         resolveClass(c);
119                     }
120                     return c;
121                 }
122             }
123             throw e;
124         }
125
126     }
127
128     @Override JavaDoc
129     public String JavaDoc toString() {
130         String JavaDoc s = this.nameOfCL + " parentCL :: " +
131                              this.parentToString + " URLs :: \n";
132         for(URL JavaDoc u:classLoaderList){
133             s += ", " + u;
134         }
135         s +="\n";
136         return s;
137     }
138 }
139
Popular Tags