KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > classloader > SharedLibrariesClassLoader


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SharedLibrariesClassLoader.java 216 2006-04-13 14:18:14Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.classloader;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * This classLoader englobes all SharedLibraries classLoader. When the
35  * ClassLoader of a component does not found a class, This classLoader search in
36  * all the SLClassLoader the class. If the class is not found in those
37  * SLClassLoader, search in the parent classLoader.
38  *
39  * @version $Rev: 216 $ $Date: 2006-04-13 16:18:14 +0200 (jeu., 13 avr. 2006) $
40  * @since Petals 1.0
41  * @author Adrien LOUIS
42  * @author ddesjardins - eBMWebsourcing
43  */

44 public class SharedLibrariesClassLoader extends ClassLoader JavaDoc {
45
46     protected Map JavaDoc<String JavaDoc, PetalsClassLoader> slClassLoaders;
47
48     /**
49      * Parent Class Loader.
50      */

51     protected ClassLoader JavaDoc parent;
52
53     /**
54      * Create a SharedLibrariesClassLoader, with SystemClassLoader as parent.
55      *
56      * @throws IOException
57      */

58     public SharedLibrariesClassLoader() throws IOException JavaDoc {
59         super();
60         parent = getSystemClassLoader();
61         slClassLoaders = Collections.synchronizedMap(new HashMap JavaDoc<String JavaDoc, PetalsClassLoader>());
62     }
63
64     public void addSharedLibrariesClassLoader(String JavaDoc id, PetalsClassLoader cl) {
65         cl.setParentFirst(false);
66         slClassLoaders.put(id, cl);
67     }
68
69     public void removeSharedLibrariesClassLoader(String JavaDoc id) {
70         slClassLoaders.remove(id);
71     }
72
73     // ----------------------------------------------------------------------
74
// overriding methods
75
// ----------------------------------------------------------------------
76

77     /**
78      * Search in all referenced SharedLibrariesClassLoaders for the requested
79      * class. If the class is not found, search in the parent classLoader.
80      *
81      * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
82      */

83     protected synchronized Class JavaDoc<?> loadClass(String JavaDoc className, boolean resolve, List JavaDoc<String JavaDoc> sharedLibrariesNameList) throws ClassNotFoundException JavaDoc {
84         Class JavaDoc<?> clazz = null;
85         clazz = findLoadedClass(className, sharedLibrariesNameList);
86         if (clazz == null) {
87             // self first
88
try {
89                 clazz = findClass(className, sharedLibrariesNameList);
90             }
91             catch (ClassNotFoundException JavaDoc cnfe) {
92                 clazz = parent.loadClass(className);
93             }
94         }
95         if (resolve) {
96             resolveClass(clazz);
97         }
98         return clazz;
99     }
100
101     /**
102      * Search in all referenced SharedLibrariesClassLoaders for the requested
103      * resource.
104      *
105      */

106     protected synchronized URL JavaDoc getResource(String JavaDoc resourceName, List JavaDoc<String JavaDoc> sharedLibrariesNameList) {
107         URL JavaDoc url = null;
108         for (Iterator JavaDoc iter = sharedLibrariesNameList.iterator(); iter.hasNext() && url == null;) {
109             PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next());
110             if (element != null) {
111                 url = element.getResource(resourceName);
112             }
113         }
114         return url;
115     }
116
117     /**
118      * Search in all referenced SharedLibrariesClassLoaders for the requested
119      * resource.
120      *
121      */

122     protected synchronized InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName, List JavaDoc<String JavaDoc> sharedLibrariesNameList) {
123         InputStream JavaDoc inputStream = null;
124         for (Iterator JavaDoc iter = sharedLibrariesNameList.iterator(); iter.hasNext() && inputStream == null;) {
125             PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next());
126             if (element != null) {
127                 inputStream = element.getResourceAsStream(resourceName);
128             }
129         }
130         return inputStream;
131     }
132
133     /**
134      * Ask to each referenced SLClassLoader (which name is specified in the
135      * list) to find the class. If none of the SLclassLoader knows this class,
136      * throw an exception
137      *
138      * @throws ClassNotFoundException
139      * @see java.net.URLClassLoader#findClass(java.lang.String)
140      */

141     protected Class JavaDoc<?> findClass(String JavaDoc className, List JavaDoc<String JavaDoc> sharedLibrariesNameList) throws ClassNotFoundException JavaDoc {
142         Class JavaDoc result = null;
143         if (sharedLibrariesNameList != null) {
144             for (Iterator JavaDoc iter = sharedLibrariesNameList.iterator(); iter.hasNext() && result == null;) {
145                 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next());
146                 if (element != null) {
147                     try {
148                         result = element.findClass(className);
149                     }
150                     catch (ClassNotFoundException JavaDoc e) {
151                         // the class is not found with this class loader,
152
// search in the next one
153
}
154                 }
155             }
156         }
157         if (result == null) {
158             throw new ClassNotFoundException JavaDoc();
159         }
160         return result;
161     }
162
163     protected Class JavaDoc<?> findLoadedClass(String JavaDoc className, List JavaDoc<String JavaDoc> sharedLibrariesNameList) {
164         Class JavaDoc result = null;
165         if (sharedLibrariesNameList != null) {
166             for (Iterator JavaDoc iter = sharedLibrariesNameList.iterator(); iter.hasNext() && result == null;) {
167                 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next());
168                 if (element != null) {
169                     try {
170                         result = element.findLoadedClazz(className);
171                     }
172                     catch (ClassNotFoundException JavaDoc e) {
173                         // the class is not found with this class loader,
174
// search in the next one
175
}
176                 }
177             }
178         }
179         return result;
180     }
181
182 }
183
Popular Tags