1 9 package org.jboss.portal.common.util; 10 11 import java.util.Comparator ; 12 import java.util.Hashtable ; 13 import java.util.Properties ; 14 15 import javax.management.InstanceNotFoundException ; 16 import javax.management.ListenerNotFoundException ; 17 import javax.management.MBeanRegistrationException ; 18 import javax.management.MBeanServer ; 19 import javax.management.MalformedObjectNameException ; 20 import javax.management.NotificationFilter ; 21 import javax.management.NotificationListener ; 22 import javax.management.ObjectName ; 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 33 public final class JMX 34 { 35 36 private static final Logger log = Logger.getLogger(JMX.class); 37 38 public static ObjectName extend(ObjectName name, Properties keyProperties) 39 { 40 try 41 { 42 Hashtable table = name.getKeyPropertyList(); 43 table.putAll(keyProperties); 44 return ObjectNameConverter.convert(name.getDomain(), table); 45 } 46 catch (MalformedObjectNameException e) 47 { 48 log.error("", e); 49 throw new RuntimeException (); 50 } 51 } 52 53 56 public static Comparator OBJECT_NAME_COMPARATOR = new Comparator () 57 { 58 public int compare(Object o1, Object o2) 59 { 60 ObjectName n1 = (ObjectName ) o1; 61 ObjectName n2 = (ObjectName ) o2; 62 return n1.getCanonicalName().compareTo(n2.getCanonicalName()); 63 } 64 }; 65 66 public static void safeUnregister(MBeanServer server, ObjectName name) 67 { 68 if (server != null) 69 { 70 if (name != null) 71 { 72 try 73 { 74 server.unregisterMBean(name); 75 } 76 catch (InstanceNotFoundException e) 77 { 78 log.error("MBean " + name + " nto here"); 79 } 80 catch (MBeanRegistrationException 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 getMBeanProxy(Class clazz, ObjectName name, MBeanServer 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 ("Unexpected error during proxy creation"); 106 } 107 } 108 109 public static boolean addNotificationListener( 110 MBeanServer server, 111 ObjectName name, 112 NotificationListener listener, 113 NotificationFilter filter, 114 Object handback) 115 { 116 try 117 { 118 server.addNotificationListener(name, listener, filter, handback); 119 return true; 120 } 121 catch (InstanceNotFoundException e) 122 { 123 return false; 124 } 125 } 126 127 public static boolean removeNotificationListener( 128 MBeanServer server, 129 ObjectName name, 130 NotificationListener listener) 131 { 132 try 133 { 134 server.removeNotificationListener(name, listener); 135 return true; 136 } 137 catch (InstanceNotFoundException e) 138 { 139 log.error("Cannot remove notification listener", e); 140 return false; 141 } 142 catch (ListenerNotFoundException e) 143 { 144 log.error("Cannot remove notification listener", e); 145 return false; 146 } 147 } 148 } 149 | Popular Tags |