diff options
author | Ingo Proetel <proetel@aicas.com> | 2003-08-12 12:01:36 +0000 |
---|---|---|
committer | Ingo Proetel <proetel@aicas.com> | 2003-08-12 12:01:36 +0000 |
commit | 0eb06f8b4a8372cf7262b23c9bad46bf7f97bafe (patch) | |
tree | f89027eea2fdbe57d6a906039f15b12ac1dba0d0 | |
parent | e2da5067d528cf4adb3761379ec12fa1d7ea0c46 (diff) | |
download | classpath-0eb06f8b4a8372cf7262b23c9bad46bf7f97bafe.tar.gz |
* java/rmi/server/RMIClassLoader.java: identify cached classloaders by
codebase and contextclassloader
* java/rmi/Naming.java: added comments, now accepts pseudo protocol "rmi"
* gnu/java/rmi/rmic/RMIC.java: uncommeted code
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | gnu/java/rmi/rmic/RMIC.java | 108 | ||||
-rw-r--r-- | java/rmi/Naming.java | 46 | ||||
-rw-r--r-- | java/rmi/server/RMIClassLoader.java | 310 |
4 files changed, 317 insertions, 154 deletions
@@ -1,3 +1,10 @@ +2003-08-12 Ingo Proetel <proetel@aicas.com> + + * java/rmi/server/RMIClassLoader.java: identify cached classloaders by + codebase and contextclassloader + * java/rmi/Naming.java: added comments, now accepts pseudo protocol "rmi" + * gnu/java/rmi/rmic/RMIC.java: uncommeted code + 2003-08-12 Jeroen Frijters <jeroen@frijters.net> * java/net/URLClassLoader: (findClass): close InputStream after diff --git a/gnu/java/rmi/rmic/RMIC.java b/gnu/java/rmi/rmic/RMIC.java index 0e5ef0bf9..81fc689cc 100644 --- a/gnu/java/rmi/rmic/RMIC.java +++ b/gnu/java/rmi/rmic/RMIC.java @@ -61,7 +61,7 @@ private boolean need11Stubs = true; private boolean need12Stubs = true; private boolean compile = true; private boolean verbose; -private String destination; +private String destination = ""; private PrintWriter out; private TabbedWriter ctrl; @@ -138,6 +138,9 @@ private boolean processClass(String classname) throws Exception { } private void analyzeClass(String cname) throws Exception { + if(verbose){ + System.out.println("[analyze class "+cname+"]"); + } int p = cname.lastIndexOf('.'); if (p != -1) { classname = cname.substring(p+1); @@ -147,29 +150,33 @@ private void analyzeClass(String cname) throws Exception { } fullclassname = cname; - // ??? + HashSet rmeths = new HashSet(); findClass(); // get the remote interface mRemoteInterface = getRemoteInterface(clazz); + if(mRemoteInterface == null) + return; + if(verbose){ + System.out.println("[implements "+mRemoteInterface.getName()+"]"); + } // check if the methods of the remote interface declare RemoteExceptions Method[] meths = mRemoteInterface.getDeclaredMethods(); for (int i = 0; i < meths.length; i++) { -// NYI: ignore check until Method.getExceptionTypes is implemented -// Class[] exceptions = meths[i].getExceptionTypes(); -// int index = 0; -// for(;index < exceptions.length; index++){ -// if(exceptions[index].equals(RemoteException.class)){ -// break; -// } -// } -// if (index < exceptions.length) { + Class[] exceptions = meths[i].getExceptionTypes(); + int index = 0; + for(;index < exceptions.length; index++){ + if(exceptions[index].equals(RemoteException.class)){ + break; + } + } + if (index < exceptions.length) { rmeths.add(meths[i]); -// } else { -// logError("Method "+meths[i]+" does not throw a java.rmi.RemoteException"); -// } + } else { + logError("Method "+meths[i]+" does not throw a java.rmi.RemoteException"); + } } @@ -192,7 +199,7 @@ private void findClass() throws ClassNotFoundException { private void generateStub() throws IOException { stubname = classname + "_Stub"; - ctrl = new TabbedWriter(new FileWriter(stubname + ".java")); + ctrl = new TabbedWriter(new FileWriter(destination + File.separator + stubname + ".java")); out = new PrintWriter(ctrl); if (verbose) { @@ -355,9 +362,7 @@ private void generateStub() throws IOException { } } out.print(") "); -// NYI hard-code java.rmi.RemoteException until Method.getExceptionTypes is implemented -// out.print("throws "); - out.print("throws java.rmi.RemoteException"); + out.print("throws "); for (int j = 0; j < except.length; j++) { out.print(getPrettyName(except[j])); if (j+1 < except.length) { @@ -615,7 +620,7 @@ private void generateStub() throws IOException { private void generateSkel() throws IOException { skelname = classname + "_Skel"; - ctrl = new TabbedWriter(new FileWriter(skelname + ".java")); + ctrl = new TabbedWriter(new FileWriter(destination + File.separator + skelname + ".java")); out = new PrintWriter(ctrl); if (verbose) { @@ -975,6 +980,35 @@ private void parseOptions() { } } +/** + * Looks for the java.rmi.Remote interface that that is implemented by theClazz. + * @param theClazz the class to look in + * @return the Remote interface of theClazz or null if theClazz does not implement a Remote interface + */ +private Class getRemoteInterface(Class theClazz) +{ + Class[] interfaces = theClazz.getInterfaces(); + for (int i = 0; i < interfaces.length; i++) + { + if (java.rmi.Remote.class.isAssignableFrom(interfaces[i])) + { + return interfaces[i]; + } + } + logError("Class "+ theClazz.getName() + + " is not a remote object. It does not implement an interface that is a java.rmi.Remote-interface."); + return null; +} + +/** + * Prints an error to System.err and increases the error count. + * @param theError + */ +private void logError(String theError){ + errorCount++; + System.err.println("error:"+theError); +} + private static void error(String message) { System.err.println("rmic: " + message); System.err.println("Try `rmic --help' for more information."); @@ -1028,40 +1062,4 @@ public int compareTo(Object obj) { } - /** - * Looks for the java.rmi.Remote interface that that is implemented by theClazz. - * @param theClazz the class to look in - * @return the Remote interface of theClazz - */ - private Class getRemoteInterface(Class theClazz) - { - System.out.println( - "[RMIC]looking for remote interface in " + theClazz.getName()); - Class[] interfaces = theClazz.getInterfaces(); - System.out.println("[RMIC] got interface array"); - for (int i = 0; i < interfaces.length; i++) - { - if (java.rmi.Remote.class.isAssignableFrom(interfaces[i])) - { - System.out.println("[RMIC]found remote interface " + interfaces[i].getName()); - return interfaces[i]; - } - } - System.out.println("[RMIC]found no remote interface"); - - logError("Class "+ theClazz.getName() - + " is not a remote object. It does not implement an interface that is a java.rmi.Remote-interface."); - return null; - } - - /** - * Prints an error to System.err and increases the error count. - * @param theError - */ - private void logError(String theError){ - errorCount++; - System.err.println("error:"+theError); - } - - } diff --git a/java/rmi/Naming.java b/java/rmi/Naming.java index b558976eb..2dd50d328 100644 --- a/java/rmi/Naming.java +++ b/java/rmi/Naming.java @@ -44,26 +44,72 @@ import java.rmi.registry.LocateRegistry; public final class Naming { +/** <pre> + * Looks for the remote object that is associated with the named service. + * Name and location is given in form of a URL without a scheme: + * + * //host:port/service-name + * + * The port is optional. + * </pre> + * @param name the service name and location + * @return Remote-object that implements the named service + * @throws NotBoundException if no object implements the service + * @throws MalformedURLException + * @throws RemoteException + */ public static Remote lookup(String name) throws NotBoundException, MalformedURLException, RemoteException { + // hack to accept "rmi://host:port/service" strings + if(name.startsWith("rmi:")){ name = name.substring(4); } URL u = new URL("http:" + name); return (getRegistry(u).lookup(u.getFile().substring(1))); } +/** + * Try to bind the given object to the given service name. + * @param name + * @param obj + * @throws AlreadyBoundException + * @throws MalformedURLException + * @throws RemoteException + */ public static void bind(String name, Remote obj) throws AlreadyBoundException, MalformedURLException, RemoteException { URL u = new URL("http:" + name); getRegistry(u).bind(u.getFile().substring(1), obj); } +/** + * Remove a binding for a given service name. + * @param name + * @throws RemoteException + * @throws NotBoundException + * @throws MalformedURLException + */ public static void unbind(String name) throws RemoteException, NotBoundException, MalformedURLException { URL u = new URL("http:" + name); getRegistry(u).unbind(u.getFile().substring(1)); } +/** + * Forces the binding between the given Remote-object and the given service name, even + * if there was already an object bound to this name. + * @param name + * @param obj + * @throws RemoteException + * @throws MalformedURLException + */ public static void rebind(String name, Remote obj) throws RemoteException, MalformedURLException { URL u = new URL("http:" + name); getRegistry(u).rebind(u.getFile().substring(1), obj); } +/** + * Lists all services at the named registry. + * @param name url that specifies the registry + * @return list of services at the name registry + * @throws RemoteException + * @throws MalformedURLException + */ public static String[] list(String name) throws RemoteException, MalformedURLException { return (getRegistry(new URL("http:" + name)).list()); } diff --git a/java/rmi/server/RMIClassLoader.java b/java/rmi/server/RMIClassLoader.java index 057ca0fd4..134a39f69 100644 --- a/java/rmi/server/RMIClassLoader.java +++ b/java/rmi/server/RMIClassLoader.java @@ -34,188 +34,300 @@ or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ - 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.net.MalformedURLException; 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; -public class RMIClassLoader -{ - static private class MyClassLoader extends URLClassLoader +/** + * This class provides a set of public static utility methods for supporting + * network-based class loading in RMI. These methods are called by RMI's + * internal marshal streams to implement the dynamic class loading of types for + * RMI parameters and return values. + */ +public class RMIClassLoader { - - private MyClassLoader(URL[] urls, ClassLoader parent, String annotation) + static private class MyClassLoader extends URLClassLoader { + private MyClassLoader(URL[] urls, ClassLoader parent, String annotation) + { super(urls, parent); this.annotation = annotation; - } + } private MyClassLoader(URL[] urls, ClassLoader parent) - { - super (urls, parent); + { + super(urls, parent); this.annotation = urlToAnnotation(urls); - } + } public static String urlToAnnotation(URL[] urls) - { + { if (urls.length == 0) - return null; + { + return null; + } - StringBuffer annotation = new StringBuffer(64*urls.length); - for(int i = 0; i < urls.length; i++) - { - annotation.append(urls[i].toExternalForm()); - annotation.append(' '); - } + StringBuffer annotation = new StringBuffer(64 * urls.length); + + for (int i = 0; i < urls.length; i++) + { + annotation.append(urls[i].toExternalForm()); + annotation.append(' '); + } return annotation.toString(); - } + } - public final String getClassAnnotation(){ + public final String getClassAnnotation() + { return annotation; - } + } private final String annotation; + + } + + /** + * This class is used to identify a cached classloader by its codebase and + * the context classloader that is its parent. + */ + private static class CacheKey + { + + private String mCodeBase; + private ClassLoader mContextClassLoader; + + public CacheKey(String theCodebase, ClassLoader theContextClassLoader) + { + mCodeBase = theCodebase; + mContextClassLoader = theContextClassLoader; + } + + + + /** + * @return true if the codebase and the context classloader are equal + */ + public boolean equals(Object theOther) + { + if(theOther != null && theOther instanceof CacheKey) + { + CacheKey key = (CacheKey) theOther; + return ( equals(this.mCodeBase,key.mCodeBase) + && equals(this.mContextClassLoader,key.mContextClassLoader) ); + } + return false; + } + + /** + * Test if the two objects are equal or both null. + * @param theOne + * @param theOther + * @return + */ + private boolean equals(Object theOne, Object theOther) + { + return (theOne != null ? theOne.equals(theOther): theOther == null); + } + + /** + * @return hashCode + */ + public int hashCode() + { + return ((mCodeBase != null ? mCodeBase.hashCode() : 0) + ^(mContextClassLoader != null ? mContextClassLoader.hashCode() : -1)); + } + + + public String toString() + { + return "["+mCodeBase+","+mContextClassLoader+"]"; + } } private static Map cacheLoaders; //map annotations to loaders private static Map cacheAnnotations; //map loaders to annotations - - //defaultAnnotation is got from system property - // "java.rmi.server.defaultAnnotation" private static String defaultAnnotation; + //URL object for defaultAnnotation private static URL defaultCodebase; + //class loader for defaultAnnotation private static MyClassLoader defaultLoader; - + static - { + { // 89 is a nice prime number for Hashtable initial capacity cacheLoaders = new Hashtable(89); cacheAnnotations = new Hashtable(89); - + defaultAnnotation = System.getProperty("java.rmi.server.defaultAnnotation"); - try + + try + { + if (defaultAnnotation != null) { - if (defaultAnnotation != null) - defaultCodebase = new URL(defaultAnnotation); + defaultCodebase = new URL(defaultAnnotation); + } } - catch(Exception _) + catch (Exception _) { - defaultCodebase = null; + defaultCodebase = null; } + if (defaultCodebase != null) { - defaultLoader = new MyClassLoader(new URL[]{ defaultCodebase }, - null, defaultAnnotation); - cacheLoaders.put(defaultAnnotation, defaultLoader); + defaultLoader = new MyClassLoader(new URL[] { defaultCodebase }, null, + defaultAnnotation); + cacheLoaders.put(new CacheKey(defaultAnnotation,Thread.currentThread().getContextClassLoader()), defaultLoader); } - } - + } + /** * @deprecated */ public static Class loadClass(String name) - throws MalformedURLException, ClassNotFoundException - { + throws MalformedURLException, ClassNotFoundException + { return (loadClass("", name)); - } + } - public static Class loadClass(String codebases, String name) - throws MalformedURLException, ClassNotFoundException - { + 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 + try { - c = loader.loadClass(name); - } - catch(ClassNotFoundException e) {} + c = loader.loadClass(name); - if (c != null) return c; + } + catch (ClassNotFoundException e) + { + // class not found in the local classpath + } 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); - } + } + else + { + loader = getClassLoader(codebases); } + if (loader == null) + { + //do not throw NullPointerException + throw new ClassNotFoundException("Could not find class (" + name + + ") at codebase (" + codebases + ")"); + } + return loader.loadClass(name); - } - + } + + /** + * 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; + CacheKey loaderKey = new CacheKey(codebases, Thread.currentThread().getContextClassLoader()); + loader = (ClassLoader) cacheLoaders.get(loaderKey); + + 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()]), + Thread.currentThread().getContextClassLoader(), + codebases); + cacheLoaders.put(loaderKey, 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) { - return ((MyClassLoader)loader).getClassAnnotation(); + return ((MyClassLoader) loader).getClassAnnotation(); } - - String s = (String)cacheAnnotations.get(loader); + + String s = (String) cacheAnnotations.get(loader); + if (s != null) + { return s; - + } + if (loader instanceof URLClassLoader) { - URL[] urls = ((URLClassLoader)loader).getURLs(); - if(urls.length == 0) - return null; + URL[] urls = ((URLClassLoader) loader).getURLs(); + + 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); + 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"); + } + /** * @deprecated */ public static Object getSecurityContext(ClassLoader loader) - { + { throw new Error("Not implemented"); + } } - -} |