author | dfuchs |
Thu, 04 Sep 2008 14:46:36 +0200 | |
changeset 1156 | bbc2d15aaf7a |
parent 1004 | 5ba8217eb504 |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
1004 | 1 |
/* |
2 |
* Copyright 2007 Sun Microsystems, Inc. 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
* |
|
25 |
* @since JMX 2.0 |
|
26 |
*/ |
|
27 |
||
28 |
package javax.management.event; |
|
29 |
||
30 |
import java.io.IOException; |
|
31 |
import java.lang.reflect.Method; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.Collection; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.UUID; |
|
36 |
||
37 |
import javax.management.InstanceNotFoundException; |
|
38 |
import javax.management.ListenerNotFoundException; |
|
39 |
import javax.management.Notification; |
|
40 |
import javax.management.NotificationFilter; |
|
41 |
import javax.management.NotificationListener; |
|
42 |
import javax.management.ObjectName; |
|
43 |
import javax.management.remote.NotificationResult; |
|
44 |
import com.sun.jmx.event.EventBuffer; |
|
45 |
import com.sun.jmx.event.LeaseManager; |
|
46 |
import com.sun.jmx.interceptor.SingleMBeanForwarder; |
|
47 |
import com.sun.jmx.mbeanserver.Util; |
|
48 |
import com.sun.jmx.remote.util.ClassLogger; |
|
49 |
import java.lang.ref.WeakReference; |
|
50 |
import java.lang.reflect.Constructor; |
|
51 |
import java.lang.reflect.InvocationHandler; |
|
52 |
import java.lang.reflect.Proxy; |
|
53 |
import java.security.AccessControlContext; |
|
54 |
import java.security.AccessController; |
|
55 |
import java.security.PrivilegedAction; |
|
56 |
import java.security.PrivilegedExceptionAction; |
|
57 |
import java.util.Arrays; |
|
58 |
import java.util.Map; |
|
59 |
import java.util.Set; |
|
60 |
import java.util.WeakHashMap; |
|
61 |
import java.util.concurrent.ConcurrentHashMap; |
|
62 |
import java.util.concurrent.atomic.AtomicInteger; |
|
63 |
import javax.management.DynamicMBean; |
|
64 |
import javax.management.MBeanException; |
|
65 |
import javax.management.MBeanPermission; |
|
66 |
import javax.management.MBeanServer; |
|
67 |
import javax.management.MBeanServerConnection; |
|
68 |
import javax.management.MBeanServerDelegate; |
|
69 |
import javax.management.MBeanServerInvocationHandler; |
|
70 |
import javax.management.MBeanServerNotification; |
|
71 |
import javax.management.ObjectInstance; |
|
72 |
import javax.management.StandardMBean; |
|
73 |
import javax.management.remote.MBeanServerForwarder; |
|
74 |
||
75 |
/** |
|
76 |
* This is the default implementation of the MBean |
|
77 |
* {@link EventClientDelegateMBean}. |
|
78 |
*/ |
|
79 |
public class EventClientDelegate implements EventClientDelegateMBean { |
|
80 |
||
81 |
private EventClientDelegate(MBeanServer server) { |
|
82 |
if (server == null) { |
|
83 |
throw new NullPointerException("Null MBeanServer."); |
|
84 |
} |
|
85 |
||
86 |
if (logger.traceOn()) { |
|
87 |
logger.trace("EventClientDelegate", "new one"); |
|
88 |
} |
|
89 |
mbeanServer = server; |
|
90 |
eventSubscriber = EventSubscriber.getEventSubscriber(mbeanServer); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Returns an {@code EventClientDelegate} instance for the given |
|
95 |
* {@code MBeanServer}. Calling this method more than once with the same |
|
96 |
* {@code server} argument may return the same object or a different object |
|
97 |
* each time. See {@link EventClientDelegateMBean} for an example use of |
|
98 |
* this method. |
|
99 |
* |
|
100 |
* @param server An MBean server instance to work with. |
|
101 |
* @return An {@code EventClientDelegate} instance. |
|
102 |
* @throws NullPointerException If {@code server} is null. |
|
103 |
*/ |
|
104 |
public static EventClientDelegate getEventClientDelegate(MBeanServer server) { |
|
105 |
EventClientDelegate delegate = null; |
|
106 |
synchronized(delegateMap) { |
|
107 |
final WeakReference wrf = delegateMap.get(server); |
|
108 |
delegate = (wrf == null) ? null : (EventClientDelegate)wrf.get(); |
|
109 |
||
110 |
if (delegate == null) { |
|
111 |
delegate = new EventClientDelegate(server); |
|
112 |
try { |
|
113 |
// TODO: this may not work with federated MBean, because |
|
114 |
// the delegate will *not* emit notifications for those MBeans. |
|
115 |
delegate.mbeanServer.addNotificationListener( |
|
116 |
MBeanServerDelegate.DELEGATE_NAME, |
|
117 |
delegate.cleanListener, null, null); |
|
118 |
} catch (InstanceNotFoundException e) { |
|
119 |
logger.fine( |
|
120 |
"getEventClientDelegate", |
|
121 |
"Could not add MBeanServerDelegate listener", e); |
|
122 |
} |
|
123 |
delegateMap.put(server, |
|
124 |
new WeakReference<EventClientDelegate>(delegate)); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
return delegate; |
|
129 |
} |
|
130 |
||
131 |
// Logic for the MBeanServerForwarder that simulates the existence of the |
|
132 |
// EventClientDelegate MBean. Things are complicated by the fact that |
|
133 |
// there may not be anything in the chain after this forwarder when it is |
|
134 |
// created - the connection to a real MBeanServer might only come later. |
|
135 |
// Recall that there are two ways of creating a JMXConnectorServer - |
|
136 |
// either you specify its MBeanServer when you create it, or you specify |
|
137 |
// no MBeanServer and register it in an MBeanServer later. In the latter |
|
138 |
// case, the forwarder chain points nowhere until this registration |
|
139 |
// happens. Since EventClientDelegate wants to add a listener to the |
|
140 |
// MBeanServerDelegate, we can't create an EventClientDelegate until |
|
141 |
// there is an MBeanServer. So the forwarder initially has |
|
142 |
// a dummy ECD where every method throws an exception, and |
|
143 |
// the real ECD is created as soon as doing so does not produce an |
|
144 |
// exception. |
|
145 |
// TODO: rewrite so that the switch from the dummy to the real ECD happens |
|
146 |
// just before we would otherwise have thrown UnsupportedOperationException. |
|
147 |
// This is more correct, because it's not guaranteed that we will see the |
|
148 |
// moment where the real MBeanServer is attached, if it happens by virtue |
|
149 |
// of a setMBeanServer on some other forwarder later in the chain. |
|
150 |
||
151 |
private static class Forwarder extends SingleMBeanForwarder { |
|
152 |
||
153 |
private static class UnsupportedInvocationHandler |
|
154 |
implements InvocationHandler { |
|
155 |
public Object invoke(Object proxy, Method method, Object[] args) |
|
156 |
throws Throwable { |
|
157 |
throw new UnsupportedOperationException( |
|
158 |
"EventClientDelegate unavailable: no MBeanServer, or " + |
|
159 |
"MBeanServer inaccessible"); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
private static DynamicMBean makeUnsupportedECD() { |
|
164 |
EventClientDelegateMBean unsupported = (EventClientDelegateMBean) |
|
165 |
Proxy.newProxyInstance( |
|
166 |
EventClientDelegateMBean.class.getClassLoader(), |
|
167 |
new Class<?>[] {EventClientDelegateMBean.class}, |
|
168 |
new UnsupportedInvocationHandler()); |
|
169 |
return new StandardMBean( |
|
170 |
unsupported, EventClientDelegateMBean.class, false); |
|
171 |
} |
|
172 |
||
173 |
private volatile boolean madeECD; |
|
174 |
||
175 |
Forwarder() { |
|
176 |
super(OBJECT_NAME, makeUnsupportedECD()); |
|
177 |
} |
|
178 |
||
179 |
@Override |
|
180 |
public synchronized void setMBeanServer(final MBeanServer mbs) { |
|
181 |
super.setMBeanServer(mbs); |
|
182 |
||
183 |
if (!madeECD) { |
|
184 |
try { |
|
185 |
EventClientDelegate ecd = |
|
186 |
AccessController.doPrivileged( |
|
187 |
new PrivilegedAction<EventClientDelegate>() { |
|
188 |
public EventClientDelegate run() { |
|
189 |
return getEventClientDelegate(Forwarder.this); |
|
190 |
} |
|
191 |
}); |
|
192 |
DynamicMBean mbean = new StandardMBean( |
|
193 |
ecd, EventClientDelegateMBean.class, false); |
|
194 |
setSingleMBean(mbean); |
|
195 |
madeECD = true; |
|
196 |
} catch (Exception e) { |
|
197 |
// OK: assume no MBeanServer |
|
198 |
logger.fine("setMBeanServer", "isRegistered", e); |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* <p>Create a new {@link MBeanServerForwarder} that simulates the existence |
|
206 |
* of an {@code EventClientDelegateMBean} with the {@linkplain |
|
207 |
* #OBJECT_NAME default name}. This forwarder intercepts MBean requests |
|
208 |
* that are targeted for that MBean and handles them itself. All other |
|
209 |
* requests are forwarded to the next element in the forwarder chain.</p> |
|
210 |
* |
|
211 |
* @return a new {@code MBeanServerForwarder} that simulates the existence |
|
212 |
* of an {@code EventClientDelegateMBean}. |
|
213 |
*/ |
|
214 |
public static MBeanServerForwarder newForwarder() { |
|
215 |
return new Forwarder(); |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Returns a proxy of the default {@code EventClientDelegateMBean}. |
|
220 |
* |
|
221 |
* @param conn An {@link MBeanServerConnection} to work with. |
|
222 |
*/ |
|
223 |
@SuppressWarnings("cast") // cast for jdk 1.5 |
|
224 |
public static EventClientDelegateMBean getProxy(MBeanServerConnection conn) { |
|
225 |
return (EventClientDelegateMBean)MBeanServerInvocationHandler. |
|
226 |
newProxyInstance(conn, |
|
227 |
OBJECT_NAME, |
|
228 |
EventClientDelegateMBean.class, |
|
229 |
false); |
|
230 |
} |
|
231 |
||
232 |
public String addClient(String className, Object[] params, String[] sig) |
|
233 |
throws MBeanException { |
|
234 |
return addClient(className, null, params, sig, true); |
|
235 |
} |
|
236 |
||
237 |
public String addClient(String className, |
|
238 |
ObjectName classLoader, |
|
239 |
Object[] params, |
|
240 |
String[] sig) throws MBeanException { |
|
241 |
return addClient(className, classLoader, params, sig, false); |
|
242 |
} |
|
243 |
||
244 |
private String addClient(String className, |
|
245 |
ObjectName classLoader, |
|
246 |
Object[] params, |
|
247 |
String[] sig, |
|
248 |
boolean classLoaderRepository) throws MBeanException { |
|
249 |
try { |
|
250 |
return addClientX( |
|
251 |
className, classLoader, params, sig, classLoaderRepository); |
|
252 |
} catch (RuntimeException e) { |
|
253 |
throw e; |
|
254 |
} catch (Exception e) { |
|
255 |
throw new MBeanException(e); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
private String addClientX(String className, |
|
260 |
ObjectName classLoader, |
|
261 |
Object[] params, |
|
262 |
String[] sig, |
|
263 |
boolean classLoaderRepository) throws Exception { |
|
264 |
if (className == null) { |
|
265 |
throw new IllegalArgumentException("Null class name."); |
|
266 |
} |
|
267 |
||
268 |
final Object o; |
|
269 |
||
270 |
// The special treatment of standard EventForwarders is so that no |
|
271 |
// special permissions are necessary to use them. Otherwise you |
|
272 |
// couldn't use EventClient if you didn't have permission to call |
|
273 |
// MBeanServer.instantiate. We do require that permission for |
|
274 |
// non-standard forwarders, because otherwise you could instantiate |
|
275 |
// any class with possibly adverse consequences. We also avoid using |
|
276 |
// MBeanInstantiator because it looks up constructors by loading each |
|
277 |
// class in the sig array, which means a remote user could cause any |
|
278 |
// class to be loaded. That's probably not hugely risky but still. |
|
279 |
if (className.startsWith("javax.management.event.")) { |
|
280 |
Class<?> c = Class.forName( |
|
281 |
className, false, this.getClass().getClassLoader()); |
|
282 |
Constructor<?> foundCons = null; |
|
283 |
if (sig == null) |
|
284 |
sig = new String[0]; |
|
285 |
for (Constructor cons : c.getConstructors()) { |
|
286 |
Class<?>[] types = cons.getParameterTypes(); |
|
287 |
String[] consSig = new String[types.length]; |
|
288 |
for (int i = 0; i < types.length; i++) |
|
289 |
consSig[i] = types[i].getName(); |
|
290 |
if (Arrays.equals(sig, consSig)) { |
|
291 |
foundCons = cons; |
|
292 |
break; |
|
293 |
} |
|
294 |
} |
|
295 |
if (foundCons == null) { |
|
296 |
throw new NoSuchMethodException( |
|
297 |
"Constructor for " + className + " with argument types " + |
|
298 |
Arrays.toString(sig)); |
|
299 |
} |
|
300 |
o = foundCons.newInstance(params); |
|
301 |
} else if (classLoaderRepository) { |
|
302 |
o = mbeanServer.instantiate(className, params, sig); |
|
303 |
} else { |
|
304 |
o = mbeanServer.instantiate(className, classLoader, params, sig); |
|
305 |
} |
|
306 |
||
307 |
if (!(o instanceof EventForwarder)) { |
|
308 |
throw new IllegalArgumentException( |
|
309 |
className+" is not an EventForwarder class."); |
|
310 |
} |
|
311 |
||
312 |
final EventForwarder forwarder = (EventForwarder)o; |
|
313 |
final String clientId = UUID.randomUUID().toString(); |
|
314 |
ClientInfo clientInfo = new ClientInfo(clientId, forwarder); |
|
315 |
||
316 |
clientInfoMap.put(clientId, clientInfo); |
|
317 |
||
318 |
forwarder.setClientId(clientId); |
|
319 |
||
320 |
if (logger.traceOn()) { |
|
321 |
logger.trace("addClient", clientId); |
|
322 |
} |
|
323 |
||
324 |
return clientId; |
|
325 |
} |
|
326 |
||
327 |
public Integer[] getListenerIds(String clientId) |
|
328 |
throws IOException, EventClientNotFoundException { |
|
329 |
ClientInfo clientInfo = getClientInfo(clientId); |
|
330 |
||
331 |
if (clientInfo == null) { |
|
332 |
throw new EventClientNotFoundException("The client is not found."); |
|
333 |
} |
|
334 |
||
335 |
Map<Integer, AddedListener> listenerInfoMap = clientInfo.listenerInfoMap; |
|
336 |
synchronized (listenerInfoMap) { |
|
337 |
Set<Integer> ids = listenerInfoMap.keySet(); |
|
338 |
return ids.toArray(new Integer[ids.size()]); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* {@inheritDoc} |
|
344 |
* |
|
345 |
* <p>The execution of this method includes a call to |
|
346 |
* {@link MBeanServer#addNotificationListener(ObjectName, |
|
347 |
* NotificationListener, NotificationFilter, Object)}.</p> |
|
348 |
*/ |
|
349 |
public Integer addListener(String clientId, |
|
350 |
final ObjectName name, |
|
351 |
NotificationFilter filter) |
|
352 |
throws EventClientNotFoundException, InstanceNotFoundException { |
|
353 |
||
354 |
if (logger.traceOn()) { |
|
355 |
logger.trace("addListener", ""); |
|
356 |
} |
|
357 |
||
358 |
return getClientInfo(clientId).addListenerInfo(name, filter); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* {@inheritDoc} |
|
363 |
* |
|
364 |
* <p>The execution of this method can include call to |
|
365 |
* {@link MBeanServer#removeNotificationListener(ObjectName, |
|
366 |
* NotificationListener, NotificationFilter, Object)}.</p> |
|
367 |
*/ |
|
368 |
public void removeListenerOrSubscriber(String clientId, Integer listenerId) |
|
369 |
throws InstanceNotFoundException, |
|
370 |
ListenerNotFoundException, |
|
371 |
EventClientNotFoundException, |
|
372 |
IOException { |
|
373 |
if (logger.traceOn()) { |
|
374 |
logger.trace("removeListener", ""+listenerId); |
|
375 |
} |
|
376 |
getClientInfo(clientId).removeListenerInfo(listenerId); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* {@inheritDoc} |
|
381 |
* |
|
382 |
* <p>The execution of this method includes a call to |
|
383 |
* {@link MBeanServer#addNotificationListener(ObjectName, |
|
384 |
* NotificationListener, NotificationFilter, Object)} for |
|
385 |
* every MBean matching {@code name}. If {@code name} is |
|
386 |
* an {@code ObjectName} pattern, then the execution of this |
|
387 |
* method will include a call to {@link MBeanServer#queryNames}.</p> |
|
388 |
*/ |
|
389 |
public Integer addSubscriber(String clientId, ObjectName name, |
|
390 |
NotificationFilter filter) |
|
391 |
throws EventClientNotFoundException, IOException { |
|
392 |
if (logger.traceOn()) { |
|
393 |
logger.trace("addSubscriber", ""); |
|
394 |
} |
|
395 |
return getClientInfo(clientId).subscribeListenerInfo(name, filter); |
|
396 |
} |
|
397 |
||
398 |
public NotificationResult fetchNotifications(String clientId, |
|
399 |
long startSequenceNumber, |
|
400 |
int maxNotifs, |
|
401 |
long timeout) |
|
402 |
throws EventClientNotFoundException { |
|
403 |
if (logger.traceOn()) { |
|
404 |
logger.trace("fetchNotifications", "for "+clientId); |
|
405 |
} |
|
406 |
return getClientInfo(clientId).fetchNotifications(startSequenceNumber, |
|
407 |
maxNotifs, |
|
408 |
timeout); |
|
409 |
} |
|
410 |
||
411 |
public void removeClient(String clientId) |
|
412 |
throws EventClientNotFoundException { |
|
413 |
if (clientId == null) |
|
414 |
throw new EventClientNotFoundException("Null clientId"); |
|
415 |
if (logger.traceOn()) { |
|
416 |
logger.trace("removeClient", clientId); |
|
417 |
} |
|
418 |
ClientInfo ci = null; |
|
419 |
ci = clientInfoMap.remove(clientId); |
|
420 |
||
421 |
if (ci == null) { |
|
422 |
throw new EventClientNotFoundException("clientId is "+clientId); |
|
423 |
} else { |
|
424 |
ci.clean(); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
public long lease(String clientId, long timeout) |
|
429 |
throws IOException, EventClientNotFoundException { |
|
430 |
if (logger.traceOn()) { |
|
431 |
logger.trace("lease", "for "+clientId); |
|
432 |
} |
|
433 |
return getClientInfo(clientId).lease(timeout); |
|
434 |
} |
|
435 |
||
436 |
// ------------------------------------ |
|
437 |
// private classes |
|
438 |
// ------------------------------------ |
|
439 |
private class ClientInfo { |
|
440 |
String clientId; |
|
441 |
EventBuffer buffer; |
|
442 |
NotificationListener clientListener; |
|
443 |
Map<Integer, AddedListener> listenerInfoMap = |
|
444 |
new HashMap<Integer, AddedListener>(); |
|
445 |
||
446 |
ClientInfo(String clientId, EventForwarder forwarder) { |
|
447 |
this.clientId = clientId; |
|
448 |
this.forwarder = forwarder; |
|
449 |
clientListener = |
|
450 |
new ForwardingClientListener(listenerInfoMap, forwarder); |
|
451 |
} |
|
452 |
||
453 |
Integer addOrSubscribeListenerInfo( |
|
454 |
ObjectName name, NotificationFilter filter, boolean subscribe) |
|
455 |
throws InstanceNotFoundException, IOException { |
|
456 |
||
457 |
final Integer listenerId = nextListenerId(); |
|
458 |
AddedListener listenerInfo = new AddedListener( |
|
459 |
listenerId, filter, name, subscribe); |
|
460 |
if (subscribe) { |
|
461 |
eventSubscriber.subscribe(name, |
|
462 |
clientListener, |
|
463 |
filter, |
|
464 |
listenerInfo); |
|
465 |
} else { |
|
466 |
mbeanServer.addNotificationListener(name, |
|
467 |
clientListener, |
|
468 |
filter, |
|
469 |
listenerInfo); |
|
470 |
} |
|
471 |
||
472 |
synchronized(listenerInfoMap) { |
|
473 |
listenerInfoMap.put(listenerId, listenerInfo); |
|
474 |
} |
|
475 |
||
476 |
return listenerId; |
|
477 |
} |
|
478 |
||
479 |
Integer addListenerInfo(ObjectName name, |
|
480 |
NotificationFilter filter) throws InstanceNotFoundException { |
|
481 |
try { |
|
482 |
return addOrSubscribeListenerInfo(name, filter, false); |
|
483 |
} catch (IOException e) { // can't happen |
|
484 |
logger.warning( |
|
485 |
"EventClientDelegate.addListenerInfo", |
|
486 |
"unexpected exception", e); |
|
487 |
throw new RuntimeException(e); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
Integer subscribeListenerInfo(ObjectName name, |
|
492 |
NotificationFilter filter) throws IOException { |
|
493 |
try { |
|
494 |
return addOrSubscribeListenerInfo(name, filter, true); |
|
495 |
} catch (InstanceNotFoundException e) { // can't happen |
|
496 |
logger.warning( |
|
497 |
"EventClientDelegate.subscribeListenerInfo", |
|
498 |
"unexpected exception", e); |
|
499 |
throw new RuntimeException(e); |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
private final AtomicInteger nextListenerId = new AtomicInteger(); |
|
504 |
||
505 |
private Integer nextListenerId() { |
|
506 |
return nextListenerId.getAndIncrement(); |
|
507 |
} |
|
508 |
||
509 |
NotificationResult fetchNotifications(long startSequenceNumber, |
|
510 |
int maxNotifs, |
|
511 |
long timeout) { |
|
512 |
||
513 |
if (!(forwarder instanceof FetchingEventForwarder)) { |
|
514 |
throw new IllegalArgumentException( |
|
515 |
"This client is using Event Postal Service!"); |
|
516 |
} |
|
517 |
||
518 |
return ((FetchingEventForwarder)forwarder). |
|
519 |
fetchNotifications(startSequenceNumber, |
|
520 |
maxNotifs, timeout); |
|
521 |
} |
|
522 |
||
523 |
void removeListenerInfo(Integer listenerId) |
|
524 |
throws InstanceNotFoundException, ListenerNotFoundException, IOException { |
|
525 |
AddedListener listenerInfo; |
|
526 |
synchronized(listenerInfoMap) { |
|
527 |
listenerInfo = listenerInfoMap.remove(listenerId); |
|
528 |
} |
|
529 |
||
530 |
if (listenerInfo == null) { |
|
531 |
throw new ListenerNotFoundException("The listener is not found."); |
|
532 |
} |
|
533 |
||
534 |
if (listenerInfo.subscription) { |
|
535 |
eventSubscriber.unsubscribe(listenerInfo.name, |
|
536 |
clientListener); |
|
537 |
} else { |
|
538 |
mbeanServer.removeNotificationListener(listenerInfo.name, |
|
539 |
clientListener, |
|
540 |
listenerInfo.filter, |
|
541 |
listenerInfo); |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
void clean(ObjectName name) { |
|
546 |
synchronized(listenerInfoMap) { |
|
547 |
for (Map.Entry<Integer, AddedListener> entry : |
|
548 |
listenerInfoMap.entrySet()) { |
|
549 |
AddedListener li = entry.getValue(); |
|
550 |
if (name.equals(li.name)) { |
|
551 |
listenerInfoMap.remove(entry.getKey()); |
|
552 |
} |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
void clean() { |
|
558 |
synchronized(listenerInfoMap) { |
|
559 |
for (AddedListener li : listenerInfoMap.values()) { |
|
560 |
try { |
|
561 |
mbeanServer.removeNotificationListener(li.name, |
|
562 |
clientListener); |
|
563 |
} catch (Exception e) { |
|
564 |
logger.trace("ClientInfo.clean", "removeNL", e); |
|
565 |
} |
|
566 |
} |
|
567 |
listenerInfoMap.clear(); |
|
568 |
} |
|
569 |
||
570 |
try { |
|
571 |
forwarder.close(); |
|
572 |
} catch (Exception e) { |
|
573 |
logger.trace( |
|
574 |
"ClientInfo.clean", "forwarder.close", e); |
|
575 |
} |
|
576 |
||
577 |
if (leaseManager != null) { |
|
578 |
leaseManager.stop(); |
|
579 |
} |
|
580 |
} |
|
581 |
||
582 |
long lease(long timeout) { |
|
583 |
return leaseManager.lease(timeout); |
|
584 |
} |
|
585 |
||
586 |
@Override |
|
587 |
public boolean equals(Object o) { |
|
588 |
if (this == o) { |
|
589 |
return true; |
|
590 |
} |
|
591 |
||
592 |
if (o instanceof ClientInfo && |
|
593 |
clientId.equals(((ClientInfo)o).clientId)) { |
|
594 |
return true; |
|
595 |
} |
|
596 |
||
597 |
return false; |
|
598 |
} |
|
599 |
||
600 |
@Override |
|
601 |
public int hashCode() { |
|
602 |
return clientId.hashCode(); |
|
603 |
} |
|
604 |
||
605 |
private EventForwarder forwarder = null; |
|
606 |
||
607 |
private final Runnable leaseExpiryCallback = new Runnable() { |
|
608 |
public void run() { |
|
609 |
try { |
|
610 |
removeClient(clientId); |
|
611 |
} catch (Exception e) { |
|
612 |
logger.trace( |
|
613 |
"ClientInfo.leaseExpiryCallback", "removeClient", e); |
|
614 |
} |
|
615 |
} |
|
616 |
}; |
|
617 |
||
618 |
private LeaseManager leaseManager = new LeaseManager(leaseExpiryCallback); |
|
619 |
} |
|
620 |
||
621 |
private class ForwardingClientListener implements NotificationListener { |
|
622 |
public ForwardingClientListener(Map<Integer, AddedListener> listenerInfoMap, |
|
623 |
EventForwarder forwarder) { |
|
624 |
this.listenerInfoMap = listenerInfoMap; |
|
625 |
this.forwarder = forwarder; |
|
626 |
} |
|
627 |
||
628 |
public void handleNotification(Notification n, Object o) { |
|
629 |
if (n == null || (!(o instanceof AddedListener))) { |
|
630 |
if (logger.traceOn()) { |
|
631 |
logger.trace("ForwardingClientListener-handleNotification", |
|
632 |
"received a unknown notif"); |
|
633 |
} |
|
634 |
return; |
|
635 |
} |
|
636 |
||
637 |
AddedListener li = (AddedListener) o; |
|
638 |
||
639 |
if (checkListenerPermission(li.name,li.acc)) { |
|
640 |
try { |
|
641 |
forwarder.forward(n, li.listenerId); |
|
642 |
} catch (Exception e) { |
|
643 |
if (logger.traceOn()) { |
|
644 |
logger.trace( |
|
645 |
"ForwardingClientListener-handleNotification", |
|
646 |
"forwarding failed.", e); |
|
647 |
} |
|
648 |
} |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
private final Map<Integer, AddedListener> listenerInfoMap; |
|
653 |
private final EventForwarder forwarder; |
|
654 |
} |
|
655 |
||
656 |
private class AddedListener { |
|
657 |
final int listenerId; |
|
658 |
final NotificationFilter filter; |
|
659 |
final ObjectName name; |
|
660 |
final boolean subscription; |
|
661 |
final AccessControlContext acc; |
|
662 |
||
663 |
public AddedListener( |
|
664 |
int listenerId, |
|
665 |
NotificationFilter filter, |
|
666 |
ObjectName name, |
|
667 |
boolean subscription) { |
|
668 |
this.listenerId = listenerId; |
|
669 |
this.filter = filter; |
|
670 |
this.name = name; |
|
671 |
this.subscription = subscription; |
|
672 |
acc = AccessController.getContext(); |
|
673 |
} |
|
674 |
} |
|
675 |
||
676 |
private class CleanListener implements NotificationListener { |
|
677 |
public void handleNotification(Notification notification, |
|
678 |
Object handback) { |
|
679 |
if (notification instanceof MBeanServerNotification) { |
|
680 |
if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals( |
|
681 |
notification.getType())) { |
|
682 |
final ObjectName name = |
|
683 |
((MBeanServerNotification)notification).getMBeanName(); |
|
684 |
||
685 |
final Collection <ClientInfo> list = |
|
686 |
Collections.unmodifiableCollection(clientInfoMap.values()); |
|
687 |
||
688 |
for (ClientInfo ci : list) { |
|
689 |
ci.clean(name); |
|
690 |
} |
|
691 |
} |
|
692 |
||
693 |
} |
|
694 |
} |
|
695 |
} |
|
696 |
||
697 |
// ------------------------------------------------- |
|
698 |
// private method |
|
699 |
// ------------------------------------------------- |
|
700 |
private ClientInfo getClientInfo(String clientId) |
|
701 |
throws EventClientNotFoundException { |
|
702 |
ClientInfo clientInfo = null; |
|
703 |
clientInfo = clientInfoMap.get(clientId); |
|
704 |
||
705 |
if (clientInfo == null) { |
|
706 |
throw new EventClientNotFoundException("The client is not found."); |
|
707 |
} |
|
708 |
||
709 |
return clientInfo; |
|
710 |
} |
|
711 |
||
712 |
/** |
|
713 |
* Explicitly check the MBeanPermission for |
|
714 |
* the current access control context. |
|
715 |
*/ |
|
716 |
private boolean checkListenerPermission(final ObjectName name, |
|
717 |
final AccessControlContext acc) { |
|
718 |
if (logger.traceOn()) { |
|
719 |
logger.trace("checkListenerPermission", ""); |
|
720 |
} |
|
721 |
SecurityManager sm = System.getSecurityManager(); |
|
722 |
if (sm != null) { |
|
723 |
try { |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
724 |
final String serverName = getMBeanServerName(); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
725 |
|
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
726 |
ObjectInstance oi = (ObjectInstance) |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
727 |
AccessController.doPrivileged( |
1004 | 728 |
new PrivilegedExceptionAction<Object>() { |
729 |
public Object run() |
|
730 |
throws InstanceNotFoundException { |
|
731 |
return mbeanServer.getObjectInstance(name); |
|
732 |
} |
|
733 |
}); |
|
734 |
||
735 |
String classname = oi.getClassName(); |
|
736 |
MBeanPermission perm = new MBeanPermission( |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
737 |
serverName, |
1004 | 738 |
classname, |
739 |
null, |
|
740 |
name, |
|
741 |
"addNotificationListener"); |
|
742 |
sm.checkPermission(perm, acc); |
|
743 |
} catch (Exception e) { |
|
744 |
if (logger.debugOn()) { |
|
745 |
logger.debug("checkListenerPermission", "refused.", e); |
|
746 |
} |
|
747 |
return false; |
|
748 |
} |
|
749 |
} |
|
750 |
return true; |
|
751 |
} |
|
752 |
||
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
753 |
private String getMBeanServerName() { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
754 |
if (mbeanServerName != null) return mbeanServerName; |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
755 |
else return (mbeanServerName = getMBeanServerName(mbeanServer)); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
756 |
} |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
757 |
|
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
758 |
private static String getMBeanServerName(final MBeanServer server) { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
759 |
final PrivilegedAction<String> action = new PrivilegedAction<String>() { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
760 |
public String run() { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
761 |
return Util.getMBeanServerSecurityName(server); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
762 |
} |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
763 |
}; |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
764 |
return AccessController.doPrivileged(action); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
765 |
} |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
1004
diff
changeset
|
766 |
|
1004 | 767 |
// ------------------------------------ |
768 |
// private variables |
|
769 |
// ------------------------------------ |
|
770 |
private final MBeanServer mbeanServer; |
|
771 |
private volatile String mbeanServerName = null; |
|
772 |
private Map<String, ClientInfo> clientInfoMap = |
|
773 |
new ConcurrentHashMap<String, ClientInfo>(); |
|
774 |
||
775 |
private final CleanListener cleanListener = new CleanListener(); |
|
776 |
private final EventSubscriber eventSubscriber; |
|
777 |
||
778 |
private static final ClassLogger logger = |
|
779 |
new ClassLogger("javax.management.event", "EventClientDelegate"); |
|
780 |
||
781 |
private static final |
|
782 |
Map<MBeanServer, WeakReference<EventClientDelegate>> delegateMap = |
|
783 |
new WeakHashMap<MBeanServer, WeakReference<EventClientDelegate>>(); |
|
784 |
} |