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

Wed, 26 Feb 2014 14:43:56 -0800

author
katleman
date
Wed, 26 Feb 2014 14:43:56 -0800
changeset 589
abe5b0157c36
parent 553
5ca1b4c282b8
child 748
6845b95cba6b
child 1294
4821afbaa2a6
permissions
-rw-r--r--

Added tag jdk8u20-b03 for changeset 9059a1c85704

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

mercurial