0N/A/*
2362N/A * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.rmi.transport;
0N/A
0N/Aimport java.lang.ref.ReferenceQueue;
0N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.dgc.VMID;
0N/Aimport java.rmi.server.ExportException;
0N/Aimport java.rmi.server.ObjID;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport sun.misc.GC;
0N/Aimport sun.rmi.runtime.Log;
0N/Aimport sun.rmi.runtime.NewThreadAction;
0N/Aimport sun.security.action.GetLongAction;
0N/A
0N/A/**
0N/A * Object table shared by all implementors of the Transport interface.
0N/A * This table maps object ids to remote object targets in this address
0N/A * space.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Peter Jones
0N/A */
0N/Apublic final class ObjectTable {
0N/A
0N/A /** maximum interval between complete garbage collections of local heap */
0N/A private final static long gcInterval = // default 1 hour
0N/A AccessController.doPrivileged(
0N/A new GetLongAction("sun.rmi.dgc.server.gcInterval", 3600000));
0N/A
0N/A /**
0N/A * lock guarding objTable and implTable.
0N/A * Holders MAY acquire a Target instance's lock or keepAliveLock.
0N/A */
0N/A private static final Object tableLock = new Object();
0N/A
0N/A /** tables mapping to Target, keyed from ObjectEndpoint and impl object */
0N/A private static final Map<ObjectEndpoint,Target> objTable =
5559N/A new HashMap<>();
0N/A private static final Map<WeakRef,Target> implTable =
5559N/A new HashMap<>();
0N/A
0N/A /**
0N/A * lock guarding keepAliveCount, reaper, and gcLatencyRequest.
0N/A * Holders may NOT acquire a Target instance's lock or tableLock.
0N/A */
0N/A private static final Object keepAliveLock = new Object();
0N/A
0N/A /** count of non-permanent objects in table or still processing calls */
0N/A private static int keepAliveCount = 0;
0N/A
0N/A /** thread to collect unreferenced objects from table */
0N/A private static Thread reaper = null;
0N/A
0N/A /** queue notified when weak refs in the table are cleared */
5559N/A static final ReferenceQueue<Object> reapQueue = new ReferenceQueue<>();
0N/A
0N/A /** handle for GC latency request (for future cancellation) */
0N/A private static GC.LatencyRequest gcLatencyRequest = null;
0N/A
0N/A /*
0N/A * Disallow anyone from creating one of these.
0N/A */
0N/A private ObjectTable() {}
0N/A
0N/A /**
0N/A * Returns the target associated with the object id.
0N/A */
0N/A static Target getTarget(ObjectEndpoint oe) {
0N/A synchronized (tableLock) {
0N/A return objTable.get(oe);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the target associated with the remote object
0N/A */
0N/A public static Target getTarget(Remote impl) {
0N/A synchronized (tableLock) {
0N/A return implTable.get(new WeakRef(impl));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the stub for the remote object <b>obj</b> passed
0N/A * as a parameter. This operation is only valid <i>after</i>
0N/A * the object has been exported.
0N/A *
0N/A * @return the stub for the remote object, <b>obj</b>.
0N/A * @exception NoSuchObjectException if the stub for the
0N/A * remote object could not be found.
0N/A */
0N/A public static Remote getStub(Remote impl)
0N/A throws NoSuchObjectException
0N/A {
0N/A Target target = getTarget(impl);
0N/A if (target == null) {
0N/A throw new NoSuchObjectException("object not exported");
0N/A } else {
0N/A return target.getStub();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Remove the remote object, obj, from the RMI runtime. If
0N/A * successful, the object can no longer accept incoming RMI calls.
0N/A * If the force parameter is true, the object is forcibly unexported
0N/A * even if there are pending calls to the remote object or the
0N/A * remote object still has calls in progress. If the force
0N/A * parameter is false, the object is only unexported if there are
0N/A * no pending or in progress calls to the object.
0N/A *
0N/A * @param obj the remote object to be unexported
0N/A * @param force if true, unexports the object even if there are
0N/A * pending or in-progress calls; if false, only unexports the object
0N/A * if there are no pending or in-progress calls
0N/A * @return true if operation is successful, false otherwise
0N/A * @exception NoSuchObjectException if the remote object is not
0N/A * currently exported
0N/A */
0N/A public static boolean unexportObject(Remote obj, boolean force)
0N/A throws java.rmi.NoSuchObjectException
0N/A {
0N/A synchronized (tableLock) {
0N/A Target target = getTarget(obj);
0N/A if (target == null) {
0N/A throw new NoSuchObjectException("object not exported");
0N/A } else {
0N/A if (target.unexport(force)) {
0N/A removeTarget(target);
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add target to object table. If it is not a permanent entry, then
0N/A * make sure that reaper thread is running to remove collected entries
0N/A * and keep VM alive.
0N/A */
0N/A static void putTarget(Target target) throws ExportException {
0N/A ObjectEndpoint oe = target.getObjectEndpoint();
0N/A WeakRef weakImpl = target.getWeakImpl();
0N/A
0N/A if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
0N/A DGCImpl.dgcLog.log(Log.VERBOSE, "add object " + oe);
0N/A }
0N/A
4005N/A synchronized (tableLock) {
4005N/A /**
4005N/A * Do nothing if impl has already been collected (see 6597112). Check while
4005N/A * holding tableLock to ensure that Reaper cannot process weakImpl in between
4005N/A * null check and put/increment effects.
4005N/A */
4005N/A if (target.getImpl() != null) {
4005N/A if (objTable.containsKey(oe)) {
4005N/A throw new ExportException(
4005N/A "internal error: ObjID already in use");
4005N/A } else if (implTable.containsKey(weakImpl)) {
4005N/A throw new ExportException("object already exported");
4005N/A }
0N/A
4005N/A objTable.put(oe, target);
4005N/A implTable.put(weakImpl, target);
0N/A
4005N/A if (!target.isPermanent()) {
4005N/A incrementKeepAliveCount();
4005N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Remove target from object table.
0N/A *
0N/A * NOTE: This method must only be invoked while synchronized on
0N/A * the "tableLock" object, because it does not do so itself.
0N/A */
0N/A private static void removeTarget(Target target) {
0N/A // assert Thread.holdsLock(tableLock);
0N/A
0N/A ObjectEndpoint oe = target.getObjectEndpoint();
0N/A WeakRef weakImpl = target.getWeakImpl();
0N/A
0N/A if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
0N/A DGCImpl.dgcLog.log(Log.VERBOSE, "remove object " + oe);
0N/A }
0N/A
0N/A objTable.remove(oe);
0N/A implTable.remove(weakImpl);
0N/A
0N/A target.markRemoved(); // handles decrementing keep-alive count
0N/A }
0N/A
0N/A /**
0N/A * Process client VM signalling reference for given ObjID: forward to
0N/A * correspoding Target entry. If ObjID is not found in table,
0N/A * no action is taken.
0N/A */
0N/A static void referenced(ObjID id, long sequenceNum, VMID vmid) {
0N/A synchronized (tableLock) {
0N/A ObjectEndpoint oe =
0N/A new ObjectEndpoint(id, Transport.currentTransport());
0N/A Target target = objTable.get(oe);
0N/A if (target != null) {
0N/A target.referenced(sequenceNum, vmid);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Process client VM dropping reference for given ObjID: forward to
0N/A * correspoding Target entry. If ObjID is not found in table,
0N/A * no action is taken.
0N/A */
0N/A static void unreferenced(ObjID id, long sequenceNum, VMID vmid,
0N/A boolean strong)
0N/A {
0N/A synchronized (tableLock) {
0N/A ObjectEndpoint oe =
0N/A new ObjectEndpoint(id, Transport.currentTransport());
0N/A Target target = objTable.get(oe);
0N/A if (target != null)
0N/A target.unreferenced(sequenceNum, vmid, strong);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Increments the "keep-alive count".
0N/A *
0N/A * The "keep-alive count" is the number of non-permanent remote objects
0N/A * that are either in the object table or still have calls in progress.
0N/A * Therefore, this method should be invoked exactly once for every
0N/A * non-permanent remote object exported (a remote object must be
0N/A * exported before it can have any calls in progress).
0N/A *
0N/A * The VM is "kept alive" while the keep-alive count is greater than
0N/A * zero; this is accomplished by keeping a non-daemon thread running.
0N/A *
0N/A * Because non-permanent objects are those that can be garbage
0N/A * collected while exported, and thus those for which the "reaper"
0N/A * thread operates, the reaper thread also serves as the non-daemon
0N/A * VM keep-alive thread; a new reaper thread is created if necessary.
0N/A */
0N/A static void incrementKeepAliveCount() {
0N/A synchronized (keepAliveLock) {
0N/A keepAliveCount++;
0N/A
0N/A if (reaper == null) {
0N/A reaper = AccessController.doPrivileged(
0N/A new NewThreadAction(new Reaper(), "Reaper", false));
0N/A reaper.start();
0N/A }
0N/A
0N/A /*
0N/A * While there are non-"permanent" objects in the object table,
0N/A * request a maximum latency for inspecting the entire heap
0N/A * from the local garbage collector, to place an upper bound
0N/A * on the time to discover remote objects that have become
0N/A * unreachable (and thus can be removed from the table).
0N/A */
0N/A if (gcLatencyRequest == null) {
0N/A gcLatencyRequest = GC.requestLatency(gcInterval);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Decrements the "keep-alive count".
0N/A *
0N/A * The "keep-alive count" is the number of non-permanent remote objects
0N/A * that are either in the object table or still have calls in progress.
0N/A * Therefore, this method should be invoked exactly once for every
0N/A * previously-exported non-permanent remote object that both has been
0N/A * removed from the object table and has no calls still in progress.
0N/A *
0N/A * If the keep-alive count is decremented to zero, then the current
0N/A * reaper thread is terminated to cease keeping the VM alive (and
0N/A * because there are no more non-permanent remote objects to reap).
0N/A */
0N/A static void decrementKeepAliveCount() {
0N/A synchronized (keepAliveLock) {
0N/A keepAliveCount--;
0N/A
0N/A if (keepAliveCount == 0) {
0N/A if (!(reaper != null)) { throw new AssertionError(); }
0N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A reaper.interrupt();
0N/A return null;
0N/A }
0N/A });
0N/A reaper = null;
0N/A
0N/A /*
0N/A * If there are no longer any non-permanent objects in the
0N/A * object table, we are no longer concerned with the latency
0N/A * of local garbage collection here.
0N/A */
0N/A gcLatencyRequest.cancel();
0N/A gcLatencyRequest = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The Reaper thread waits for notifications that weak references in the
0N/A * object table have been cleared. When it receives a notification, it
0N/A * removes the corresponding entry from the table.
0N/A *
0N/A * Since the Reaper is created as a non-daemon thread, it also serves
0N/A * to keep the VM from exiting while there are objects in the table
0N/A * (other than permanent entries that should neither be reaped nor
0N/A * keep the VM alive).
0N/A */
0N/A private static class Reaper implements Runnable {
0N/A
0N/A public void run() {
0N/A try {
0N/A do {
0N/A // wait for next cleared weak reference
0N/A WeakRef weakImpl = (WeakRef) reapQueue.remove();
0N/A
0N/A synchronized (tableLock) {
0N/A Target target = implTable.get(weakImpl);
0N/A if (target != null) {
0N/A if (!target.isEmpty()) {
0N/A throw new Error(
0N/A "object with known references collected");
0N/A } else if (target.isPermanent()) {
0N/A throw new Error("permanent object collected");
0N/A }
0N/A removeTarget(target);
0N/A }
0N/A }
0N/A } while (!Thread.interrupted());
0N/A } catch (InterruptedException e) {
0N/A // pass away if interrupted
0N/A }
0N/A }
0N/A }
0N/A}