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

Thu, 24 May 2018 16:41:12 +0800

author
aoqi
date
Thu, 24 May 2018 16:41:12 +0800
changeset 1410
9c913ea7e4a1
parent 1377
ec7b9aafd7e1
parent 748
6845b95cba6b
permissions
-rw-r--r--

Merge

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

mercurial