001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * MasterClassLoader.java
020: *
021: * This singleton is responsible for acting as the master class loader. It is
022: * responsible for loading classes into the global class space.
023: */
024:
025: // package path
026: package com.rift.coad.lib.loader;
027:
028: // logging import
029: import org.apache.log4j.Logger;
030:
031: // java imports
032: import java.lang.ClassLoader;
033: import java.net.URLClassLoader;
034: import java.net.URL;
035: import java.io.File;
036:
037: // coadunation imports
038: import com.rift.coad.BaseClassLoader;
039:
040: /**
041: * This singleton is responsible for acting as the master class loader. It is
042: * responsible for loading classes into the global class space.
043: *
044: * @author Brett Chaldecott
045: */
046: public class MasterClassLoader {
047:
048: // the singleton variable
049: private static MasterClassLoader singleton = null;
050:
051: // the class log variable
052: protected Logger log = Logger.getLogger(MasterClassLoader.class
053: .getName());
054:
055: // the refeference
056: private URLClassLoader classLoader = null;
057:
058: /**
059: * Creates a new instance of MasterClassLoader
060: *
061: * @exception LoaderException
062: */
063: private MasterClassLoader() throws LoaderException {
064: try {
065: classLoader = (URLClassLoader) this .getClass()
066: .getClassLoader();
067: } catch (Exception ex) {
068: throw new LoaderException(
069: "Failed to retrieve the parent class loader : "
070: + ex.getMessage(), ex);
071: }
072: }
073:
074: /**
075: * This method returns an instance of the MasterClass Loader wrapper object.
076: */
077: public static synchronized MasterClassLoader init()
078: throws LoaderException {
079: if (singleton == null) {
080: singleton = new MasterClassLoader();
081: }
082: return singleton;
083: }
084:
085: /**
086: * This method returns an instance of the MasterClass Loader wrapper object.
087: */
088: public static synchronized MasterClassLoader getInstance()
089: throws LoaderException {
090: if (singleton == null) {
091: singleton = new MasterClassLoader();
092: }
093: return singleton;
094: }
095:
096: /**
097: * This method is responsible for adding a
098: */
099: public void addLib(String path) throws LoaderException {
100: try {
101: if (classLoader instanceof BaseClassLoader) {
102: ((BaseClassLoader) classLoader).addLib(path);
103: } else {
104: log
105: .error("This object has not been loaded by a BaseClassLoader");
106: }
107: } catch (Exception ex) {
108: throw new LoaderException("Failed to add the library ["
109: + path + "] because : " + ex.getMessage(), ex);
110: }
111: }
112:
113: /**
114: * This method returns the reference to the url class loader.
115: *
116: * @return The referencd to the class loader.
117: */
118: public ClassLoader getLoader() {
119: return classLoader;
120: }
121: }
|