KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > classloader > ClassPath


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: ClassPath.java,v 1.3 2005/06/10 20:43:51 slobodan Exp $
23  */

24
25 package com.lutris.classloader;
26
27 // lutris packages
28
import java.io.File JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import com.lutris.logging.LogChannel;
34 import com.lutris.util.FatalExceptionError;
35
36 /**
37  * <P>A class path that is composed of <CODE>ClassPathEntry</CODE> objects.
38  * This class can be used in conjunction with a class loader to load
39  * classes and resources.
40  *
41  * @author Kristen Pol, Lutris Technologies
42  * @version $Revision : 1.0 $
43  * @see com.lutris.classloader.ClassPathEntry
44  * @see com.lutris.classloader.Resource
45  */

46 class ClassPath {
47
48     // private data members
49

50     /** The class path Vector made up of ClassPathEntry objects. */
51     private Vector JavaDoc classPath = null;
52
53     /** Is logging enabled? */
54     private boolean loggingEnabled = false;
55
56     /** Log channel to write messages to */
57 // v. strahinja, 23 sep 2002
58
private LogChannel logChannel;
59 // private Logger logger;
60

61     /** Numeric log level number for LOGLEVEL string */
62 // v. strahinja, 23 sep 2002
63
private int logLevel;
64 // private Level logLevel;
65

66     // constructors
67

68     /**
69      * Constructs empty class path with no entries.
70      *
71      * @param loadLogChannel The log channel, maybe null.
72      * @see #set
73      * @see #add
74      */

75 // v. strahinja, 23 sep 2002
76
public ClassPath(LogChannel loadLogChannel) {
77 // v. strahinja, 23 sep 2002
78
this((Vector JavaDoc)null, loadLogChannel);
79 // public ClassPath(Logger loadLogger) {
80
// this((Vector)null, loadLogger);
81
}
82
83     /**
84      * Constructs class path with specified class path entries. The
85      * parameter is assumed to be an array of directories, URLs, and/or
86      * zip files.
87      *
88      * @param entries The class path represented by a String array.
89      * @param loadLogChannel The log channel, maybe null.
90      */

91 // v. strahinja, 23 sep 2002
92
public ClassPath(String JavaDoc[] entries, LogChannel loadLogChannel) {
93 // v. strahinja, 23 sep 2002
94
this(convertArrayToVector(entries, loadLogChannel), loadLogChannel);
95 // public ClassPath(String[] entries, Logger loadLogger) {
96
// this(convertArrayToVector(entries, loadLogger), loadLogger);
97
}
98
99     /**
100      * Constructs class path with specified class path entries. The
101      * parameter is assumed to be an array of zip files and/or directories.
102      *
103      * @param entries The class path represented by a File array.
104      * @param loadLogChannel The log channel, maybe null.
105      */

106 // v. strahinja, 23 sep 2002
107
public ClassPath(File JavaDoc[] entries, LogChannel loadLogChannel) {
108 // v. strahinja, 23 sep 2002
109
this(convertArrayToVector(entries, loadLogChannel), loadLogChannel);
110 // public ClassPath(File[] entries, Logger loadLogger) {
111
// this(convertArrayToVector(entries, loadLogger), loadLogger);
112
}
113
114     /**
115      * Constructs class path with specified class path entries. The URLs can
116      * represent directories and/or zip files on the local machine and/or
117      * on remote machines.
118      *
119      * @param entries The class path represented by a URL array.
120      * @param loadLogChannel The log channel, maybe null.
121      */

122 // v. strahinja, 23 sep 2002
123
public ClassPath(URL JavaDoc[] entries, LogChannel loadLogChannel) {
124 // v. strahinja, 23 sep 2002
125
this(convertArrayToVector(entries, loadLogChannel), loadLogChannel);
126 // public ClassPath(URL[] entries, Logger loadLogger) {
127
// this(convertArrayToVector(entries, loadLogger), loadLogger);
128
}
129
130     // private helper constructors
131

132     /**
133      * Constructs class path with specified class path entries.
134      * Vector entries must be <CODE>ClassPathEntry</CODE> objects.
135      *
136      * @param entries The class path entries.
137      * @param loadLogChannel The log channel, maybe null.
138      * @see ClassPathEntry
139      */

140 // v. strahinja, 23 sep 2002
141
private ClassPath(Vector JavaDoc entries, LogChannel loadLogChannel) {
142 // private ClassPath(Vector entries, Logger loadLogger) {
143
/*
144      * This constructor actually does all the work, because all
145      * other constructors call this one.
146      */

147         classPath = new Vector JavaDoc();
148         set(entries);
149
150 // v. strahinja, 23 sep 2002
151
logChannel = loadLogChannel;
152 // v. strahinja, 23 sep 2002
153
if (logChannel != null) {
154 // v. strahinja, 23 sep 2002
155
logLevel = logChannel.getLevel(MultiClassLoader.LOG_LEVEL);
156 // v. strahinja, 23 sep 2002
157
loggingEnabled = logChannel.isEnabled(logLevel);
158 // v. strahinja, 23 sep 2002
159
}
160 // logger = loadLogger;
161
// if (logger != null) {
162
//logLevel = logger.getLevel();
163
//if (logLevel == null) {
164
// logLevel = Level.DEBUG;
165
//}
166
// loggingEnabled = logger.isEnabledFor(logLevel);
167
//}
168
}
169
170     // public methods
171

172     /**
173      * Sets class path with specified class path entries. The
174      * parameter is assumed to be an array of directories, URLs, and/or
175      * zip files.
176      *
177      * @param entries The class path represented by a String array.
178      */

179     public void set(String JavaDoc[] entries) {
180 // v. strahinja, 23 sep 2002
181
set(convertArrayToVector(entries, logChannel));
182 // set(convertArrayToVector(entries, logger));
183
}
184
185     /**
186      * Sets class path with specified class path entries. The
187      * parameter is assumed to be an array of zip files and/or directories.
188      *
189      * @param entries The class path represented by a File array.
190      */

191     public void set(File JavaDoc[] entries) {
192 // v. strahinja, 23 sep 2002
193
set(convertArrayToVector(entries, logChannel));
194 // set(convertArrayToVector(entries, logger));
195
}
196
197     /**
198      * Sets class path with specified class path entries. The URLs can
199      * represent directories and/or zip files on the local machine and/or
200      * on remote machines.
201      *
202      * @param entries The class path represented by a URL array.
203      */

204     public void set(URL JavaDoc[] entries) {
205 // v. strahinja, 23 sep 2002
206
set(convertArrayToVector(entries, logChannel));
207 // set(convertArrayToVector(entries, logger));
208
}
209
210     /**
211      * Adds specified class path entries to class path. The
212      * parameter is assumed to be an array of directories, URLs, and/or
213      * zip files.
214      *
215      * @param entries The class path entries to add.
216      */

217     public void add(String JavaDoc[] entries) {
218 // v. strahinja, 23 sep 2002
219
add(convertArrayToVector(entries, logChannel));
220 // add(convertArrayToVector(entries, logger));
221
}
222
223     /**
224      * Adds specified class path entries to class path. The
225      * parameter is assumed to be an array of zip files and/or directories.
226      *
227      * @param entries The class path entries to add.
228      */

229     public void add(File JavaDoc[] entries) {
230 // v. strahinja, 23 sep 2002
231
add(convertArrayToVector(entries, logChannel));
232 // add(convertArrayToVector(entries, logger));
233
}
234
235     /**
236      * Adds specified class path entries to class path. The URLs can
237      * represent directories and/or zip files on the local machine and/or
238      * on remote machines.
239      *
240      * @param entries The class path entries to add.
241      */

242     public void add(URL JavaDoc[] entries) {
243 // v. strahinja, 23 sep 2002
244
add(convertArrayToVector(entries, logChannel));
245 // add(convertArrayToVector(entries, logger));
246
}
247
248     /**
249      * Clears class path by removing all entries.
250      *
251      * @see #set
252      * @see #add
253      */

254     public void clear() {
255         for (int i=0; i<classPath.capacity(); i++){
256             try {
257                 ClassPathEntry cpe = (ClassPathEntry)classPath.elementAt(i);
258                 if (cpe.isZipFile()){
259                     cpe.getZipFile().close();
260                 }
261             }catch(Exception JavaDoc ex){}
262         }
263         classPath.removeAllElements();
264     }
265
266     /**
267      * Get the number of entries in the classpath.
268      *
269      * @return The length ot the class path.
270      */

271     public int getLength() {
272     return classPath.size();
273     }
274
275     /**
276      * Gets an Enumeration of class path entries.
277      *
278      * @return an Enumeration of ClassPathEntry objects.
279      * @see ClassPathEntry
280      * @see #set
281      */

282     public Enumeration JavaDoc getPath() {
283     return classPath.elements();
284     }
285
286     /**
287      * Gets resource represented by specified file name. The class path
288      * entries are searched in order to find the desired resource. If the
289      * resource is not found in the class path, null is returned.
290      *
291      * @param name The file name of the resource.
292      * @return the resource associated with the given file name, or null if
293      * it can not be found.
294      * @see Resource
295      */

296     public Resource getResource(String JavaDoc name) {
297
298     if (name == null) {
299         throw new NullPointerException JavaDoc("Null resource name passed to " +
300         "getResource() for class path, " + this);
301     }
302     Resource resource = null;
303     for (int i = 0; i < classPath.size(); i++) {
304         ClassPathEntry entry = null;
305             entry = (ClassPathEntry)classPath.elementAt(i);
306
307             if (loggingEnabled) {
308 // v. strahinja, 23 sep 2002
309
logChannel.write(logLevel, " checking: \"" + entry.getName()
310 // logger.log(logLevel, " checking: \"" + entry.getName()
311
+ "\"");
312             }
313         resource = entry.getResource(name);
314         if (resource != null) {
315                 if (loggingEnabled) {
316 // v. strahinja, 23 sep 2002
317
logChannel.write(logLevel, " found: " + name);
318 // logger.log(logLevel, " found: " + name);
319
}
320         return resource;
321         }
322     }
323         if (loggingEnabled) {
324 // v. strahinja, 23 sep 2002
325
logChannel.write(logLevel, " not found: " + name);
326 // logger.log(logLevel, " not found: " + name);
327
}
328     return null;
329     }
330
331
332     // private helper methods
333

334     /**
335      * Sets class path with specified class path entries.
336      * Vector entries must be <CODE>ClassPathEntry</CODE> objects.
337      * All null and duplicate entries are removed.
338      *
339      * @param entries The class path entries.
340      * @see ClassPathEntry
341      */

342     private void set(Vector JavaDoc entries) {
343     /*
344      * This set method actually does all the work,
345      * since all the other set methods call this one.
346      */

347     classPath.removeAllElements();
348     add(entries);
349     }
350
351     /**
352      * Adds specified class path entries to class path.
353      * Vector entries must be <CODE>ClassPathEntry</CODE> objects.
354      * All null and duplicate entries are removed.
355      *
356      * @param entries The class path entries.
357      * @see ClassPathEntry
358      */

359     private void add(Vector JavaDoc entries) {
360     /*
361      * This add method actually does all the work,
362      * since all the other add methods call this one.
363      */

364     if (entries != null) {
365         for (int i = 0; i < entries.size(); i++) {
366         classPath.insertElementAt(entries.elementAt(i), i);
367         }
368     }
369     }
370
371     /**
372      * Converts array of Objects to Vector. Converts all array objects
373      * to ClassPathEntry objects and removes nulls and duplicates.
374      *
375      * @param array The array to convert.
376      * @return the Vector representation of the array.
377      * @see ClassPathEntry
378      */

379     private static Vector JavaDoc convertArrayToVector(Object JavaDoc[] array,
380 // v. strahinja, 23 sep 2002
381
LogChannel loadLogChannel) {
382 // Logger loadLogger) {
383
//FIXME: Is there really a need to support a nul array??
384
if (array != null) {
385         Vector JavaDoc vector = new Vector JavaDoc();
386         for (int i = 0; i < array.length; i++) {
387         Object JavaDoc object = array[i];
388         ClassPathEntry entry = null;
389         if (object instanceof String JavaDoc) {
390 // v. strahinja, 23 sep 2002
391
entry = new ClassPathEntry((String JavaDoc)object, loadLogChannel);
392 // entry = new ClassPathEntry((String)object, loadLogger);
393
} else if (object instanceof File JavaDoc) {
394 // v. strahinja, 23 sep 2002
395
entry = new ClassPathEntry((File JavaDoc)object, loadLogChannel);
396 // entry = new ClassPathEntry((File)object, loadLogger);
397
} else if (object instanceof URL JavaDoc) {
398 // v. strahinja, 23 sep 2002
399
entry = new ClassPathEntry((URL JavaDoc)object, loadLogChannel);
400 // entry = new ClassPathEntry((URL)object, loadLogger);
401
} else {
402             // This should not happen because the only public set
403
// methods are for Strings, Files, and URLs
404
throw new FatalExceptionError(new ClassCastException JavaDoc(
405             "Type, " + object.getClass() + ", is not supported. " +
406             "Expecting a String, File, or URL."));
407         }
408         if (entry != null && ! vector.contains(entry)) {
409             vector.addElement(entry);
410         }
411         }
412         return vector;
413     }
414     return null;
415     }
416 }
417
418
Popular Tags