1 | /* RMIClassLoader.java
|
---|
2 | Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.rmi.server;
|
---|
39 |
|
---|
40 | import java.net.URL;
|
---|
41 | import java.net.URLConnection;
|
---|
42 | import java.net.URLClassLoader;
|
---|
43 | import java.io.IOException;
|
---|
44 | import java.io.DataInputStream;
|
---|
45 | import java.net.MalformedURLException;
|
---|
46 | import java.util.ArrayList;
|
---|
47 | import java.util.Collection;
|
---|
48 | import java.util.Collections;
|
---|
49 | import java.util.Hashtable;
|
---|
50 | import java.util.Map;
|
---|
51 | import java.util.StringTokenizer;
|
---|
52 | import java.util.WeakHashMap;
|
---|
53 |
|
---|
54 | public class RMIClassLoader
|
---|
55 | {
|
---|
56 |
|
---|
57 | static private class MyClassLoader extends URLClassLoader
|
---|
58 | {
|
---|
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 | for(int i = 0; i < urls.length; i++)
|
---|
79 | {
|
---|
80 | annotation.append(urls[i].toExternalForm());
|
---|
81 | annotation.append(' ');
|
---|
82 | }
|
---|
83 |
|
---|
84 | return annotation.toString();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public final String getClassAnnotation(){
|
---|
88 | return annotation;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private final String annotation;
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static Map cacheLoaders; //map annotations to loaders
|
---|
96 | private static Map cacheAnnotations; //map loaders to annotations
|
---|
97 |
|
---|
98 | //defaultAnnotation is got from system property
|
---|
99 | // "java.rmi.server.defaultAnnotation"
|
---|
100 | private static String defaultAnnotation;
|
---|
101 | //URL object for defaultAnnotation
|
---|
102 | private static URL defaultCodebase;
|
---|
103 | //class loader for defaultAnnotation
|
---|
104 | private static MyClassLoader defaultLoader;
|
---|
105 |
|
---|
106 | static
|
---|
107 | {
|
---|
108 | // 89 is a nice prime number for Hashtable initial capacity
|
---|
109 | cacheLoaders = new Hashtable(89);
|
---|
110 | cacheAnnotations = new Hashtable(89);
|
---|
111 |
|
---|
112 | defaultAnnotation = System.getProperty("java.rmi.server.defaultAnnotation");
|
---|
113 | try
|
---|
114 | {
|
---|
115 | if (defaultAnnotation != null)
|
---|
116 | defaultCodebase = new URL(defaultAnnotation);
|
---|
117 | }
|
---|
118 | catch(Exception _)
|
---|
119 | {
|
---|
120 | defaultCodebase = null;
|
---|
121 | }
|
---|
122 | if (defaultCodebase != null)
|
---|
123 | {
|
---|
124 | defaultLoader = new MyClassLoader(new URL[]{ defaultCodebase },
|
---|
125 | null, defaultAnnotation);
|
---|
126 | cacheLoaders.put(defaultAnnotation, defaultLoader);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @deprecated
|
---|
132 | */
|
---|
133 | public static Class loadClass(String name)
|
---|
134 | throws MalformedURLException, ClassNotFoundException
|
---|
135 | {
|
---|
136 | return (loadClass("", name));
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static Class loadClass(String codebases, String name)
|
---|
140 | throws MalformedURLException, ClassNotFoundException
|
---|
141 | {
|
---|
142 | Class c = null;
|
---|
143 | ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
---|
144 | //try context class loader first
|
---|
145 | try
|
---|
146 | {
|
---|
147 | c = loader.loadClass(name);
|
---|
148 | }
|
---|
149 | catch(ClassNotFoundException e) {}
|
---|
150 |
|
---|
151 | if (c != null)
|
---|
152 | return c;
|
---|
153 |
|
---|
154 | if (codebases.length() == 0) //==""
|
---|
155 | loader = defaultLoader;
|
---|
156 | else
|
---|
157 | {
|
---|
158 | loader = (ClassLoader)cacheLoaders.get(codebases);
|
---|
159 | if (loader == null)
|
---|
160 | {
|
---|
161 | //create an entry in cacheLoaders mapping a loader to codebases.
|
---|
162 |
|
---|
163 | // codebases are separated by " "
|
---|
164 | StringTokenizer tok = new StringTokenizer(codebases, " ");
|
---|
165 | ArrayList urls = new ArrayList();
|
---|
166 | while (tok.hasMoreTokens())
|
---|
167 | urls.add(new URL(tok.nextToken()));
|
---|
168 |
|
---|
169 | loader = new MyClassLoader((URL[])urls.toArray(new URL[urls.size()]),
|
---|
170 | null, codebases);
|
---|
171 | cacheLoaders.put(codebases, loader);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | return loader.loadClass(name);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public static String getClassAnnotation(Class cl)
|
---|
179 | {
|
---|
180 | ClassLoader loader = cl.getClassLoader();
|
---|
181 | if (loader == null || loader == ClassLoader.getSystemClassLoader())
|
---|
182 | {
|
---|
183 | return null; //??
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (loader instanceof MyClassLoader)
|
---|
187 | {
|
---|
188 | return ((MyClassLoader)loader).getClassAnnotation();
|
---|
189 | }
|
---|
190 |
|
---|
191 | String s = (String)cacheAnnotations.get(loader);
|
---|
192 | if (s != null)
|
---|
193 | return s;
|
---|
194 |
|
---|
195 | if (loader instanceof URLClassLoader)
|
---|
196 | {
|
---|
197 | URL[] urls = ((URLClassLoader)loader).getURLs();
|
---|
198 | if(urls.length == 0)
|
---|
199 | return null;
|
---|
200 |
|
---|
201 | StringBuffer annotation = new StringBuffer(64*urls.length);
|
---|
202 | for(int i = 0; i < urls.length; i++)
|
---|
203 | {
|
---|
204 | annotation.append(urls[i].toExternalForm());
|
---|
205 | annotation.append(' ');
|
---|
206 | }
|
---|
207 | s = annotation.toString();
|
---|
208 | cacheAnnotations.put(loader, s);
|
---|
209 | }
|
---|
210 | return null;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * @deprecated
|
---|
215 | */
|
---|
216 | public static Object getSecurityContext(ClassLoader loader)
|
---|
217 | {
|
---|
218 | throw new Error("Not implemented");
|
---|
219 | }
|
---|
220 |
|
---|
221 | }
|
---|