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