source: trunk/gcc/libjava/java/rmi/server/RMIClassLoader.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.1 KB
Line 
1/* RMIClassLoader.java
2 Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.rmi.server;
39
40import java.net.URL;
41import java.net.URLConnection;
42import java.net.URLClassLoader;
43import java.io.IOException;
44import java.io.DataInputStream;
45import java.net.MalformedURLException;
46import java.util.ArrayList;
47import java.util.Collection;
48import java.util.Collections;
49import java.util.Hashtable;
50import java.util.Map;
51import java.util.StringTokenizer;
52import java.util.WeakHashMap;
53
54public 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}
Note: See TracBrowser for help on using the repository browser.