KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > loader > AbsModuleClassLoader


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Guillaume SAUTHIER
22  * --------------------------------------------------------------------------
23  * $Id: AbsModuleClassLoader.java,v 1.6 2004/12/16 16:09:11 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.loader;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import org.objectweb.jonas_lib.loader.factory.URLFactory;
37 import org.objectweb.jonas_lib.loader.locator.Locator;
38
39 /**
40  * The <code>AbsModuleClassLoader</code> class is used in JOnAS
41  * for loading web, ejbjars and client modules.
42  *
43  * @author Guillaume Sauthier
44  */

45 public abstract class AbsModuleClassLoader extends URLClassLoader JavaDoc {
46
47     /** factories used to create URLs for URLClassLoader */
48     private URLFactory[] factories;
49
50     /** locators used to search jar/dir for files, directories, ... */
51     private Locator[] locators;
52
53     /** bases URLs */
54     private URL JavaDoc[] bases;
55
56     /**
57      * Create a new AbsModuleClassLoader for a list of URLs.
58      *
59      * @param modules the list of URL to be used in the ClassLoader.
60      *
61      * @throws IOException when Initialization fails.
62      */

63     public AbsModuleClassLoader(URL JavaDoc[] modules) throws IOException JavaDoc {
64         super(new URL JavaDoc[0]);
65         bases = modules;
66         init();
67     }
68
69     /**
70      * Create a new AbsModuleClassLoader for a list of URLs with a specified parent loader.
71      *
72      * @param modules the list of URL to be used in the ClassLoader.
73      * @param parent the parent ClassLoader to be used.
74      *
75      * @throws IOException when Initialization fails.
76      */

77     public AbsModuleClassLoader(URL JavaDoc[] modules, ClassLoader JavaDoc parent) throws IOException JavaDoc {
78         super(new URL JavaDoc[0], parent);
79         bases = modules;
80         init();
81     }
82
83     /**
84      * Base Initialization of the ClassLoader, storage of URL list.
85      *
86      * @throws IOException when URL pointed file is not supported (not an jar nor a directory).
87      */

88     protected void init() throws IOException JavaDoc {
89         factories = new URLFactory[bases.length];
90         locators = new Locator[bases.length];
91
92         // Create factories and locator for each URL
93
for (int i = 0; i < bases.length; i++) {
94
95             factories[i] = URLFactory.getFactory(bases[i]);
96             locators[i] = Locator.getLocator(bases[i]);
97         }
98     }
99
100     /**
101      * Add specified location into the repository.
102      * If location is found in multiple URLs of the bases, new
103      * URLs will be added multiple times.
104      *
105      * @param location an entry name (for a jar) or a path name (for a directory)
106      *
107      * @throws IOException when constructed URL is malformed
108      */

109     protected void addInRepository(String JavaDoc location) throws IOException JavaDoc {
110         // test existence in each base URL
111
for (int i = 0; i < bases.length; i++) {
112             if (locators[i].hasDirectory(location)
113                 || locators[i].hasFile(location)) {
114                 addURL(factories[i].getURL(location));
115             }
116         }
117     }
118
119     /**
120      * Add specified location childs into the repository.
121      * Search will be performed on the bases URLs.
122      *
123      * @param location an entry name (for a jar) or a path name (for a directory)
124      *
125      * @throws IOException When a directory content cannot be explored.
126      *
127      * TODO test existence is now done in AbsModuleClassLoader
128      */

129     protected void addContentInRepository(String JavaDoc location) throws IOException JavaDoc {
130         // test existence in each base URL
131
for (int i = 0; i < bases.length; i++) {
132             List JavaDoc list = locators[i].listContent(location);
133             for (Iterator JavaDoc l = list.iterator(); l.hasNext();) {
134                 addURL(factories[i].getURL((String JavaDoc) l.next()));
135             }
136         }
137     }
138
139     /**
140      * @return Returns a String representation of the AbsModuleClassLoader
141      */

142     public String JavaDoc toString() {
143         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144
145         sb.append("classloader : " + getClass().getName() + "\n");
146         sb.append("\tmodules bases (not in loader!) : \n");
147         for (int i = 0; i < bases.length; i++) {
148             sb.append("\t\t -" + bases[i] + "\n");
149         }
150         sb.append("\trepositories :\n");
151         URL JavaDoc[] rep = getURLs();
152         for (int i = 0; i < rep.length; i++) {
153             sb.append("\t\t -" + rep[i] + "\n");
154         }
155         sb.append("\tparent : " + getParent() + "\n");
156
157         return sb.toString();
158     }
159
160     /**
161      * @return Returns the bases.
162      */

163     public URL JavaDoc[] getBases() {
164         return bases;
165     }
166
167     /**
168      * @return Returns a String representation of the classpath used by this classloader
169      */

170     public String JavaDoc getClasspath() {
171         URL JavaDoc[] urls = getURLs();
172         StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
173         for (int i = 0; i < urls.length; i++) {
174             String JavaDoc url = urls[i].getFile();
175             // do not add URL with !/ inside
176
if (url.indexOf("!/") == -1) {
177                 cp.append(File.pathSeparator + url);
178             }
179         }
180         return cp.toString();
181     }
182 }
183
Popular Tags