KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > AbstractMBeanServer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx;
30
31 import com.caucho.loader.Environment;
32 import com.caucho.loader.WeakCloseListener;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import javax.management.*;
37 import javax.management.loading.ClassLoaderRepository JavaDoc;
38 import java.io.ObjectInputStream JavaDoc;
39 import java.lang.reflect.Constructor JavaDoc;
40 import java.lang.reflect.InvocationTargetException JavaDoc;
41 import java.lang.reflect.Modifier JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Set JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * The main interface for retrieving and managing JMX objects.
50  */

51 abstract public class AbstractMBeanServer implements MBeanServer {
52   private static final L10N L = new L10N(AbstractMBeanServer.class);
53   private static final Logger JavaDoc log = Log.open(AbstractMBeanServer.class);
54
55   static ObjectName SERVER_DELEGATE_NAME;
56
57   // default domain
58
private String JavaDoc _defaultDomain;
59
60   /**
61    * Creats a new MBeanServer implementation.
62    */

63   public AbstractMBeanServer(String JavaDoc defaultDomain)
64   {
65     _defaultDomain = defaultDomain;
66
67     Environment.addClassLoaderListener(new WeakCloseListener(this));
68   }
69
70   /**
71    * Returns the context implementation.
72    */

73   protected MBeanContext getContext()
74   {
75     return getContext(Thread.currentThread().getContextClassLoader());
76   }
77
78   /**
79    * Returns the context implementation.
80    */

81   protected MBeanContext getExistingContext()
82   {
83     return getExistingContext(Thread.currentThread().getContextClassLoader());
84   }
85
86   /**
87    * Returns the context implementation.
88    */

89   protected MBeanContext getGlobalContext()
90   {
91     return getContext(ClassLoader.getSystemClassLoader());
92   }
93
94   /**
95    * Returns the context implementation.
96    */

97   abstract protected MBeanContext getContext(ClassLoader JavaDoc loader);
98
99   /**
100    * Returns the context implementation.
101    */

102   abstract protected MBeanContext getExistingContext(ClassLoader JavaDoc loader);
103
104   /**
105    * Removes the context implementation.
106    */

107   protected void removeContext(MBeanContext context, ClassLoader JavaDoc loader)
108   {
109   }
110
111   /**
112    * Returns the view implementation.
113    */

114   protected MBeanView getView()
115   {
116     return getContext().getView();
117   }
118
119   /**
120    * Returns the view implementation.
121    */

122   protected MBeanView getGlobalView()
123   {
124     return getGlobalContext().getView();
125   }
126
127   /**
128    * Returns the view implementation.
129    */

130   protected MBeanView getParentView()
131   {
132     return null;
133   }
134
135   /**
136    * Instantiate an MBean object to be registered with the server.
137    *
138    * @param className the className to be instantiated.
139    *
140    * @return the instantiated object.
141    */

142   public Object JavaDoc instantiate(String JavaDoc className)
143     throws ReflectionException, MBeanException
144   {
145     try {
146       Class JavaDoc cl = getClassLoaderRepository().loadClass(className);
147
148       return cl.newInstance();
149     } catch (ClassNotFoundException JavaDoc e) {
150       throw new ReflectionException(e);
151     } catch (InstantiationException JavaDoc e) {
152       throw new ReflectionException(e);
153     } catch (ExceptionInInitializerError JavaDoc e) {
154       Throwable JavaDoc cause = e.getCause();
155       if (cause instanceof Exception JavaDoc)
156         throw new MBeanException((Exception JavaDoc) cause);
157       else
158         throw e;
159     } catch (IllegalAccessException JavaDoc e) {
160       throw new ReflectionException(e);
161     }
162   }
163
164   /**
165    * Instantiate an MBean object to be registered with the server.
166    *
167    * @param className the className to be instantiated.
168    * @param loaderName names the classloader to be used
169    *
170    * @return the instantiated object.
171    */

172   public Object JavaDoc instantiate(String JavaDoc className, ObjectName loaderName)
173     throws ReflectionException, MBeanException, InstanceNotFoundException
174   {
175     MBeanWrapper mbean = getMBean(loaderName);
176
177     if (mbean == null)
178       throw new InstanceNotFoundException(String.valueOf(loaderName));
179     else if (! (mbean.getObject() instanceof ClassLoader JavaDoc))
180       throw new InstanceNotFoundException(L.l("{0} is not a class loader",
181                                               loaderName));
182
183     try {
184       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) mbean.getObject();
185
186       Class JavaDoc cl = loader.loadClass(className);
187
188       return cl.newInstance();
189     } catch (ClassNotFoundException JavaDoc e) {
190       throw new ReflectionException(e);
191     } catch (InstantiationException JavaDoc e) {
192       throw new ReflectionException(e);
193     } catch (IllegalAccessException JavaDoc e) {
194       throw new ReflectionException(e);
195     }
196   }
197
198   /**
199    * Instantiate an MBean object with the given arguments to be
200    * passed to the constructor.
201    *
202    * @param className the className to be instantiated.
203    * @param params the parameters for the constructor.
204    * @param signature the signature of the constructor
205    *
206    * @return the instantiated object.
207    */

