author | sjiang |
Thu, 31 Jul 2008 15:31:13 +0200 | |
changeset 1004 | 5ba8217eb504 |
parent 715 | f16baef3a20e |
child 1156 | bbc2d15aaf7a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. 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 |
|
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 |
||
26 |
package com.sun.jmx.remote.internal; |
|
27 |
||
1004 | 28 |
import com.sun.jmx.mbeanserver.Util; |
2 | 29 |
import com.sun.jmx.remote.security.NotificationAccessController; |
30 |
import com.sun.jmx.remote.util.ClassLogger; |
|
31 |
import com.sun.jmx.remote.util.EnvHelp; |
|
32 |
import java.io.IOException; |
|
33 |
import java.security.AccessControlContext; |
|
34 |
import java.security.AccessController; |
|
1004 | 35 |
import java.security.PrivilegedAction; |
2 | 36 |
import java.security.PrivilegedActionException; |
37 |
import java.security.PrivilegedExceptionAction; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.HashMap; |
|
40 |
import java.util.HashSet; |
|
41 |
import java.util.List; |
|
42 |
import java.util.Map; |
|
43 |
import java.util.Set; |
|
44 |
import javax.management.InstanceNotFoundException; |
|
45 |
import javax.management.ListenerNotFoundException; |
|
46 |
import javax.management.MBeanPermission; |
|
47 |
import javax.management.MBeanServer; |
|
48 |
import javax.management.Notification; |
|
49 |
import javax.management.NotificationBroadcaster; |
|
50 |
import javax.management.NotificationFilter; |
|
51 |
import javax.management.ObjectInstance; |
|
52 |
import javax.management.ObjectName; |
|
53 |
import javax.management.remote.NotificationResult; |
|
54 |
import javax.management.remote.TargetedNotification; |
|
55 |
import javax.management.MalformedObjectNameException; |
|
56 |
import javax.security.auth.Subject; |
|
57 |
||
58 |
public class ServerNotifForwarder { |
|
59 |
||
60 |
public ServerNotifForwarder(MBeanServer mbeanServer, |
|
61 |
Map env, |
|
62 |
NotificationBuffer notifBuffer, |
|
63 |
String connectionId) { |
|
64 |
this.mbeanServer = mbeanServer; |
|
65 |
this.notifBuffer = notifBuffer; |
|
66 |
this.connectionId = connectionId; |
|
67 |
connectionTimeout = EnvHelp.getServerConnectionTimeout(env); |
|
68 |
checkNotificationEmission = EnvHelp.computeBooleanFromString( |
|
69 |
env, |
|
1004 | 70 |
"jmx.remote.x.check.notification.emission",false); |
71 |
notificationAccessController = |
|
72 |
EnvHelp.getNotificationAccessController(env); |
|
2 | 73 |
} |
74 |
||
75 |
public Integer addNotificationListener(final ObjectName name, |
|
76 |
final NotificationFilter filter) |
|
77 |
throws InstanceNotFoundException, IOException { |
|
78 |
||
79 |
if (logger.traceOn()) { |
|
80 |
logger.trace("addNotificationListener", |
|
81 |
"Add a listener at " + name); |
|
82 |
} |
|
83 |
||
84 |
checkState(); |
|
85 |
||
86 |
// Explicitly check MBeanPermission for addNotificationListener |
|
87 |
// |
|
88 |
checkMBeanPermission(name, "addNotificationListener"); |
|
89 |
if (notificationAccessController != null) { |
|
90 |
notificationAccessController.addNotificationListener( |
|
1004 | 91 |
connectionId, name, getSubject()); |
2 | 92 |
} |
93 |
try { |
|
94 |
boolean instanceOf = |
|
95 |
AccessController.doPrivileged( |
|
96 |
new PrivilegedExceptionAction<Boolean>() { |
|
97 |
public Boolean run() throws InstanceNotFoundException { |
|
98 |
return mbeanServer.isInstanceOf(name, broadcasterClass); |
|
99 |
} |
|
100 |
}); |
|
101 |
if (!instanceOf) { |
|
102 |
throw new IllegalArgumentException("The specified MBean [" + |
|
103 |
name + "] is not a " + |
|
104 |
"NotificationBroadcaster " + |
|
105 |
"object."); |
|
106 |
} |
|
107 |
} catch (PrivilegedActionException e) { |
|
108 |
throw (InstanceNotFoundException) extractException(e); |
|
109 |
} |
|
110 |
||
111 |
final Integer id = getListenerID(); |
|
112 |
||
113 |
// 6238731: set the default domain if no domain is set. |
|
114 |
ObjectName nn = name; |
|
115 |
if (name.getDomain() == null || name.getDomain().equals("")) { |
|
116 |
try { |
|
117 |
nn = ObjectName.getInstance(mbeanServer.getDefaultDomain(), |
|
118 |
name.getKeyPropertyList()); |
|
119 |
} catch (MalformedObjectNameException mfoe) { |
|
120 |
// impossible, but... |
|
121 |
IOException ioe = new IOException(mfoe.getMessage()); |
|
122 |
ioe.initCause(mfoe); |
|
123 |
throw ioe; |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
synchronized (listenerMap) { |
|
128 |
IdAndFilter idaf = new IdAndFilter(id, filter); |
|
129 |
Set<IdAndFilter> set = listenerMap.get(nn); |
|
130 |
// Tread carefully because if set.size() == 1 it may be the |
|
131 |
// Collections.singleton we make here, which is unmodifiable. |
|
132 |
if (set == null) |
|
133 |
set = Collections.singleton(idaf); |
|
134 |
else { |
|
135 |
if (set.size() == 1) |
|
136 |
set = new HashSet<IdAndFilter>(set); |
|
137 |
set.add(idaf); |
|
138 |
} |
|
139 |
listenerMap.put(nn, set); |
|
140 |
} |
|
141 |
||
142 |
return id; |
|
143 |
} |
|
144 |
||
145 |
public void removeNotificationListener(ObjectName name, |
|
146 |
Integer[] listenerIDs) |
|
147 |
throws Exception { |
|
148 |
||
149 |
if (logger.traceOn()) { |
|
150 |
logger.trace("removeNotificationListener", |
|
151 |
"Remove some listeners from " + name); |
|
152 |
} |
|
153 |
||
154 |
checkState(); |
|
155 |
||
156 |
// Explicitly check MBeanPermission for removeNotificationListener |
|
157 |
// |
|
158 |
checkMBeanPermission(name, "removeNotificationListener"); |
|
159 |
if (notificationAccessController != null) { |
|
160 |
notificationAccessController.removeNotificationListener( |
|
1004 | 161 |
connectionId, name, getSubject()); |
2 | 162 |
} |
163 |
||
164 |
Exception re = null; |
|
165 |
for (int i = 0 ; i < listenerIDs.length ; i++) { |
|
166 |
try { |
|
167 |
removeNotificationListener(name, listenerIDs[i]); |
|
168 |
} catch (Exception e) { |
|
169 |
// Give back the first exception |
|
170 |
// |
|
171 |
if (re != null) { |
|
172 |
re = e; |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
if (re != null) { |
|
177 |
throw re; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
public void removeNotificationListener(ObjectName name, Integer listenerID) |
|
182 |
throws |
|
183 |
InstanceNotFoundException, |
|
184 |
ListenerNotFoundException, |
|
185 |
IOException { |
|
186 |
||
187 |
if (logger.traceOn()) { |
|
188 |
logger.trace("removeNotificationListener", |
|
189 |
"Remove the listener " + listenerID + " from " + name); |
|
190 |
} |
|
191 |
||
192 |
checkState(); |
|
193 |
||
194 |
if (name != null && !name.isPattern()) { |
|
195 |
if (!mbeanServer.isRegistered(name)) { |
|
196 |
throw new InstanceNotFoundException("The MBean " + name + |
|
197 |
" is not registered."); |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
synchronized (listenerMap) { |
|
202 |
// Tread carefully because if set.size() == 1 it may be a |
|
203 |
// Collections.singleton, which is unmodifiable. |
|
204 |
Set<IdAndFilter> set = listenerMap.get(name); |
|
205 |
IdAndFilter idaf = new IdAndFilter(listenerID, null); |
|
206 |
if (set == null || !set.contains(idaf)) |
|
207 |
throw new ListenerNotFoundException("Listener not found"); |
|
208 |
if (set.size() == 1) |
|
209 |
listenerMap.remove(name); |
|
210 |
else |
|
211 |
set.remove(idaf); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
/* This is the object that will apply our filtering to candidate |
|
216 |
* notifications. First of all, if there are no listeners for the |
|
217 |
* ObjectName that the notification is coming from, we go no further. |
|
218 |
* Then, for each listener, we must apply the corresponding filter (if any) |
|
219 |
* and ignore the listener if the filter rejects. Finally, we apply |
|
220 |
* some access checks which may also reject the listener. |
|
221 |
* |
|
222 |
* A given notification may trigger several listeners on the same MBean, |
|
223 |
* which is why listenerMap is a Map<ObjectName, Set<IdAndFilter>> and |
|
224 |
* why we add the found notifications to a supplied List rather than |
|
225 |
* just returning a boolean. |
|
226 |
*/ |
|
227 |
private final NotificationBufferFilter bufferFilter = |
|
228 |
new NotificationBufferFilter() { |
|
229 |
public void apply(List<TargetedNotification> targetedNotifs, |
|
230 |
ObjectName source, Notification notif) { |
|
231 |
// We proceed in two stages here, to avoid holding the listenerMap |
|
232 |
// lock while invoking the filters (which are user code). |
|
233 |
final IdAndFilter[] candidates; |
|
234 |
synchronized (listenerMap) { |
|
235 |
final Set<IdAndFilter> set = listenerMap.get(source); |
|
236 |
if (set == null) { |
|
237 |
logger.debug("bufferFilter", "no listeners for this name"); |
|
238 |
return; |
|
239 |
} |
|
240 |
candidates = new IdAndFilter[set.size()]; |
|
241 |
set.toArray(candidates); |
|
242 |
} |
|
243 |
// We don't synchronize on targetedNotifs, because it is a local |
|
244 |
// variable of our caller and no other thread can see it. |
|
245 |
for (IdAndFilter idaf : candidates) { |
|
246 |
final NotificationFilter nf = idaf.getFilter(); |
|
247 |
if (nf == null || nf.isNotificationEnabled(notif)) { |
|
248 |
logger.debug("bufferFilter", "filter matches"); |
|
249 |
final TargetedNotification tn = |
|
250 |
new TargetedNotification(notif, idaf.getId()); |
|
251 |
if (allowNotificationEmission(source, tn)) |
|
252 |
targetedNotifs.add(tn); |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
}; |
|
257 |
||
258 |
public NotificationResult fetchNotifs(long startSequenceNumber, |
|
259 |
long timeout, |
|
260 |
int maxNotifications) { |
|
261 |
if (logger.traceOn()) { |
|
262 |
logger.trace("fetchNotifs", "Fetching notifications, the " + |
|
263 |
"startSequenceNumber is " + startSequenceNumber + |
|
264 |
", the timeout is " + timeout + |
|
265 |
", the maxNotifications is " + maxNotifications); |
|
266 |
} |
|
267 |
||
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
268 |
NotificationResult nr; |
2 | 269 |
final long t = Math.min(connectionTimeout, timeout); |
270 |
try { |
|
271 |
nr = notifBuffer.fetchNotifications(bufferFilter, |
|
272 |
startSequenceNumber, |
|
273 |
t, maxNotifications); |
|
274 |
} catch (InterruptedException ire) { |
|
275 |
nr = new NotificationResult(0L, 0L, new TargetedNotification[0]); |
|
276 |
} |
|
277 |
||
278 |
if (logger.traceOn()) { |
|
279 |
logger.trace("fetchNotifs", "Forwarding the notifs: "+nr); |
|
280 |
} |
|
281 |
||
282 |
return nr; |
|
283 |
} |
|
284 |
||
285 |
public void terminate() { |
|
286 |
if (logger.traceOn()) { |
|
287 |
logger.trace("terminate", "Be called."); |
|
288 |
} |
|
289 |
||
290 |
synchronized(terminationLock) { |
|
291 |
if (terminated) { |
|
292 |
return; |
|
293 |
} |
|
294 |
||
295 |
terminated = true; |
|
296 |
||
297 |
synchronized(listenerMap) { |
|
298 |
listenerMap.clear(); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
if (logger.traceOn()) { |
|
303 |
logger.trace("terminate", "Terminated."); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
//---------------- |
|
308 |
// PRIVATE METHODS |
|
309 |
//---------------- |
|
310 |
||
1004 | 311 |
private Subject getSubject() { |
312 |
return Subject.getSubject(AccessController.getContext()); |
|
313 |
} |
|
314 |
||
2 | 315 |
private void checkState() throws IOException { |
316 |
synchronized(terminationLock) { |
|
317 |
if (terminated) { |
|
318 |
throw new IOException("The connection has been terminated."); |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
private Integer getListenerID() { |
|
324 |
synchronized(listenerCounterLock) { |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
325 |
return listenerCounter++; |
2 | 326 |
} |
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Explicitly check the MBeanPermission for |
|
331 |
* the current access control context. |
|
332 |
*/ |
|
333 |
private void checkMBeanPermission(final ObjectName name, |
|
334 |
final String actions) |
|
1004 | 335 |
throws InstanceNotFoundException, SecurityException { |
336 |
checkMBeanPermission(mbeanServer, name, actions); |
|
337 |
} |
|
338 |
||
339 |
public static void checkMBeanPermission( |
|
340 |
final MBeanServer mbs, final ObjectName name, final String actions) |
|
341 |
throws InstanceNotFoundException, SecurityException { |
|
2 | 342 |
SecurityManager sm = System.getSecurityManager(); |
343 |
if (sm != null) { |
|
344 |
AccessControlContext acc = AccessController.getContext(); |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
345 |
ObjectInstance oi; |
2 | 346 |
try { |
347 |
oi = AccessController.doPrivileged( |
|
348 |
new PrivilegedExceptionAction<ObjectInstance>() { |
|
349 |
public ObjectInstance run() |
|
350 |
throws InstanceNotFoundException { |
|
1004 | 351 |
return mbs.getObjectInstance(name); |
2 | 352 |
} |
353 |
}); |
|
354 |
} catch (PrivilegedActionException e) { |
|
355 |
throw (InstanceNotFoundException) extractException(e); |
|
356 |
} |
|
357 |
String classname = oi.getClassName(); |
|
358 |
MBeanPermission perm = new MBeanPermission(classname, |
|
359 |
null, |
|
360 |
name, |
|
361 |
actions); |
|
362 |
sm.checkPermission(perm, acc); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Check if the caller has the right to get the following notifications. |
|
368 |
*/ |
|
369 |
private boolean allowNotificationEmission(ObjectName name, |
|
370 |
TargetedNotification tn) { |
|
371 |
try { |
|
372 |
if (checkNotificationEmission) { |
|
1004 | 373 |
checkMBeanPermission( |
374 |
name, "addNotificationListener"); |
|
2 | 375 |
} |
376 |
if (notificationAccessController != null) { |
|
377 |
notificationAccessController.fetchNotification( |
|
1004 | 378 |
connectionId, name, tn.getNotification(), getSubject()); |
2 | 379 |
} |
380 |
return true; |
|
381 |
} catch (SecurityException e) { |
|
382 |
if (logger.debugOn()) { |
|
383 |
logger.debug("fetchNotifs", "Notification " + |
|
384 |
tn.getNotification() + " not forwarded: the " + |
|
385 |
"caller didn't have the required access rights"); |
|
386 |
} |
|
387 |
return false; |
|
388 |
} catch (Exception e) { |
|
389 |
if (logger.debugOn()) { |
|
390 |
logger.debug("fetchNotifs", "Notification " + |
|
391 |
tn.getNotification() + " not forwarded: " + |
|
392 |
"got an unexpected exception: " + e); |
|
393 |
} |
|
394 |
return false; |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Iterate until we extract the real exception |
|
400 |
* from a stack of PrivilegedActionExceptions. |
|
401 |
*/ |
|
402 |
private static Exception extractException(Exception e) { |
|
403 |
while (e instanceof PrivilegedActionException) { |
|
404 |
e = ((PrivilegedActionException)e).getException(); |
|
405 |
} |
|
406 |
return e; |
|
407 |
} |
|
408 |
||
409 |
private static class IdAndFilter { |
|
410 |
private Integer id; |
|
411 |
private NotificationFilter filter; |
|
412 |
||
413 |
IdAndFilter(Integer id, NotificationFilter filter) { |
|
414 |
this.id = id; |
|
415 |
this.filter = filter; |
|
416 |
} |
|
417 |
||
418 |
Integer getId() { |
|
419 |
return this.id; |
|
420 |
} |
|
421 |
||
422 |
NotificationFilter getFilter() { |
|
423 |
return this.filter; |
|
424 |
} |
|
425 |
||
426 |
public int hashCode() { |
|
427 |
return id.hashCode(); |
|
428 |
} |
|
429 |
||
430 |
public boolean equals(Object o) { |
|
431 |
return ((o instanceof IdAndFilter) && |
|
432 |
((IdAndFilter) o).getId().equals(getId())); |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
//------------------ |
|
437 |
// PRIVATE VARIABLES |
|
438 |
//------------------ |
|
439 |
||
440 |
private MBeanServer mbeanServer; |
|
441 |
||
442 |
private final String connectionId; |
|
443 |
||
444 |
private final long connectionTimeout; |
|
445 |
||
446 |
private static int listenerCounter = 0; |
|
447 |
private final static int[] listenerCounterLock = new int[0]; |
|
448 |
||
449 |
private NotificationBuffer notifBuffer; |
|
450 |
private Map<ObjectName, Set<IdAndFilter>> listenerMap = |
|
451 |
new HashMap<ObjectName, Set<IdAndFilter>>(); |
|
452 |
||
453 |
private boolean terminated = false; |
|
454 |
private final int[] terminationLock = new int[0]; |
|
455 |
||
456 |
static final String broadcasterClass = |
|
457 |
NotificationBroadcaster.class.getName(); |
|
458 |
||
459 |
private final boolean checkNotificationEmission; |
|
460 |
||
461 |
private final NotificationAccessController notificationAccessController; |
|
462 |
||
463 |
private static final ClassLogger logger = |
|
464 |
new ClassLogger("javax.management.remote.misc", "ServerNotifForwarder"); |
|
465 |
} |