KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > JMX


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.util;
10
11 import java.util.Comparator JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import javax.management.InstanceNotFoundException JavaDoc;
16 import javax.management.ListenerNotFoundException JavaDoc;
17 import javax.management.MBeanRegistrationException JavaDoc;
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.NotificationFilter JavaDoc;
21 import javax.management.NotificationListener JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.jboss.mx.util.MBeanProxy;
26 import org.jboss.mx.util.MBeanProxyCreationException;
27 import org.jboss.mx.util.ObjectNameConverter;
28
29 /**
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
31  * @version $Revision: 1.2 $
32  */

33 public final class JMX
34 {
35
36    private static final Logger log = Logger.getLogger(JMX.class);
37
38    public static ObjectName JavaDoc extend(ObjectName JavaDoc name, Properties JavaDoc keyProperties)
39    {
40       try
41       {
42          Hashtable JavaDoc table = name.getKeyPropertyList();
43          table.putAll(keyProperties);
44          return ObjectNameConverter.convert(name.getDomain(), table);
45       }
46       catch (MalformedObjectNameException JavaDoc e)
47       {
48          log.error("", e);
49          throw new RuntimeException JavaDoc();
50       }
51    }
52
53    /**
54     * ObjectName comparator based on canonical name lexicography.
55     */

56    public static Comparator JavaDoc OBJECT_NAME_COMPARATOR = new Comparator JavaDoc()
57    {
58       public int compare(Object JavaDoc o1, Object JavaDoc o2)
59       {
60          ObjectName JavaDoc n1 = (ObjectName JavaDoc) o1;
61          ObjectName JavaDoc n2 = (ObjectName JavaDoc) o2;
62          return n1.getCanonicalName().compareTo(n2.getCanonicalName());
63       }
64    };
65
66    public static void safeUnregister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
67    {
68       if (server != null)
69       {
70          if (name != null)
71          {
72             try
73             {
74                server.unregisterMBean(name);
75             }
76             catch (InstanceNotFoundException JavaDoc e)
77             {
78                log.error("MBean " + name + " nto here");
79             }
80             catch (MBeanRegistrationException JavaDoc e)
81             {
82                log.error("MBean threw an exception during unregistration", e.getTargetException());
83             }
84          }
85          else
86          {
87             log.error("Cannot unregister a null MBean");
88          }
89       }
90       else
91       {
92          log.error("Cannot unregister with a null MBeanServer");
93       }
94    }
95
96    public static Object JavaDoc getMBeanProxy(Class JavaDoc clazz, ObjectName JavaDoc name, MBeanServer JavaDoc server)
97    {
98       try
99       {
100          return MBeanProxy.get(clazz, name, server);
101       }
102       catch (MBeanProxyCreationException e)
103       {
104          log.error("Unexpected error during proxy creation", e);
105          throw new Error JavaDoc("Unexpected error during proxy creation");
106       }
107    }
108
109    public static boolean addNotificationListener(
110          MBeanServer JavaDoc server,
111          ObjectName JavaDoc name,
112          NotificationListener JavaDoc listener,
113          NotificationFilter JavaDoc filter,
114          Object JavaDoc handback)
115    {
116       try
117       {
118          server.addNotificationListener(name, listener, filter, handback);
119          return true;
120       }
121       catch (InstanceNotFoundException JavaDoc e)
122       {
123          return false;
124       }
125    }
126
127    public static boolean removeNotificationListener(
128          MBeanServer JavaDoc server,
129          ObjectName JavaDoc name,
130          NotificationListener JavaDoc listener)
131    {
132       try
133       {
134          server.removeNotificationListener(name, listener);
135          return true;
136       }
137       catch (InstanceNotFoundException JavaDoc e)
138       {
139          log.error("Cannot remove notification listener", e);
140          return false;
141       }
142       catch (ListenerNotFoundException JavaDoc e)
143       {
144          log.error("Cannot remove notification listener", e);
145          return false;
146       }
147    }
148 }
149
Popular Tags