208   public Object JavaDoc instantiate(String JavaDoc className,
209                             Object JavaDoc []params, String JavaDoc []signature)
210     throws ReflectionException, MBeanException
211   {
212     try {
213       Class JavaDoc cl = getClassLoaderRepository().loadClass(className);
214
215       Constructor JavaDoc constructor = getConstructor(cl, signature);
216
217       return constructor.newInstance(params);
218     } catch (ClassNotFoundException JavaDoc e) {
219       throw new ReflectionException(e);
220     } catch (InstantiationException JavaDoc e) {
221       throw new ReflectionException(e);
222     } catch (InvocationTargetException JavaDoc e) {
223         throw new MBeanException(e);
224     } catch (IllegalAccessException JavaDoc e) {
225       throw new ReflectionException(e);
226     }
227   }
228
229   /**
230    * Instantiate an MBean object with the given arguments to be
231    * passed to the constructor.
232    *
233    * @param className the className to be instantiated.
234    * @param loaderName names the classloader to be used
235    * @param params the parameters for the constructor.
236    * @param signature the signature of the constructor
237    *
238    * @return the instantiated object.
239    */

240   public Object JavaDoc instantiate(String JavaDoc className, ObjectName loaderName,
241                             Object JavaDoc []params, String JavaDoc []signature)
242     throws ReflectionException, MBeanException, InstanceNotFoundException
243   {
244     MBeanWrapper mbean = getMBean(loaderName);
245
246     if (mbean == null)
247       throw new InstanceNotFoundException(String.valueOf(loaderName));
248     else if (! (mbean.getObject() instanceof ClassLoader JavaDoc))
249       throw new InstanceNotFoundException(L.l("{0} is not a class loader",
250                                               loaderName));
251
252     try {
253       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) mbean.getObject();
254
255       Class JavaDoc cl = loader.loadClass(className);
256
257       Constructor JavaDoc constructor = getConstructor(cl, signature);
258
259       return constructor.newInstance(params);
260     } catch (ClassNotFoundException JavaDoc e) {
261       throw new ReflectionException(e);
262     } catch (InstantiationException JavaDoc e) {
263       throw new ReflectionException(e);
264     } catch (InvocationTargetException JavaDoc e) {
265         throw new MBeanException(e);
266     } catch (IllegalAccessException JavaDoc e) {
267       throw new ReflectionException(e);
268     }
269   }
270
271   /**
272    * Returns the class's constructor with the matching sig.
273    */

274   private Constructor JavaDoc getConstructor(Class JavaDoc cl, String JavaDoc []sig)
275   {
276     Constructor JavaDoc []constructors = cl.getConstructors();
277
278     for (int i = 0; i < constructors.length; i++) {
279       if (! Modifier.isPublic(constructors[i].getModifiers()))
280         continue;
281
282       if (isMatch(constructors[i].getParameterTypes(), sig))
283         return constructors[i];
284     }
285
286     return null;
287   }
288
289   /**
290    * Matches the parameters the sig.
291    */

