001 /* RMIClassLoader.java -- 002 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003, 2004 003 Free Software Foundation, Inc. 004 005 This file is part of GNU Classpath. 006 007 GNU Classpath is free software; you can redistribute it and/or modify 008 it under the terms of the GNU General Public License as published by 009 the Free Software Foundation; either version 2, or (at your option) 010 any later version. 011 012 GNU Classpath is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of 014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 General Public License for more details. 016 017 You should have received a copy of the GNU General Public License 018 along with GNU Classpath; see the file COPYING. If not, write to the 019 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 020 02110-1301 USA. 021 022 Linking this library statically or dynamically with other modules is 023 making a combined work based on this library. Thus, the terms and 024 conditions of the GNU General Public License cover the whole 025 combination. 026 027 As a special exception, the copyright holders of this library give you 028 permission to link this library with independent modules to produce an 029 executable, regardless of the license terms of these independent 030 modules, and to copy and distribute the resulting executable under 031 terms of your choice, provided that you also meet, for each linked 032 independent module, the terms and conditions of the license of that 033 module. An independent module is a module which is not derived from 034 or based on this library. If you modify this library, you may extend 035 this exception to your version of the library, but you are not 036 obligated to do so. If you do not wish to do so, delete this 037 exception statement from your version. */ 038 039 package java.rmi.server; 040 041 import gnu.classpath.ServiceFactory; 042 import gnu.classpath.SystemProperties; 043 import gnu.java.rmi.server.RMIClassLoaderImpl; 044 045 import java.net.MalformedURLException; 046 import java.net.URL; 047 import java.util.Iterator; 048 049 /** 050 * This class provides a set of public static utility methods for supporting 051 * network-based class loading in RMI. These methods are called by RMI's 052 * internal marshal streams to implement the dynamic class loading of types for 053 * RMI parameters and return values. 054 * @since 1.1 055 */ 056 public class RMIClassLoader 057 { 058 /** 059 * This class isn't intended to be instantiated. 060 */ 061 private RMIClassLoader() {} 062 063 /** 064 * @deprecated 065 */ 066 public static Class<?> loadClass(String name) 067 throws MalformedURLException, ClassNotFoundException 068 { 069 return loadClass("", name); 070 } 071 072 public static Class<?> loadClass(String codebase, String name) 073 throws MalformedURLException, ClassNotFoundException 074 { 075 RMIClassLoaderSpi spi = getProviderInstance(); 076 if (spi == null) 077 spi = getDefaultProviderInstance(); 078 return spi.loadClass(codebase, name, null); 079 } 080 081 public static Class<?> loadClass(String codebase, String name, 082 ClassLoader defaultLoader) 083 throws MalformedURLException, ClassNotFoundException 084 { 085 RMIClassLoaderSpi spi = getProviderInstance(); 086 if (spi == null) 087 spi = getDefaultProviderInstance(); 088 return spi.loadClass(codebase, name, defaultLoader); 089 } 090 091 public static Class<?> loadProxyClass (String codeBase, String[] interfaces, 092 ClassLoader defaultLoader) 093 throws MalformedURLException, ClassNotFoundException 094 { 095 RMIClassLoaderSpi spi = getProviderInstance(); 096 if (spi == null) 097 spi = getDefaultProviderInstance(); 098 return spi.loadProxyClass(codeBase, interfaces, defaultLoader); 099 } 100 101 /** 102 * Loads a class from <code>codeBase</code>. 103 * 104 * This method delegates to 105 * {@link RMIClassLoaderSpi#loadClass(String, String, ClassLoader)} and 106 * passes <code>codeBase.toString()</code> as first argument, 107 * <code>name</code> as second argument and <code>null</code> as third 108 * argument. 109 * 110 * @param codeBase the code base from which to load the class 111 * @param name the name of the class 112 * 113 * @return the loaded class 114 * 115 * @throws MalformedURLException if the URL is not well formed 116 * @throws ClassNotFoundException if the requested class cannot be found 117 */ 118 public static Class<?> loadClass(URL codeBase, String name) 119 throws MalformedURLException, ClassNotFoundException 120 { 121 RMIClassLoaderSpi spi = getProviderInstance(); 122 if (spi == null) 123 spi = getDefaultProviderInstance(); 124 return spi.loadClass(codeBase.toString(), name, null); 125 } 126 127 /** 128 * Gets a classloader for the given codebase and with the current 129 * context classloader as parent. 130 * 131 * @param codebase 132 * 133 * @return a classloader for the given codebase 134 * 135 * @throws MalformedURLException if the codebase contains a malformed URL 136 */ 137 public static ClassLoader getClassLoader(String codebase) 138 throws MalformedURLException 139 { 140 RMIClassLoaderSpi spi = getProviderInstance(); 141 if (spi == null) 142 spi = getDefaultProviderInstance(); 143 return spi.getClassLoader(codebase); 144 } 145 146 /** 147 * Returns a string representation of the network location where a remote 148 * endpoint can get the class-definition of the given class. 149 * 150 * @param cl 151 * 152 * @return a space seperated list of URLs where the class-definition 153 * of cl may be found 154 */ 155 public static String getClassAnnotation(Class<?> cl) 156 { 157 RMIClassLoaderSpi spi = getProviderInstance(); 158 if (spi == null) 159 spi = getDefaultProviderInstance(); 160 return spi.getClassAnnotation(cl); 161 } 162 163 /** 164 * @deprecated 165 */ 166 public static Object getSecurityContext (ClassLoader loader) 167 { 168 throw new Error ("Not implemented"); 169 } 170 171 /** 172 * Returns the default service provider for <code>RMIClassLoader</code>. 173 * 174 * @return the default provider for <code>RMIClassLoader</code> 175 */ 176 public static RMIClassLoaderSpi getDefaultProviderInstance() 177 { 178 return RMIClassLoaderImpl.getInstance(); 179 } 180 181 /** 182 * Chooses, instantiates and returns a service provider. 183 * 184 * @return a service provider 185 */ 186 private static RMIClassLoaderSpi getProviderInstance() 187 { 188 // If the user asked for the default, return it. We do a special 189 // check here because our standard service lookup function does not 190 // handle this -- nor should it. 191 String prop = SystemProperties.getProperty("java.rmi.server.RMIClassLoaderSpi"); 192 if ("default".equals(prop)) 193 return null; 194 Iterator it = ServiceFactory.lookupProviders(RMIClassLoaderSpi.class, 195 null); 196 if (it == null || ! it.hasNext()) 197 return null; 198 // FIXME: the spec says we ought to throw an Error of some kind if 199 // the specified provider is not suitable for some reason. However 200 // our service factory simply logs the problem and moves on to the next 201 // provider in this situation. 202 return (RMIClassLoaderSpi) it.next(); 203 } 204 }