001: /*
002: * CoadunationBase: The base for a Coadunation instance.
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: * RemoteClassLoaderSpi.java
020: *
021: * This object implements the remote class loader.
022: */
023:
024: // package path
025: package com.rift.coad.client.interceptor.iiop;
026:
027: // java imports
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.net.URLClassLoader;
031: import java.util.Enumeration;
032: import java.rmi.server.RMIClassLoader;
033: import java.rmi.server.RMIClassLoaderSpi;
034:
035: /**
036: * This object implements the remote class loader for coadunation. This is here
037: * purely for debug purposes. Do not use in a production environment.
038: *
039: * @author Brett Chaldecott
040: */
041: public class RemoteClassLoaderSpi extends RMIClassLoaderSpi {
042:
043: // private member variables
044: private RMIClassLoaderSpi delegate = RMIClassLoader
045: .getDefaultProviderInstance();
046:
047: /**
048: * Creates a new instance of RemoteClassLoaderSpi
049: */
050: public RemoteClassLoaderSpi() {
051: }
052:
053: /**
054: * Load a proxy class to be used by RMI.
055: *
056: * @return The reference to the required proxy class.
057: * @param codeBase The code base for this object.
058: * @param interfaces The interface array to be loaded.
059: * @param default The class loader that would have been used by the JVM.
060: * @exception MalformedURLException
061: * @exception ClassNotFoundException
062: */
063: public Class loadProxyClass(String codebase, String[] interfaces,
064: ClassLoader defaultLoader) throws MalformedURLException,
065: ClassNotFoundException {
066: // use the thread context class loader.
067: System.out.println("Load a proxy class [" + codebase + "]");
068: return delegate.loadProxyClass(codebase, interfaces,
069: defaultLoader);
070: }
071:
072: /**
073: * This method returns a reference to the loaded class using the thread
074: * context class loader.
075: *
076: * @return The class loaded from the appropriate class loader.
077: * @param codebase The code base to load from.
078: * @param name The name of the class to load.
079: * @param defaultLoader The loader that would have been used by the jvm
080: * @exception MalformedURLException
081: * @exception ClassNotFoundException
082: */
083: public Class loadClass(String codebase, String name,
084: ClassLoader defaultLoader) throws MalformedURLException,
085: ClassNotFoundException {
086: // use the thread context class loader.
087: if (defaultLoader == null) {
088: System.out.println("Load a class [" + codebase + "] name ["
089: + name + "] defaultLoader [null]");
090: } else {
091: System.out.println("Load a class [" + codebase + "] name ["
092: + name + "] defaultLoader ["
093: + defaultLoader.getClass().getName() + "]");
094: }
095: System.out.println("Thread class loader : "
096: + Thread.currentThread().getContextClassLoader()
097: .getClass().getName());
098: Class result = null;
099: try {
100: result = delegate.loadClass(codebase, name, Thread
101: .currentThread().getContextClassLoader());
102: } catch (MalformedURLException ex) {
103: System.out.println("Failed to narrow : " + ex.getMessage());
104: ex.printStackTrace(System.out);
105: throw ex;
106: } catch (ClassNotFoundException ex) {
107: System.out.println("Failed to narrow : " + ex.getMessage());
108: ex.printStackTrace(System.out);
109: throw ex;
110: }
111: try {
112: if (null == result && (codebase != null)) {
113: ClassLoader loader = getClassLoader(codebase);
114: System.out
115: .println("Retrieve the result and enumerate through them");
116: loadClasses(loader, Thread.currentThread()
117: .getContextClassLoader(), loader
118: .getResources("/"));
119: } else if (result != null) {
120: System.out.println("The class was loaded");
121: } else {
122: System.out
123: .println("Nothing loaded and the class is null ["
124: + codebase + "]");
125: }
126: } catch (Exception ex) {
127: System.out
128: .println("Caught an exception in the main load class : "
129: + ex.getMessage());
130: ex.printStackTrace(System.out);
131: }
132: return result;
133: }
134:
135: /**
136: * This method returns the appropriate class loader for the specified code
137: * base.
138: *
139: * @return The reference to the required class loader.
140: * @param codebase The code base for the class loader.
141: * @exception MalformedURLException
142: */
143: public ClassLoader getClassLoader(String codebase)
144: throws MalformedURLException {
145: System.out.println("Get class loader for : " + codebase);
146: URL url = new URL(codebase);
147: if (url.getProtocol().equals("http")) {
148: return new URLClassLoader(new URL[] { url });
149: }
150: return delegate.getClassLoader(codebase);
151: }
152:
153: /**
154: * This method returns the annotation for the specified class
155: *
156: * @return The string containing the path to the class.
157: * @param cl The class to look up for.
158: */
159: public String getClassAnnotation(Class cl) {
160: String annotation = null;
161: try {
162: annotation = delegate.getClassAnnotation(cl);
163: } catch (Throwable ex) {
164: annotation = System.getProperty("java.rmi.server.codebase");
165: }
166: return annotation;
167: }
168:
169: /**
170: * This method is called to load the entries into a class loader from a stub
171: * code object.
172: *
173: * @param source The source to loader from.
174: * @param target The target to loade the entries into.
175: * @param entries The entries to load in.
176: */
177: private void loadClasses(ClassLoader source, ClassLoader target,
178: Enumeration entries) {
179: try {
180: while (entries.hasMoreElements()) {
181: URL url = (URL) entries.nextElement();
182: System.out.println(url.toURI().toString());
183: }
184: } catch (Exception ex) {
185: System.out.println("Caught an exception : "
186: + ex.getMessage());
187: ex.printStackTrace(System.out);
188: }
189: }
190: }
|