292   private boolean isMatch(Class JavaDoc []param, String JavaDoc []sig)
293   {
294     if (param.length != sig.length)
295       return false;
296
297     for (int i = 0; i < param.length; i++) {
298       if (! param[i].getName().equals(sig[i]))
299         return false;
300     }
301
302     return true;
303   }
304
305   /**
306    * Instantiate and register an MBean.
307    *
308    * @param className the className to be instantiated.
309    * @param name the name of the mbean.
310    *
311    * @return the instantiated object.
312    */

313   public ObjectInstance createMBean(String JavaDoc className, ObjectName name)
314     throws ReflectionException, InstanceAlreadyExistsException,
315            MBeanException, NotCompliantMBeanException
316   {
317     return registerMBean(instantiate(className), name);
318   }
319
320
321   /**
322    * Instantiate and register an MBean.
323    *
324    * @param className the className to be instantiated.
325    * @param name the name of the mbean.
326    * @param loaderName the name of the class loader to user
327    *
328    * @return the instantiated object.
329    */

330   public ObjectInstance createMBean(String JavaDoc className, ObjectName name,
331                                     ObjectName loaderName)
332     throws ReflectionException, InstanceAlreadyExistsException,
333            MBeanException, NotCompliantMBeanException, InstanceNotFoundException
334   {
335     return registerMBean(instantiate(className, loaderName), name);
336   }
337
338   /**
339    * Instantiate and register an MBean.
340    *
341    * @param className the className to be instantiated.
342    * @param name the name of the mbean.
343    * @param params the parameters for the constructor.
344    * @param signature the signature of the constructor
345    *
346    * @return the instantiated object.
347    */

348   public ObjectInstance createMBean(String JavaDoc className, ObjectName name,
349                                     Object JavaDoc []params, String JavaDoc []signature)
350     throws ReflectionException, InstanceAlreadyExistsException,
351            MBeanException, NotCompliantMBeanException
352   {
353     return registerMBean(instantiate(className, params, signature),
354                          name);
355   }
356
357   /**
358    * Instantiate and register an MBean.
359    *
360    * @param className the className to be instantiated.
361    * @param name the name of the mbean.
362    * @param loaderName the loader name for the mbean.
363    * @param params the parameters for the constructor.
364    * @param signature the signature of the constructor
365    *
366    * @return the instantiated object.
367    */

368   public ObjectInstance createMBean(String JavaDoc className, ObjectName name,
369                                     ObjectName loaderName,
370                                     Object JavaDoc []params, String JavaDoc []signature)
371     throws ReflectionException, InstanceAlreadyExistsException,
372            MBeanException, NotCompliantMBeanException, InstanceNotFoundException
373   {
374     return registerMBean(instantiate(className, loaderName, params, signature),
375                          name);
376   }
377
378   /**
379    * Registers an MBean with the server.
380    *
381    * @param object the object to be registered as an MBean
382    * @param name the name of the mbean.
383    *
384    * @return the instantiated object.
385    */

386   public ObjectInstance registerMBean(Object JavaDoc object, ObjectName name)
387     throws InstanceAlreadyExistsException,
388            MBeanRegistrationException,
389            NotCompliantMBeanException
390   {
391     if (object == null)
392       throw new NullPointerException JavaDoc();
393
394     MBeanContext context = getContext();
395
396     if (context.getMBean(name) != null) {
397       throw new InstanceAlreadyExistsException(String.valueOf(name));
398     }
399
400     DynamicMBean dynMBean = createMBean(object, name);
401
402     if (object instanceof IntrospectionMBean)
403       object = ((IntrospectionMBean) object).getImplementation();
404     else if (object instanceof StandardMBean) {
405       object = ((StandardMBean) object).getImplementation();
406     }
407
408     MBeanWrapper mbean = new MBeanWrapper(context, name, object, dynMBean);
409
410     return context.registerMBean(mbean, name);
411   }
412
413   /**
414    * Creates the dynamic mbean.
415    */

