Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.rmi/share/classes/sun/rmi/transport/DGCAckHandler.java
41154 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.rmi.transport;
27
28
import java.rmi.server.UID;
29
import java.security.AccessController;
30
import java.security.PrivilegedAction;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.concurrent.Future;
37
import java.util.concurrent.ScheduledExecutorService;
38
import java.util.concurrent.TimeUnit;
39
import sun.rmi.runtime.RuntimeUtil;
40
41
/**
42
* Holds strong references to a set of remote objects, or live remote
43
* references to remote objects, after they have been marshalled (as
44
* remote references) as parts of the arguments or the result of a
45
* remote invocation. The purpose is to prevent remote objects or
46
* live remote references that might otherwise be determined to be
47
* unreachable in this VM from being locally garbage collected before
48
* the receiver has had an opportunity to register the unmarshalled
49
* remote references for DGC.
50
*
51
* The references are held strongly until an acknowledgment has been
52
* received that the receiver has had an opportunity to process the
53
* remote references or until a timeout has expired. For remote
54
* references sent as parts of the arguments of a remote invocation,
55
* the acknowledgment is the beginning of the response indicating
56
* completion of the remote invocation. For remote references sent as
57
* parts of the result of a remote invocation, a UID is included as
58
* part of the result, and the acknowledgment is a transport-level
59
* "DGCAck" message containing that UID.
60
*
61
* @author Ann Wollrath
62
* @author Peter Jones
63
**/
64
public class DGCAckHandler {
65
66
/** timeout for holding references without receiving an acknowledgment */
67
@SuppressWarnings("removal")
68
private static final long dgcAckTimeout = // default 5 minutes
69
AccessController.doPrivileged((PrivilegedAction<Long>) () ->
70
Long.getLong("sun.rmi.dgc.ackTimeout", 300000));
71
72
/** thread pool for scheduling delayed tasks */
73
@SuppressWarnings("removal")
74
private static final ScheduledExecutorService scheduler =
75
AccessController.doPrivileged(
76
new RuntimeUtil.GetInstanceAction()).getScheduler();
77
78
/** table mapping ack ID to handler */
79
private static final Map<UID,DGCAckHandler> idTable =
80
Collections.synchronizedMap(new HashMap<UID,DGCAckHandler>());
81
82
private final UID id;
83
private List<Object> objList = new ArrayList<>(); // null if released
84
private Future<?> task = null;
85
86
/**
87
* Creates a new DGCAckHandler, associated with the specified UID
88
* if the argument is not null.
89
*
90
* References added to this DGCAckHandler will be held strongly
91
* until its "release" method is invoked or (after the
92
* "startTimer" method has been invoked) the timeout has expired.
93
* If the argument is not null, then invoking the static
94
* "received" method with the specified UID is equivalent to
95
* invoking this instance's "release" method.
96
**/
97
DGCAckHandler(UID id) {
98
this.id = id;
99
if (id != null) {
100
assert !idTable.containsKey(id);
101
idTable.put(id, this);
102
}
103
}
104
105
/**
106
* Adds the specified reference to this DGCAckHandler.
107
**/
108
synchronized void add(Object obj) {
109
if (objList != null) {
110
objList.add(obj);
111
}
112
}
113
114
/**
115
* Starts the timer for this DGCAckHandler. After the timeout has
116
* expired, the references are released even if the acknowledgment
117
* has not been received.
118
**/
119
synchronized void startTimer() {
120
if (objList != null && task == null) {
121
task = scheduler.schedule(new Runnable() {
122
public void run() {
123
if (id != null) {
124
idTable.remove(id);
125
}
126
release();
127
}
128
}, dgcAckTimeout, TimeUnit.MILLISECONDS);
129
}
130
}
131
132
/**
133
* Releases the references held by this DGCAckHandler.
134
**/
135
synchronized void release() {
136
if (task != null) {
137
task.cancel(false);
138
task = null;
139
}
140
objList = null;
141
}
142
143
/**
144
* Causes the DGCAckHandler associated with the specified UID to
145
* release its references.
146
**/
147
public static void received(UID id) {
148
DGCAckHandler h = idTable.remove(id);
149
if (h != null) {
150
h.release();
151
}
152
}
153
}
154
155