corba/src/share/classes/com/sun/corba/se/impl/javax/rmi/PortableRemoteObject.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
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) 1999, 2004, 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 com.sun.corba.se.impl.javax.rmi;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import javax.rmi.CORBA.Tie;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import javax.rmi.CORBA.Util;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import java.rmi.RemoteException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import java.rmi.NoSuchObjectException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import java.rmi.Remote;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import java.util.Properties;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import org.omg.CORBA.ORB;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import org.omg.CORBA.portable.Delegate;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import org.omg.CORBA.SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import java.rmi.server.UnicastRemoteObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import java.rmi.server.RemoteStub;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
import java.rmi.server.ExportException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import java.net.URL;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
import com.sun.corba.se.impl.util.JDKBridge;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
import com.sun.corba.se.impl.util.Utility;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
import com.sun.corba.se.impl.util.RepositoryId;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
import java.security.AccessController;
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
import com.sun.corba.se.impl.orbutil.GetPropertyAction;
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
 * Server implementation objects may either inherit from
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
 * javax.rmi.PortableRemoteObject or they may implement a remote interface
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
 * and then use the exportObject method to register themselves as a server object.
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
 * The toStub method takes a server implementation and returns a stub that
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
 * can be used to access that server object.
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
 * The connect method makes a Remote object ready for remote communication.
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
 * The unexportObject method is used to deregister a server object, allowing it to become
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
 * available for garbage collection.
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
 * The narrow method takes an object reference or abstract interface type and
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
 * attempts to narrow it to conform to
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
 * the given interface. If the operation is successful the result will be an
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
 * object of the specified type, otherwise an exception will be thrown.
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
public class PortableRemoteObject
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
        implements javax.rmi.CORBA.PortableRemoteObjectDelegate {
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
     * Makes a server object ready to receive remote calls. Note
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
     * that subclasses of PortableRemoteObject do not need to call this
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
     * method, as it is called by the constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
     * @param obj the server object to export.
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
     * @exception RemoteException if export fails.
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    public void exportObject(Remote obj)
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
        throws RemoteException {
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        if (obj == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
            throw new NullPointerException("invalid argument");
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        // Has this object already been exported to IIOP?
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        if (Util.getTie(obj) != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
            // Yes, so this is an error...
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
            throw new ExportException (obj.getClass().getName() + " already exported");
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        // Can we load a Tie?
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        Tie theTie = Utility.loadTie(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        if (theTie != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
            // Yes, so export it to IIOP...
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
            Util.registerTarget(theTie,obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
            // No, so export to JRMP. If this is called twice for the
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            // same object, it will throw an ExportException...
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
            UnicastRemoteObject.exportObject(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
     * Returns a stub for the given server object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
     * @param obj the server object for which a stub is required. Must either be a subclass
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
     * of PortableRemoteObject or have been previously the target of a call to
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
     * {@link #exportObject}.
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
     * @return the most derived stub for the object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
     * @exception NoSuchObjectException if a stub cannot be located for the given server object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
    public Remote toStub (Remote obj)
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        throws NoSuchObjectException
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        Remote result = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
        if (obj == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
            throw new NullPointerException("invalid argument");
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
        // If the class is already an IIOP stub then return it.
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        if (StubAdapter.isStub( obj )) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
            return obj;
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
        // If the class is already a JRMP stub then return it.
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
        if (obj instanceof java.rmi.server.RemoteStub) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
            return obj;
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
        // Has it been exported to IIOP?
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
        Tie theTie = Util.getTie(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
        if (theTie != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
            result = Utility.loadStub(theTie,null,null,true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
            if (Utility.loadTie(obj) == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
                result = java.rmi.server.RemoteObject.toStub(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
        if (result == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
            throw new NoSuchObjectException("object not exported");
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
     * Deregisters a server object from the runtime, allowing the object to become
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
     * available for garbage collection.
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
     * @param obj the object to unexport.
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
     * @exception NoSuchObjectException if the remote object is not
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
     * currently exported.
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
    public void unexportObject(Remote obj)
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
        throws NoSuchObjectException {
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
        if (obj == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
            throw new NullPointerException("invalid argument");
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        if (StubAdapter.isStub(obj) ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
            obj instanceof java.rmi.server.RemoteStub) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
            throw new NoSuchObjectException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
                "Can only unexport a server object.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        Tie theTie = Util.getTie(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        if (theTie != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
            Util.unexportObject(obj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
            if (Utility.loadTie(obj) == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
                UnicastRemoteObject.unexportObject(obj,true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
                throw new NoSuchObjectException("Object not exported.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
     * Checks to ensure that an object of a remote or abstract interface type
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
     * can be cast to a desired type.
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
     * @param narrowFrom the object to check.
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
     * @param narrowTo the desired type.
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
     * @return an object which can be cast to the desired type.
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
     * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
    public java.lang.Object narrow ( java.lang.Object narrowFrom,
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
        java.lang.Class narrowTo) throws ClassCastException
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
        java.lang.Object result = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        if (narrowFrom == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
            return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
        if (narrowTo == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
            throw new NullPointerException("invalid argument");
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
            if (narrowTo.isAssignableFrom(narrowFrom.getClass()))
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
                return narrowFrom;
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
            // Is narrowTo an interface that might be
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
            // implemented by a servant running on iiop?
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
            if (narrowTo.isInterface() &&
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
                narrowTo != java.io.Serializable.class &&
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
                narrowTo != java.io.Externalizable.class) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
                org.omg.CORBA.Object narrowObj
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
                    = (org.omg.CORBA.Object) narrowFrom;
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
                // Create an id from the narrowTo type...
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
                String id = RepositoryId.createForAnyType(narrowTo);
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
                if (narrowObj._is_a(id)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
                    return Utility.loadStub(narrowObj,narrowTo);
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
                    throw new ClassCastException( "Object is not of remote type " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
                        narrowTo.getName() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
                throw new ClassCastException( "Class " + narrowTo.getName() +
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
                    " is not a valid remote interface" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
        } catch(Exception error) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
            ClassCastException cce = new ClassCastException() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
            cce.initCause( error ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
            throw cce ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
     * Makes a Remote object ready for remote communication. This normally
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
     * happens implicitly when the object is sent or received as an argument
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
     * on a remote method call, but in some circumstances it is useful to
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
     * perform this action by making an explicit call.  See the
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
     * {@link Stub#connect} method for more information.
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
     * @param target the object to connect.
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
     * @param source a previously connected object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
     * @throws RemoteException if <code>source</code> is not connected
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
     * or if <code>target</code> is already connected to a different ORB than
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
     * <code>source</code>.
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
    public void connect (Remote target, Remote source)
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        throws RemoteException
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
        if (target == null || source == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
            throw new NullPointerException("invalid argument");
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
        ORB orb = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
            if (StubAdapter.isStub( source )) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
                orb = StubAdapter.getORB( source ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
                // Is this a servant that was exported to iiop?
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
                Tie tie = Util.getTie(source);
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
                if (tie == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
                    /* loadTie always succeeds for dynamic RMI-IIOP
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
                    // No, can we get a tie for it?  If not,
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
                    // assume that source is a JRMP object...
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
                    if (Utility.loadTie(source) != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
                        // Yes, so it is an iiop object which
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
                        // has not been exported...
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
                        throw new RemoteException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
                            "'source' object not exported");
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
                    */
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
                    orb = tie.orb();
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
        } catch (SystemException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
            throw new RemoteException("'source' object not connected", e );
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
        boolean targetIsIIOP = false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
        Tie targetTie = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
        if (StubAdapter.isStub(target)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
            targetIsIIOP = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
            targetTie = Util.getTie(target);
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
            if (targetTie != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
                targetIsIIOP = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
                /* loadTie always succeeds for dynamic RMI-IIOP
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
                if (Utility.loadTie(target) != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
                    throw new RemoteException("'target' servant not exported");
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
                */
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
        if (!targetIsIIOP) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
            // Yes. Do we have an ORB from the source object?
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
            // If not, we're done - there is nothing to do to
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
            // connect a JRMP object. If so, it is an error because
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
            // the caller mixed JRMP and IIOP...
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
            if (orb != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
                throw new RemoteException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
                    "'source' object exported to IIOP, 'target' is JRMP");
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
            // The target object is IIOP. Make sure we have a
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
            // valid ORB from the source object...
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
            if (orb == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
                throw new RemoteException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
                    "'source' object is JRMP, 'target' is IIOP");
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
            // And, finally, connect it up...
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
                if (targetTie != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
                    // Is the tie already connected?
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
                        ORB existingOrb = targetTie.orb();
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
                        // Yes. Is it the same orb?
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                        if (existingOrb == orb) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
                            // Yes, so nothing to do...
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
                            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
                        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
                            // No, so this is an error...
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
                            throw new RemoteException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
                                "'target' object was already connected");
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
                    } catch (SystemException e) {}
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
                    // No, so do it...
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
                    targetTie.orb(orb);
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
                    StubAdapter.connect( target, orb ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
            } catch (SystemException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
                // The stub or tie was already connected...
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
                throw new RemoteException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
                    "'target' object was already connected", e );
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
}