416   private DynamicMBean createMBean(Object JavaDoc obj, ObjectName name)
417     throws NotCompliantMBeanException
418   {
419     if (obj == null)
420       throw new NotCompliantMBeanException(L.l("{0} mbean is null", name));
421     else if (obj instanceof DynamicMBean)
422       return (DynamicMBean) obj;
423
424     Class JavaDoc ifc = getMBeanInterface(obj.getClass());
425
426     if (ifc == null)
427       throw new NotCompliantMBeanException(L.l("{0} mbean has no MBean interface", name));
428
429     return new IntrospectionMBean(obj, ifc);
430   }
431
432   /**
433    * Returns the mbean interface.
434    */

435   private Class JavaDoc getMBeanInterface(Class JavaDoc cl)
436   {
437     for (; cl != null; cl = cl.getSuperclass()) {
438       Class JavaDoc []interfaces = cl.getInterfaces();
439
440       String JavaDoc mbeanName = cl.getName() + "MBean";
441       String JavaDoc mxbeanName = cl.getName() + "MXBean";
442
443       int p = mbeanName.lastIndexOf('.');
444       mbeanName = mbeanName.substring(p);
445
446       p = mxbeanName.lastIndexOf('.');
447       mxbeanName = mxbeanName.substring(p);
448
449       for (int i = 0; i < interfaces.length; i++) {
450         Class JavaDoc ifc = interfaces[i];
451
452         if (ifc.getName().endsWith(mbeanName)
453         || ifc.getName().endsWith(mxbeanName))
454           return ifc;
455       }
456     }
457
458     return null;
459   }
460
461   /**
462    * Unregisters an MBean from the server.
463    *
464    * @param name the name of the mbean.
465    */

466   public void unregisterMBean(ObjectName name)
467     throws InstanceNotFoundException,
468            MBeanRegistrationException
469   {
470     MBeanContext context = getExistingContext();
471
472     if (context != null) {
473       context.unregisterMBean(name);
474
475       log.finer("unregistered " + name);
476     }
477
478     // XXX: getDelegate().sendUnregisterNotification(name);
479
}
480
481   /**
482    * Returns the MBean registered with the given name.
483    *
484    * @param name the name of the mbean.
485    *
486    * @return the matching mbean object.
487    */

488   public ObjectInstance getObjectInstance(ObjectName name)
489     throws InstanceNotFoundException
490   {
491     MBeanWrapper mbean = getMBean(name);
492
493     if (mbean == null)
494       throw new InstanceNotFoundException(String.valueOf(name));
495
496     return mbean.getObjectInstance();
497   }
498
499   /**
500    * Returns a set of MBeans matching the query.
501    *
502    * @param name the name of the mbean to match.
503    * @param query the queryd to match.
504    *
505    * @return the set of matching mbean object.
506    */

507   public Set JavaDoc<ObjectInstance> queryMBeans(ObjectName name, QueryExp query)
508   {
509     try {
510       if (query != null) {
511         query.setMBeanServer(this);
512       }
513
514       return getView().queryMBeans(name, query);
515     } catch (Exception JavaDoc e) {
516       log.log(Level.WARNING, e.toString(), e);
517
518       return null;
519     }
520   }
521
522   /**
523    * Returns a set of names for MBeans matching the query.
524    *
525    * @param name the name of the mbean to match.
526    * @param query the query to match.
527    *
528    * @return the set of matching mbean names.
529    */

530   public Set JavaDoc<ObjectName> queryNames(ObjectName name, QueryExp query)
531   {
532     try {
533       if (query != null) {
534         query.setMBeanServer(this);
535       }
536
537       return getView().queryNames(name, query);
538     } catch (Exception JavaDoc e) {
539       log.log(Level.WARNING, e.toString(), e);
540
541       return null;
542     }
543   }
544
545   /**
546    * Tests if the name matches.
547    */

