01: /*
02: * CoadunationBase: The base for a Coadunation instance.
03: * Copyright (C) 2006 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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: // the package path
26: package com.rift.coad;
27:
28: // java imports
29: import java.net.URLClassLoader;
30: import java.net.URL;
31: import java.io.File;
32: import java.util.Vector;
33:
34: /**
35: * The class responsible for loading the libraries required to run a Coadunation
36: * instance.
37: *
38: * @author Brett Chaldecott
39: */
40: public class BaseClassLoader extends URLClassLoader {
41:
42: // the vector containing the loaded urls
43: private Vector urls = new Vector();
44:
45: /**
46: * Creates a new instance of BaseClassLoader
47: */
48: public BaseClassLoader(URL[] urls, ClassLoader parent) {
49: super (urls, parent);
50: for (int index = 0; index < urls.length; index++) {
51: this .urls.add(urls[index]);
52: }
53: }
54:
55: /**
56: * This method will add a jar to the search path
57: *
58: * @param path The path to the jar.
59: * @exception CoadException
60: */
61: public void addLib(String path) throws CoadException {
62: try {
63: File file = new File(path);
64: if (file.isFile() == false) {
65: throw new CoadException("The path [" + path
66: + "] does not point to a valid file.");
67: }
68: URL url = file.toURL();
69:
70: // synchronize on the list
71: synchronized (urls) {
72: for (int index = 0; index < urls.size(); index++) {
73: URL loadedURL = (URL) urls.get(index);
74: if (loadedURL.equals(url)) {
75: // already loaded
76: return;
77: }
78: }
79: urls.add(url);
80: }
81:
82: // load into the path
83: addURL(url);
84: } catch (CoadException ex) {
85: throw ex;
86: } catch (Exception ex) {
87: throw new CoadException("Failed to load file [" + path
88: + "] because : " + ex.getMessage(), ex);
89: }
90: }
91:
92: }
|