src/java.rmi/share/classes/sun/rmi/transport/Target.java
author smarks
Tue, 28 Mar 2017 12:10:20 -0700
changeset 47412 194f4c32678b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8174966: Unreferenced references Reviewed-by: rriggs, skoivu, rhalade, robm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47412
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.rmi.NoSuchObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.dgc.VMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.server.Unreferenced;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
47412
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
    34
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.rmi.runtime.NewThreadAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.rmi.server.Dispatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A target contains information pertaining to a remote object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * resides in this address space.  Targets are located via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ObjectTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class Target {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /** object id for target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final ObjID id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** flag indicating whether target is subject to collection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final boolean permanent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** weak reference to remote object implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final WeakRef weakImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** dispatcher for remote object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private volatile Dispatcher disp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** stub for remote object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final Remote stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** set of clients that hold references to this target */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    57
    private final Vector<VMID> refSet = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** table that maps client endpoints to sequence numbers */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    59
    private final Hashtable<VMID, SequenceEntry> sequenceTable =
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    60
        new Hashtable<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** access control context in which target was created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** context class loader in which target was created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final ClassLoader ccl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** number of pending/executing calls */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int callCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** true if this target has been removed from the object table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean removed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the transport through which this target was exported and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * through which remote calls will be allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private volatile Transport exportedTransport = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** number to identify next callback thread created here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static int nextThreadNum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Construct a Target for a remote object "impl" with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * a specific object id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * If "permanent" is true, then the impl is pinned permanently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * (the impl will not be collected via distributed and/or local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * GC).  If "on" is false, than the impl is subject to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * collection. Permanent objects do not keep a server from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * exiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Target(Remote impl, Dispatcher disp, Remote stub, ObjID id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                  boolean permanent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.weakImpl = new WeakRef(impl, ObjectTable.reapQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.disp = disp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.stub = stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * Fix for 4149366: so that downloaded parameter types unmarshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * for this impl will be compatible with types known only to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * impl class's class loader (when it's not identical to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * exporting thread's context class loader), mark the impl's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * loader as the loader to use as the context class loader in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * server's dispatch thread while a call to this impl is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * processed (unless this exporting thread's context class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * a child of the impl's class loader, such as when a registry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * exported by an application, in which case this thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * class loader is preferred).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ClassLoader threadContextLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ClassLoader serverLoader = impl.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (checkLoaderAncestry(threadContextLoader, serverLoader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.ccl = threadContextLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            this.ccl = serverLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.permanent = permanent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (permanent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            pinImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Return true if the first class loader is a child of (or identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * to) the second class loader.  Either loader may be "null", which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * considered to be the parent of any non-null class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * (utility method added for the 1.2beta4 fix for 4149366)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static boolean checkLoaderAncestry(ClassLoader child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                               ClassLoader ancestor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (ancestor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            for (ClassLoader parent = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 parent != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                 parent = parent.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (parent == ancestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /** Get the stub (proxy) object for this target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public Remote getStub() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the object endpoint for the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    ObjectEndpoint getObjectEndpoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return new ObjectEndpoint(id, exportedTransport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Get the weak reference for the Impl of this target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    WeakRef getWeakImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return weakImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the dispatcher for this remote object target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    Dispatcher getDispatcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return disp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    AccessControlContext getAccessControlContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    ClassLoader getContextClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return ccl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Get the impl for this target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Note: this may return null if the impl has been garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * (currently, there is no need to make this method public)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    Remote getImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (Remote)weakImpl.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns true if the target is permanent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    boolean isPermanent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return permanent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Pin impl in target. Pin the WeakRef object so it holds a strong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * reference to the object to it will not be garbage collected locally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * This way there is a single object responsible for the weak ref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    synchronized void pinImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        weakImpl.pin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Unpin impl in target.  Weaken the reference to impl so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * can be garbage collected locally. But only if there the refSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * is empty.  All of the weak/strong handling is in WeakRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    synchronized void unpinImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        /* only unpin if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * a) impl is not permanent, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * b) impl is not already unpinned, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * c) there are no external references (outside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         *    address space) for the impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (!permanent && refSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            weakImpl.unpin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
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
     * Enable the transport through which remote calls to this target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * are allowed to be set if it has not already been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    void setExportedTransport(Transport exportedTransport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (this.exportedTransport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.exportedTransport = exportedTransport;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Add an endpoint to the remembered set.  Also adds a notifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * to call back if the address space associated with the endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * dies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    synchronized void referenced(long sequenceNum, VMID vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // check sequence number for vmid
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   246
        SequenceEntry entry = sequenceTable.get(vmid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            sequenceTable.put(vmid, new SequenceEntry(sequenceNum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (entry.sequenceNum < sequenceNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            entry.update(sequenceNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // late dirty call; ignore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (!refSet.contains(vmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
             * A Target must be pinned while its refSet is not empty.  It may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             * have become unpinned if external LiveRefs only existed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
             * serialized form for some period of time, or if a client failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
             * to renew its lease due to a transient network failure.  So,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
             * make sure that it is pinned here; this fixes bugid 4069644.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            pinImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (getImpl() == null)      // too late if impl was collected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                DGCImpl.dgcLog.log(Log.VERBOSE, "add to dirty set: " + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            refSet.addElement(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            DGCImpl.getDGCImpl().registerTarget(vmid, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Remove endpoint from remembered set.  If set becomes empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * remove server from Transport's object table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    synchronized void unreferenced(long sequenceNum, VMID vmid, boolean strong)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // check sequence number for vmid
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   285
        SequenceEntry entry = sequenceTable.get(vmid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (entry == null || entry.sequenceNum > sequenceNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // late clean call; ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else if (strong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // strong clean call; retain sequenceNum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            entry.retain(sequenceNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else if (entry.keep == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // get rid of sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            sequenceTable.remove(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            DGCImpl.dgcLog.log(Log.VERBOSE, "remove from dirty set: " + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        refSetRemove(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Remove endpoint from the reference set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    synchronized private void refSetRemove(VMID vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // remove notification request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        DGCImpl.getDGCImpl().unregisterTarget(vmid, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (refSet.removeElement(vmid) && refSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // reference set is empty, so server can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // remove object from table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                DGCImpl.dgcLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    "reference set is empty: target = " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             * If the remote object implements the Unreferenced interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             * invoke its unreferenced callback in a separate thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Remote obj = getImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (obj instanceof Unreferenced) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                final Unreferenced unrefObj = (Unreferenced) obj;
47412
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   326
                AccessController.doPrivileged(
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   327
                    new NewThreadAction(() -> {
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   328
                        Thread.currentThread().setContextClassLoader(ccl);
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   329
                        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   330
                            unrefObj.unreferenced();
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   331
                            return null;
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   332
                        }, acc);
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   333
                    }, "Unreferenced-" + nextThreadNum++, false, true)).start();
194f4c32678b 8174966: Unreferenced references
smarks
parents: 47216
diff changeset
   334
                    // REMIND: access to nextThreadNum not synchronized; you care?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            unpinImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Mark this target as not accepting new calls if any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * following conditions exist: a) the force parameter is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * b) the target's call count is zero, or c) the object is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * not accepting calls. Returns true if target is marked as not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * accepting new calls; returns false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    synchronized boolean unexport(boolean force) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if ((force == true) || (callCount == 0) || (disp == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            disp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
             * Fix for 4331349: unpin object so that it may be gc'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
             * Also, unregister all vmids referencing this target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
             * so target can be gc'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            unpinImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            DGCImpl dgc = DGCImpl.getDGCImpl();
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   359
            Enumeration<VMID> enum_ = refSet.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            while (enum_.hasMoreElements()) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   361
                VMID vmid = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                dgc.unregisterTarget(vmid, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Mark this target as having been removed from the object table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    synchronized void markRemoved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (!(!removed)) { throw new AssertionError(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        removed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (!permanent && callCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            ObjectTable.decrementKeepAliveCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (exportedTransport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            exportedTransport.targetUnexported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Increment call count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    synchronized void incrementCallCount() throws NoSuchObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (disp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            callCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new NoSuchObjectException("object not accepting new calls");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Decrement call count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    synchronized void decrementCallCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (--callCount < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new Error("internal error: call count less than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * The "keep-alive count" is the number of non-permanent remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * objects that are either in the object table or still have calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * in progress.  Therefore, this state change may affect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * keep-alive count: if this target is for a non-permanent remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * object that has been removed from the object table and now has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * call count of zero, it needs to be decremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (!permanent && removed && callCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            ObjectTable.decrementKeepAliveCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns true if remembered set is empty; otherwise returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return refSet.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * This method is called if the address space associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * vmid dies.  In that case, the vmid should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * from the reference set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    synchronized public void vmidDead(VMID vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (DGCImpl.dgcLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            DGCImpl.dgcLog.log(Log.BRIEF, "removing endpoint " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            vmid + " from reference set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        sequenceTable.remove(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        refSetRemove(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
class SequenceEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    long sequenceNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    boolean keep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    SequenceEntry(long sequenceNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        this.sequenceNum = sequenceNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        keep = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    void retain(long sequenceNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        this.sequenceNum = sequenceNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        keep = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    void update(long sequenceNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        this.sequenceNum = sequenceNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}