548   private boolean isMatch(ObjectName testName,
549                           ObjectName queryName,
550                           QueryExp query)
551   {
552     if (queryName == null)
553       return true;
554
555     if (! queryName.isPattern() &&
556         ! testName.getDomain().equals(queryName.getDomain()))
557       return false;
558
559     if (queryName.isPropertyPattern()) {
560       // If the queryName has a '*' in the properties, then check
561
// the queryName properties to see if they match
562

563       Hashtable JavaDoc map = queryName.getKeyPropertyList();
564       Iterator JavaDoc iter = map.keySet().iterator();
565       while (iter.hasNext()) {
566         String JavaDoc key = (String JavaDoc) iter.next();
567         String JavaDoc value = (String JavaDoc) map.get(key);
568
569         if (! value.equals(testName.getKeyProperty(key)))
570           return false;
571       }
572     }
573     else {
574       String JavaDoc testProps = testName.getCanonicalKeyPropertyListString();
575       String JavaDoc queryProps = queryName.getCanonicalKeyPropertyListString();
576
577       if (! testProps.equals(queryProps))
578         return false;
579     }
580
581     return true;
582   }
583
584   /**
585    * Returns true if the given object is registered with the server.
586    *
587    * @param name the name of the mbean to test.
588    *
589    * @return true if the object is registered.
590    */

591   public boolean isRegistered(ObjectName name)
592   {
593     return getView().getMBean(name) != null;
594   }
595
596   /**
597    * Returns the number of MBeans registered.
598    *
599    * @return the number of registered mbeans.
600    */

601   public Integer JavaDoc getMBeanCount()
602   {
603     return new Integer JavaDoc(getView().getMBeanCount());
604   }
605
606   /**
607    * Returns a specific attribute of a named MBean.
608    *
609    * @param name the name of the mbean to test
610    * @param attribute the name of the attribute to retrieve
611    *
612    * @return the attribute value
613    */

614   public Object JavaDoc getAttribute(ObjectName name, String JavaDoc attribute)
615     throws MBeanException, AttributeNotFoundException,
616            InstanceNotFoundException, ReflectionException
617   {
618     MBeanWrapper mbean = getMBean(name);
619
620     if (mbean == null) {
621       throw new InstanceNotFoundException(String.valueOf(name));
622     }
623
624     return mbean.getAttribute(attribute);
625   }
626
627   /**
628    * Returns a list of several MBean attributes.
629    *
630    * @param name the name of the mbean
631    * @param attributes the name of the attributes to retrieve
632    *
633    * @return the attribute value
634    */

635   public AttributeList getAttributes(ObjectName name, String JavaDoc []attributes)
636     throws InstanceNotFoundException, ReflectionException
637   {
638     MBeanWrapper mbean = getMBean(name);
639
640     if (mbean == null)
641       throw new InstanceNotFoundException(String.valueOf(name));
642
643     return mbean.getAttributes(attributes);
644   }
645
646   /**
647    * Sets an attribute in the MBean.
648    *
649    * @param name the name of the mbean
650    * @param attribute the name/value of the attribute to set.
651    */

652   public void setAttribute(ObjectName name, Attribute attribute)
653     throws InstanceNotFoundException, AttributeNotFoundException,
654            InvalidAttributeValueException, MBeanException, ReflectionException
655   {
656     MBeanWrapper mbean = getMBean(name);
657
658     if (mbean == null)
659       throw new InstanceNotFoundException(String.valueOf(name));
660
661     mbean.setAttribute(attribute);
662   }
663
664   /**
665    * Set an attributes in the MBean.
666    *
667    * @param name the name of the mbean
668    * @param attributes the name/value list of the attribute to set.
669    */

