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/A
0N/Apackage sun.rmi.transport;
0N/A
0N/Aimport java.rmi.server.UID;
0N/Aimport java.security.AccessController;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.concurrent.Future;
0N/Aimport java.util.concurrent.ScheduledExecutorService;
0N/Aimport java.util.concurrent.TimeUnit;
0N/Aimport sun.rmi.runtime.RuntimeUtil;
0N/Aimport sun.security.action.GetLongAction;
0N/A
0N/A/**
0N/A * Holds strong references to a set of remote objects, or live remote
0N/A * references to remote objects, after they have been marshalled (as
0N/A * remote references) as parts of the arguments or the result of a
0N/A * remote invocation. The purpose is to prevent remote objects or
0N/A * live remote references that might otherwise be determined to be
0N/A * unreachable in this VM from being locally garbage collected before
0N/A * the receiver has had an opportunity to register the unmarshalled
0N/A * remote references for DGC.
0N/A *
0N/A * The references are held strongly until an acknowledgment has been
0N/A * received that the receiver has had an opportunity to process the
0N/A * remote references or until a timeout has expired. For remote
0N/A * references sent as parts of the arguments of a remote invocation,
0N/A * the acknowledgment is the beginning of the response indicating
0N/A * completion of the remote invocation. For remote references sent as
0N/A * parts of the result of a remote invocation, a UID is included as
0N/A * part of the result, and the acknowledgment is a transport-level
0N/A * "DGCAck" message containing that UID.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Peter Jones
0N/A **/
0N/Apublic class DGCAckHandler {
0N/A
0N/A /** timeout for holding references without receiving an acknowledgment */
0N/A private static final long dgcAckTimeout = // default 5 minutes
0N/A AccessController.doPrivileged(
0N/A new GetLongAction("sun.rmi.dgc.ackTimeout", 300000));
0N/A
0N/A /** thread pool for scheduling delayed tasks */
0N/A private static final ScheduledExecutorService scheduler =
0N/A AccessController.doPrivileged(
0N/A new RuntimeUtil.GetInstanceAction()).getScheduler();
0N/A
0N/A /** table mapping ack ID to handler */
0N/A private static final Map<UID,DGCAckHandler> idTable =
0N/A Collections.synchronizedMap(new HashMap<UID,DGCAckHandler>());
0N/A
0N/A private final UID id;
5559N/A private List<Object> objList = new ArrayList<>(); // null if released
0N/A private Future<?> task = null;
0N/A
0N/A /**
0N/A * Creates a new DGCAckHandler, associated with the specified UID
0N/A * if the argument is not null.
0N/A *
0N/A * References added to this DGCAckHandler will be held strongly
0N/A * until its "release" method is invoked or (after the
0N/A * "startTimer" method has been invoked) the timeout has expired.
0N/A * If the argument is not null, then invoking the static
0N/A * "received" method with the specified UID is equivalent to
0N/A * invoking this instance's "release" method.
0N/A **/
0N/A DGCAckHandler(UID id) {
0N/A this.id = id;
0N/A if (id != null) {
0N/A assert !idTable.containsKey(id);
0N/A idTable.put(id, this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified reference to this DGCAckHandler.
0N/A **/
0N/A synchronized void add(Object obj) {
0N/A if (objList != null) {
0N/A objList.add(obj);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Starts the timer for this DGCAckHandler. After the timeout has
0N/A * expired, the references are released even if the acknowledgment
0N/A * has not been received.
0N/A **/
0N/A synchronized void startTimer() {
0N/A if (objList != null && task == null) {
0N/A task = scheduler.schedule(new Runnable() {
0N/A public void run() {
0N/A release();
0N/A }
0N/A }, dgcAckTimeout, TimeUnit.MILLISECONDS);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Releases the references held by this DGCAckHandler.
0N/A **/
0N/A synchronized void release() {
0N/A if (task != null) {
0N/A task.cancel(false);
0N/A task = null;
0N/A }
0N/A objList = null;
0N/A }
0N/A
0N/A /**
0N/A * Causes the DGCAckHandler associated with the specified UID to
0N/A * release its references.
0N/A **/
0N/A public static void received(UID id) {
0N/A DGCAckHandler h = idTable.remove(id);
0N/A if (h != null) {
0N/A h.release();
0N/A }
0N/A }
0N/A}