001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.bootstrap;
028:
029: import java.net.URL;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: /** A Classloader which uses slightly different rules for class loading:
034: * Prefer classes loaded via this loader rather than
035: * the parent.
036: * @property org.cougaar.bootstrapper.exclusions Colon-separated list of
037: * packages not to be loaded directly by this classloader.
038: **/
039:
040: public class BootstrapClassLoader extends XURLClassLoader {
041: private static final String PROP_EXCLUSIONS = "org.cougaar.bootstrapper.exclusions";
042: private static final List exclusions = new ArrayList();
043: private static int loudness = Bootstrapper.getLoudness();
044:
045: static {
046: exclusions.add("java."); // avoids javaiopatch.jar
047: String s = SystemProperties.getProperty(PROP_EXCLUSIONS);
048: if (s != null) {
049: String extras[] = s.split(":");
050: for (int i = 0; i < extras.length; i++) {
051: exclusions.add(extras[i]);
052: }
053: }
054: }
055:
056: private boolean excludedP(String classname) {
057: int l = exclusions.size();
058: for (int i = 0; i < l; i++) {
059: String s = (String) exclusions.get(i);
060: if (classname.startsWith(s))
061: return true;
062: }
063: return false;
064: }
065:
066: public BootstrapClassLoader(URL urls[]) {
067: super (urls);
068: if (loudness > 0) {
069: synchronized (System.err) {
070: System.err.println();
071: System.err.println("Bootstrapper URLs: ");
072: for (int i = 0; i < urls.length; i++) {
073: System.err.println("\t" + urls[i]);
074: }
075: System.err.println();
076: }
077: }
078: }
079:
080: protected synchronized Class loadClass(String name, boolean resolve)
081: throws ClassNotFoundException {
082: // First, check if the class has already been loaded
083: Class c = findLoadedClass(name);
084: if (c == null) {
085: // make sure not to use this classloader to load
086: // java.*. We patch java.io. to support persistence, so it
087: // may be in our jar files, yet those classes must absolutely
088: // be loaded by the same loader as the rest of core java.
089: if (!excludedP(name)) {
090: try {
091: c = findClass(name);
092: } catch (ClassNotFoundException e) {
093: // If still not found, then call findClass in order
094: // to find the class.
095: }
096: }
097: if (c == null) {
098: ClassLoader parent = getParent();
099: if (parent == null)
100: parent = getSystemClassLoader();
101: c = parent.loadClass(name);
102: }
103: if (loudness > 1 && c != null) {
104: java.security.ProtectionDomain pd = c
105: .getProtectionDomain();
106: if (pd != null) {
107: java.security.CodeSource cs = pd.getCodeSource();
108: if (cs != null) {
109: System.err.println("BCL: " + c
110: + " loaded from " + cs.getLocation());
111: }
112: }
113: }
114: }
115: if (resolve) {
116: resolveClass(c);
117: }
118: return c;
119: }
120: }
|