670   public AttributeList setAttributes(ObjectName name, AttributeList attributes)
671     throws InstanceNotFoundException, ReflectionException
672   {
673     MBeanWrapper mbean = getMBean(name);
674
675     if (mbean == null)
676       throw new InstanceNotFoundException(String.valueOf(name));
677
678     return mbean.setAttributes(attributes);
679   }
680
681   /**
682    * Invokers an operation on an MBean.
683    *
684    * @param name the name of the mbean
685    * @param operationName the name of the method to invoke
686    * @param params the parameters for the invocation
687    * @param signature the signature of the operation
688    */

689   public Object JavaDoc invoke(ObjectName name,
690                        String JavaDoc operationName,
691                        Object JavaDoc []params,
692                        String JavaDoc []signature)
693     throws InstanceNotFoundException, MBeanException, ReflectionException
694   {
695     MBeanWrapper mbean = getMBean(name);
696
697     if (mbean == null)
698       throw new InstanceNotFoundException(String.valueOf(name));
699
700     return mbean.invoke(operationName, params, signature);
701   }
702
703   /**
704    * Returns the default domain for naming the MBean
705    */

706   public String JavaDoc getDefaultDomain()
707   {
708     return _defaultDomain;
709   }
710
711   /**
712    * Adds a listener to a registered MBean
713    *
714    * @param name the name of the mbean
715    * @param listener the listener object
716    * @param filter filters events the listener is interested in
717    * @param handback context to be returned to the listener
718    */

719   public void addNotificationListener(ObjectName name,
720                                       NotificationListener listener,
721                                       NotificationFilter filter,
722                                       Object JavaDoc handback)
723     throws InstanceNotFoundException
724   {
725     MBeanWrapper mbean = getMBean(name);
726
727     if (mbean == null)
728       throw new InstanceNotFoundException(String.valueOf(name));
729
730     mbean.addNotificationListener(listener, filter, handback);
731
732     getContext().addNotificationListener(name, listener, filter, handback);
733   }
734
735   /**
736    * Adds a listener to a registered MBean
737    *
738    * @param name the name of the mbean
739    * @param listenerName the name of the listener
740    * @param filter filters events the listener is interested in
741    * @param handback context to be returned to the listener
742    */

743   public void addNotificationListener(ObjectName name,
744                                       ObjectName listenerName,
745                                       NotificationFilter filter,
746                                       Object JavaDoc handback)
747     throws InstanceNotFoundException
748   {
749     MBeanWrapper listenerMBean = getMBean(listenerName);
750
751     if (listenerMBean == null)
752       throw new InstanceNotFoundException(String.valueOf(listenerName));
753
754     NotificationListener listener = listenerMBean.getListener();
755
756     if (listener == null) {
757       IllegalArgumentException JavaDoc exn = new IllegalArgumentException JavaDoc(L.l("{0} does not implement NotificationListener.", listenerName));
758       throw new RuntimeOperationsException(exn);
759     }
760
761     addNotificationListener(name, listener, filter, handback);
762   }
763
764   /**
765    * Removes a listener from a registered MBean
766    *
767    * @param name the name of the mbean
768    * @param listener the listener object
769    */

770   public void removeNotificationListener(ObjectName name,
771                                          NotificationListener listener)
772     throws InstanceNotFoundException, ListenerNotFoundException
773   {
774     MBeanWrapper mbean = getMBean(name);
775
776     if (mbean == null)
777       throw new InstanceNotFoundException(String.valueOf(name));
778
779     mbean.removeNotificationListener(listener);
780
781     getContext().removeNotificationListener(name, listener);
782   }
783
784   /**
785    * Removes a listener from a registered MBean
786    *
787    * @param name the name of the mbean
788    * @param listenerName the name of the listener
789    */

790   public void removeNotificationListener(ObjectName name,
791                                          ObjectName listenerName)
792     throws InstanceNotFoundException, ListenerNotFoundException
793   {
794     MBeanWrapper listenerMBean = getMBean(listenerName);
795
796     if (listenerMBean == null)
797       throw new InstanceNotFoundException(String.valueOf(listenerName));
798
799     NotificationListener listener = listenerMBean.getListener();
800
801     if (listener == null) {
802       IllegalArgumentException JavaDoc exn = new IllegalArgumentException JavaDoc(L.l("{0} does not implement NotificationListener."));
803       throw new RuntimeOperationsException(exn);
804     }
805
806     removeNotificationListener(name, listener);
807   }
808
809   /**
810    * Removes a listener from a registered MBean
811    *
812    * @param name the name of the mbean
813    * @param listenerName the name of the listener
814    * @param filter the notification filter
815    * @param handback context to the listener
816    *
817    * @since JMX 1.2
818    */

