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