JDKBridge.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25/* 26 * Licensed Materials - Property of IBM 27 * RMI-IIOP v1.0 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved 29 * 30 */ 31 32package com.sun.corba.se.impl.util; 33 34import java.rmi.Remote; 35import java.rmi.NoSuchObjectException; 36import java.rmi.server.RMIClassLoader; 37import java.rmi.server.UnicastRemoteObject; 38import org.omg.CORBA.BAD_PARAM; 39import org.omg.CORBA.CompletionStatus; 40import java.util.Properties; 41import java.io.File; 42import java.io.FileInputStream; 43import java.security.AccessController; 44import java.security.PrivilegedAction; 45import java.net.MalformedURLException; 46import com.sun.corba.se.impl.orbutil.GetPropertyAction; 47 48/** 49 * Utility methods for doing various method calls which are used 50 * by multiple classes 51 */ 52public class JDKBridge { 53 54 /** 55 * Get local codebase System property (java.rmi.server.codebase). 56 * May be null or a space separated array of URLS. 57 */ 58 public static String getLocalCodebase () { 59 return localCodebase; 60 } 61 62 /** 63 * Return true if the system property "java.rmi.server.useCodebaseOnly" 64 * is set, false otherwise. 65 */ 66 public static boolean useCodebaseOnly () { 67 return useCodebaseOnly; 68 } 69 70 /** 71 * Returns a class instance for the specified class. 72 * @param className the name of the class 73 * @param remoteCodebase a space-separated array of urls at which 74 * the class might be found. May be null. 75 * @param loader a ClassLoader who may be used to 76 * load the class if all other methods fail. 77 * @return the <code>Class</code> object representing the loaded class. 78 * @exception throws ClassNotFoundException if class cannot be loaded. 79 */ 80 public static Class loadClass (String className, 81 String remoteCodebase, 82 ClassLoader loader) 83 throws ClassNotFoundException { 84 85 if (loader == null) { 86 return loadClassM(className,remoteCodebase,useCodebaseOnly); 87 } else { 88 try { 89 return loadClassM(className,remoteCodebase,useCodebaseOnly); 90 } catch (ClassNotFoundException e) { 91 return loader.loadClass(className); 92 } 93 } 94 } 95 96 /** 97 * Returns a class instance for the specified class. 98 * @param className the name of the class 99 * @param remoteCodebase a space-separated array of urls at which 100 * the class might be found. May be null. 101 * @return the <code>Class</code> object representing the loaded class. 102 * @exception throws ClassNotFoundException if class cannot be loaded. 103 */ 104 public static Class loadClass (String className, 105 String remoteCodebase) 106 throws ClassNotFoundException { 107 return loadClass(className,remoteCodebase,null); 108 } 109 110 /** 111 * Returns a class instance for the specified class. 112 * @param className the name of the class 113 * @return the <code>Class</code> object representing the loaded class. 114 * @exception throws ClassNotFoundException if class cannot be loaded. 115 */ 116 public static Class loadClass (String className) 117 throws ClassNotFoundException { 118 return loadClass(className,null,null); 119 } 120 121 private static final String LOCAL_CODEBASE_KEY = "java.rmi.server.codebase"; 122 private static final String USE_CODEBASE_ONLY_KEY = "java.rmi.server.useCodebaseOnly"; 123 private static String localCodebase = null; 124 private static boolean useCodebaseOnly; 125 126 static { 127 setCodebaseProperties(); 128 } 129 130 public static final void main (String[] args) { 131 System.out.println("1.2 VM"); 132 133 /* 134 // If on 1.2, use a policy with all permissions. 135 System.setSecurityManager (new javax.rmi.download.SecurityManager()); 136 String targetClass = "[[Lrmic.Typedef;"; 137 System.out.println("localCodebase = "+localCodebase); 138 System.out.println("Trying to load "+targetClass); 139 try { 140 Class clz = loadClass(targetClass,null,localCodebase); 141 System.out.println("Loaded: "+clz); 142 } catch (ClassNotFoundException e) { 143 System.out.println("Caught "+e); 144 } 145 */ 146 } 147 148 /** 149 * Set the codebase and useCodebaseOnly properties. This is public 150 * only for test code. 151 */ 152 public static synchronized void setCodebaseProperties () { 153 String prop = (String)AccessController.doPrivileged( 154 new GetPropertyAction(LOCAL_CODEBASE_KEY) 155 ); 156 if (prop != null && prop.trim().length() > 0) { 157 localCodebase = prop; 158 } 159 160 prop = (String)AccessController.doPrivileged( 161 new GetPropertyAction(USE_CODEBASE_ONLY_KEY) 162 ); 163 if (prop != null && prop.trim().length() > 0) { 164 useCodebaseOnly = Boolean.valueOf(prop).booleanValue(); 165 } 166 } 167 168 /** 169 * Set the default code base. This method is here only 170 * for test code. 171 */ 172 public static synchronized void setLocalCodebase(String codebase) { 173 localCodebase = codebase; 174 } 175 176 private static Class loadClassM (String className, 177 String remoteCodebase, 178 boolean useCodebaseOnly) 179 throws ClassNotFoundException { 180 181 try { 182 return JDKClassLoader.loadClass(null,className); 183 } catch (ClassNotFoundException e) {} 184 try { 185 if (!useCodebaseOnly && remoteCodebase != null) { 186 return RMIClassLoader.loadClass(remoteCodebase, 187 className); 188 } else { 189 return RMIClassLoader.loadClass(className); 190 } 191 } catch (MalformedURLException e) { 192 className = className + ": " + e.toString(); 193 } 194 195 throw new ClassNotFoundException(className); 196 } 197} 198