KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > loader > MasterClassLoader


1 /*
2  * CoadunationLib: The coaduntion implementation library.
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  * MasterClassLoader.java
20  *
21  * This singleton is responsible for acting as the master class loader. It is
22  * responsible for loading classes into the global class space.
23  */

24
25 // package path
26
package com.rift.coad.lib.loader;
27
28 // logging import
29
import org.apache.log4j.Logger;
30
31
32 // java imports
33
import java.lang.ClassLoader JavaDoc;
34 import java.net.URLClassLoader JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.io.File JavaDoc;
37
38 // coadunation imports
39
import com.rift.coad.BaseClassLoader;
40
41 /**
42  * This singleton is responsible for acting as the master class loader. It is
43  * responsible for loading classes into the global class space.
44  *
45  * @author Brett Chaldecott
46  */

47 public class MasterClassLoader {
48     
49     // the singleton variable
50
private static MasterClassLoader singleton = null;
51     
52     // the class log variable
53
protected Logger log =
54         Logger.getLogger(MasterClassLoader.class.getName());
55     
56     // the refeference
57
private URLClassLoader JavaDoc classLoader = null;
58     
59     
60     /**
61      * Creates a new instance of MasterClassLoader
62      *
63      * @exception LoaderException
64      */

65     private MasterClassLoader() throws LoaderException {
66         try {
67             classLoader = (URLClassLoader JavaDoc)this.getClass().getClassLoader();
68         } catch (Exception JavaDoc ex) {
69             throw new LoaderException(
70                     "Failed to retrieve the parent class loader : " +
71                     ex.getMessage(),ex);
72         }
73     }
74     
75     /**
76      * This method returns an instance of the MasterClass Loader wrapper object.
77      */

78     public static synchronized MasterClassLoader init() throws
79             LoaderException {
80         if (singleton == null) {
81             singleton = new MasterClassLoader();
82         }
83         return singleton;
84     }
85     
86     
87     /**
88      * This method returns an instance of the MasterClass Loader wrapper object.
89      */

90     public static synchronized MasterClassLoader getInstance() throws
91             LoaderException {
92         if (singleton == null) {
93             singleton = new MasterClassLoader();
94         }
95         return singleton;
96     }
97     
98     
99     /**
100      * This method is responsible for adding a
101      */

102     public void addLib(String JavaDoc path) throws LoaderException {
103         try {
104             if (classLoader instanceof BaseClassLoader) {
105                 ((BaseClassLoader)classLoader).addLib(path);
106             } else {
107                 log.error(
108                         "This object has not been loaded by a BaseClassLoader");
109             }
110         } catch (Exception JavaDoc ex) {
111             throw new LoaderException("Failed to add the library [" + path +
112                     "] because : " + ex.getMessage(),ex);
113         }
114     }
115     
116     
117     /**
118      * This method returns the reference to the url class loader.
119      *
120      * @return The referencd to the class loader.
121      */

122     public ClassLoader JavaDoc getLoader() {
123         return classLoader;
124     }
125 }
126
Popular Tags