author | martin |
Thu, 30 Oct 2014 07:31:41 -0700 | |
changeset 28059 | e576535359cc |
parent 25859 | 3317bb8137f4 |
child 29273 | f32302d0ba47 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
14912
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package com.sun.jmx.remote.internal; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.io.NotSerializableException; |
|
29 |
||
30 |
import java.util.ArrayList; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.concurrent.Executor; |
|
35 |
||
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
36 |
import java.security.AccessControlContext; |
2 | 37 |
import java.security.AccessController; |
38 |
import java.security.PrivilegedAction; |
|
39 |
import javax.security.auth.Subject; |
|
40 |
||
41 |
import javax.management.Notification; |
|
42 |
import javax.management.NotificationListener; |
|
43 |
import javax.management.NotificationFilter; |
|
44 |
import javax.management.ObjectName; |
|
45 |
import javax.management.MBeanServerNotification; |
|
46 |
import javax.management.InstanceNotFoundException; |
|
47 |
import javax.management.ListenerNotFoundException; |
|
48 |
||
49 |
import javax.management.remote.NotificationResult; |
|
50 |
import javax.management.remote.TargetedNotification; |
|
51 |
||
52 |
import com.sun.jmx.remote.util.ClassLogger; |
|
53 |
import com.sun.jmx.remote.util.EnvHelp; |
|
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
54 |
import java.rmi.UnmarshalException; |
2 | 55 |
|
56 |
||
57 |
public abstract class ClientNotifForwarder { |
|
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
58 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
59 |
private final AccessControlContext acc; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
60 |
|
24871
224e298c3978
8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents:
23706
diff
changeset
|
61 |
public ClientNotifForwarder(Map<String, ?> env) { |
2 | 62 |
this(null, env); |
63 |
} |
|
64 |
||
65 |
private static int threadId; |
|
66 |
||
67 |
/* An Executor that allows at most one executing and one pending |
|
68 |
Runnable. It uses at most one thread -- as soon as there is |
|
69 |
no pending Runnable the thread can exit. Another thread is |
|
70 |
created as soon as there is a new pending Runnable. This |
|
71 |
Executor is adapted for use in a situation where each Runnable |
|
72 |
usually schedules up another Runnable. On return from the |
|
73 |
first one, the second one is immediately executed. So this |
|
74 |
just becomes a complicated way to write a while loop, but with |
|
75 |
the advantage that you can replace it with another Executor, |
|
76 |
for instance one that you are using to execute a bunch of other |
|
77 |
unrelated work. |
|
78 |
||
79 |
You might expect that a java.util.concurrent.ThreadPoolExecutor |
|
80 |
with corePoolSize=0 and maximumPoolSize=1 would have the same |
|
81 |
behavior, but it does not. A ThreadPoolExecutor only creates |
|
82 |
a new thread when a new task is submitted and the number of |
|
83 |
existing threads is < corePoolSize. This can never happen when |
|
84 |
corePoolSize=0, so new threads are never created. Surprising, |
|
85 |
but there you are. |
|
86 |
*/ |
|
87 |
private static class LinearExecutor implements Executor { |
|
88 |
public synchronized void execute(Runnable command) { |
|
89 |
if (this.command != null) |
|
90 |
throw new IllegalArgumentException("More than one command"); |
|
91 |
this.command = command; |
|
92 |
if (thread == null) { |
|
93 |
thread = new Thread() { |
|
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
94 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
95 |
@Override |
2 | 96 |
public void run() { |
97 |
while (true) { |
|
98 |
Runnable r; |
|
99 |
synchronized (LinearExecutor.this) { |
|
100 |
if (LinearExecutor.this.command == null) { |
|
101 |
thread = null; |
|
102 |
return; |
|
103 |
} else { |
|
104 |
r = LinearExecutor.this.command; |
|
105 |
LinearExecutor.this.command = null; |
|
106 |
} |
|
107 |
} |
|
108 |
r.run(); |
|
109 |
} |
|
110 |
} |
|
111 |
}; |
|
112 |
thread.setDaemon(true); |
|
113 |
thread.setName("ClientNotifForwarder-" + ++threadId); |
|
114 |
thread.start(); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
private Runnable command; |
|
119 |
private Thread thread; |
|
120 |
} |
|
121 |
||
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1243
diff
changeset
|
122 |
public ClientNotifForwarder(ClassLoader defaultClassLoader, Map<String, ?> env) { |
2 | 123 |
maxNotifications = EnvHelp.getMaxFetchNotifNumber(env); |
124 |
timeout = EnvHelp.getFetchTimeout(env); |
|
125 |
||
126 |
/* You can supply an Executor in which the remote call to |
|
127 |
fetchNotifications will be made. The Executor's execute |
|
128 |
method reschedules another task, so you must not use |
|
129 |
an Executor that executes tasks in the caller's thread. */ |
|
130 |
Executor ex = (Executor) |
|
131 |
env.get("jmx.remote.x.fetch.notifications.executor"); |
|
132 |
if (ex == null) |
|
133 |
ex = new LinearExecutor(); |
|
134 |
else if (logger.traceOn()) |
|
135 |
logger.trace("ClientNotifForwarder", "executor is " + ex); |
|
136 |
||
137 |
this.defaultClassLoader = defaultClassLoader; |
|
138 |
this.executor = ex; |
|
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
139 |
this.acc = AccessController.getContext(); |
2 | 140 |
} |
141 |
||
142 |
/** |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
143 |
* Called to fetch notifications from a server. |
2 | 144 |
*/ |
145 |
abstract protected NotificationResult fetchNotifs(long clientSequenceNumber, |
|
146 |
int maxNotifications, |
|
147 |
long timeout) |
|
148 |
throws IOException, ClassNotFoundException; |
|
149 |
||
150 |
abstract protected Integer addListenerForMBeanRemovedNotif() |
|
151 |
throws IOException, InstanceNotFoundException; |
|
152 |
||
153 |
abstract protected void removeListenerForMBeanRemovedNotif(Integer id) |
|
154 |
throws IOException, InstanceNotFoundException, |
|
155 |
ListenerNotFoundException; |
|
156 |
||
157 |
/** |
|
158 |
* Used to send out a notification about lost notifs |
|
159 |
*/ |
|
160 |
abstract protected void lostNotifs(String message, long number); |
|
161 |
||
162 |
||
163 |
public synchronized void addNotificationListener(Integer listenerID, |
|
164 |
ObjectName name, |
|
165 |
NotificationListener listener, |
|
166 |
NotificationFilter filter, |
|
167 |
Object handback, |
|
168 |
Subject delegationSubject) |
|
169 |
throws IOException, InstanceNotFoundException { |
|
170 |
||
171 |
if (logger.traceOn()) { |
|
172 |
logger.trace("addNotificationListener", |
|
173 |
"Add the listener "+listener+" at "+name); |
|
174 |
} |
|
175 |
||
176 |
infoList.put(listenerID, |
|
177 |
new ClientListenerInfo(listenerID, |
|
178 |
name, |
|
179 |
listener, |
|
180 |
filter, |
|
181 |
handback, |
|
182 |
delegationSubject)); |
|
183 |
||
184 |
||
185 |
init(false); |
|
186 |
} |
|
187 |
||
188 |
public synchronized Integer[] |
|
189 |
removeNotificationListener(ObjectName name, |
|
190 |
NotificationListener listener) |
|
191 |
throws ListenerNotFoundException, IOException { |
|
192 |
||
193 |
beforeRemove(); |
|
194 |
||
195 |
if (logger.traceOn()) { |
|
196 |
logger.trace("removeNotificationListener", |
|
197 |
"Remove the listener "+listener+" from "+name); |
|
198 |
} |
|
199 |
||
200 |
List<Integer> ids = new ArrayList<Integer>(); |
|
201 |
List<ClientListenerInfo> values = |
|
202 |
new ArrayList<ClientListenerInfo>(infoList.values()); |
|
203 |
for (int i=values.size()-1; i>=0; i--) { |
|
204 |
ClientListenerInfo li = values.get(i); |
|
205 |
||
206 |
if (li.sameAs(name, listener)) { |
|
207 |
ids.add(li.getListenerID()); |
|
208 |
||
209 |
infoList.remove(li.getListenerID()); |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
if (ids.isEmpty()) |
|
214 |
throw new ListenerNotFoundException("Listener not found"); |
|
215 |
||
216 |
return ids.toArray(new Integer[0]); |
|
217 |
} |
|
218 |
||
219 |
public synchronized Integer |
|
220 |
removeNotificationListener(ObjectName name, |
|
221 |
NotificationListener listener, |
|
222 |
NotificationFilter filter, |
|
223 |
Object handback) |
|
224 |
throws ListenerNotFoundException, IOException { |
|
225 |
||
226 |
if (logger.traceOn()) { |
|
227 |
logger.trace("removeNotificationListener", |
|
228 |
"Remove the listener "+listener+" from "+name); |
|
229 |
} |
|
230 |
||
231 |
beforeRemove(); |
|
232 |
||
233 |
Integer id = null; |
|
234 |
||
235 |
List<ClientListenerInfo> values = |
|
236 |
new ArrayList<ClientListenerInfo>(infoList.values()); |
|
237 |
for (int i=values.size()-1; i>=0; i--) { |
|
238 |
ClientListenerInfo li = values.get(i); |
|
239 |
if (li.sameAs(name, listener, filter, handback)) { |
|
240 |
id=li.getListenerID(); |
|
241 |
||
242 |
infoList.remove(id); |
|
243 |
||
244 |
break; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
if (id == null) |
|
249 |
throw new ListenerNotFoundException("Listener not found"); |
|
250 |
||
251 |
return id; |
|
252 |
} |
|
253 |
||
254 |
public synchronized Integer[] removeNotificationListener(ObjectName name) { |
|
255 |
if (logger.traceOn()) { |
|
256 |
logger.trace("removeNotificationListener", |
|
257 |
"Remove all listeners registered at "+name); |
|
258 |
} |
|
259 |
||
260 |
List<Integer> ids = new ArrayList<Integer>(); |
|
261 |
||
262 |
List<ClientListenerInfo> values = |
|
263 |
new ArrayList<ClientListenerInfo>(infoList.values()); |
|
264 |
for (int i=values.size()-1; i>=0; i--) { |
|
265 |
ClientListenerInfo li = values.get(i); |
|
266 |
if (li.sameAs(name)) { |
|
267 |
ids.add(li.getListenerID()); |
|
268 |
||
269 |
infoList.remove(li.getListenerID()); |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
return ids.toArray(new Integer[0]); |
|
274 |
} |
|
275 |
||
276 |
/* |
|
277 |
* Called when a connector is doing reconnection. Like <code>postReconnection</code>, |
|
278 |
* this method is intended to be called only by a client connector: |
|
279 |
* <code>RMIConnector</code> and <code>ClientIntermediary</code>. |
|
280 |
* Call this method will set the flag beingReconnection to <code>true</code>, |
|
281 |
* and the thread used to fetch notifis will be stopped, a new thread can be |
|
282 |
* created only after the method <code>postReconnection</code> is called. |
|
283 |
* |
|
284 |
* It is caller's responsiblity to not re-call this method before calling |
|
285 |
* <code>postReconnection</code>. |
|
286 |
*/ |
|
287 |
public synchronized ClientListenerInfo[] preReconnection() throws IOException { |
|
288 |
if (state == TERMINATED || beingReconnected) { // should never |
|
289 |
throw new IOException("Illegal state."); |
|
290 |
} |
|
291 |
||
292 |
final ClientListenerInfo[] tmp = |
|
293 |
infoList.values().toArray(new ClientListenerInfo[0]); |
|
294 |
||
295 |
||
296 |
beingReconnected = true; |
|
297 |
||
298 |
infoList.clear(); |
|
299 |
||
300 |
return tmp; |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Called after reconnection is finished. |
|
305 |
* This method is intended to be called only by a client connector: |
|
306 |
* <code>RMIConnector</code> and <code>ClientIntermediary</code>. |
|
307 |
*/ |
|
308 |
public synchronized void postReconnection(ClientListenerInfo[] listenerInfos) |
|
309 |
throws IOException { |
|
310 |
||
311 |
if (state == TERMINATED) { |
|
312 |
return; |
|
313 |
} |
|
314 |
||
315 |
while (state == STOPPING) { |
|
316 |
try { |
|
317 |
wait(); |
|
318 |
} catch (InterruptedException ire) { |
|
319 |
IOException ioe = new IOException(ire.toString()); |
|
320 |
EnvHelp.initCause(ioe, ire); |
|
321 |
throw ioe; |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
final boolean trace = logger.traceOn(); |
|
326 |
final int len = listenerInfos.length; |
|
327 |
||
328 |
for (int i=0; i<len; i++) { |
|
329 |
if (trace) { |
|
330 |
logger.trace("addNotificationListeners", |
|
331 |
"Add a listener at "+ |
|
332 |
listenerInfos[i].getListenerID()); |
|
333 |
} |
|
334 |
||
335 |
infoList.put(listenerInfos[i].getListenerID(), listenerInfos[i]); |
|
336 |
} |
|
337 |
||
338 |
beingReconnected = false; |
|
339 |
notifyAll(); |
|
340 |
||
1243
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
341 |
if (currentFetchThread == Thread.currentThread() || |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
342 |
state == STARTING || state == STARTED) { // doing or waiting reconnection |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
343 |
// only update mbeanRemovedNotifID |
2 | 344 |
try { |
345 |
mbeanRemovedNotifID = addListenerForMBeanRemovedNotif(); |
|
346 |
} catch (Exception e) { |
|
347 |
final String msg = |
|
348 |
"Failed to register a listener to the mbean " + |
|
349 |
"server: the client will not do clean when an MBean " + |
|
350 |
"is unregistered"; |
|
351 |
if (logger.traceOn()) { |
|
352 |
logger.trace("init", msg, e); |
|
353 |
} |
|
354 |
} |
|
1243
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
355 |
} else { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
356 |
while (state == STOPPING) { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
357 |
try { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
358 |
wait(); |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
359 |
} catch (InterruptedException ire) { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
360 |
IOException ioe = new IOException(ire.toString()); |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
361 |
EnvHelp.initCause(ioe, ire); |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
362 |
throw ioe; |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
363 |
} |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
364 |
} |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
365 |
|
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
366 |
if (listenerInfos.length > 0) { // old listeners are re-added |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
367 |
init(true); // not update clientSequenceNumber |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
368 |
} else if (infoList.size() > 0) { // only new listeners added during reconnection |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
369 |
init(false); // need update clientSequenceNumber |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
370 |
} |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
371 |
} |
2 | 372 |
} |
373 |
||
374 |
public synchronized void terminate() { |
|
375 |
if (state == TERMINATED) { |
|
376 |
return; |
|
377 |
} |
|
378 |
||
379 |
if (logger.traceOn()) { |
|
380 |
logger.trace("terminate", "Terminating..."); |
|
381 |
} |
|
382 |
||
383 |
if (state == STARTED) { |
|
384 |
infoList.clear(); |
|
385 |
} |
|
386 |
||
387 |
setState(TERMINATED); |
|
388 |
} |
|
389 |
||
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
390 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
391 |
// ------------------------------------------------- |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
392 |
// private classes |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
393 |
// ------------------------------------------------- |
2 | 394 |
// |
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
395 |
|
2 | 396 |
private class NotifFetcher implements Runnable { |
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
397 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
398 |
private volatile boolean alreadyLogged = false; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
399 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
400 |
private void logOnce(String msg, SecurityException x) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
401 |
if (alreadyLogged) return; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
402 |
// Log only once. |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
403 |
logger.config("setContextClassLoader",msg); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
404 |
if (x != null) logger.fine("setContextClassLoader", x); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
405 |
alreadyLogged = true; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
406 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
407 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
408 |
// Set new context class loader, returns previous one. |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
409 |
private final ClassLoader setContextClassLoader(final ClassLoader loader) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
410 |
final AccessControlContext ctxt = ClientNotifForwarder.this.acc; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
411 |
// if ctxt is null, log a config message and throw a |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
412 |
// SecurityException. |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
413 |
if (ctxt == null) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
414 |
logOnce("AccessControlContext must not be null.",null); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
415 |
throw new SecurityException("AccessControlContext must not be null"); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
416 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
417 |
return AccessController.doPrivileged( |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
418 |
new PrivilegedAction<ClassLoader>() { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
419 |
public ClassLoader run() { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
420 |
try { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
421 |
// get context class loader - may throw |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
422 |
// SecurityException - though unlikely. |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
423 |
final ClassLoader previous = |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
424 |
Thread.currentThread().getContextClassLoader(); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
425 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
426 |
// if nothing needs to be done, break here... |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
427 |
if (loader == previous) return previous; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
428 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
429 |
// reset context class loader - may throw |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
430 |
// SecurityException |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
431 |
Thread.currentThread().setContextClassLoader(loader); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
432 |
return previous; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
433 |
} catch (SecurityException x) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
434 |
logOnce("Permission to set ContextClassLoader missing. " + |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
435 |
"Notifications will not be dispatched. " + |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
436 |
"Please check your Java policy configuration: " + |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
437 |
x, x); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
438 |
throw x; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
439 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
440 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
441 |
}, ctxt); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
442 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
443 |
|
2 | 444 |
public void run() { |
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
445 |
final ClassLoader previous; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
446 |
if (defaultClassLoader != null) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
447 |
previous = setContextClassLoader(defaultClassLoader); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
448 |
} else { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
449 |
previous = null; |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
450 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
451 |
try { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
452 |
doRun(); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
453 |
} finally { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
454 |
if (defaultClassLoader != null) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
455 |
setContextClassLoader(previous); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
456 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
457 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
458 |
} |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
459 |
|
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
460 |
private void doRun() { |
2 | 461 |
synchronized (ClientNotifForwarder.this) { |
462 |
currentFetchThread = Thread.currentThread(); |
|
463 |
||
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
464 |
if (state == STARTING) { |
2 | 465 |
setState(STARTED); |
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
466 |
} |
2 | 467 |
} |
468 |
||
469 |
||
470 |
NotificationResult nr = null; |
|
471 |
if (!shouldStop() && (nr = fetchNotifs()) != null) { |
|
472 |
// nr == null means got exception |
|
473 |
||
474 |
final TargetedNotification[] notifs = |
|
475 |
nr.getTargetedNotifications(); |
|
476 |
final int len = notifs.length; |
|
477 |
final Map<Integer, ClientListenerInfo> listeners; |
|
478 |
final Integer myListenerID; |
|
479 |
||
480 |
long missed = 0; |
|
481 |
||
482 |
synchronized(ClientNotifForwarder.this) { |
|
483 |
// check sequence number. |
|
484 |
// |
|
485 |
if (clientSequenceNumber >= 0) { |
|
486 |
missed = nr.getEarliestSequenceNumber() - |
|
487 |
clientSequenceNumber; |
|
488 |
} |
|
489 |
||
490 |
clientSequenceNumber = nr.getNextSequenceNumber(); |
|
491 |
||
492 |
listeners = new HashMap<Integer, ClientListenerInfo>(); |
|
493 |
||
494 |
for (int i = 0 ; i < len ; i++) { |
|
495 |
final TargetedNotification tn = notifs[i]; |
|
496 |
final Integer listenerID = tn.getListenerID(); |
|
497 |
||
498 |
// check if an mbean unregistration notif |
|
499 |
if (!listenerID.equals(mbeanRemovedNotifID)) { |
|
500 |
final ClientListenerInfo li = infoList.get(listenerID); |
|
2615
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
501 |
if (li != null) { |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
502 |
listeners.put(listenerID, li); |
be2f497eb537
6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents:
526
diff
changeset
|
503 |
} |
2 | 504 |
continue; |
505 |
} |
|
506 |
final Notification notif = tn.getNotification(); |
|
507 |
final String unreg = |
|
508 |
MBeanServerNotification.UNREGISTRATION_NOTIFICATION; |
|
509 |
if (notif instanceof MBeanServerNotification && |
|
510 |
notif.getType().equals(unreg)) { |
|
511 |
||
512 |
MBeanServerNotification mbsn = |
|
513 |
(MBeanServerNotification) notif; |
|
514 |
ObjectName name = mbsn.getMBeanName(); |
|
515 |
||
516 |
removeNotificationListener(name); |
|
517 |
} |
|
518 |
} |
|
519 |
myListenerID = mbeanRemovedNotifID; |
|
520 |
} |
|
521 |
||
522 |
if (missed > 0) { |
|
523 |
final String msg = |
|
524 |
"May have lost up to " + missed + |
|
525 |
" notification" + (missed == 1 ? "" : "s"); |
|
526 |
lostNotifs(msg, missed); |
|
527 |
logger.trace("NotifFetcher.run", msg); |
|
528 |
} |
|
529 |
||
530 |
// forward |
|
531 |
for (int i = 0 ; i < len ; i++) { |
|
532 |
final TargetedNotification tn = notifs[i]; |
|
533 |
dispatchNotification(tn,myListenerID,listeners); |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
synchronized (ClientNotifForwarder.this) { |
|
538 |
currentFetchThread = null; |
|
539 |
} |
|
540 |
||
541 |
if (nr == null || shouldStop()) { |
|
542 |
// tell that the thread is REALLY stopped |
|
543 |
setState(STOPPED); |
|
1243
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
544 |
|
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
545 |
try { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
546 |
removeListenerForMBeanRemovedNotif(mbeanRemovedNotifID); |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
547 |
} catch (Exception e) { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
548 |
if (logger.traceOn()) { |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
549 |
logger.trace("NotifFetcher-run", |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
550 |
"removeListenerForMBeanRemovedNotif", e); |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
551 |
} |
f5d91fb6df32
6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents:
1004
diff
changeset
|
552 |
} |
2 | 553 |
} else { |
554 |
executor.execute(this); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
void dispatchNotification(TargetedNotification tn, |
|
559 |
Integer myListenerID, |
|
560 |
Map<Integer, ClientListenerInfo> listeners) { |
|
561 |
final Notification notif = tn.getNotification(); |
|
562 |
final Integer listenerID = tn.getListenerID(); |
|
563 |
||
564 |
if (listenerID.equals(myListenerID)) return; |
|
565 |
final ClientListenerInfo li = listeners.get(listenerID); |
|
566 |
||
567 |
if (li == null) { |
|
568 |
logger.trace("NotifFetcher.dispatch", |
|
569 |
"Listener ID not in map"); |
|
570 |
return; |
|
571 |
} |
|
572 |
||
573 |
NotificationListener l = li.getListener(); |
|
574 |
Object h = li.getHandback(); |
|
575 |
try { |
|
576 |
l.handleNotification(notif, h); |
|
577 |
} catch (RuntimeException e) { |
|
578 |
final String msg = |
|
579 |
"Failed to forward a notification " + |
|
580 |
"to a listener"; |
|
581 |
logger.trace("NotifFetcher-run", msg, e); |
|
582 |
} |
|
583 |
||
584 |
} |
|
585 |
||
586 |
private NotificationResult fetchNotifs() { |
|
587 |
try { |
|
588 |
NotificationResult nr = ClientNotifForwarder.this. |
|
589 |
fetchNotifs(clientSequenceNumber,maxNotifications, |
|
590 |
timeout); |
|
591 |
||
592 |
if (logger.traceOn()) { |
|
593 |
logger.trace("NotifFetcher-run", |
|
594 |
"Got notifications from the server: "+nr); |
|
595 |
} |
|
596 |
||
597 |
return nr; |
|
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
598 |
} catch (ClassNotFoundException | NotSerializableException | UnmarshalException e) { |
2 | 599 |
logger.trace("NotifFetcher.fetchNotifs", e); |
600 |
return fetchOneNotif(); |
|
601 |
} catch (IOException ioe) { |
|
602 |
if (!shouldStop()) { |
|
603 |
logger.error("NotifFetcher-run", |
|
604 |
"Failed to fetch notification, " + |
|
605 |
"stopping thread. Error is: " + ioe, ioe); |
|
606 |
logger.debug("NotifFetcher-run",ioe); |
|
607 |
} |
|
608 |
||
609 |
// no more fetching |
|
610 |
return null; |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
/* Fetch one notification when we suspect that it might be a |
|
615 |
notification that we can't deserialize (because of a |
|
616 |
missing class). First we ask for 0 notifications with 0 |
|
617 |
timeout. This allows us to skip sequence numbers for |
|
618 |
notifications that don't match our filters. Then we ask |
|
619 |
for one notification. If that produces a |
|
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
620 |
ClassNotFoundException, NotSerializableException or |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
621 |
UnmarshalException, we increase our sequence number and ask again. |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
622 |
Eventually we will either get a successful notification, or a |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
623 |
return with 0 notifications. In either case we can return a |
2 | 624 |
NotificationResult. This algorithm works (albeit less |
625 |
well) even if the server implementation doesn't optimize a |
|
626 |
request for 0 notifications to skip sequence numbers for |
|
627 |
notifications that don't match our filters. |
|
628 |
||
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
629 |
If we had at least one |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
630 |
ClassNotFoundException/NotSerializableException/UnmarshalException, |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
631 |
then we must emit a JMXConnectionNotification.LOST_NOTIFS. |
2 | 632 |
*/ |
633 |
private NotificationResult fetchOneNotif() { |
|
634 |
ClientNotifForwarder cnf = ClientNotifForwarder.this; |
|
635 |
||
636 |
long startSequenceNumber = clientSequenceNumber; |
|
637 |
||
638 |
int notFoundCount = 0; |
|
639 |
||
640 |
NotificationResult result = null; |
|
1004 | 641 |
long firstEarliest = -1; |
2 | 642 |
|
643 |
while (result == null && !shouldStop()) { |
|
644 |
NotificationResult nr; |
|
645 |
||
646 |
try { |
|
647 |
// 0 notifs to update startSequenceNumber |
|
648 |
nr = cnf.fetchNotifs(startSequenceNumber, 0, 0L); |
|
649 |
} catch (ClassNotFoundException e) { |
|
650 |
logger.warning("NotifFetcher.fetchOneNotif", |
|
651 |
"Impossible exception: " + e); |
|
652 |
logger.debug("NotifFetcher.fetchOneNotif",e); |
|
653 |
return null; |
|
654 |
} catch (IOException e) { |
|
655 |
if (!shouldStop()) |
|
656 |
logger.trace("NotifFetcher.fetchOneNotif", e); |
|
657 |
return null; |
|
658 |
} |
|
659 |
||
660 |
if (shouldStop()) |
|
661 |
return null; |
|
662 |
||
663 |
startSequenceNumber = nr.getNextSequenceNumber(); |
|
1004 | 664 |
if (firstEarliest < 0) |
665 |
firstEarliest = nr.getEarliestSequenceNumber(); |
|
2 | 666 |
|
667 |
try { |
|
668 |
// 1 notif to skip possible missing class |
|
669 |
result = cnf.fetchNotifs(startSequenceNumber, 1, 0L); |
|
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
670 |
} catch (ClassNotFoundException | NotSerializableException | UnmarshalException e) { |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
671 |
logger.warning("NotifFetcher.fetchOneNotif", |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
672 |
"Failed to deserialize a notification: "+e.toString()); |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
673 |
if (logger.traceOn()) { |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
674 |
logger.trace("NotifFetcher.fetchOneNotif", |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
675 |
"Failed to deserialize a notification.", e); |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
676 |
} |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
677 |
|
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
678 |
notFoundCount++; |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
679 |
startSequenceNumber++; |
2 | 680 |
} catch (Exception e) { |
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
681 |
if (!shouldStop()) |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
682 |
logger.trace("NotifFetcher.fetchOneNotif", e); |
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
683 |
return null; |
2 | 684 |
} |
685 |
} |
|
686 |
||
687 |
if (notFoundCount > 0) { |
|
688 |
final String msg = |
|
689 |
"Dropped " + notFoundCount + " notification" + |
|
690 |
(notFoundCount == 1 ? "" : "s") + |
|
14912
e6fc057a8011
6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents:
5506
diff
changeset
|
691 |
" because classes were missing locally or incompatible"; |
2 | 692 |
lostNotifs(msg, notFoundCount); |
1004 | 693 |
// Even if result.getEarliestSequenceNumber() is now greater than |
694 |
// it was initially, meaning some notifs have been dropped |
|
695 |
// from the buffer, we don't want the caller to see that |
|
696 |
// because it is then likely to renotify about the lost notifs. |
|
697 |
// So we put back the first value of earliestSequenceNumber |
|
698 |
// that we saw. |
|
699 |
if (result != null) { |
|
700 |
result = new NotificationResult( |
|
701 |
firstEarliest, result.getNextSequenceNumber(), |
|
702 |
result.getTargetedNotifications()); |
|
703 |
} |
|
2 | 704 |
} |
705 |
||
706 |
return result; |
|
707 |
} |
|
708 |
||
709 |
private boolean shouldStop() { |
|
710 |
synchronized (ClientNotifForwarder.this) { |
|
711 |
if (state != STARTED) { |
|
712 |
return true; |
|
713 |
} else if (infoList.size() == 0) { |
|
714 |
// no more listener, stop fetching |
|
715 |
setState(STOPPING); |
|
716 |
||
717 |
return true; |
|
718 |
} |
|
719 |
||
720 |
return false; |
|
721 |
} |
|
722 |
} |
|
723 |
} |
|
724 |
||
725 |
||
726 |
// ------------------------------------------------- |
|
727 |
// private methods |
|
728 |
// ------------------------------------------------- |
|
729 |
private synchronized void setState(int newState) { |
|
730 |
if (state == TERMINATED) { |
|
731 |
return; |
|
732 |
} |
|
733 |
||
734 |
state = newState; |
|
735 |
this.notifyAll(); |
|
736 |
} |
|
737 |
||
738 |
/* |
|
739 |
* Called to decide whether need to start a thread for fetching notifs. |
|
740 |
* <P>The parameter reconnected will decide whether to initilize the clientSequenceNumber, |
|
741 |
* initilaizing the clientSequenceNumber means to ignore all notifications arrived before. |
|
742 |
* If it is reconnected, we will not initialize in order to get all notifications arrived |
|
743 |
* during the reconnection. It may cause the newly registered listeners to receive some |
|
744 |
* notifications arrived before its registray. |
|
745 |
*/ |
|
746 |
private synchronized void init(boolean reconnected) throws IOException { |
|
747 |
switch (state) { |
|
748 |
case STARTED: |
|
749 |
return; |
|
750 |
case STARTING: |
|
751 |
return; |
|
752 |
case TERMINATED: |
|
753 |
throw new IOException("The ClientNotifForwarder has been terminated."); |
|
754 |
case STOPPING: |
|
755 |
if (beingReconnected == true) { |
|
756 |
// wait for another thread to do, which is doing reconnection |
|
757 |
return; |
|
758 |
} |
|
759 |
||
760 |
while (state == STOPPING) { // make sure only one fetching thread. |
|
761 |
try { |
|
762 |
wait(); |
|
763 |
} catch (InterruptedException ire) { |
|
764 |
IOException ioe = new IOException(ire.toString()); |
|
765 |
EnvHelp.initCause(ioe, ire); |
|
766 |
||
767 |
throw ioe; |
|
768 |
} |
|
769 |
} |
|
770 |
||
771 |
// re-call this method to check the state again, |
|
772 |
// the state can be other value like TERMINATED. |
|
773 |
init(reconnected); |
|
774 |
||
775 |
return; |
|
776 |
case STOPPED: |
|
777 |
if (beingReconnected == true) { |
|
778 |
// wait for another thread to do, which is doing reconnection |
|
779 |
return; |
|
780 |
} |
|
781 |
||
782 |
if (logger.traceOn()) { |
|
783 |
logger.trace("init", "Initializing..."); |
|
784 |
} |
|
785 |
||
786 |
// init the clientSequenceNumber if not reconnected. |
|
787 |
if (!reconnected) { |
|
788 |
try { |
|
789 |
NotificationResult nr = fetchNotifs(-1, 0, 0); |
|
23706
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
790 |
|
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
791 |
if (state != STOPPED) { // JDK-8038940 |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
792 |
// reconnection must happen during |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
793 |
// fetchNotifs(-1, 0, 0), and a new |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
794 |
// thread takes over the fetching job |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
795 |
return; |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
796 |
} |
5ea287e13cdb
8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents:
23010
diff
changeset
|
797 |
|
2 | 798 |
clientSequenceNumber = nr.getNextSequenceNumber(); |
799 |
} catch (ClassNotFoundException e) { |
|
800 |
// can't happen |
|
801 |
logger.warning("init", "Impossible exception: "+ e); |
|
802 |
logger.debug("init",e); |
|
803 |
} |
|
804 |
} |
|
805 |
||
806 |
// for cleaning |
|
807 |
try { |
|
808 |
mbeanRemovedNotifID = addListenerForMBeanRemovedNotif(); |
|
809 |
} catch (Exception e) { |
|
810 |
final String msg = |
|
811 |
"Failed to register a listener to the mbean " + |
|
812 |
"server: the client will not do clean when an MBean " + |
|
813 |
"is unregistered"; |
|
814 |
if (logger.traceOn()) { |
|
815 |
logger.trace("init", msg, e); |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
setState(STARTING); |
|
820 |
||
821 |
// start fetching |
|
822 |
executor.execute(new NotifFetcher()); |
|
823 |
||
824 |
return; |
|
825 |
default: |
|
826 |
// should not |
|
827 |
throw new IOException("Unknown state."); |
|
828 |
} |
|
829 |
} |
|
830 |
||
831 |
/** |
|
832 |
* Import: should not remove a listener during reconnection, the reconnection |
|
833 |
* needs to change the listener list and that will possibly make removal fail. |
|
834 |
*/ |
|
835 |
private synchronized void beforeRemove() throws IOException { |
|
836 |
while (beingReconnected) { |
|
837 |
if (state == TERMINATED) { |
|
838 |
throw new IOException("Terminated."); |
|
839 |
} |
|
840 |
||
841 |
try { |
|
842 |
wait(); |
|
843 |
} catch (InterruptedException ire) { |
|
844 |
IOException ioe = new IOException(ire.toString()); |
|
845 |
EnvHelp.initCause(ioe, ire); |
|
846 |
||
847 |
throw ioe; |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
if (state == TERMINATED) { |
|
852 |
throw new IOException("Terminated."); |
|
853 |
} |
|
854 |
} |
|
855 |
||
856 |
// ------------------------------------------------- |
|
857 |
// private variables |
|
858 |
// ------------------------------------------------- |
|
859 |
||
860 |
private final ClassLoader defaultClassLoader; |
|
861 |
private final Executor executor; |
|
862 |
||
863 |
private final Map<Integer, ClientListenerInfo> infoList = |
|
864 |
new HashMap<Integer, ClientListenerInfo>(); |
|
865 |
||
866 |
// notif stuff |
|
867 |
private long clientSequenceNumber = -1; |
|
868 |
private final int maxNotifications; |
|
869 |
private final long timeout; |
|
870 |
private Integer mbeanRemovedNotifID = null; |
|
871 |
private Thread currentFetchThread; |
|
872 |
||
873 |
// state |
|
874 |
/** |
|
875 |
* This state means that a thread is being created for fetching and forwarding notifications. |
|
876 |
*/ |
|
877 |
private static final int STARTING = 0; |
|
878 |
||
879 |
/** |
|
880 |
* This state tells that a thread has been started for fetching and forwarding notifications. |
|
881 |
*/ |
|
882 |
private static final int STARTED = 1; |
|
883 |
||
884 |
/** |
|
885 |
* This state means that the fetching thread is informed to stop. |
|
886 |
*/ |
|
887 |
private static final int STOPPING = 2; |
|
888 |
||
889 |
/** |
|
890 |
* This state means that the fetching thread is already stopped. |
|
891 |
*/ |
|
892 |
private static final int STOPPED = 3; |
|
893 |
||
894 |
/** |
|
895 |
* This state means that this object is terminated and no more thread will be created |
|
896 |
* for fetching notifications. |
|
897 |
*/ |
|
898 |
private static final int TERMINATED = 4; |
|
899 |
||
900 |
private int state = STOPPED; |
|
901 |
||
902 |
/** |
|
903 |
* This variable is used to tell whether a connector (RMIConnector or ClientIntermediary) |
|
904 |
* is doing reconnection. |
|
905 |
* This variable will be set to true by the method <code>preReconnection</code>, and set |
|
906 |
* to false by <code>postReconnection</code>. |
|
907 |
* When beingReconnected == true, no thread will be created for fetching notifications. |
|
908 |
*/ |
|
909 |
private boolean beingReconnected = false; |
|
910 |
||
911 |
private static final ClassLogger logger = |
|
912 |
new ClassLogger("javax.management.remote.misc", |
|
913 |
"ClientNotifForwarder"); |
|
914 |
} |