KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > whack > container > ComponentClassLoader


1 /**
2  * $RCSfile: ComponentClassLoader.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/04/12 07:06:38 $
5  *
6  * Copyright 2005 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.whack.container;
22
23 import java.io.File JavaDoc;
24 import java.io.FilenameFilter JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * ClassLoader for components. It searches the component directory for classes
34  * and JAR files, then constructs a class loader for the resources found.
35  * Resources are loaded as follows:<ul>
36  *
37  * <li>Any JAR files in the <tt>lib</tt> will be added to the classpath.
38  * <li>Any files in the classes directory will be added to the classpath.
39  * </ul>
40  *
41  * @author Derek DeMoro
42  * @author Gaston Dombiak
43  */

44 class ComponentClassLoader {
45
46     private URLClassLoader JavaDoc classLoader;
47
48     /**
49      * Constructs a component loader for the given component directory.
50      *
51      * @param componentDir the component directory.
52      * @throws java.lang.SecurityException if the created class loader violates
53      * existing security constraints.
54      * @throws java.net.MalformedURLException if a located resource name cannot be
55      * properly converted to a URL.
56      */

57     public ComponentClassLoader(File JavaDoc componentDir) throws MalformedURLException JavaDoc, SecurityException JavaDoc {
58         final List JavaDoc list = new ArrayList JavaDoc();
59         File JavaDoc classesDir = new File JavaDoc(componentDir, "classes");
60         if (classesDir.exists()) {
61             list.add(classesDir.toURL());
62         }
63         File JavaDoc libDir = new File JavaDoc(componentDir, "lib");
64         File JavaDoc[] jars = libDir.listFiles(new FilenameFilter JavaDoc() {
65             public boolean accept(File JavaDoc dir, String JavaDoc name) {
66                 return name.endsWith(".jar") || name.endsWith(".zip");
67             }
68         });
69         if (jars != null) {
70             for (int i = 0; i < jars.length; i++) {
71                 if (jars[i] != null && jars[i].isFile()) {
72                     list.add(jars[i].toURL());
73                 }
74             }
75         }
76         Iterator JavaDoc urls = list.iterator();
77         URL JavaDoc[] urlArray = new URL JavaDoc[list.size()];
78         for (int i = 0; urls.hasNext(); i++) {
79             urlArray[i] = (URL JavaDoc)urls.next();
80         }
81         classLoader = new URLClassLoader JavaDoc(urlArray, findParentClassLoader());
82     }
83
84     /**
85      * Load a class using this component class loader.
86      *
87      * @param name the fully qualified name of the class to load.
88      * @return The module object loaded
89      * @throws ClassNotFoundException if the class could not be loaded by this class loader.
90      * @throws IllegalAccessException if the class constructor was private or protected.
91      * @throws InstantiationException if the class could not be instantiated (initialization error).
92      * @throws SecurityException if the custom class loader not allowed.
93      */

94     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc,
95             InstantiationException JavaDoc, SecurityException JavaDoc
96     {
97         return classLoader.loadClass(name);
98     }
99
100     /**
101      * Destroys this class loader.
102      */

103     public void destroy() {
104         classLoader = null;
105     }
106
107     /**
108      * Locates the best parent class loader based on context.
109      *
110      * @return the best parent classloader to use.
111      */

112     private ClassLoader JavaDoc findParentClassLoader() {
113         ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
114         if (parent == null) {
115             parent = this.getClass().getClassLoader();
116         }
117         if (parent == null) {
118             parent = ClassLoader.getSystemClassLoader();
119         }
120         return parent;
121     }
122 }
Popular Tags