RMIClassLoader.java: Removed unused imports, little reformatings.
2003-10-09 Michael Koch <konqueror@gmx.de> * java/rmi/server/RMIClassLoader.java: Removed unused imports, little reformatings. (getClassLoader): New method, implementation was part of old loadCLass method. (loadClass): Simplified by moving functionality to new method and reworking the code a bit. (getClassAnnotation): Merged documentation from classpath. From-SVN: r72267
This commit is contained in:
parent
cf48cabb7d
commit
69c7b82769
2 changed files with 104 additions and 58 deletions
|
@ -40,17 +40,11 @@ package java.rmi.server;
|
|||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLClassLoader;
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -106,6 +100,7 @@ public class RMIClassLoader
|
|||
//defaultAnnotation is got from system property
|
||||
// "java.rmi.server.defaultAnnotation"
|
||||
private static String defaultAnnotation;
|
||||
|
||||
//URL object for defaultAnnotation
|
||||
private static URL defaultCodebase;
|
||||
|
||||
|
@ -121,19 +116,19 @@ public class RMIClassLoader
|
|||
defaultAnnotation = System.getProperty ("java.rmi.server.defaultAnnotation");
|
||||
|
||||
try
|
||||
{
|
||||
if (defaultAnnotation != null)
|
||||
defaultCodebase = new URL (defaultAnnotation);
|
||||
}
|
||||
{
|
||||
if (defaultAnnotation != null)
|
||||
defaultCodebase = new URL (defaultAnnotation);
|
||||
}
|
||||
catch (Exception _)
|
||||
{
|
||||
defaultCodebase = null;
|
||||
}
|
||||
{
|
||||
defaultCodebase = null;
|
||||
}
|
||||
|
||||
if (defaultCodebase != null)
|
||||
{
|
||||
defaultLoader = new MyClassLoader(new URL[]{ defaultCodebase },
|
||||
null, defaultAnnotation);
|
||||
defaultLoader = new MyClassLoader (new URL[] { defaultCodebase }, null,
|
||||
defaultAnnotation);
|
||||
cacheLoaders.put(defaultAnnotation, defaultLoader);
|
||||
}
|
||||
}
|
||||
|
@ -150,49 +145,87 @@ public class RMIClassLoader
|
|||
public static Class loadClass (String codebases, String name)
|
||||
throws MalformedURLException, ClassNotFoundException
|
||||
{
|
||||
Class c = null;
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
//try context class loader first
|
||||
try
|
||||
{
|
||||
c = loader.loadClass(name);
|
||||
return loader.loadClass (name);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
// class not found in the local classpath
|
||||
}
|
||||
catch(ClassNotFoundException e) {}
|
||||
|
||||
if (c != null)
|
||||
return c;
|
||||
|
||||
if (codebases.length() == 0) //==""
|
||||
loader = defaultLoader;
|
||||
else
|
||||
{
|
||||
loader = (ClassLoader)cacheLoaders.get(codebases);
|
||||
if (loader == null)
|
||||
{
|
||||
//create an entry in cacheLoaders mapping a loader to codebases.
|
||||
|
||||
// codebases are separated by " "
|
||||
StringTokenizer tok = new StringTokenizer(codebases, " ");
|
||||
ArrayList urls = new ArrayList();
|
||||
while (tok.hasMoreTokens())
|
||||
urls.add(new URL(tok.nextToken()));
|
||||
|
||||
loader = new MyClassLoader((URL[])urls.toArray(new URL[urls.size()]),
|
||||
null, codebases);
|
||||
cacheLoaders.put(codebases, loader);
|
||||
}
|
||||
loader = defaultLoader;
|
||||
}
|
||||
else
|
||||
{
|
||||
loader = getClassLoader(codebases);
|
||||
}
|
||||
|
||||
return loader.loadClass(name);
|
||||
if (loader == null)
|
||||
{
|
||||
//do not throw NullPointerException
|
||||
throw new ClassNotFoundException ("Could not find class (" + name +
|
||||
") at codebase (" + codebases + ")");
|
||||
}
|
||||
|
||||
return loader.loadClass (name);
|
||||
}
|
||||
|
||||
public static String getClassAnnotation(Class cl)
|
||||
|
||||
/**
|
||||
* Gets a classloader for the given codebase and with the current
|
||||
* context classloader as parent.
|
||||
*
|
||||
* @param codebases
|
||||
*
|
||||
* @return a classloader for the given codebase
|
||||
*
|
||||
* @throws MalformedURLException if the codebase contains a malformed URL
|
||||
*/
|
||||
private static ClassLoader getClassLoader (String codebases)
|
||||
throws MalformedURLException
|
||||
{
|
||||
ClassLoader loader = (ClassLoader) cacheLoaders.get (codebases);
|
||||
|
||||
if (loader == null)
|
||||
{
|
||||
//create an entry in cacheLoaders mapping a loader to codebases.
|
||||
// codebases are separated by " "
|
||||
StringTokenizer tok = new StringTokenizer (codebases, " ");
|
||||
ArrayList urls = new ArrayList();
|
||||
|
||||
while (tok.hasMoreTokens())
|
||||
urls.add (new URL (tok.nextToken()));
|
||||
|
||||
loader = new MyClassLoader ((URL[]) urls.toArray (new URL [urls.size()]),
|
||||
null, codebases);
|
||||
cacheLoaders.put (codebases, loader);
|
||||
}
|
||||
|
||||
return loader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the network location where a remote
|
||||
* endpoint can get the class-definition of the given class.
|
||||
*
|
||||
* @param cl
|
||||
*
|
||||
* @return a space seperated list of URLs where the class-definition
|
||||
* of cl may be found
|
||||
*/
|
||||
public static String getClassAnnotation (Class cl)
|
||||
{
|
||||
ClassLoader loader = cl.getClassLoader();
|
||||
if (loader == null || loader == ClassLoader.getSystemClassLoader())
|
||||
|
||||
if (loader == null
|
||||
|| loader == ClassLoader.getSystemClassLoader())
|
||||
{
|
||||
return null; //??
|
||||
return System.getProperty ("java.rmi.server.codebase");
|
||||
}
|
||||
|
||||
if (loader instanceof MyClassLoader)
|
||||
|
@ -203,26 +236,29 @@ public class RMIClassLoader
|
|||
String s = (String) cacheAnnotations.get (loader);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
return s;
|
||||
|
||||
if (loader instanceof URLClassLoader)
|
||||
{
|
||||
URL[] urls = ((URLClassLoader)loader).getURLs();
|
||||
if(urls.length == 0)
|
||||
return null;
|
||||
URL[] urls = ((URLClassLoader) loader).getURLs();
|
||||
|
||||
StringBuffer annotation = new StringBuffer(64*urls.length);
|
||||
for(int i = 0; i < urls.length; i++)
|
||||
{
|
||||
annotation.append(urls[i].toExternalForm());
|
||||
annotation.append(' ');
|
||||
}
|
||||
s = annotation.toString();
|
||||
cacheAnnotations.put(loader, s);
|
||||
if (urls.length == 0)
|
||||
return null;
|
||||
|
||||
StringBuffer annotation = new StringBuffer (64 * urls.length);
|
||||
|
||||
for (int i = 0; i < urls.length; i++)
|
||||
{
|
||||
annotation.append (urls [i].toExternalForm());
|
||||
annotation.append (' ');
|
||||
}
|
||||
|
||||
s = annotation.toString();
|
||||
cacheAnnotations.put (loader, s);
|
||||
return s;
|
||||
}
|
||||
return null;
|
||||
|
||||
return System.getProperty ("java.rmi.server.codebase");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue