1. /*
  2. * @(#)JDKBridge.java 1.97 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.util;
  16. import java.rmi.Remote;
  17. import java.rmi.NoSuchObjectException;
  18. import java.rmi.server.RMIClassLoader;
  19. import java.rmi.server.UnicastRemoteObject;
  20. import org.omg.CORBA.BAD_PARAM;
  21. import org.omg.CORBA.CompletionStatus;
  22. import com.sun.corba.se.internal.io.LibraryManager;
  23. import java.util.Properties;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.security.AccessController;
  27. import java.security.PrivilegedAction;
  28. import java.net.MalformedURLException;
  29. import sun.security.action.GetPropertyAction;
  30. /**
  31. * Utility methods for doing various method calls which are used
  32. * by multiple classes
  33. */
  34. public class JDKBridge {
  35. /**
  36. * Get local codebase System property (java.rmi.server.codebase).
  37. * May be null or a space separated array of URLS.
  38. */
  39. public static String getLocalCodebase () {
  40. return localCodebase;
  41. }
  42. /**
  43. * Return true if the system property "java.rmi.server.useCodebaseOnly"
  44. * is set, false otherwise.
  45. */
  46. public static boolean useCodebaseOnly () {
  47. return useCodebaseOnly;
  48. }
  49. /**
  50. * Returns a class instance for the specified class.
  51. * @param className the name of the class
  52. * @param remoteCodebase a space-separated array of urls at which
  53. * the class might be found. May be null.
  54. * @param loader a ClassLoader who may be used to
  55. * load the class if all other methods fail.
  56. * @return the <code>Class</code> object representing the loaded class.
  57. * @exception throws ClassNotFoundException if class cannot be loaded.
  58. */
  59. public static Class loadClass (String className,
  60. String remoteCodebase,
  61. ClassLoader loader)
  62. throws ClassNotFoundException {
  63. // Make sure we've loaded the native code so that we can
  64. // use JDKClassLoader. Do this here rather than at static
  65. // init time to ensure that we only do it when needed...
  66. if (!libraryLoaded) {
  67. LibraryManager.load();
  68. libraryLoaded = true;
  69. }
  70. if (loader == null) {
  71. return loadClassM(className,remoteCodebase,useCodebaseOnly);
  72. } else {
  73. try {
  74. return loadClassM(className,remoteCodebase,useCodebaseOnly);
  75. } catch (ClassNotFoundException e) {
  76. return loader.loadClass(className);
  77. }
  78. }
  79. }
  80. /**
  81. * Returns a class instance for the specified class.
  82. * @param className the name of the class
  83. * @param remoteCodebase a space-separated array of urls at which
  84. * the class might be found. May be null.
  85. * @return the <code>Class</code> object representing the loaded class.
  86. * @exception throws ClassNotFoundException if class cannot be loaded.
  87. */
  88. public static Class loadClass (String className,
  89. String remoteCodebase)
  90. throws ClassNotFoundException {
  91. return loadClass(className,remoteCodebase,null);
  92. }
  93. /**
  94. * Returns a class instance for the specified class.
  95. * @param className the name of the class
  96. * @return the <code>Class</code> object representing the loaded class.
  97. * @exception throws ClassNotFoundException if class cannot be loaded.
  98. */
  99. public static Class loadClass (String className)
  100. throws ClassNotFoundException {
  101. return loadClass(className,null,null);
  102. }
  103. private static final String LOCAL_CODEBASE_KEY = "java.rmi.server.codebase";
  104. private static final String USE_CODEBASE_ONLY_KEY = "java.rmi.server.useCodebaseOnly";
  105. private static String localCodebase = null;
  106. private static boolean useCodebaseOnly;
  107. private static boolean libraryLoaded = false;
  108. static {
  109. setCodebaseProperties();
  110. }
  111. public static final void main (String[] args) {
  112. System.out.println("1.2 VM");
  113. /*
  114. // If on 1.2, use a policy with all permissions.
  115. System.setSecurityManager (new javax.rmi.download.SecurityManager());
  116. String targetClass = "[[Lrmic.Typedef;";
  117. System.out.println("localCodebase = "+localCodebase);
  118. System.out.println("Trying to load "+targetClass);
  119. try {
  120. Class clz = loadClass(targetClass,null,localCodebase);
  121. System.out.println("Loaded: "+clz);
  122. } catch (ClassNotFoundException e) {
  123. System.out.println("Caught "+e);
  124. }
  125. */
  126. }
  127. /**
  128. * Set the codebase and useCodebaseOnly properties. This is public
  129. * only for test code.
  130. */
  131. public static synchronized void setCodebaseProperties () {
  132. String prop = (String)AccessController.doPrivileged(
  133. new GetPropertyAction(LOCAL_CODEBASE_KEY)
  134. );
  135. if (prop != null && prop.trim().length() > 0) {
  136. localCodebase = prop;
  137. }
  138. prop = (String)AccessController.doPrivileged(
  139. new GetPropertyAction(USE_CODEBASE_ONLY_KEY)
  140. );
  141. if (prop != null && prop.trim().length() > 0) {
  142. useCodebaseOnly = Boolean.valueOf(prop).booleanValue();
  143. }
  144. }
  145. /**
  146. * Set the default code base. This method is here only
  147. * for test code.
  148. */
  149. public static synchronized void setLocalCodebase(String codebase) {
  150. localCodebase = codebase;
  151. }
  152. private static Class loadClassM (String className,
  153. String remoteCodebase,
  154. boolean useCodebaseOnly)
  155. throws ClassNotFoundException {
  156. try {
  157. return JDKClassLoader.loadClass(null,className);
  158. } catch (ClassNotFoundException e) {}
  159. try {
  160. if (!useCodebaseOnly && remoteCodebase != null) {
  161. return RMIClassLoader.loadClass(remoteCodebase,
  162. className);
  163. } else {
  164. return RMIClassLoader.loadClass(className);
  165. }
  166. } catch (MalformedURLException e) {
  167. className = className + ": " + e.toString();
  168. }
  169. throw new ClassNotFoundException(className);
  170. }
  171. }