OSDN Git Service

c6187983eb1df6cc77cc8a535a222ce150682ae0
[pf3gnuchains/gcc-fork.git] / libjava / java / rmi / server / RMIClassLoader.java
1 /* RMIClassLoader.java
2   Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003
3   Free Software Foundation, Inc.
4
5 This file is part of GNU Classpath.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307 USA.
21
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
25 combination.
26
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. */
38
39 package java.rmi.server;
40
41 import java.net.MalformedURLException;
42 import java.net.URL;
43 import java.net.URLClassLoader;
44 import java.util.ArrayList;
45 import java.util.Hashtable;
46 import java.util.Map;
47 import java.util.StringTokenizer;
48
49
50 /**
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.
55  */
56 public class RMIClassLoader
57 {
58   static private class MyClassLoader extends URLClassLoader
59   {
60     private MyClassLoader (URL[] urls, ClassLoader parent, String annotation)
61     {
62       super (urls, parent);
63       this.annotation = annotation;
64     }
65
66     private MyClassLoader (URL[] urls, ClassLoader parent)
67     {
68       super (urls, parent);
69       this.annotation = urlToAnnotation (urls);
70     }
71
72     public static String urlToAnnotation (URL[] urls)
73     {
74       if (urls.length == 0)
75         return null;
76
77       StringBuffer annotation = new StringBuffer (64 * urls.length);
78
79       for (int i = 0; i < urls.length; i++)
80       {
81         annotation.append (urls [i].toExternalForm());
82         annotation.append (' ');
83       }
84
85       return annotation.toString();
86     }
87
88     public final String getClassAnnotation()
89     {
90       return annotation;
91     }
92
93     private final String annotation;
94
95   }
96
97   private static Map cacheLoaders; //map annotations to loaders
98   private static Map cacheAnnotations; //map loaders to annotations
99
100   //defaultAnnotation is got from system property
101   // "java.rmi.server.defaultAnnotation"
102   private static String defaultAnnotation;
103
104   //URL object for defaultAnnotation
105   private static URL defaultCodebase;
106
107   //class loader for defaultAnnotation
108   private static MyClassLoader defaultLoader;
109
110   static
111   {
112     // 89 is a nice prime number for Hashtable initial capacity
113     cacheLoaders = new Hashtable (89);
114     cacheAnnotations = new Hashtable (89);
115
116     defaultAnnotation = System.getProperty ("java.rmi.server.defaultAnnotation");
117
118     try
119       {
120         if (defaultAnnotation != null)
121           defaultCodebase = new URL (defaultAnnotation);
122       }
123     catch (Exception _)
124       {
125         defaultCodebase = null;
126       }
127
128     if (defaultCodebase != null)
129       {
130         defaultLoader = new MyClassLoader (new URL[] { defaultCodebase }, null,
131                                            defaultAnnotation);
132         cacheLoaders.put(defaultAnnotation, defaultLoader);
133       }
134     }
135
136   /**
137    * @deprecated
138    */
139   public static Class loadClass (String name)
140     throws MalformedURLException, ClassNotFoundException
141   {
142     return loadClass ("", name);
143   }
144
145   public static Class loadClass (String codebases, String name)
146     throws MalformedURLException, ClassNotFoundException
147   {
148     ClassLoader loader = Thread.currentThread().getContextClassLoader();
149
150     //try context class loader first
151     try 
152       {
153         return loader.loadClass (name);
154       }
155     catch (ClassNotFoundException e)
156       {
157         // class not found in the local classpath
158       }
159
160     if (codebases.length() == 0) //==""
161       {
162         loader = defaultLoader;
163       }
164     else
165       {
166         loader = getClassLoader(codebases);
167       }
168
169     if (loader == null)
170       {
171         //do not throw NullPointerException
172         throw new ClassNotFoundException ("Could not find class (" + name +
173                                           ") at codebase (" + codebases + ")");
174       }
175       
176     return loader.loadClass (name);
177   }
178
179   /**
180    * Gets a classloader for the given codebase and with the current
181    * context classloader as parent.
182    * 
183    * @param codebases
184    * 
185    * @return a classloader for the given codebase
186    * 
187    * @throws MalformedURLException if the codebase contains a malformed URL
188    */
189   private static ClassLoader getClassLoader (String codebases) 
190     throws MalformedURLException
191   {
192     ClassLoader loader = (ClassLoader) cacheLoaders.get (codebases);
193         
194     if (loader == null)
195       {
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();
200       
201         while (tok.hasMoreTokens())
202           urls.add (new URL (tok.nextToken()));
203       
204         loader = new MyClassLoader ((URL[]) urls.toArray (new URL [urls.size()]),
205                                     null, codebases);
206         cacheLoaders.put (codebases, loader);
207       }
208            
209     return loader;
210   }
211  
212   /**
213    * Returns a string representation of the network location where a remote
214    * endpoint can get the class-definition of the given class.
215    *
216    * @param cl
217    *
218    * @return a space seperated list of URLs where the class-definition
219    * of cl may be found
220    */
221   public static String getClassAnnotation (Class cl)
222   {
223     ClassLoader loader = cl.getClassLoader();
224     
225     if (loader == null
226         || loader == ClassLoader.getSystemClassLoader())
227       {
228         return System.getProperty ("java.rmi.server.codebase");
229       }
230
231     if (loader instanceof MyClassLoader)
232       {
233         return ((MyClassLoader) loader).getClassAnnotation();
234       }
235
236     String s = (String) cacheAnnotations.get (loader);
237
238     if (s != null)
239       return s;
240
241     if (loader instanceof URLClassLoader)
242       {
243         URL[] urls = ((URLClassLoader) loader).getURLs();
244
245         if (urls.length == 0)
246           return null;
247
248         StringBuffer annotation = new StringBuffer (64 * urls.length);
249
250         for (int i = 0; i < urls.length; i++)
251           {
252             annotation.append (urls [i].toExternalForm());
253             annotation.append (' ');
254           }
255
256         s = annotation.toString();
257         cacheAnnotations.put (loader, s);
258         return s;
259       }
260
261     return System.getProperty ("java.rmi.server.codebase");
262   }
263
264   /**
265    * @deprecated
266    */
267   public static Object getSecurityContext (ClassLoader loader)
268   {
269     throw new Error ("Not implemented");
270   }
271 }