Source for java.rmi.server.RMIClassLoader

   1: /* RMIClassLoader.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003, 2004
   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., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 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 gnu.classpath.ServiceFactory;
  42: import gnu.classpath.SystemProperties;
  43: import gnu.java.rmi.server.RMIClassLoaderImpl;
  44: 
  45: import java.net.MalformedURLException;
  46: import java.net.URL;
  47: import java.util.Iterator;
  48: 
  49: /**
  50:  * This class provides a set of public static utility methods for supporting
  51:  * network-based class loading in RMI. These methods are called by RMI's
  52:  * internal marshal streams to implement the dynamic class loading of types for
  53:  * RMI parameters and return values.
  54:  */
  55: public class RMIClassLoader
  56: {
  57:   /**
  58:    * This class isn't intended to be instantiated.
  59:    */
  60:   private RMIClassLoader() {}
  61: 
  62:   /**
  63:    * @deprecated
  64:    */
  65:   public static Class loadClass(String name)
  66:     throws MalformedURLException, ClassNotFoundException
  67:   {
  68:     return loadClass("", name);
  69:   }
  70: 
  71:   public static Class loadClass(String codebase, String name)
  72:     throws MalformedURLException, ClassNotFoundException
  73:   {
  74:     RMIClassLoaderSpi spi = getProviderInstance();
  75:     if (spi == null)
  76:       spi = getDefaultProviderInstance(); 
  77:     return spi.loadClass(codebase, name, null);
  78:   }
  79: 
  80:   public static Class loadClass(String codebase, String name,
  81:                                 ClassLoader defaultLoader)
  82:     throws MalformedURLException, ClassNotFoundException
  83:   {
  84:     RMIClassLoaderSpi spi = getProviderInstance();
  85:     if (spi == null)
  86:       spi = getDefaultProviderInstance(); 
  87:     return spi.loadClass(codebase, name, defaultLoader);
  88:   }
  89: 
  90:   public static Class loadProxyClass (String codeBase, String[] interfaces,
  91:                                       ClassLoader defaultLoader)
  92:     throws MalformedURLException, ClassNotFoundException
  93:   {
  94:     RMIClassLoaderSpi spi = getProviderInstance();
  95:     if (spi == null)
  96:       spi = getDefaultProviderInstance();
  97:     return spi.loadProxyClass(codeBase, interfaces, defaultLoader);
  98:   }
  99: 
 100:   /**
 101:    * Loads a class from <code>codeBase</code>.
 102:    *
 103:    * This method delegates to
 104:    * {@link RMIClassLoaderSpi#loadClass(String, String, ClassLoader)} and
 105:    * passes <code>codeBase.toString()</code> as first argument,
 106:    * <code>name</code> as second argument and <code>null</code> as third
 107:    * argument.
 108:    *
 109:    * @param codeBase the code base from which to load the class
 110:    * @param name the name of the class
 111:    *
 112:    * @return the loaded class
 113:    *
 114:    * @throws MalformedURLException if the URL is not well formed
 115:    * @throws ClassNotFoundException if the requested class cannot be found
 116:    */
 117:   public static Class loadClass(URL codeBase, String name)
 118:     throws MalformedURLException, ClassNotFoundException
 119:   {
 120:     RMIClassLoaderSpi spi = getProviderInstance();
 121:     if (spi == null)
 122:       spi = getDefaultProviderInstance(); 
 123:     return spi.loadClass(codeBase.toString(), name, null);
 124:   }
 125: 
 126:   /**
 127:    * Gets a classloader for the given codebase and with the current
 128:    * context classloader as parent.
 129:    * 
 130:    * @param codebase
 131:    * 
 132:    * @return a classloader for the given codebase
 133:    * 
 134:    * @throws MalformedURLException if the codebase contains a malformed URL
 135:    */
 136:   public static ClassLoader getClassLoader(String codebase) 
 137:     throws MalformedURLException
 138:   {
 139:     RMIClassLoaderSpi spi = getProviderInstance();
 140:     if (spi == null)
 141:       spi = getDefaultProviderInstance(); 
 142:     return spi.getClassLoader(codebase);
 143:   }
 144:  
 145:   /**
 146:    * Returns a string representation of the network location where a remote
 147:    * endpoint can get the class-definition of the given class.
 148:    *
 149:    * @param cl
 150:    *
 151:    * @return a space seperated list of URLs where the class-definition
 152:    * of cl may be found
 153:    */
 154:   public static String getClassAnnotation(Class cl)
 155:   {
 156:     RMIClassLoaderSpi spi = getProviderInstance();
 157:     if (spi == null)
 158:       spi = getDefaultProviderInstance(); 
 159:     return spi.getClassAnnotation(cl);
 160:   }
 161: 
 162:   /**
 163:    * @deprecated
 164:    */
 165:   public static Object getSecurityContext (ClassLoader loader)
 166:   {
 167:     throw new Error ("Not implemented");
 168:   }
 169: 
 170:   /**
 171:    * Returns the default service provider for <code>RMIClassLoader</code>.
 172:    *
 173:    * @return the default provider for <code>RMIClassLoader</code>
 174:    */
 175:   public static RMIClassLoaderSpi getDefaultProviderInstance()
 176:   {
 177:     return RMIClassLoaderImpl.getInstance();
 178:   }
 179: 
 180:   /**
 181:    * Chooses, instantiates and returns a service provider.
 182:    *
 183:    * @return a service provider
 184:    */
 185:   private static RMIClassLoaderSpi getProviderInstance()
 186:   {
 187:     // If the user asked for the default, return it.  We do a special
 188:     // check here because our standard service lookup function does not
 189:     // handle this -- nor should it.
 190:     String prop = SystemProperties.getProperty("java.rmi.server.RMIClassLoaderSpi");
 191:     if ("default".equals(prop))
 192:       return null;
 193:     Iterator it = ServiceFactory.lookupProviders(RMIClassLoaderSpi.class,
 194:                                                  null);
 195:     if (it == null || ! it.hasNext())
 196:       return null;
 197:     // FIXME: the spec says we ought to throw an Error of some kind if
 198:     // the specified provider is not suitable for some reason.  However
 199:     // our service factory simply logs the problem and moves on to the next
 200:     // provider in this situation.
 201:     return (RMIClassLoaderSpi) it.next();
 202:   }
 203: }