src/java.rmi/share/classes/sun/rmi/server/ActivatableRef.java
author rehn
Fri, 29 Nov 2019 12:09:25 +0100
changeset 59325 3636bab5e81e
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8234086: VM operation can be simplified Reviewed-by: kbarrett, dholmes, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.activation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.server.Operation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.server.RMIClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.RemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.server.RemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.server.RemoteObjectInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.server.RemoteRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.server.RemoteStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    44
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class ActivatableRef implements RemoteRef {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final long serialVersionUID = 7579060052569229166L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected ActivationID id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected RemoteRef ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    transient boolean force = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int MAX_RETRIES = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String versionComplaint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "activation requires 1.2 stubs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Create a new (empty) ActivatableRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public ActivatableRef()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Create a ActivatableRef with the specified id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public ActivatableRef(ActivationID id, RemoteRef ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.ref = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns the stub for the remote object whose class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * specified in the activation descriptor. The ActivatableRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * in the resulting stub has its activation id set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * activation id supplied as the second argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static Remote getStub(ActivationDesc desc, ActivationID id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throws StubNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String className = desc.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    84
            Class<?> cl =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                RMIClassLoader.loadClass(desc.getLocation(), className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            RemoteRef clientRef = new ActivatableRef(id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return Util.createProxy(cl, clientRef, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new StubNotFoundException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "class implements an illegal remote interface", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new StubNotFoundException("unable to load class: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                            className, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new StubNotFoundException("malformed URL", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Invoke method on remote object. This method delegates remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * method invocation to the underlying ref type.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * underlying reference is not known (is null), then the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * must be activated first.  If an attempt at method invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * fails, the object should force reactivation.  Method invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * must preserve "at most once" call semantics.  In RMI, "at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * once" applies to parameter deserialization at the remote site
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * and the remote object's method execution.  "At most once" does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * not apply to parameter serialization at the client so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * parameters of a call don't need to be buffered in anticipation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * of call retry. Thus, a method call is only be retried if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * initial method invocation does not execute at all at the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * (including parameter deserialization).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Object invoke(Remote obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                         java.lang.reflect.Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                         Object[] params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                         long opnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        boolean force = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        RemoteRef localRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Exception exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * Attempt object activation if active ref is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Throws a RemoteException if object can't be activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                localRef = activate(force);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                force = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                localRef = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int retries = MAX_RETRIES; retries > 0; retries--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return localRef.invoke(obj, method, params, opnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } catch (NoSuchObjectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                 * Object is not active in VM; retry call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                 * Failure during connection setup; retry call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                 * Failure during connection setup; retry call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (ConnectIOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                /*
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 25859
diff changeset
   161
                 * Failure reusing cached connection; retry call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } catch (MarshalException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 * Failure during parameter serialization; call may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 * have reached server, so call retry not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } catch (ServerError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                 * Call reached server; propagate remote exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } catch (ServerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                 * Call reached server; propagate remote exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 * This is a catch-all for other RemoteExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 * UnmarshalException being the only one relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                 * StubNotFoundException should never show up because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                 * it is generally thrown when attempting to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                 * a stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                 * UnexpectedException should never show up because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 * it is only thrown by a stub and would be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                 * in a ServerException if it was propagated by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                 * remote call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    if (localRef == ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        ref = null;     // this may be overly conservative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (retries > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                 * Activate object, since object could not be reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (localRef.remoteEquals(ref) || ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        RemoteRef newRef = activate(force);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        if (newRef.remoteEquals(localRef) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            exception instanceof NoSuchObjectException &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            force == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                             * If last exception was NoSuchObjectException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                             * then old value of ref is definitely wrong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                             * so make sure that it is different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            newRef = activate(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        localRef = newRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        force = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        localRef = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        force = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * Retries unsuccessful, so throw last exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * private method to obtain the ref for a call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private synchronized RemoteRef getRef()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            ref = activate(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * private method to activate the remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * NOTE: the caller must be synchronized on "this" before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private RemoteRef activate(boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        ref = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
             * Activate the object and retrieve the remote reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
             * from inside the stub returned as the result. Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             * set this activatable ref's internal ref to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
             * ref inside the ref of the stub. In more clear terms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
             * the stub returned from the activate call contains an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
             * ActivatableRef. We need to set the ref in *this*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
             * ActivatableRef to the ref inside the ActivatableRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
             * retrieved from the stub. The ref type embedded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             * ActivatableRef is typically a UnicastRef.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            Remote proxy = id.activate(force);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            ActivatableRef newRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (proxy instanceof RemoteStub) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                newRef = (ActivatableRef) ((RemoteStub) proxy).getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                 * Assume that proxy is an instance of a dynamic proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                 * class.  If that assumption is not correct, or either of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                 * the casts below fails, the resulting exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                 * wrapped in an ActivateFailedException below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                RemoteObjectInvocationHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    (RemoteObjectInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    Proxy.getInvocationHandler(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                newRef = (ActivatableRef) handler.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            ref = newRef.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } catch (ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new ConnectException("activation failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new ConnectIOException("activation failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (UnknownObjectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new NoSuchObjectException("object not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (ActivationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new ActivateFailedException("activation failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * This call is used by the old 1.1 stub protocol and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * unsupported since activation requires 1.2 stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public synchronized RemoteCall newCall(RemoteObject obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                           Operation[] ops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                           int opnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                           long hash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        throw new UnsupportedOperationException(versionComplaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * This call is used by the old 1.1 stub protocol and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * unsupported since activation requires 1.2 stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void invoke(RemoteCall call) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        throw new UnsupportedOperationException(versionComplaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * This call is used by the old 1.1 stub protocol and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * unsupported since activation requires 1.2 stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void done(RemoteCall call) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        throw new UnsupportedOperationException(versionComplaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Returns the class of the ref type to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public String getRefClass(ObjectOutput out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return "ActivatableRef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Write out external representation for remote ref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public void writeExternal(ObjectOutput out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        RemoteRef localRef = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        out.writeObject(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (localRef == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            out.writeUTF("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            out.writeUTF(localRef.getRefClass(out));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            localRef.writeExternal(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Read in external representation for remote ref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception ClassNotFoundException If the class for an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * being restored cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        id = (ActivationID)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        ref = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        String className = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   373
        if (className.isEmpty()) return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   376
            Class<?> refClass = Class.forName(RemoteRef.packagePrefix + "." +
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   377
                                              className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            ref = (RemoteRef)refClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            ref.readExternal(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new UnmarshalException("Unable to create remote reference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                         e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new UnmarshalException("Illegal access creating remote reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    //----------------------------------------------------------------------;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Method from object, forward from RemoteObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public String remoteToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return Util.getUnqualifiedName(getClass()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                " [remoteRef: " + ref + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * default implementation of hashCode for remote objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public int remoteHashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return id.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /** default implementation of equals for remote objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public boolean remoteEquals(RemoteRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (ref instanceof ActivatableRef)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return id.equals(((ActivatableRef)ref).id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}