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