819   public void removeNotificationListener(ObjectName name,
820                                          ObjectName listenerName,
821                                          NotificationFilter filter,
822                                          Object JavaDoc handback)
823     throws InstanceNotFoundException, ListenerNotFoundException
824   {
825     MBeanWrapper listenerMBean = getMBean(listenerName);
826
827     if (listenerMBean == null)
828       throw new InstanceNotFoundException(String.valueOf(listenerName));
829
830     NotificationListener listener = listenerMBean.getListener();
831
832     if (listener == null) {
833       IllegalArgumentException JavaDoc exn = new IllegalArgumentException JavaDoc(L.l("{0} does not implement NotificationListener."));
834       throw new RuntimeOperationsException(exn);
835     }
836
837     removeNotificationListener(name, listener, filter, handback);
838   }
839
840   /**
841    * Removes a listener from a registered MBean
842    *
843    * @param name the name of the mbean
844    * @param listenerName the name of the listener
845    * @param filter the notification filter
846    * @param handback context to the listener
847    *
848    * @since JMX 1.2
849    */

850   public void removeNotificationListener(ObjectName name,
851                                          NotificationListener listener,
852                                          NotificationFilter filter,
853                                          Object JavaDoc handback)
854     throws InstanceNotFoundException, ListenerNotFoundException
855   {
856     MBeanWrapper mbean = getMBean(name);
857
858     if (mbean == null)
859       throw new InstanceNotFoundException(String.valueOf(name));
860
861     getContext().removeNotificationListener(name, listener, filter, handback);
862
863     mbean.removeNotificationListener(listener, filter, handback);
864   }
865
866   /**
867    * Returns the analyzed information for an MBean
868    *
869    * @param name the name of the mbean
870    *
871    * @return the introspected information
872    */

873   public MBeanInfo getMBeanInfo(ObjectName name)
874     throws InstanceNotFoundException, IntrospectionException,
875            ReflectionException
876   {
877     MBeanWrapper mbean = getMBean(name);
878
879     if (mbean == null)
880       throw new InstanceNotFoundException(String.valueOf(name));
881
882     MBeanInfo info = mbean.getMBeanInfo();
883
884     return info;
885   }
886
887   /**
888    * Returns true if the MBean is an instance of the specified class.
889    *
890    * @param name the name of the mbean
891    * @param className the className to test.
892    *
893    * @return true if the bean is an instance
894    */

895   public boolean isInstanceOf(ObjectName name, String JavaDoc className)
896     throws InstanceNotFoundException
897   {
898     MBeanWrapper mbean = getMBean(name);
899
900     if (mbean == null)
901       throw new InstanceNotFoundException(String.valueOf(name));
902
903     Object JavaDoc obj = mbean.getObject();
904     Class JavaDoc cl = obj.getClass();
905
906     return isInstanceOf(cl, className);
907   }
908
909   private boolean isInstanceOf(Class JavaDoc cl, String JavaDoc className)
910   {
911     if (cl == null)
912       return false;
913
914     if (cl.getName().equals(className))
915       return true;
916
917     if (isInstanceOf(cl.getSuperclass(), className))
918       return true;
919
920     Class JavaDoc []ifs = cl.getInterfaces();
921     for (int i = 0; i < ifs.length; i++) {
922       if (isInstanceOf(ifs[i], className))
923         return true;
924     }
925
926     return false;
927   }
928
929   /**
930    * Returns the ClassLoader that was used for loading the MBean.
931    *
932    * @param mbeanName the name of the mbean
933    *
934    * @return the class loader
935    *
936    * @since JMX 1.2
937    */

