|
1 /* |
|
2 * Copyright 2008 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 |
|
26 package com.sun.jmx.namespace; |
|
27 |
|
28 |
|
29 import com.sun.jmx.mbeanserver.Util; |
|
30 import java.io.IOException; |
|
31 import java.io.ObjectInputStream; |
|
32 import java.lang.reflect.UndeclaredThrowableException; |
|
33 import java.util.Collections; |
|
34 import java.util.Set; |
|
35 |
|
36 import javax.management.Attribute; |
|
37 import javax.management.AttributeList; |
|
38 import javax.management.AttributeNotFoundException; |
|
39 import javax.management.InstanceAlreadyExistsException; |
|
40 import javax.management.InstanceNotFoundException; |
|
41 import javax.management.IntrospectionException; |
|
42 import javax.management.InvalidAttributeValueException; |
|
43 import javax.management.ListenerNotFoundException; |
|
44 import javax.management.MBeanException; |
|
45 import javax.management.MBeanInfo; |
|
46 import javax.management.MBeanRegistrationException; |
|
47 import javax.management.MBeanServer; |
|
48 import javax.management.NotCompliantMBeanException; |
|
49 import javax.management.NotificationFilter; |
|
50 import javax.management.NotificationListener; |
|
51 import javax.management.ObjectInstance; |
|
52 import javax.management.ObjectName; |
|
53 import javax.management.OperationsException; |
|
54 import javax.management.QueryExp; |
|
55 import javax.management.ReflectionException; |
|
56 import javax.management.loading.ClassLoaderRepository; |
|
57 import javax.management.namespace.JMXNamespaces; |
|
58 |
|
59 /** |
|
60 * A RoutingServerProxy is an MBeanServer proxy that proxies a |
|
61 * source name space in a source MBeanServer. |
|
62 * It wraps a source MBeanServer, and rewrites routing ObjectNames. |
|
63 * It is typically use for implementing 'cd' operations, and |
|
64 * will add the source name space to routing ObjectNames at input, |
|
65 * and remove it at output. |
|
66 * <p><b> |
|
67 * This API is a Sun internal API and is subject to changes without notice. |
|
68 * </b></p> |
|
69 * |
|
70 * @since 1.7 |
|
71 */ |
|
72 public class RoutingServerProxy |
|
73 extends RoutingProxy<MBeanServer> |
|
74 implements MBeanServer { |
|
75 |
|
76 /** |
|
77 * Creates a new instance of RoutingServerProxy |
|
78 */ |
|
79 public RoutingServerProxy(MBeanServer source, |
|
80 String sourceNs) { |
|
81 this(source,sourceNs,"",false); |
|
82 } |
|
83 |
|
84 public RoutingServerProxy(MBeanServer source, |
|
85 String sourceNs, |
|
86 String targetNs, |
|
87 boolean forwardsContext) { |
|
88 super(source,sourceNs,targetNs,forwardsContext); |
|
89 } |
|
90 |
|
91 /** |
|
92 * This method is called each time an IOException is raised when |
|
93 * trying to forward an operation to the underlying |
|
94 * MBeanServerConnection, as a result of calling |
|
95 * {@link #getMBeanServerConnection()} or as a result of invoking the |
|
96 * operation on the returned connection. |
|
97 * Subclasses may redefine this method if they need to perform any |
|
98 * specific handling of IOException (logging etc...). |
|
99 * @param x The raised IOException. |
|
100 * @param method The name of the method in which the exception was |
|
101 * raised. This is one of the methods of the MBeanServer |
|
102 * interface. |
|
103 * @return A RuntimeException that should be thrown by the caller. |
|
104 * In this default implementation, this is an |
|
105 * {@link UndeclaredThrowableException} wrapping <var>x</var>. |
|
106 **/ |
|
107 protected RuntimeException handleIOException(IOException x, |
|
108 String method) { |
|
109 return Util.newRuntimeIOException(x); |
|
110 } |
|
111 |
|
112 |
|
113 //-------------------------------------------- |
|
114 //-------------------------------------------- |
|
115 // |
|
116 // Implementation of the MBeanServer interface |
|
117 // |
|
118 //-------------------------------------------- |
|
119 //-------------------------------------------- |
|
120 @Override |
|
121 public void addNotificationListener(ObjectName name, |
|
122 NotificationListener listener, |
|
123 NotificationFilter filter, |
|
124 Object handback) |
|
125 throws InstanceNotFoundException { |
|
126 try { |
|
127 super.addNotificationListener(name, listener, |
|
128 filter, handback); |
|
129 } catch (IOException x) { |
|
130 throw handleIOException(x,"addNotificationListener"); |
|
131 } |
|
132 } |
|
133 |
|
134 @Override |
|
135 public void addNotificationListener(ObjectName name, |
|
136 ObjectName listener, |
|
137 NotificationFilter filter, |
|
138 Object handback) |
|
139 throws InstanceNotFoundException { |
|
140 try { |
|
141 super.addNotificationListener(name, listener, |
|
142 filter, handback); |
|
143 } catch (IOException x) { |
|
144 throw handleIOException(x,"addNotificationListener"); |
|
145 } |
|
146 } |
|
147 |
|
148 @Override |
|
149 public ObjectInstance createMBean(String className, ObjectName name) |
|
150 throws |
|
151 ReflectionException, |
|
152 InstanceAlreadyExistsException, |
|
153 MBeanRegistrationException, |
|
154 MBeanException, |
|
155 NotCompliantMBeanException { |
|
156 try { |
|
157 return super.createMBean(className, name); |
|
158 } catch (IOException x) { |
|
159 throw handleIOException(x,"createMBean"); |
|
160 } |
|
161 } |
|
162 |
|
163 @Override |
|
164 public ObjectInstance createMBean(String className, ObjectName name, |
|
165 Object params[], String signature[]) |
|
166 throws |
|
167 ReflectionException, |
|
168 InstanceAlreadyExistsException, |
|
169 MBeanRegistrationException, |
|
170 MBeanException, |
|
171 NotCompliantMBeanException { |
|
172 try { |
|
173 return super.createMBean(className, name, |
|
174 params, signature); |
|
175 } catch (IOException x) { |
|
176 throw handleIOException(x,"createMBean"); |
|
177 } |
|
178 } |
|
179 |
|
180 @Override |
|
181 public ObjectInstance createMBean(String className, |
|
182 ObjectName name, |
|
183 ObjectName loaderName) |
|
184 throws |
|
185 ReflectionException, |
|
186 InstanceAlreadyExistsException, |
|
187 MBeanRegistrationException, |
|
188 MBeanException, |
|
189 NotCompliantMBeanException, |
|
190 InstanceNotFoundException { |
|
191 try { |
|
192 return super.createMBean(className, name, loaderName); |
|
193 } catch (IOException x) { |
|
194 throw handleIOException(x,"createMBean"); |
|
195 } |
|
196 } |
|
197 |
|
198 @Override |
|
199 public ObjectInstance createMBean(String className, |
|
200 ObjectName name, |
|
201 ObjectName loaderName, |
|
202 Object params[], |
|
203 String signature[]) |
|
204 throws |
|
205 ReflectionException, |
|
206 InstanceAlreadyExistsException, |
|
207 MBeanRegistrationException, |
|
208 MBeanException, |
|
209 NotCompliantMBeanException, |
|
210 InstanceNotFoundException { |
|
211 try { |
|
212 return super.createMBean(className, name, loaderName, |
|
213 params, signature); |
|
214 } catch (IOException x) { |
|
215 throw handleIOException(x,"createMBean"); |
|
216 } |
|
217 } |
|
218 |
|
219 /** |
|
220 * @deprecated see {@link MBeanServer#deserialize(ObjectName,byte[]) |
|
221 * MBeanServer} |
|
222 **/ |
|
223 @Deprecated |
|
224 public ObjectInputStream deserialize(ObjectName name, byte[] data) |
|
225 throws InstanceNotFoundException, OperationsException { |
|
226 final ObjectName sourceName = toSourceOrRuntime(name); |
|
227 try { |
|
228 return source().deserialize(sourceName,data); |
|
229 } catch (RuntimeException x) { |
|
230 throw makeCompliantRuntimeException(x); |
|
231 } |
|
232 } |
|
233 |
|
234 /** |
|
235 * @deprecated see {@link MBeanServer#deserialize(String,byte[]) |
|
236 * MBeanServer} |
|
237 */ |
|
238 @Deprecated |
|
239 public ObjectInputStream deserialize(String className, byte[] data) |
|
240 throws OperationsException, ReflectionException { |
|
241 try { |
|
242 return source().deserialize(className,data); |
|
243 } catch (RuntimeException x) { |
|
244 throw makeCompliantRuntimeException(x); |
|
245 } |
|
246 } |
|
247 |
|
248 /** |
|
249 * @deprecated see {@link MBeanServer#deserialize(String,ObjectName,byte[]) |
|
250 * MBeanServer} |
|
251 */ |
|
252 @Deprecated |
|
253 public ObjectInputStream deserialize(String className, |
|
254 ObjectName loaderName, |
|
255 byte[] data) |
|
256 throws |
|
257 InstanceNotFoundException, |
|
258 OperationsException, |
|
259 ReflectionException { |
|
260 try { |
|
261 return source().deserialize(className,loaderName,data); |
|
262 } catch (RuntimeException x) { |
|
263 throw makeCompliantRuntimeException(x); |
|
264 } |
|
265 } |
|
266 |
|
267 @Override |
|
268 public Object getAttribute(ObjectName name, String attribute) |
|
269 throws |
|
270 MBeanException, |
|
271 AttributeNotFoundException, |
|
272 InstanceNotFoundException, |
|
273 ReflectionException { |
|
274 try { |
|
275 return super.getAttribute(name, attribute); |
|
276 } catch (IOException x) { |
|
277 throw handleIOException(x,"getAttribute"); |
|
278 } |
|
279 } |
|
280 |
|
281 @Override |
|
282 public AttributeList getAttributes(ObjectName name, String[] attributes) |
|
283 throws InstanceNotFoundException, ReflectionException { |
|
284 try { |
|
285 return super.getAttributes(name, attributes); |
|
286 } catch (IOException x) { |
|
287 throw handleIOException(x,"getAttributes"); |
|
288 } |
|
289 } |
|
290 |
|
291 public ClassLoader getClassLoader(ObjectName loaderName) |
|
292 throws InstanceNotFoundException { |
|
293 final ObjectName sourceName = toSourceOrRuntime(loaderName); |
|
294 try { |
|
295 return source().getClassLoader(sourceName); |
|
296 } catch (RuntimeException x) { |
|
297 throw makeCompliantRuntimeException(x); |
|
298 } |
|
299 } |
|
300 |
|
301 public ClassLoader getClassLoaderFor(ObjectName mbeanName) |
|
302 throws InstanceNotFoundException { |
|
303 final ObjectName sourceName = toSourceOrRuntime(mbeanName); |
|
304 try { |
|
305 return source().getClassLoaderFor(sourceName); |
|
306 } catch (RuntimeException x) { |
|
307 throw makeCompliantRuntimeException(x); |
|
308 } |
|
309 } |
|
310 |
|
311 public ClassLoaderRepository getClassLoaderRepository() { |
|
312 try { |
|
313 return source().getClassLoaderRepository(); |
|
314 } catch (RuntimeException x) { |
|
315 throw makeCompliantRuntimeException(x); |
|
316 } |
|
317 } |
|
318 |
|
319 @Override |
|
320 public String getDefaultDomain() { |
|
321 try { |
|
322 return super.getDefaultDomain(); |
|
323 } catch (IOException x) { |
|
324 throw handleIOException(x,"getDefaultDomain"); |
|
325 } |
|
326 } |
|
327 |
|
328 @Override |
|
329 public String[] getDomains() { |
|
330 try { |
|
331 return super.getDomains(); |
|
332 } catch (IOException x) { |
|
333 throw handleIOException(x,"getDomains"); |
|
334 } |
|
335 } |
|
336 |
|
337 @Override |
|
338 public Integer getMBeanCount() { |
|
339 try { |
|
340 return super.getMBeanCount(); |
|
341 } catch (IOException x) { |
|
342 throw handleIOException(x,"getMBeanCount"); |
|
343 } |
|
344 } |
|
345 |
|
346 @Override |
|
347 public MBeanInfo getMBeanInfo(ObjectName name) |
|
348 throws |
|
349 InstanceNotFoundException, |
|
350 IntrospectionException, |
|
351 ReflectionException { |
|
352 try { |
|
353 return super.getMBeanInfo(name); |
|
354 } catch (IOException x) { |
|
355 throw handleIOException(x,"getMBeanInfo"); |
|
356 } |
|
357 } |
|
358 |
|
359 @Override |
|
360 public ObjectInstance getObjectInstance(ObjectName name) |
|
361 throws InstanceNotFoundException { |
|
362 try { |
|
363 return super.getObjectInstance(name); |
|
364 } catch (IOException x) { |
|
365 throw handleIOException(x,"getObjectInstance"); |
|
366 } |
|
367 } |
|
368 |
|
369 public Object instantiate(String className) |
|
370 throws ReflectionException, MBeanException { |
|
371 try { |
|
372 return source().instantiate(className); |
|
373 } catch (RuntimeException x) { |
|
374 throw makeCompliantRuntimeException(x); |
|
375 } |
|
376 } |
|
377 |
|
378 public Object instantiate(String className, |
|
379 Object params[], |
|
380 String signature[]) |
|
381 throws ReflectionException, MBeanException { |
|
382 try { |
|
383 return source().instantiate(className, |
|
384 params,signature); |
|
385 } catch (RuntimeException x) { |
|
386 throw makeCompliantRuntimeException(x); |
|
387 } |
|
388 } |
|
389 |
|
390 public Object instantiate(String className, ObjectName loaderName) |
|
391 throws ReflectionException, MBeanException, |
|
392 InstanceNotFoundException { |
|
393 final ObjectName srcLoaderName = toSourceOrRuntime(loaderName); |
|
394 try { |
|
395 return source().instantiate(className,srcLoaderName); |
|
396 } catch (RuntimeException x) { |
|
397 throw makeCompliantRuntimeException(x); |
|
398 } |
|
399 } |
|
400 |
|
401 public Object instantiate(String className, ObjectName loaderName, |
|
402 Object params[], String signature[]) |
|
403 throws ReflectionException, MBeanException, |
|
404 InstanceNotFoundException { |
|
405 final ObjectName srcLoaderName = toSourceOrRuntime(loaderName); |
|
406 try { |
|
407 return source().instantiate(className,srcLoaderName, |
|
408 params,signature); |
|
409 } catch (RuntimeException x) { |
|
410 throw makeCompliantRuntimeException(x); |
|
411 } |
|
412 } |
|
413 |
|
414 @Override |
|
415 public Object invoke(ObjectName name, String operationName, |
|
416 Object params[], String signature[]) |
|
417 throws |
|
418 InstanceNotFoundException, |
|
419 MBeanException, |
|
420 ReflectionException { |
|
421 try { |
|
422 return super.invoke(name,operationName,params,signature); |
|
423 } catch (IOException x) { |
|
424 throw handleIOException(x,"invoke"); |
|
425 } |
|
426 } |
|
427 |
|
428 @Override |
|
429 public boolean isInstanceOf(ObjectName name, String className) |
|
430 throws InstanceNotFoundException { |
|
431 try { |
|
432 return super.isInstanceOf(name, className); |
|
433 } catch (IOException x) { |
|
434 throw handleIOException(x,"isInstanceOf"); |
|
435 } |
|
436 } |
|
437 |
|
438 @Override |
|
439 public boolean isRegistered(ObjectName name) { |
|
440 try { |
|
441 return super.isRegistered(name); |
|
442 } catch (IOException x) { |
|
443 throw handleIOException(x,"isRegistered"); |
|
444 } |
|
445 } |
|
446 |
|
447 @Override |
|
448 public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { |
|
449 try { |
|
450 return super.queryMBeans(name, query); |
|
451 } catch (IOException x) { |
|
452 handleIOException(x,"queryMBeans"); |
|
453 return Collections.emptySet(); |
|
454 } |
|
455 } |
|
456 |
|
457 @Override |
|
458 public Set<ObjectName> queryNames(ObjectName name, QueryExp query) { |
|
459 try { |
|
460 return super.queryNames(name, query); |
|
461 } catch (IOException x) { |
|
462 handleIOException(x,"queryNames"); |
|
463 return Collections.emptySet(); |
|
464 } |
|
465 } |
|
466 |
|
467 public ObjectInstance registerMBean(Object object, ObjectName name) |
|
468 throws |
|
469 InstanceAlreadyExistsException, |
|
470 MBeanRegistrationException, |
|
471 NotCompliantMBeanException { |
|
472 final ObjectName sourceName = newSourceMBeanName(name); |
|
473 try { |
|
474 return processOutputInstance( |
|
475 source().registerMBean(object,sourceName)); |
|
476 } catch (RuntimeException x) { |
|
477 throw makeCompliantRuntimeException(x); |
|
478 } |
|
479 } |
|
480 |
|
481 @Override |
|
482 public void removeNotificationListener(ObjectName name, |
|
483 NotificationListener listener) |
|
484 throws InstanceNotFoundException, ListenerNotFoundException { |
|
485 try { |
|
486 super.removeNotificationListener(name, listener); |
|
487 } catch (IOException x) { |
|
488 throw handleIOException(x,"removeNotificationListener"); |
|
489 } |
|
490 } |
|
491 |
|
492 @Override |
|
493 public void removeNotificationListener(ObjectName name, |
|
494 NotificationListener listener, |
|
495 NotificationFilter filter, |
|
496 Object handback) |
|
497 throws InstanceNotFoundException, ListenerNotFoundException { |
|
498 try { |
|
499 super.removeNotificationListener(name, listener, |
|
500 filter, handback); |
|
501 } catch (IOException x) { |
|
502 throw handleIOException(x,"removeNotificationListener"); |
|
503 } |
|
504 } |
|
505 |
|
506 @Override |
|
507 public void removeNotificationListener(ObjectName name, |
|
508 ObjectName listener) |
|
509 throws InstanceNotFoundException, ListenerNotFoundException { |
|
510 try { |
|
511 super.removeNotificationListener(name, listener); |
|
512 } catch (IOException x) { |
|
513 throw handleIOException(x,"removeNotificationListener"); |
|
514 } |
|
515 } |
|
516 |
|
517 @Override |
|
518 public void removeNotificationListener(ObjectName name, |
|
519 ObjectName listener, |
|
520 NotificationFilter filter, |
|
521 Object handback) |
|
522 throws InstanceNotFoundException, ListenerNotFoundException { |
|
523 try { |
|
524 super.removeNotificationListener(name, listener, |
|
525 filter, handback); |
|
526 } catch (IOException x) { |
|
527 throw handleIOException(x,"removeNotificationListener"); |
|
528 } |
|
529 } |
|
530 |
|
531 @Override |
|
532 public void setAttribute(ObjectName name, Attribute attribute) |
|
533 throws |
|
534 InstanceNotFoundException, |
|
535 AttributeNotFoundException, |
|
536 InvalidAttributeValueException, |
|
537 MBeanException, |
|
538 ReflectionException { |
|
539 try { |
|
540 super.setAttribute(name, attribute); |
|
541 } catch (IOException x) { |
|
542 throw handleIOException(x,"setAttribute"); |
|
543 } |
|
544 } |
|
545 |
|
546 @Override |
|
547 public AttributeList setAttributes(ObjectName name, |
|
548 AttributeList attributes) |
|
549 throws InstanceNotFoundException, ReflectionException { |
|
550 try { |
|
551 return super.setAttributes(name, attributes); |
|
552 } catch (IOException x) { |
|
553 throw handleIOException(x,"setAttributes"); |
|
554 } |
|
555 } |
|
556 |
|
557 @Override |
|
558 public void unregisterMBean(ObjectName name) |
|
559 throws InstanceNotFoundException, MBeanRegistrationException { |
|
560 try { |
|
561 super.unregisterMBean(name); |
|
562 } catch (IOException x) { |
|
563 throw handleIOException(x,"unregisterMBean"); |
|
564 } |
|
565 } |
|
566 |
|
567 |
|
568 public static MBeanServer cd(MBeanServer source, String sourcePath) { |
|
569 if (source == null) throw new IllegalArgumentException("null"); |
|
570 if (source.getClass().equals(RoutingServerProxy.class)) { |
|
571 // cast is OK here, but findbugs complains unless we use class.cast |
|
572 final RoutingServerProxy other = |
|
573 RoutingServerProxy.class.cast(source); |
|
574 final String target = other.getTargetNamespace(); |
|
575 |
|
576 // Avoid multiple layers of serialization. |
|
577 // |
|
578 // We construct a new proxy from the original source instead of |
|
579 // stacking a new proxy on top of the old one. |
|
580 // - that is we replace |
|
581 // cd ( cd ( x, dir1), dir2); |
|
582 // by |
|
583 // cd (x, dir1//dir2); |
|
584 // |
|
585 // We can do this only when the source class is exactly |
|
586 // NamespaceServerProxy. |
|
587 // |
|
588 if (target == null || target.equals("")) { |
|
589 final String path = |
|
590 JMXNamespaces.concat(other.getSourceNamespace(), |
|
591 sourcePath); |
|
592 return new RoutingServerProxy(other.source(),path,"", |
|
593 other.forwardsContext); |
|
594 } |
|
595 // Note: we could do possibly something here - but it would involve |
|
596 // removing part of targetDir, and possibly adding |
|
597 // something to sourcePath. |
|
598 // Too complex to bother! => simply default to stacking... |
|
599 } |
|
600 return new RoutingServerProxy(source,sourcePath); |
|
601 } |
|
602 } |