KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > BaseClassLoader


1 /*
2  * CoadunationBase: The base for a Coadunation instance.
3  * Copyright (C) 2006 Rift IT Contracting
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  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * BaseClassLoader.java
20  *
21  * The class responsible for loading the libraries required to run a Coadunation
22  * instance.
23  */

24
25
26 // the package path
27
package com.rift.coad;
28
29 // java imports
30
import java.net.URLClassLoader JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.io.File JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 /**
36  * The class responsible for loading the libraries required to run a Coadunation
37  * instance.
38  *
39  * @author Brett Chaldecott
40  */

41 public class BaseClassLoader extends URLClassLoader JavaDoc {
42     
43     // the vector containing the loaded urls
44
private Vector JavaDoc urls = new Vector JavaDoc();
45     
46     
47     /**
48      * Creates a new instance of BaseClassLoader
49      */

50     public BaseClassLoader(URL JavaDoc[] urls,ClassLoader JavaDoc parent) {
51         super(urls,parent);
52         for (int index = 0; index < urls.length; index++) {
53             this.urls.add(urls[index]);
54         }
55     }
56     
57     
58     /**
59      * This method will add a jar to the search path
60      *
61      * @param path The path to the jar.
62      * @exception CoadException
63      */

64     public void addLib(String JavaDoc path) throws CoadException {
65         try {
66             File JavaDoc file = new File JavaDoc(path);
67             if (file.isFile() == false) {
68                 throw new CoadException("The path [" + path
69                         + "] does not point to a valid file.");
70             }
71             URL JavaDoc url = file.toURL();
72             
73             // synchronize on the list
74
synchronized (urls) {
75                 for (int index = 0; index < urls.size(); index++) {
76                     URL JavaDoc loadedURL = (URL JavaDoc)urls.get(index);
77                     if (loadedURL.equals(url)) {
78                         // already loaded
79
return;
80                     }
81                 }
82                 urls.add(url);
83             }
84             
85             // load into the path
86
addURL(url);
87         } catch (CoadException ex) {
88             throw ex;
89         } catch (Exception JavaDoc ex) {
90             throw new CoadException("Failed to load file [" + path
91                     + "] because : " + ex.getMessage(),ex);
92         }
93     }
94     
95 }
96
Popular Tags