Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/transport/DGCAckHandler.java
38831 views
/*1* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.rmi.transport;2627import java.rmi.server.UID;28import java.security.AccessController;29import java.util.ArrayList;30import java.util.Collections;31import java.util.HashMap;32import java.util.List;33import java.util.Map;34import java.util.concurrent.Future;35import java.util.concurrent.ScheduledExecutorService;36import java.util.concurrent.TimeUnit;37import sun.rmi.runtime.RuntimeUtil;38import sun.security.action.GetLongAction;3940/**41* Holds strong references to a set of remote objects, or live remote42* references to remote objects, after they have been marshalled (as43* remote references) as parts of the arguments or the result of a44* remote invocation. The purpose is to prevent remote objects or45* live remote references that might otherwise be determined to be46* unreachable in this VM from being locally garbage collected before47* the receiver has had an opportunity to register the unmarshalled48* remote references for DGC.49*50* The references are held strongly until an acknowledgment has been51* received that the receiver has had an opportunity to process the52* remote references or until a timeout has expired. For remote53* references sent as parts of the arguments of a remote invocation,54* the acknowledgment is the beginning of the response indicating55* completion of the remote invocation. For remote references sent as56* parts of the result of a remote invocation, a UID is included as57* part of the result, and the acknowledgment is a transport-level58* "DGCAck" message containing that UID.59*60* @author Ann Wollrath61* @author Peter Jones62**/63public class DGCAckHandler {6465/** timeout for holding references without receiving an acknowledgment */66private static final long dgcAckTimeout = // default 5 minutes67AccessController.doPrivileged(68new GetLongAction("sun.rmi.dgc.ackTimeout", 300000));6970/** thread pool for scheduling delayed tasks */71private static final ScheduledExecutorService scheduler =72AccessController.doPrivileged(73new RuntimeUtil.GetInstanceAction()).getScheduler();7475/** table mapping ack ID to handler */76private static final Map<UID,DGCAckHandler> idTable =77Collections.synchronizedMap(new HashMap<UID,DGCAckHandler>());7879private final UID id;80private List<Object> objList = new ArrayList<>(); // null if released81private Future<?> task = null;8283/**84* Creates a new DGCAckHandler, associated with the specified UID85* if the argument is not null.86*87* References added to this DGCAckHandler will be held strongly88* until its "release" method is invoked or (after the89* "startTimer" method has been invoked) the timeout has expired.90* If the argument is not null, then invoking the static91* "received" method with the specified UID is equivalent to92* invoking this instance's "release" method.93**/94DGCAckHandler(UID id) {95this.id = id;96if (id != null) {97assert !idTable.containsKey(id);98idTable.put(id, this);99}100}101102/**103* Adds the specified reference to this DGCAckHandler.104**/105synchronized void add(Object obj) {106if (objList != null) {107objList.add(obj);108}109}110111/**112* Starts the timer for this DGCAckHandler. After the timeout has113* expired, the references are released even if the acknowledgment114* has not been received.115**/116synchronized void startTimer() {117if (objList != null && task == null) {118task = scheduler.schedule(new Runnable() {119public void run() {120if (id != null) {121idTable.remove(id);122}123release();124}125}, dgcAckTimeout, TimeUnit.MILLISECONDS);126}127}128129/**130* Releases the references held by this DGCAckHandler.131**/132synchronized void release() {133if (task != null) {134task.cancel(false);135task = null;136}137objList = null;138}139140/**141* Causes the DGCAckHandler associated with the specified UID to142* release its references.143**/144public static void received(UID id) {145DGCAckHandler h = idTable.remove(id);146if (h != null) {147h.release();148}149}150}151152153