1. /*
  2. * @(#)Util.java 1.42 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.CORBA;
  16. import java.rmi.RemoteException;
  17. import org.omg.CORBA.ORB;
  18. import org.omg.CORBA.SystemException;
  19. import org.omg.CORBA.Any;
  20. import org.omg.CORBA.portable.InputStream;
  21. import org.omg.CORBA.portable.OutputStream;
  22. import org.omg.CORBA.portable.ObjectImpl;
  23. import javax.rmi.CORBA.Tie;
  24. import java.rmi.Remote;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.security.AccessController;
  28. import java.security.PrivilegedAction;
  29. import sun.security.action.GetPropertyAction;
  30. import java.util.Properties;
  31. import com.sun.corba.se.internal.util.JDKBridge;
  32. /**
  33. * Provides utility methods that can be used by stubs and ties to
  34. * perform common operations.
  35. */
  36. public class Util {
  37. // This can only be set at static initialization time (no sync necessary).
  38. private static javax.rmi.CORBA.UtilDelegate utilDelegate = null;
  39. private static final String UtilClassKey = "javax.rmi.CORBA.UtilClass";
  40. private static final String defaultUtilImplName =
  41. "com.sun.corba.se.internal.POA.ShutdownUtilDelegate";
  42. static {
  43. utilDelegate = (javax.rmi.CORBA.UtilDelegate)
  44. createDelegateIfSpecified(UtilClassKey, defaultUtilImplName);
  45. }
  46. private Util(){}
  47. /**
  48. * Maps a SystemException to a RemoteException.
  49. * @param ex the SystemException to map.
  50. * @return the mapped exception.
  51. */
  52. public static RemoteException mapSystemException(SystemException ex) {
  53. if (utilDelegate != null) {
  54. return utilDelegate.mapSystemException(ex);
  55. }
  56. return null;
  57. }
  58. /**
  59. * Writes any java.lang.Object as a CORBA any.
  60. * @param out the stream in which to write the any.
  61. * @param obj the object to write as an any.
  62. */
  63. public static void writeAny(OutputStream out, Object obj) {
  64. if (utilDelegate != null) {
  65. utilDelegate.writeAny(out, obj);
  66. }
  67. }
  68. /**
  69. * Reads a java.lang.Object as a CORBA any.
  70. * @param in the stream from which to read the any.
  71. * @return the object read from the stream.
  72. */
  73. public static Object readAny(InputStream in) {
  74. if (utilDelegate != null) {
  75. return utilDelegate.readAny(in);
  76. }
  77. return null;
  78. }
  79. /**
  80. * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
  81. * an exported RMI-IIOP server object, the tie is found
  82. * and wired to <code>obj</code>, then written to
  83. <code>out.write_Object(org.omg.CORBA.Object)</code>.
  84. * If <code>obj</code> is a CORBA Object, it is written to
  85. * <code>out.write_Object(org.omg.CORBA.Object)</code>.
  86. * @param out the stream in which to write the object.
  87. * @param obj the object to write.
  88. */
  89. public static void writeRemoteObject(OutputStream out,
  90. java.lang.Object obj) {
  91. if (utilDelegate != null) {
  92. utilDelegate.writeRemoteObject(out, obj);
  93. }
  94. }
  95. /**
  96. * Writes a java.lang.Object as either a value or a CORBA Object.
  97. * If <code>obj</code> is a value object or a stub object, it is written to
  98. * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code>
  99. is
  100. an exported
  101. * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
  102. * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
  103. * @param out the stream in which to write the object.
  104. * @param obj the object to write.
  105. */
  106. public static void writeAbstractObject(OutputStream out,
  107. java.lang.Object obj) {
  108. if (utilDelegate != null) {
  109. utilDelegate.writeAbstractObject(out, obj);
  110. }
  111. }
  112. /**
  113. * Registers a target for a tie. Adds the tie to an internal table and calls
  114. * {@link Tie#setTarget} on the tie object.
  115. * @param tie the tie to register.
  116. * @param target the target for the tie.
  117. */
  118. public static void registerTarget(javax.rmi.CORBA.Tie tie,
  119. java.rmi.Remote target) {
  120. if (utilDelegate != null) {
  121. utilDelegate.registerTarget(tie, target);
  122. }
  123. }
  124. /**
  125. * Removes the associated tie from an internal table and calls {@link
  126. Tie#deactivate}
  127. * to deactivate the object.
  128. * @param target the object to unexport.
  129. */
  130. public static void unexportObject(java.rmi.Remote target)
  131. throws java.rmi.NoSuchObjectException
  132. {
  133. if (utilDelegate != null) {
  134. utilDelegate.unexportObject(target);
  135. }
  136. }
  137. /**
  138. * Returns the tie (if any) for a given target object.
  139. * @return the tie or null if no tie is registered for the given target.
  140. */
  141. public static Tie getTie (Remote target) {
  142. if (utilDelegate != null) {
  143. return utilDelegate.getTie(target);
  144. }
  145. return null;
  146. }
  147. /**
  148. * Returns a singleton instance of a class that implements the
  149. * {@link ValueHandler} interface.
  150. * @return a class which implements the ValueHandler interface.
  151. */
  152. public static ValueHandler createValueHandler() {
  153. if (utilDelegate != null) {
  154. return utilDelegate.createValueHandler();
  155. }
  156. return null;
  157. }
  158. /**
  159. * Returns the codebase, if any, for the given class.
  160. * @param clz the class to get a codebase for.
  161. * @return a space-separated list of URLs, or null.
  162. */
  163. public static String getCodebase(java.lang.Class clz) {
  164. if (utilDelegate != null) {
  165. return utilDelegate.getCodebase(clz);
  166. }
  167. return null;
  168. }
  169. /**
  170. * Returns a class instance for the specified class.
  171. * <P>The spec for this method is the "Java to IDL language
  172. * mapping", ptc/00-01-06.
  173. * <P>In Java 2 Platform, this method works as follows:
  174. * <UL><LI>Find the first non-null <tt>ClassLoader</tt> on the
  175. * call stack and attempt to load the class using this
  176. * <tt>ClassLoader</tt>.
  177. * <LI>If the first step fails, and if <tt>remoteCodebase</tt>
  178. * is non-null and
  179. * <tt>useCodebaseOnly</tt> is false, then call
  180. * <tt>java.rmi.server.RMIClassLoader.loadClass(remoteCodebase, className)</tt>.
  181. * <LI>If <tt>remoteCodebase</tt> is null or <tt>useCodebaseOnly</tt>
  182. * is true, then call <tt>java.rmi.server.RMIClassLoader.loadClass(className)</tt>.
  183. * <LI>If a class was not successfully loaded by step 1, 2, or 3,
  184. * and <tt>loader</tt> is non-null, then call <tt>loader.loadClass(className)</tt>.
  185. * <LI>If a class was successfully loaded by step 1, 2, 3, or 4, then
  186. * return the loaded class, else throw <tt>ClassNotFoundException</tt>.
  187. * @param className the name of the class.
  188. * @param remoteCodebase a space-separated list of URLs at which
  189. * the class might be found. May be null.
  190. * @param loader a <tt>ClassLoader</tt> that may be used to
  191. * load the class if all other methods fail.
  192. * @return the <code>Class</code> object representing the loaded class.
  193. * @exception ClassNotFoundException if class cannot be loaded.
  194. */
  195. public static Class loadClass(String className,
  196. String remoteCodebase,
  197. ClassLoader loader)
  198. throws ClassNotFoundException {
  199. if (utilDelegate != null) {
  200. return utilDelegate.loadClass(className,remoteCodebase,loader);
  201. }
  202. return JDKBridge.loadClass(className, remoteCodebase, loader);
  203. }
  204. /**
  205. * The <tt>isLocal</tt> method has the same semantics as the
  206. * <tt>ObjectImpl._is_local</tt>
  207. * method, except that it can throw a <tt>RemoteException</tt>.
  208. *
  209. * The <tt>_is_local()</tt> method is provided so that stubs may determine if a
  210. * particular object is implemented by a local servant and hence local
  211. * invocation APIs may be used.
  212. *
  213. * @param stub the stub to test.
  214. *
  215. * @return The <tt>_is_local()</tt> method returns true if
  216. * the servant incarnating the object is located in the same process as
  217. * the stub and they both share the same ORB instance. The <tt>_is_local()</tt>
  218. * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
  219. * to return false.
  220. *
  221. * @throws RemoteException The Java to IDL specification does not
  222. * specify the conditions that cause a <tt>RemoteException</tt> to be thrown.
  223. */
  224. public static boolean isLocal(Stub stub) throws RemoteException {
  225. if (utilDelegate != null) {
  226. return utilDelegate.isLocal(stub);
  227. }
  228. return false;
  229. }
  230. /**
  231. * Wraps an exception thrown by an implementation
  232. * method. It returns the corresponding client-side exception.
  233. * @param orig the exception to wrap.
  234. * @return the wrapped exception.
  235. */
  236. public static RemoteException wrapException(Throwable orig) {
  237. if (utilDelegate != null) {
  238. return utilDelegate.wrapException(orig);
  239. }
  240. return null;
  241. }
  242. /**
  243. * Copies or connects an array of objects. Used by local stubs
  244. * to copy any number of actual parameters, preserving sharing
  245. * across parameters as necessary to support RMI semantics.
  246. * @param obj the objects to copy or connect.
  247. * @param orb the ORB.
  248. * @return the copied or connected objects.
  249. * @exception RemoteException if any object could not be copied or connected.
  250. */
  251. public static Object[] copyObjects (Object[] obj, ORB orb)
  252. throws RemoteException {
  253. if (utilDelegate != null) {
  254. return utilDelegate.copyObjects(obj, orb);
  255. }
  256. return null;
  257. }
  258. /**
  259. * Copies or connects an object. Used by local stubs to copy
  260. * an actual parameter, result object, or exception.
  261. * @param obj the object to copy.
  262. * @param orb the ORB.
  263. * @return the copy or connected object.
  264. * @exception RemoteException if the object could not be copied or connected.
  265. */
  266. public static Object copyObject (Object obj, ORB orb)
  267. throws RemoteException {
  268. if (utilDelegate != null) {
  269. return utilDelegate.copyObject(obj, orb);
  270. }
  271. return null;
  272. }
  273. // Same code as in PortableRemoteObject. Can not be shared because they
  274. // are in different packages and the visibility needs to be package for
  275. // security reasons. If you know a better solution how to share this code
  276. // then remove it from PortableRemoteObject. Also in Stub.java
  277. private static Object createDelegateIfSpecified(String classKey, String
  278. defaultClassName) {
  279. String className = (String)
  280. AccessController.doPrivileged(new GetPropertyAction(classKey));
  281. if (className == null) {
  282. Properties props = getORBPropertiesFile();
  283. if (props != null) {
  284. className = props.getProperty(classKey);
  285. }
  286. }
  287. if (className == null) {
  288. className = defaultClassName;
  289. }
  290. try {
  291. return Util.loadClass(className, null, null).newInstance();
  292. } catch (ClassNotFoundException ex) {
  293. throw new org.omg.CORBA.INITIALIZE(
  294. "cannot instantiate " + className);
  295. } catch (Exception ex) {
  296. throw new org.omg.CORBA.INITIALIZE(
  297. "cannot instantiate " + className);
  298. }
  299. }
  300. /**
  301. * Load the orb.properties file.
  302. */
  303. private static Properties getORBPropertiesFile () {
  304. return (Properties) AccessController.doPrivileged(new
  305. GetORBPropertiesFileAction());
  306. }
  307. }