1. /*
  2. * @(#)PortableRemoteObject.java 1.28 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 javax.rmi;
  16. import java.lang.reflect.Method ;
  17. import javax.rmi.CORBA.Util;
  18. import java.rmi.RemoteException;
  19. import java.rmi.NoSuchObjectException;
  20. import java.rmi.Remote;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.util.Properties;
  24. import java.security.AccessController;
  25. import java.security.PrivilegedAction;
  26. import sun.security.action.GetPropertyAction;
  27. /**
  28. * Server implementation objects may either inherit from
  29. * javax.rmi.PortableRemoteObject or they may implement a remote interface
  30. * and then use the exportObject method to register themselves as a server object.
  31. * The toStub method takes a server implementation and returns a stub that
  32. * can be used to access that server object.
  33. * The connect method makes a Remote object ready for remote communication.
  34. * The unexportObject method is used to deregister a server object, allowing it to become
  35. * available for garbage collection.
  36. * The narrow method takes an object reference or abstract interface type and
  37. * attempts to narrow it to conform to
  38. * the given interface. If the operation is successful the result will be an
  39. * object of the specified type, otherwise an exception will be thrown.
  40. */
  41. public class PortableRemoteObject {
  42. private static javax.rmi.CORBA.PortableRemoteObjectDelegate proDelegate = null;
  43. private static final String PortableRemoteObjectClassKey =
  44. "javax.rmi.CORBA.PortableRemoteObjectClass";
  45. private static final String defaultPortableRemoteObjectImplName =
  46. "com.sun.corba.se.internal.javax.rmi.PortableRemoteObject";
  47. static {
  48. proDelegate = (javax.rmi.CORBA.PortableRemoteObjectDelegate)
  49. createDelegateIfSpecified(PortableRemoteObjectClassKey);
  50. }
  51. /**
  52. * Initializes the object by calling <code>exportObject(this)</code>.
  53. * @exception RemoteException if export fails.
  54. */
  55. protected PortableRemoteObject() throws RemoteException {
  56. if (proDelegate != null) {
  57. PortableRemoteObject.exportObject((Remote)this);
  58. }
  59. }
  60. /**
  61. * Makes a server object ready to receive remote calls. Note
  62. * that subclasses of PortableRemoteObject do not need to call this
  63. * method, as it is called by the constructor.
  64. * @param obj the server object to export.
  65. * @exception RemoteException if export fails.
  66. */
  67. public static void exportObject(Remote obj)
  68. throws RemoteException {
  69. // Let the delegate do everything, including error handling.
  70. if (proDelegate != null) {
  71. proDelegate.exportObject(obj);
  72. }
  73. }
  74. /**
  75. * Returns a stub for the given server object.
  76. * @param obj the server object for which a stub is required. Must either be a subclass
  77. * of PortableRemoteObject or have been previously the target of a call to
  78. * {@link #exportObject}.
  79. * @return the most derived stub for the object.
  80. * @exception NoSuchObjectException if a stub cannot be located for the given server object.
  81. */
  82. public static Remote toStub (Remote obj)
  83. throws NoSuchObjectException {
  84. if (proDelegate != null) {
  85. return proDelegate.toStub(obj);
  86. }
  87. return null;
  88. }
  89. /**
  90. * Deregisters a server object from the runtime, allowing the object to become
  91. * available for garbage collection.
  92. * @param obj the object to unexport.
  93. * @exception NoSuchObjectException if the remote object is not
  94. * currently exported.
  95. */
  96. public static void unexportObject(Remote obj)
  97. throws NoSuchObjectException {
  98. if (proDelegate != null) {
  99. proDelegate.unexportObject(obj);
  100. }
  101. }
  102. /**
  103. * Checks to ensure that an object of a remote or abstract interface type
  104. * can be cast to a desired type.
  105. * @param narrowFrom the object to check.
  106. * @param narrowTo the desired type.
  107. * @return an object which can be cast to the desired type.
  108. * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
  109. */
  110. public static java.lang.Object narrow ( java.lang.Object narrowFrom,
  111. java.lang.Class narrowTo)
  112. throws ClassCastException {
  113. if (proDelegate != null) {
  114. return proDelegate.narrow(narrowFrom, narrowTo);
  115. }
  116. return null;
  117. }
  118. /**
  119. * Makes a Remote object ready for remote communication. This normally
  120. * happens implicitly when the object is sent or received as an argument
  121. * on a remote method call, but in some circumstances it is useful to
  122. * perform this action by making an explicit call. See the
  123. * {@link Stub#connect} method for more information.
  124. * @param target the object to connect.
  125. * @param source a previously connected object.
  126. * @throws RemoteException if <code>source</code> is not connected
  127. * or if <code>target</code> is already connected to a different ORB than
  128. * <code>source</code>.
  129. */
  130. public static void connect (Remote target, Remote source)
  131. throws RemoteException {
  132. if (proDelegate != null) {
  133. proDelegate.connect(target, source);
  134. }
  135. }
  136. // Same code as in javax.rmi.CORBA.Util. Can not be shared because they
  137. // are in different packages and the visibility needs to be package for
  138. // security reasons. If you know a better solution how to share this code
  139. // then remove it from here.
  140. private static Object createDelegateIfSpecified(String classKey) {
  141. String className = (String)
  142. AccessController.doPrivileged(new GetPropertyAction(classKey));
  143. if (className == null) {
  144. Properties props = getORBPropertiesFile();
  145. if (props != null) {
  146. className = props.getProperty(classKey);
  147. }
  148. }
  149. if (className == null) {
  150. className = defaultPortableRemoteObjectImplName;
  151. }
  152. try {
  153. return (Object) Util.loadClass(className, null, null).newInstance();
  154. } catch (ClassNotFoundException ex) {
  155. throw new org.omg.CORBA.INITIALIZE(
  156. "cannot instantiate " + className);
  157. } catch (Exception ex) {
  158. throw new org.omg.CORBA.INITIALIZE(
  159. "cannot instantiate " + className);
  160. }
  161. }
  162. /**
  163. * Load the orb.properties file.
  164. */
  165. private static Properties getORBPropertiesFile () {
  166. return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction());
  167. }
  168. }
  169. class GetORBPropertiesFileAction implements PrivilegedAction {
  170. private boolean debug = false ;
  171. public GetORBPropertiesFileAction () {
  172. }
  173. private String getSystemProperty(final String name) {
  174. // This will not throw a SecurityException because this
  175. // class was loaded from rt.jar using the bootstrap classloader.
  176. String propValue = (String) AccessController.doPrivileged(
  177. new PrivilegedAction() {
  178. public java.lang.Object run() {
  179. return System.getProperty(name);
  180. }
  181. }
  182. );
  183. return propValue;
  184. }
  185. private void getPropertiesFromFile( Properties props, String fileName )
  186. {
  187. try {
  188. File file = new File( fileName ) ;
  189. if (!file.exists())
  190. return ;
  191. FileInputStream in = new FileInputStream( file ) ;
  192. try {
  193. props.load( in ) ;
  194. } finally {
  195. in.close() ;
  196. }
  197. } catch (Exception exc) {
  198. if (debug)
  199. System.out.println( "ORB properties file " + fileName +
  200. " not found: " + exc) ;
  201. }
  202. }
  203. public Object run()
  204. {
  205. Properties defaults = new Properties() ;
  206. String javaHome = getSystemProperty( "java.home" ) ;
  207. String fileName = javaHome + File.separator + "lib" + File.separator +
  208. "orb.properties" ;
  209. getPropertiesFromFile( defaults, fileName ) ;
  210. Properties results = new Properties( defaults ) ;
  211. String userHome = getSystemProperty( "user.home" ) ;
  212. fileName = userHome + File.separator + "orb.properties" ;
  213. getPropertiesFromFile( results, fileName ) ;
  214. return results ;
  215. }
  216. }