2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
39 package java.rmi.server;
41 import java.net.MalformedURLException;
43 import java.net.URLClassLoader;
44 import java.util.ArrayList;
45 import java.util.Hashtable;
47 import java.util.StringTokenizer;
51 * This class provides a set of public static utility methods for supporting
52 * network-based class loading in RMI. These methods are called by RMI's
53 * internal marshal streams to implement the dynamic class loading of types for
54 * RMI parameters and return values.
56 public class RMIClassLoader
58 static private class MyClassLoader extends URLClassLoader
60 private MyClassLoader (URL[] urls, ClassLoader parent, String annotation)
63 this.annotation = annotation;
66 private MyClassLoader (URL[] urls, ClassLoader parent)
69 this.annotation = urlToAnnotation (urls);
72 public static String urlToAnnotation (URL[] urls)
77 StringBuffer annotation = new StringBuffer (64 * urls.length);
79 for (int i = 0; i < urls.length; i++)
81 annotation.append (urls [i].toExternalForm());
82 annotation.append (' ');
85 return annotation.toString();
88 public final String getClassAnnotation()
93 private final String annotation;
97 private static Map cacheLoaders; //map annotations to loaders
98 private static Map cacheAnnotations; //map loaders to annotations
100 //defaultAnnotation is got from system property
101 // "java.rmi.server.defaultAnnotation"
102 private static String defaultAnnotation;
104 //URL object for defaultAnnotation
105 private static URL defaultCodebase;
107 //class loader for defaultAnnotation
108 private static MyClassLoader defaultLoader;
112 // 89 is a nice prime number for Hashtable initial capacity
113 cacheLoaders = new Hashtable (89);
114 cacheAnnotations = new Hashtable (89);
116 defaultAnnotation = System.getProperty ("java.rmi.server.defaultAnnotation");
120 if (defaultAnnotation != null)
121 defaultCodebase = new URL (defaultAnnotation);
125 defaultCodebase = null;
128 if (defaultCodebase != null)
130 defaultLoader = new MyClassLoader (new URL[] { defaultCodebase }, null,
132 cacheLoaders.put(defaultAnnotation, defaultLoader);
139 public static Class loadClass (String name)
140 throws MalformedURLException, ClassNotFoundException
142 return loadClass ("", name);
145 public static Class loadClass (String codebases, String name)
146 throws MalformedURLException, ClassNotFoundException
148 ClassLoader loader = Thread.currentThread().getContextClassLoader();
150 //try context class loader first
153 return loader.loadClass (name);
155 catch (ClassNotFoundException e)
157 // class not found in the local classpath
160 if (codebases.length() == 0) //==""
162 loader = defaultLoader;
166 loader = getClassLoader(codebases);
171 //do not throw NullPointerException
172 throw new ClassNotFoundException ("Could not find class (" + name +
173 ") at codebase (" + codebases + ")");
176 return loader.loadClass (name);
180 * Gets a classloader for the given codebase and with the current
181 * context classloader as parent.
185 * @return a classloader for the given codebase
187 * @throws MalformedURLException if the codebase contains a malformed URL
189 private static ClassLoader getClassLoader (String codebases)
190 throws MalformedURLException
192 ClassLoader loader = (ClassLoader) cacheLoaders.get (codebases);
196 //create an entry in cacheLoaders mapping a loader to codebases.
197 // codebases are separated by " "
198 StringTokenizer tok = new StringTokenizer (codebases, " ");
199 ArrayList urls = new ArrayList();
201 while (tok.hasMoreTokens())
202 urls.add (new URL (tok.nextToken()));
204 loader = new MyClassLoader ((URL[]) urls.toArray (new URL [urls.size()]),
206 cacheLoaders.put (codebases, loader);
213 * Returns a string representation of the network location where a remote
214 * endpoint can get the class-definition of the given class.
218 * @return a space seperated list of URLs where the class-definition
221 public static String getClassAnnotation (Class cl)
223 ClassLoader loader = cl.getClassLoader();
226 || loader == ClassLoader.getSystemClassLoader())
228 return System.getProperty ("java.rmi.server.codebase");
231 if (loader instanceof MyClassLoader)
233 return ((MyClassLoader) loader).getClassAnnotation();
236 String s = (String) cacheAnnotations.get (loader);
241 if (loader instanceof URLClassLoader)
243 URL[] urls = ((URLClassLoader) loader).getURLs();
245 if (urls.length == 0)
248 StringBuffer annotation = new StringBuffer (64 * urls.length);
250 for (int i = 0; i < urls.length; i++)
252 annotation.append (urls [i].toExternalForm());
253 annotation.append (' ');
256 s = annotation.toString();
257 cacheAnnotations.put (loader, s);
261 return System.getProperty ("java.rmi.server.codebase");
267 public static Object getSecurityContext (ClassLoader loader)
269 throw new Error ("Not implemented");