KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > ConnectorClassLoader


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.enterprise.util;
25
26 import java.io.File JavaDoc;
27 import java.util.*;
28 import java.net.*;
29
30
31 //START OF IASRI 4660742
32
import java.util.logging.*;
33 import com.sun.logging.*;
34 //END OF IASRI 4660742
35

36 // IASRI 4709925 - updated ejb class loader
37
import com.sun.enterprise.loader.EJBClassLoader;
38 import com.sun.enterprise.server.ResourcesUtil;
39
40
41 /**
42  * This class loader is responsible for loading standalone
43  * RAR files. This class loader is the parent of JarClassLoader
44  * (the application class loader)
45  *
46  * @author Tony Ng, Sivakumar Thyagarajan
47  */

48 public class ConnectorClassLoader extends EJBClassLoader {
49
50     // START OF IASRI 4660742
51
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
52     // END OF IASRI 4660742
53

54     private static ConnectorClassLoader classLoader = null;
55
56     /**
57      * A linked list of URL classloaders representing each deployed connector
58      * module
59      */

60     private List classLoaderChain= new LinkedList();
61
62     /**
63      * The parent class loader for the connector Class Loader [ie the common
64      * Classloader]
65      */

66     private ClassLoader JavaDoc parent= null;
67
68     /**
69      * Maintains a mapping between rar name and a classloader that has services
70      * that RAR module.
71      */

72     private Map rarModuleClassLoaders = new HashMap();
73
74     public static ConnectorClassLoader getInstance() {
75     if (classLoader == null) {
76         synchronized(ConnectorClassLoader.class) {
77         classLoader = new ConnectorClassLoader();
78         }
79     }
80     return classLoader;
81     }
82
83     private ConnectorClassLoader() {
84         super();
85     }
86
87     // START OF IASRI 4628197: allows to pass in parent class loader
88
private ConnectorClassLoader(ClassLoader JavaDoc parent) {
89         super(parent);
90         this.parent= parent;
91     }
92
93     /**
94      * Initializes this sigleton with the given parent class loader
95      * if not already created.
96      *
97      * @param parent parent class loader
98      * @return the instance
99      */

100     public static ConnectorClassLoader getInstance(ClassLoader JavaDoc parent) {
101         if (classLoader == null) {
102             synchronized(ConnectorClassLoader.class) {
103                 classLoader = new ConnectorClassLoader(parent);
104             }
105         }
106         return classLoader;
107     }
108     // END OF IASRI 4628197: allows to pass in parent class loader
109

110
111     /**
112      * Adds the requested resource adapter to the ConnectorClassLoader. A
113      * ConnectorClassLoader is created with the moduleDir as its search path
114      * and this classloader is added to the classloader chain.
115      *
116      * @param rarName
117      * the resourceAdapter module name to add
118      * @param moduleDir
119      * the directory location where the RAR contents are exploded
120      */

121     public void addResourceAdapter(String JavaDoc rarName, String JavaDoc moduleDir) {
122
123         try {
124             //System RARs are placed in classpath and hence would be handled
125
//by the parent classloader
126
if(!ResourcesUtil.getInstance().belongToSystemRar(rarName)) {
127                 File JavaDoc file= new File JavaDoc(moduleDir);
128                 EJBClassLoader cl= new EJBClassLoader(parent);
129                 cl.appendURL(file.toURI().toURL());
130                 classLoaderChain.add(cl);
131                 rarModuleClassLoaders.put(rarName, cl);
132             }
133         } catch (MalformedURLException ex) {
134             _logger.log(
135                     Level.SEVERE,
136                     "enterprise_util.connector_malformed_url",
137                     ex);
138         }
139     }
140
141     /**
142      * Removes the resource adapter's class loader from the classloader linked
143      * list
144      *
145      * @param moduleName the connector module that needs to be removed.
146      */

147     public void removeResourceAdapter(String JavaDoc moduleName) {
148         EJBClassLoader classLoaderToRemove=
149         (EJBClassLoader) rarModuleClassLoaders.get(moduleName);
150         if (classLoaderToRemove != null) {
151             classLoaderChain.remove(classLoaderToRemove);
152             rarModuleClassLoaders.remove(moduleName);
153             classLoaderToRemove = null;
154             _logger.log(
155                 Level.WARNING,
156                 "The Connector module "
157                 + moduleName
158                 + " has been removed. Please redeploy all applications that "
159                 + "are using this connector module's resources");
160         }
161     }
162
163
164     /*
165      * Loads the class with the specified name and resolves it if specified.
166      *
167      * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
168      */

169     public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
170         throws ClassNotFoundException JavaDoc {
171         Class JavaDoc clz = null;
172         //Use the delegation model to service class requests that could be
173
//satisfied by parent [common class loader].
174
if(parent != null) {
175             try {
176                 clz = parent.loadClass(name);
177                 if (clz != null) {
178                     if(resolve) {
179                     resolveClass(clz);
180                     }
181                 return clz;
182                 }
183             } catch (ClassNotFoundException JavaDoc e) {
184             //ignore and try the connector modules classloader
185
//chain.
186
}
187         } else {
188             return super.loadClass(name,resolve);
189         }
190             
191         //Going through the connector module classloader chain to find
192
// class and return the first match.
193
for (Iterator iter= classLoaderChain.iterator(); iter.hasNext();) {
194             EJBClassLoader ccl= (EJBClassLoader) iter.next();
195             try {
196                 clz= ccl.loadClass(name);
197                 if (clz != null){
198                     if(resolve) {
199                         resolveClass(clz);
200                     }
201                     return clz;
202                 }
203             } catch (ClassNotFoundException JavaDoc cnfe) {
204                 //ignore this exception and continue with next classloader in
205
// chain
206
continue;
207             }
208         }
209         
210         //Can't find requested class in parent and in our classloader chain
211
throw new ClassNotFoundException JavaDoc(name);
212     }
213
214    /**
215      * Returns all the resources of the connector classloaders in the chain,
216      * concatenated to a classpath string.
217      *
218      * Notice that this method is called by the setClassPath() method of
219      * org.apache.catalina.loader.WebappLoader, since the ConnectorClassLoader does
220      * not extend off of URLClassLoader.
221      *
222      * @return Classpath string containing all the resources of the connectors
223      * in the chain. An empty string if there exists no connectors in the chain.
224      */

225     
226     public String JavaDoc getClasspath(){
227             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
228             for (int i = 0; i < classLoaderChain.size(); i++) {
229                     EJBClassLoader ecl= (EJBClassLoader) classLoaderChain.get(i);
230                     String JavaDoc eclClasspath = ecl.getClasspath();
231                     if ( eclClasspath != null) {
232                             if (i > 0) strBuf.append(File.pathSeparator);
233                             strBuf.append(eclClasspath);
234                     }
235             }
236             return strBuf.toString();
237     }
238 }
239
Popular Tags