KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > JClassLoader


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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  * --------------------------------------------------------------------------
22  * $Id: JClassLoader.java,v 1.12 2005/04/22 13:45:57 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.server;
27
28 import java.io.File JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31
32 /**
33  * This class implements a URLClassLoader, that permits to add some URLs in it.
34  * @author Ludovic Bert
35  * @author Florent Benoit
36  */

37 public class JClassLoader extends URLClassLoader JavaDoc {
38
39     /**
40      * JClassLoader name
41      */

42     private String JavaDoc name = null;
43
44     /**
45      * Need to recompute toString() value ? (urls have changed)
46      * True by default (not done).
47      * Then, compute is done only when required and if needed
48      */

49     private boolean recomputeToString = true;
50
51     /**
52      * Need to recompute getClassPath() value ? (urls have changed)
53      * True by default (not done).
54      * Then, compute is done only when required and if needed
55      */

56     private boolean recomputeClassPath = true;
57
58     /**
59      * String representation used by toString() method
60      */

61     private String JavaDoc toStringValue = null;
62
63     /**
64      * Classpath value
65      */

66     private String JavaDoc classpath = null;
67
68     /**
69      * Constructs a new ClassLoader with the specified URLs.
70      * @param name ClassLoader name (used for Display)
71      * @param urls the URLs to add at the ClassLoader creation.
72      * @param parent parent ClassLoader, null if no parent.
73      */

74     public JClassLoader(String JavaDoc name, URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
75         super(urls, parent);
76         this.name = name;
77         this.recomputeToString = true;
78         this.recomputeClassPath = true;
79     }
80
81     /**
82      * Constructs a new ClassLoader with the specified URLs.
83      * Uses the default delegation parent classloader.
84      * @param name ClassLoader name (used for Display)
85      * @param urls the URLs to add at the ClassLoader creation.
86      */

87     public JClassLoader(String JavaDoc name, URL JavaDoc[] urls) {
88         super(urls);
89         this.name = name;
90         this.recomputeToString = true;
91         this.recomputeClassPath = true;
92     }
93
94     /**
95      * Add the specified URL to this ClassLoader.
96      * @param url the URL to add to this ClassLoader.
97      */

98     public void addURL(URL JavaDoc url) {
99         if (url != null) {
100             super.addURL(url);
101         }
102         this.recomputeToString = true;
103         this.recomputeClassPath = true;
104     }
105
106     /**
107      * Add the specified URLs to this ClassLoader.
108      * @param urls the URLs to add to this ClassLoader.
109      */

110     public void addURLs(URL JavaDoc[] urls) {
111         if (urls != null) {
112             for (int i = 0; i < urls.length; i++) {
113                 if (urls[i] != null) {
114                     super.addURL(urls[i]);
115                 }
116             }
117         }
118         this.recomputeToString = true;
119         this.recomputeClassPath = true;
120     }
121
122     /**
123      * Display all the URLs from the class loader
124      */

125     public void printURLs() {
126         System.out.println(name + " ClassLoader :");
127         URL JavaDoc[] urls = super.getURLs();
128         for (int i = 0; i < urls.length; i++) {
129             System.out.println("url=" + (new File JavaDoc(urls[i].getFile())).getAbsolutePath());
130         }
131         // display parent classloader
132
if (getParent() != null && getParent() instanceof JClassLoader) {
133             System.out.println("parent :");
134             ((JClassLoader) getParent()).printURLs();
135         }
136     }
137
138     /**
139      * Get the class path of this classloader
140      * @return the class path of this classloader
141      */

142     public String JavaDoc getClassPath() {
143         // urls have changed, need to build value
144
if (recomputeClassPath) {
145             computeClassPath();
146         }
147         return classpath;
148     }
149
150     /**
151      * Displays useful information
152      * @return information
153      */

154     public String JavaDoc toString() {
155         // urls have changed, need to build value
156
if (recomputeToString) {
157             computeToString();
158         }
159         return toStringValue;
160     }
161
162     /**
163      * Compute a string representation used by toString() method
164      */

165     private void computeToString() {
166         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
167         sb.append(this.getClass().getName());
168         sb.append("[");
169         sb.append(name);
170         sb.append(", urls=");
171         URL JavaDoc[] urls = getURLs();
172         for (int u = 0; u < urls.length; u++) {
173             sb.append(urls[u]);
174             if (u != urls.length - 1) {
175                 sb.append(";");
176             }
177         }
178         sb.append("]");
179         toStringValue = sb.toString();
180
181         // value is updated, no need to do it again.
182
recomputeToString = false;
183    }
184
185     /**
186      * Compute classpath value
187      */

188     private void computeClassPath() {
189         String JavaDoc cp = "";
190         // add parent classpath before
191
if (getParent() != null && getParent() instanceof JClassLoader) {
192             cp += ((JClassLoader) getParent()).getClassPath();
193         }
194         URL JavaDoc[] urls = super.getURLs();
195         for (int i = 0; i < urls.length; i++) {
196             cp = cp + File.pathSeparator + (new File JavaDoc(urls[i].getFile())).getAbsolutePath();
197         }
198         classpath = cp;
199         // update value
200
recomputeClassPath = false;
201     }
202
203
204 }
205
Popular Tags