KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > client > interceptor > iiop > RemoteClassLoaderSpi


1 /*
2  * CoadunationBase: The base for a Coadunation instance.
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  * RemoteClassLoaderSpi.java
20  *
21  * This object implements the remote class loader.
22  */

23
24 // package path
25
package com.rift.coad.client.interceptor.iiop;
26
27 // java imports
28
import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.rmi.server.RMIClassLoader JavaDoc;
33 import java.rmi.server.RMIClassLoaderSpi JavaDoc;
34
35 /**
36  * This object implements the remote class loader for coadunation. This is here
37  * purely for debug purposes. Do not use in a production environment.
38  *
39  * @author Brett Chaldecott
40  */

41 public class RemoteClassLoaderSpi extends RMIClassLoaderSpi JavaDoc {
42     
43     // private member variables
44
private RMIClassLoaderSpi JavaDoc delegate =
45             RMIClassLoader.getDefaultProviderInstance();
46     
47     /**
48      * Creates a new instance of RemoteClassLoaderSpi
49      */

50     public RemoteClassLoaderSpi() {
51     }
52     
53     
54     /**
55      * Load a proxy class to be used by RMI.
56      *
57      * @return The reference to the required proxy class.
58      * @param codeBase The code base for this object.
59      * @param interfaces The interface array to be loaded.
60      * @param default The class loader that would have been used by the JVM.
61      * @exception MalformedURLException
62      * @exception ClassNotFoundException
63      */

64     public Class JavaDoc loadProxyClass (String JavaDoc codebase, String JavaDoc[] interfaces,
65             ClassLoader JavaDoc defaultLoader) throws MalformedURLException JavaDoc,
66             ClassNotFoundException JavaDoc {
67         // use the thread context class loader.
68
System.out.println("Load a proxy class [" + codebase + "]");
69         return delegate.loadProxyClass(codebase,interfaces, defaultLoader);
70     }
71     
72     
73     /**
74      * This method returns a reference to the loaded class using the thread
75      * context class loader.
76      *
77      * @return The class loaded from the appropriate class loader.
78      * @param codebase The code base to load from.
79      * @param name The name of the class to load.
80      * @param defaultLoader The loader that would have been used by the jvm
81      * @exception MalformedURLException
82      * @exception ClassNotFoundException
83      */

84     public Class JavaDoc loadClass(String JavaDoc codebase, String JavaDoc name,
85             ClassLoader JavaDoc defaultLoader) throws MalformedURLException JavaDoc,
86             ClassNotFoundException JavaDoc {
87         // use the thread context class loader.
88
if (defaultLoader == null) {
89             System.out.println("Load a class [" + codebase + "] name [" +
90                     name + "] defaultLoader [null]");
91         } else {
92             System.out.println("Load a class [" + codebase + "] name [" +
93                     name + "] defaultLoader ["+
94                     defaultLoader.getClass().getName()+"]");
95         }
96         System.out.println("Thread class loader : " + Thread.currentThread().
97                 getContextClassLoader().getClass().getName());
98         Class JavaDoc result = null;
99         try {
100             result = delegate.loadClass(codebase,name, Thread.currentThread().
101                     getContextClassLoader());
102         } catch (MalformedURLException JavaDoc ex) {
103             System.out.println("Failed to narrow : " + ex.getMessage());
104             ex.printStackTrace(System.out);
105             throw ex;
106         } catch (ClassNotFoundException JavaDoc 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 JavaDoc loader = getClassLoader(codebase);
114                 System.out.println("Retrieve the result and enumerate through them");
115                 loadClasses(loader, Thread.currentThread().getContextClassLoader(),
116                         loader.getResources("/"));
117             } else if (result != null){
118                 System.out.println("The class was loaded");
119             } else {
120                 System.out.println("Nothing loaded and the class is null [" +
121                         codebase + "]");
122             }
123         } catch (Exception JavaDoc ex) {
124             System.out.println("Caught an exception in the main load class : "
125                     + ex.getMessage());
126             ex.printStackTrace(System.out);
127         }
128         return result;
129     }
130     
131     
132     /**
133      * This method returns the appropriate class loader for the specified code
134      * base.
135      *
136      * @return The reference to the required class loader.
137      * @param codebase The code base for the class loader.
138      * @exception MalformedURLException
139      */

140     public ClassLoader JavaDoc getClassLoader(String JavaDoc codebase) throws
141             MalformedURLException JavaDoc {
142         System.out.println("Get class loader for : " + codebase);
143         URL JavaDoc url = new URL JavaDoc(codebase);
144         if (url.getProtocol().equals("http")) {
145             return new URLClassLoader JavaDoc(new URL JavaDoc[] {url});
146         }
147         return delegate.getClassLoader(codebase);
148     }
149     
150     
151     /**
152      * This method returns the annotation for the specified class
153      *
154      * @return The string containing the path to the class.
155      * @param cl The class to look up for.
156      */

157     public String JavaDoc getClassAnnotation(Class JavaDoc cl) {
158         String JavaDoc annotation = null;
159         try {
160             annotation = delegate.getClassAnnotation(cl);
161         } catch (Throwable JavaDoc ex) {
162             annotation = System.getProperty("java.rmi.server.codebase");
163         }
164         return annotation;
165     }
166     
167     
168     /**
169      * This method is called to load the entries into a class loader from a stub
170      * code object.
171      *
172      * @param source The source to loader from.
173      * @param target The target to loade the entries into.
174      * @param entries The entries to load in.
175      */

176     private void loadClasses(ClassLoader JavaDoc source, ClassLoader JavaDoc target,
177             Enumeration JavaDoc entries) {
178         try {
179             while(entries.hasMoreElements()) {
180                 URL JavaDoc url = (URL JavaDoc)entries.nextElement();
181                 System.out.println(url.toURI().toString());
182             }
183         } catch (Exception JavaDoc ex) {
184             System.out.println("Caught an exception : " + ex.getMessage());
185             ex.printStackTrace(System.out);
186         }
187     }
188 }
189
Popular Tags