src/share/classes/javax/rmi/CORBA/Util.java

Thu, 17 Jun 2010 16:27:56 -0700

author
mikejwre
date
Thu, 17 Jun 2010 16:27:56 -0700
changeset 171
95db968660e7
parent 158
91006f157c46
child 533
52ad44f9a3ec
permissions
-rw-r--r--

Added tag jdk7-b98 for changeset 3b99409057e4

duke@1 1 /*
ohair@158 2 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25 /*
duke@1 26 * Licensed Materials - Property of IBM
duke@1 27 * RMI-IIOP v1.0
duke@1 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 29 *
duke@1 30 */
duke@1 31
duke@1 32 package javax.rmi.CORBA;
duke@1 33
duke@1 34 import java.rmi.RemoteException;
duke@1 35
duke@1 36 import org.omg.CORBA.ORB;
duke@1 37 import org.omg.CORBA.INITIALIZE;
duke@1 38 import org.omg.CORBA.SystemException;
duke@1 39 import org.omg.CORBA.Any;
duke@1 40 import org.omg.CORBA.portable.InputStream;
duke@1 41 import org.omg.CORBA.portable.OutputStream;
duke@1 42 import org.omg.CORBA.portable.ObjectImpl;
duke@1 43
duke@1 44 import javax.rmi.CORBA.Tie;
duke@1 45 import java.rmi.Remote;
duke@1 46 import java.io.File;
duke@1 47 import java.io.FileInputStream;
duke@1 48 import java.net.MalformedURLException ;
duke@1 49 import java.security.AccessController;
duke@1 50 import java.security.PrivilegedAction;
duke@1 51 import java.util.Properties;
duke@1 52 import java.rmi.server.RMIClassLoader;
duke@1 53
duke@1 54 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
duke@1 55
duke@1 56 /**
duke@1 57 * Provides utility methods that can be used by stubs and ties to
duke@1 58 * perform common operations.
duke@1 59 */
duke@1 60 public class Util {
duke@1 61
duke@1 62 // This can only be set at static initialization time (no sync necessary).
duke@1 63 private static javax.rmi.CORBA.UtilDelegate utilDelegate = null;
duke@1 64 private static final String UtilClassKey = "javax.rmi.CORBA.UtilClass";
duke@1 65 private static final String defaultUtilImplName =
duke@1 66 "com.sun.corba.se.impl.javax.rmi.CORBA.Util";
duke@1 67
duke@1 68 static {
duke@1 69 utilDelegate = (javax.rmi.CORBA.UtilDelegate)
duke@1 70 createDelegateIfSpecified(UtilClassKey, defaultUtilImplName);
duke@1 71 }
duke@1 72
duke@1 73 private Util(){}
duke@1 74
duke@1 75 /**
duke@1 76 * Maps a SystemException to a RemoteException.
duke@1 77 * @param ex the SystemException to map.
duke@1 78 * @return the mapped exception.
duke@1 79 */
duke@1 80 public static RemoteException mapSystemException(SystemException ex) {
duke@1 81
duke@1 82 if (utilDelegate != null) {
duke@1 83 return utilDelegate.mapSystemException(ex);
duke@1 84 }
duke@1 85 return null;
duke@1 86 }
duke@1 87
duke@1 88 /**
duke@1 89 * Writes any java.lang.Object as a CORBA any.
duke@1 90 * @param out the stream in which to write the any.
duke@1 91 * @param obj the object to write as an any.
duke@1 92 */
duke@1 93 public static void writeAny(OutputStream out, Object obj) {
duke@1 94
duke@1 95 if (utilDelegate != null) {
duke@1 96 utilDelegate.writeAny(out, obj);
duke@1 97 }
duke@1 98 }
duke@1 99
duke@1 100 /**
duke@1 101 * Reads a java.lang.Object as a CORBA any.
duke@1 102 * @param in the stream from which to read the any.
duke@1 103 * @return the object read from the stream.
duke@1 104 */
duke@1 105 public static Object readAny(InputStream in) {
duke@1 106
duke@1 107 if (utilDelegate != null) {
duke@1 108 return utilDelegate.readAny(in);
duke@1 109 }
duke@1 110 return null;
duke@1 111 }
duke@1 112
duke@1 113 /**
duke@1 114 * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
duke@1 115 * an exported RMI-IIOP server object, the tie is found
duke@1 116 * and wired to <code>obj</code>, then written to
duke@1 117 <code>out.write_Object(org.omg.CORBA.Object)</code>.
duke@1 118 * If <code>obj</code> is a CORBA Object, it is written to
duke@1 119 * <code>out.write_Object(org.omg.CORBA.Object)</code>.
duke@1 120 * @param out the stream in which to write the object.
duke@1 121 * @param obj the object to write.
duke@1 122 */
duke@1 123 public static void writeRemoteObject(OutputStream out,
duke@1 124 java.lang.Object obj) {
duke@1 125
duke@1 126 if (utilDelegate != null) {
duke@1 127 utilDelegate.writeRemoteObject(out, obj);
duke@1 128 }
duke@1 129
duke@1 130 }
duke@1 131
duke@1 132 /**
duke@1 133 * Writes a java.lang.Object as either a value or a CORBA Object.
duke@1 134 * If <code>obj</code> is a value object or a stub object, it is written to
duke@1 135 * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code>
duke@1 136 is
duke@1 137 an exported
duke@1 138 * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
duke@1 139 * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
duke@1 140 * @param out the stream in which to write the object.
duke@1 141 * @param obj the object to write.
duke@1 142 */
duke@1 143 public static void writeAbstractObject(OutputStream out,
duke@1 144 java.lang.Object obj) {
duke@1 145
duke@1 146 if (utilDelegate != null) {
duke@1 147 utilDelegate.writeAbstractObject(out, obj);
duke@1 148 }
duke@1 149 }
duke@1 150
duke@1 151 /**
duke@1 152 * Registers a target for a tie. Adds the tie to an internal table and calls
duke@1 153 * {@link Tie#setTarget} on the tie object.
duke@1 154 * @param tie the tie to register.
duke@1 155 * @param target the target for the tie.
duke@1 156 */
duke@1 157 public static void registerTarget(javax.rmi.CORBA.Tie tie,
duke@1 158 java.rmi.Remote target) {
duke@1 159
duke@1 160 if (utilDelegate != null) {
duke@1 161 utilDelegate.registerTarget(tie, target);
duke@1 162 }
duke@1 163
duke@1 164 }
duke@1 165
duke@1 166 /**
duke@1 167 * Removes the associated tie from an internal table and calls {@link
duke@1 168 Tie#deactivate}
duke@1 169 * to deactivate the object.
duke@1 170 * @param target the object to unexport.
duke@1 171 */
duke@1 172 public static void unexportObject(java.rmi.Remote target)
duke@1 173 throws java.rmi.NoSuchObjectException
duke@1 174 {
duke@1 175
duke@1 176 if (utilDelegate != null) {
duke@1 177 utilDelegate.unexportObject(target);
duke@1 178 }
duke@1 179
duke@1 180 }
duke@1 181
duke@1 182 /**
duke@1 183 * Returns the tie (if any) for a given target object.
duke@1 184 * @return the tie or null if no tie is registered for the given target.
duke@1 185 */
duke@1 186 public static Tie getTie (Remote target) {
duke@1 187
duke@1 188 if (utilDelegate != null) {
duke@1 189 return utilDelegate.getTie(target);
duke@1 190 }
duke@1 191 return null;
duke@1 192 }
duke@1 193
duke@1 194
duke@1 195 /**
duke@1 196 * Returns a singleton instance of a class that implements the
duke@1 197 * {@link ValueHandler} interface.
duke@1 198 * @return a class which implements the ValueHandler interface.
duke@1 199 */
duke@1 200 public static ValueHandler createValueHandler() {
duke@1 201
duke@1 202 if (utilDelegate != null) {
duke@1 203 return utilDelegate.createValueHandler();
duke@1 204 }
duke@1 205 return null;
duke@1 206 }
duke@1 207
duke@1 208 /**
duke@1 209 * Returns the codebase, if any, for the given class.
duke@1 210 * @param clz the class to get a codebase for.
duke@1 211 * @return a space-separated list of URLs, or null.
duke@1 212 */
duke@1 213 public static String getCodebase(java.lang.Class clz) {
duke@1 214 if (utilDelegate != null) {
duke@1 215 return utilDelegate.getCodebase(clz);
duke@1 216 }
duke@1 217 return null;
duke@1 218 }
duke@1 219
duke@1 220 /**
duke@1 221 * Returns a class instance for the specified class.
duke@1 222 * <P>The spec for this method is the "Java to IDL language
duke@1 223 * mapping", ptc/00-01-06.
duke@1 224 * <P>In Java SE Platform, this method works as follows:
duke@1 225 * <UL><LI>Find the first non-null <tt>ClassLoader</tt> on the
duke@1 226 * call stack and attempt to load the class using this
duke@1 227 * <tt>ClassLoader</tt>.
duke@1 228 * <LI>If the first step fails, and if <tt>remoteCodebase</tt>
duke@1 229 * is non-null and
duke@1 230 * <tt>useCodebaseOnly</tt> is false, then call
duke@1 231 * <tt>java.rmi.server.RMIClassLoader.loadClass(remoteCodebase, className)</tt>.
duke@1 232 * <LI>If <tt>remoteCodebase</tt> is null or <tt>useCodebaseOnly</tt>
duke@1 233 * is true, then call <tt>java.rmi.server.RMIClassLoader.loadClass(className)</tt>.
duke@1 234 * <LI>If a class was not successfully loaded by step 1, 2, or 3,
duke@1 235 * and <tt>loader</tt> is non-null, then call <tt>loader.loadClass(className)</tt>.
duke@1 236 * <LI>If a class was successfully loaded by step 1, 2, 3, or 4, then
duke@1 237 * return the loaded class, else throw <tt>ClassNotFoundException</tt>.
duke@1 238 * @param className the name of the class.
duke@1 239 * @param remoteCodebase a space-separated list of URLs at which
duke@1 240 * the class might be found. May be null.
duke@1 241 * @param loader a <tt>ClassLoader</tt> that may be used to
duke@1 242 * load the class if all other methods fail.
duke@1 243 * @return the <code>Class</code> object representing the loaded class.
duke@1 244 * @exception ClassNotFoundException if class cannot be loaded.
duke@1 245 */
duke@1 246 public static Class loadClass(String className,
duke@1 247 String remoteCodebase,
duke@1 248 ClassLoader loader)
duke@1 249 throws ClassNotFoundException {
duke@1 250 if (utilDelegate != null) {
duke@1 251 return utilDelegate.loadClass(className,remoteCodebase,loader);
duke@1 252 }
duke@1 253 return null ;
duke@1 254 }
duke@1 255
duke@1 256
duke@1 257 /**
duke@1 258 * The <tt>isLocal</tt> method has the same semantics as the
duke@1 259 * <tt>ObjectImpl._is_local</tt>
duke@1 260 * method, except that it can throw a <tt>RemoteException</tt>.
duke@1 261 *
duke@1 262 * The <tt>_is_local()</tt> method is provided so that stubs may determine if a
duke@1 263 * particular object is implemented by a local servant and hence local
duke@1 264 * invocation APIs may be used.
duke@1 265 *
duke@1 266 * @param stub the stub to test.
duke@1 267 *
duke@1 268 * @return The <tt>_is_local()</tt> method returns true if
duke@1 269 * the servant incarnating the object is located in the same process as
duke@1 270 * the stub and they both share the same ORB instance. The <tt>_is_local()</tt>
duke@1 271 * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
duke@1 272 * to return false.
duke@1 273 *
duke@1 274 * @throws RemoteException The Java to IDL specification does not
duke@1 275 * specify the conditions that cause a <tt>RemoteException</tt> to be thrown.
duke@1 276 */
duke@1 277 public static boolean isLocal(Stub stub) throws RemoteException {
duke@1 278
duke@1 279 if (utilDelegate != null) {
duke@1 280 return utilDelegate.isLocal(stub);
duke@1 281 }
duke@1 282
duke@1 283 return false;
duke@1 284 }
duke@1 285
duke@1 286 /**
duke@1 287 * Wraps an exception thrown by an implementation
duke@1 288 * method. It returns the corresponding client-side exception.
duke@1 289 * @param orig the exception to wrap.
duke@1 290 * @return the wrapped exception.
duke@1 291 */
duke@1 292 public static RemoteException wrapException(Throwable orig) {
duke@1 293
duke@1 294 if (utilDelegate != null) {
duke@1 295 return utilDelegate.wrapException(orig);
duke@1 296 }
duke@1 297
duke@1 298 return null;
duke@1 299 }
duke@1 300
duke@1 301 /**
duke@1 302 * Copies or connects an array of objects. Used by local stubs
duke@1 303 * to copy any number of actual parameters, preserving sharing
duke@1 304 * across parameters as necessary to support RMI semantics.
duke@1 305 * @param obj the objects to copy or connect.
duke@1 306 * @param orb the ORB.
duke@1 307 * @return the copied or connected objects.
duke@1 308 * @exception RemoteException if any object could not be copied or connected.
duke@1 309 */
duke@1 310 public static Object[] copyObjects (Object[] obj, ORB orb)
duke@1 311 throws RemoteException {
duke@1 312
duke@1 313 if (utilDelegate != null) {
duke@1 314 return utilDelegate.copyObjects(obj, orb);
duke@1 315 }
duke@1 316
duke@1 317 return null;
duke@1 318 }
duke@1 319
duke@1 320 /**
duke@1 321 * Copies or connects an object. Used by local stubs to copy
duke@1 322 * an actual parameter, result object, or exception.
duke@1 323 * @param obj the object to copy.
duke@1 324 * @param orb the ORB.
duke@1 325 * @return the copy or connected object.
duke@1 326 * @exception RemoteException if the object could not be copied or connected.
duke@1 327 */
duke@1 328 public static Object copyObject (Object obj, ORB orb)
duke@1 329 throws RemoteException {
duke@1 330
duke@1 331 if (utilDelegate != null) {
duke@1 332 return utilDelegate.copyObject(obj, orb);
duke@1 333 }
duke@1 334 return null;
duke@1 335 }
duke@1 336
duke@1 337 // Same code as in PortableRemoteObject. Can not be shared because they
duke@1 338 // are in different packages and the visibility needs to be package for
duke@1 339 // security reasons. If you know a better solution how to share this code
duke@1 340 // then remove it from PortableRemoteObject. Also in Stub.java
duke@1 341 private static Object createDelegateIfSpecified(String classKey,
duke@1 342 String defaultClassName)
duke@1 343 {
duke@1 344 String className = (String)
duke@1 345 AccessController.doPrivileged(new GetPropertyAction(classKey));
duke@1 346 if (className == null) {
duke@1 347 Properties props = getORBPropertiesFile();
duke@1 348 if (props != null) {
duke@1 349 className = props.getProperty(classKey);
duke@1 350 }
duke@1 351 }
duke@1 352
duke@1 353 if (className == null) {
duke@1 354 className = defaultClassName;
duke@1 355 }
duke@1 356
duke@1 357 try {
duke@1 358 return loadDelegateClass(className).newInstance();
duke@1 359 } catch (ClassNotFoundException ex) {
duke@1 360 INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
duke@1 361 exc.initCause( ex ) ;
duke@1 362 throw exc ;
duke@1 363 } catch (Exception ex) {
duke@1 364 INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
duke@1 365 exc.initCause( ex ) ;
duke@1 366 throw exc ;
duke@1 367 }
duke@1 368 }
duke@1 369
duke@1 370 private static Class loadDelegateClass( String className ) throws ClassNotFoundException
duke@1 371 {
duke@1 372 try {
duke@1 373 ClassLoader loader = Thread.currentThread().getContextClassLoader();
duke@1 374 return Class.forName(className, false, loader);
duke@1 375 } catch (ClassNotFoundException e) {
duke@1 376 // ignore, then try RMIClassLoader
duke@1 377 }
duke@1 378
duke@1 379 try {
duke@1 380 return RMIClassLoader.loadClass(className);
duke@1 381 } catch (MalformedURLException e) {
duke@1 382 String msg = "Could not load " + className + ": " + e.toString();
duke@1 383 ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
duke@1 384 throw exc ;
duke@1 385 }
duke@1 386 }
duke@1 387 /**
duke@1 388 * Load the orb.properties file.
duke@1 389 */
duke@1 390 private static Properties getORBPropertiesFile ()
duke@1 391 {
duke@1 392 return (Properties) AccessController.doPrivileged(
duke@1 393 new GetORBPropertiesFileAction());
duke@1 394 }
duke@1 395
duke@1 396 }

mercurial