1. /*
  2. * @(#)JDKBridge.java 1.100 04/03/01
  3. *
  4. * Copyright 2004 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.impl.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 java.util.Properties;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.security.AccessController;
  26. import java.security.PrivilegedAction;
  27. import java.net.MalformedURLException;
  28. import com.sun.corba.se.impl.orbutil.GetPropertyAction;
  29. /**
  30. * Utility methods for doing various method calls which are used
  31. * by multiple classes
  32. */
  33. public class JDKBridge {
  34. /**
  35. * Get local codebase System property (java.rmi.server.codebase).
  36. * May be null or a space separated array of URLS.
  37. */
  38. public static String getLocalCodebase () {
  39. return localCodebase;
  40. }
  41. /**
  42. * Return true if the system property "java.rmi.server.useCodebaseOnly"
  43. * is set, false otherwise.
  44. */
  45. public static boolean useCodebaseOnly () {
  46. return useCodebaseOnly;
  47. }
  48. /**
  49. * Returns a class instance for the specified class.
  50. * @param className the name of the class
  51. * @param remoteCodebase a space-separated array of urls at which
  52. * the class might be found. May be null.
  53. * @param loader a ClassLoader who may be used to
  54. * load the class if all other methods fail.
  55. * @return the <code>Class</code> object representing the loaded class.
  56. * @exception throws ClassNotFoundException if class cannot be loaded.
  57. */
  58. public static Class loadClass (String className,
  59. String remoteCodebase,
  60. ClassLoader loader)
  61. throws ClassNotFoundException {
  62. if (loader == null) {
  63. return loadClassM(className,remoteCodebase,useCodebaseOnly);
  64. } else {
  65. try {
  66. return loadClassM(className,remoteCodebase,useCodebaseOnly);
  67. } catch (ClassNotFoundException e) {
  68. return loader.loadClass(className);
  69. }
  70. }
  71. }
  72. /**
  73. * Returns a class instance for the specified class.
  74. * @param className the name of the class
  75. * @param remoteCodebase a space-separated array of urls at which
  76. * the class might be found. May be null.
  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. throws ClassNotFoundException {
  83. return loadClass(className,remoteCodebase,null);
  84. }
  85. /**
  86. * Returns a class instance for the specified class.
  87. * @param className the name of the class
  88. * @return the <code>Class</code> object representing the loaded class.
  89. * @exception throws ClassNotFoundException if class cannot be loaded.
  90. */
  91. public static Class loadClass (String className)
  92. throws ClassNotFoundException {
  93. return loadClass(className,null,null);
  94. }
  95. private static final String LOCAL_CODEBASE_KEY = "java.rmi.server.codebase";
  96. private static final String USE_CODEBASE_ONLY_KEY = "java.rmi.server.useCodebaseOnly";
  97. private static String localCodebase = null;
  98. private static boolean useCodebaseOnly;
  99. static {
  100. setCodebaseProperties();
  101. }
  102. public static final void main (String[] args) {
  103. System.out.println("1.2 VM");
  104. /*
  105. // If on 1.2, use a policy with all permissions.
  106. System.setSecurityManager (new javax.rmi.download.SecurityManager());
  107. String targetClass = "[[Lrmic.Typedef;";
  108. System.out.println("localCodebase = "+localCodebase);
  109. System.out.println("Trying to load "+targetClass);
  110. try {
  111. Class clz = loadClass(targetClass,null,localCodebase);
  112. System.out.println("Loaded: "+clz);
  113. } catch (ClassNotFoundException e) {
  114. System.out.println("Caught "+e);
  115. }
  116. */
  117. }
  118. /**
  119. * Set the codebase and useCodebaseOnly properties. This is public
  120. * only for test code.
  121. */
  122. public static synchronized void setCodebaseProperties () {
  123. String prop = (String)AccessController.doPrivileged(
  124. new GetPropertyAction(LOCAL_CODEBASE_KEY)
  125. );
  126. if (prop != null && prop.trim().length() > 0) {
  127. localCodebase = prop;
  128. }
  129. prop = (String)AccessController.doPrivileged(
  130. new GetPropertyAction(USE_CODEBASE_ONLY_KEY)
  131. );
  132. if (prop != null && prop.trim().length() > 0) {
  133. useCodebaseOnly = Boolean.valueOf(prop).booleanValue();
  134. }
  135. }
  136. /**
  137. * Set the default code base. This method is here only
  138. * for test code.
  139. */
  140. public static synchronized void setLocalCodebase(String codebase) {
  141. localCodebase = codebase;
  142. }
  143. private static Class loadClassM (String className,
  144. String remoteCodebase,
  145. boolean useCodebaseOnly)
  146. throws ClassNotFoundException {
  147. try {
  148. return JDKClassLoader.loadClass(null,className);
  149. } catch (ClassNotFoundException e) {}
  150. try {
  151. if (!useCodebaseOnly && remoteCodebase != null) {
  152. return RMIClassLoader.loadClass(remoteCodebase,
  153. className);
  154. } else {
  155. return RMIClassLoader.loadClass(className);
  156. }
  157. } catch (MalformedURLException e) {
  158. className = className + ": " + e.toString();
  159. }
  160. throw new ClassNotFoundException(className);
  161. }
  162. }