KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.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: ComponentClassLoaderTest.java 154 2006-03-27 15:30:10Z 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.List JavaDoc;
28
29 /**
30  * @author ofabre - eBMWebsourcing
31  * @author ddesjardins - eBMWebsourcing
32  */

33 public class ComponentClassLoader extends PetalsClassLoader {
34
35     /**
36      * List of the sharedlibraries that must be included in the classloading
37      * strategy.
38      */

39     private List JavaDoc<String JavaDoc> sharedLibrariesNameList;
40
41     private SharedLibrariesClassLoader sharedLibrariesClassLoader;
42
43     public ComponentClassLoader(URL JavaDoc[] baseUrls, List JavaDoc<String JavaDoc> classpathLocations, SharedLibrariesClassLoader parent, List JavaDoc<String JavaDoc> sharedLibrariesNameList) throws IOException JavaDoc {
44         super(baseUrls, classpathLocations, parent);
45
46         this.sharedLibrariesNameList = sharedLibrariesNameList;
47
48         this.sharedLibrariesClassLoader = parent;
49     }
50
51     /**
52      * If class is not found, search in the sharedLibrariesClassLoader, by
53      * specifying the sharedLibraries to use.
54      *
55      * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
56      */

57     @Override JavaDoc
58     protected synchronized Class JavaDoc<?> loadClass(String JavaDoc className, boolean resolve) throws ClassNotFoundException JavaDoc {
59         Class JavaDoc<?> clazz = null;
60         clazz = findLoadedClass(className);
61         if (clazz == null) {
62             // parent first
63
if (useParentFirst(className, true)) {
64                 try {
65                     clazz = sharedLibrariesClassLoader.loadClass(className, resolve, sharedLibrariesNameList);
66                 }
67                 catch (ClassNotFoundException JavaDoc cnfe) {
68                     clazz = findClass(className);
69                 }
70             }
71             // self first
72
else {
73                 try {
74                     clazz = findClass(className);
75                 }
76                 catch (ClassNotFoundException JavaDoc cnfe) {
77                     if (isIsolated()) {
78                         throw cnfe;
79                     }
80                     else {
81                         clazz = sharedLibrariesClassLoader.loadClass(className, resolve, sharedLibrariesNameList);
82                     }
83                 }
84             }
85         }
86         if (resolve) {
87             resolveClass(clazz);
88         }
89         return clazz;
90     }
91
92     /**
93      * If resource is not found, search in the sharedLibrariesClassLoader, by
94      * specifying the sharedLibraries to use.
95      *
96      * @see java.lang.ClassLoader#getResource(java.lang.String)
97      */

98     @Override JavaDoc
99     public synchronized URL JavaDoc getResource(String JavaDoc resourceName) {
100         URL JavaDoc url = null;
101         // parent first
102
if (isParentFirst()) {
103             url = sharedLibrariesClassLoader.getResource(resourceName, sharedLibrariesNameList);
104             if (url == null){
105                 // Try to search in the parent classpath
106
url = super.getResource(resourceName);
107             }
108         }
109         // self first
110
else {
111             url = super.getResource(resourceName);
112             if (url == null){
113                 // Try to search in its own classpath
114
url = sharedLibrariesClassLoader.getResource(resourceName, sharedLibrariesNameList);
115             }
116         }
117         return url;
118     }
119     
120     /**
121      * If resource is not found, search in the sharedLibrariesClassLoader, by
122      * specifying the sharedLibraries to use.
123      *
124      * @see java.lang.ClassLoader#getResourceAsStream(java.lang.String)
125      */

126     @Override JavaDoc
127     public synchronized InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName) {
128         InputStream JavaDoc inputStream = null;
129         // parent first
130
if (isParentFirst()) {
131             inputStream = sharedLibrariesClassLoader.getResourceAsStream(resourceName, sharedLibrariesNameList);
132             if (inputStream == null){
133                 // Try to search in the parent classpath
134
inputStream = super.getResourceAsStream(resourceName);
135             }
136         }
137         // self first
138
else {
139             inputStream = super.getResourceAsStream(resourceName);
140             if (inputStream == null){
141                 // Try to search in its own classpath
142
inputStream = sharedLibrariesClassLoader.getResourceAsStream(resourceName, sharedLibrariesNameList);
143             }
144         }
145         return inputStream;
146     }
147
148
149 }
150
Popular Tags