938   public ClassLoader JavaDoc getClassLoaderFor(ObjectName name)
939     throws InstanceNotFoundException
940   {
941     MBeanWrapper mbean = getMBean(name);
942
943     if (mbean == null)
944       throw new InstanceNotFoundException(String.valueOf(name));
945
946     return mbean.getContext().getClassLoader();
947   }
948
949   /**
950    * Returns the named ClassLoader.
951    *
952    * @param loaderName the name of the class loader
953    *
954    * @return the class loader
955    *
956    * @since JMX 1.2
957    */

958   public ClassLoader JavaDoc getClassLoader(ObjectName loaderName)
959     throws InstanceNotFoundException
960   {
961     return null;
962   }
963
964   /**
965    * Returns the ClassLoaderRepository for this MBeanServer
966    *
967    * @since JMX 1.2
968    */

969   public ClassLoaderRepository JavaDoc getClassLoaderRepository()
970   {
971     return getContext().getClassLoaderRepository();
972   }
973
974   /**
975    * Deserializes a byte array in the class loader of the mbean.
976    *
977    * @param name the name of the mbean
978    * @param data the data to deserialize
979    *
980    * @return the deserialization stream
981    */

982   public ObjectInputStream JavaDoc deserialize(ObjectName name, byte []data)
983     throws InstanceNotFoundException, OperationsException
984   {
985     throw new UnsupportedOperationException JavaDoc();
986   }
987
988   /**
989    * Deserializes a byte array in the class loader of the mbean.
990    *
991    * @param className the className matches to the loader
992    * @param data the data to deserialize
993    *
994    * @return the deserialization stream
995    */

996   public ObjectInputStream JavaDoc deserialize(String JavaDoc className, byte []data)
997     throws OperationsException, ReflectionException
998   {
999     throw new UnsupportedOperationException JavaDoc();
1000  }
1001
1002  /**
1003   * Deserializes a byte array in the class loader of the mbean.
1004   *
1005   * @param className the className matches to the loader
1006   * @param loaderName the loader to use for deserialization
1007   * @param data the data to deserialize
1008   *
1009   * @return the deserialization stream
1010   */

1011  public ObjectInputStream JavaDoc deserialize(String JavaDoc className,
1012                                       ObjectName loaderName,
1013                                       byte []data)
1014    throws OperationsException, ReflectionException,
1015           InstanceNotFoundException
1016  {
1017    throw new UnsupportedOperationException JavaDoc();
1018  }
1019
1020  /**
1021   * Returns the domains for all registered MBeans
1022   *
1023   * @since JMX 1.2
1024   */

1025  public String JavaDoc []getDomains()
1026  {
1027    return getView().getDomains();
1028  }
1029
1030  /**
1031   * Finds the MBean implementation.
1032   */

1033  MBeanWrapper getMBean(ObjectName name)
1034  {
1035    return getView().getMBean(name);
1036  }
1037
1038  /**
1039   * Handles the case where a class loader is dropped.
1040   */

1041  public void destroy()
1042  {
1043    try {
1044      MBeanServerFactory.releaseMBeanServer(this);
1045    } catch (IllegalArgumentException JavaDoc e) {
1046      log.log(Level.FINEST, e.toString(), e);
1047    } catch (Throwable JavaDoc e) {
1048      log.log(Level.FINER, e.toString(), e);
1049    }
1050  }
1051
1052  /**
1053   * Returns the string form.
1054   */

1055  public String JavaDoc toString()
1056  {
1057    if (_defaultDomain != null)
1058      return "MBeanServerImpl[domain=" + _defaultDomain + "]";
1059    else
1060      return "MBeanServerImpl[]";
1061  }
1062
1063  static {
1064    try {
1065      SERVER_DELEGATE_NAME = new ObjectName("JMImplementation:type=MBeanServerDelegate");
1066    } catch (Throwable JavaDoc e) {
1067      e.printStackTrace();
1068    }
1069  }
1070}
1071
Popular Tags