1 22 package org.jboss.mx.util; 23 24 import java.lang.reflect.Proxy ; 25 26 import javax.management.DynamicMBean ; 27 import javax.management.InstanceAlreadyExistsException ; 28 import javax.management.MBeanException ; 29 import javax.management.MBeanRegistrationException ; 30 import javax.management.MBeanServer ; 31 import javax.management.MBeanServerFactory ; 32 import javax.management.NotCompliantMBeanException ; 33 import javax.management.ObjectName ; 34 import javax.management.ReflectionException ; 35 36 44 public class MBeanProxy 45 { 46 47 49 60 public static Object get(Class intrface, ObjectName name, String agentID) throws MBeanProxyCreationException 61 { 62 return get(intrface, name, (MBeanServer )MBeanServerFactory.findMBeanServer(agentID).get(0)); 63 } 64 65 76 public static Object get(Class intrface, ObjectName name, MBeanServer server) throws MBeanProxyCreationException 77 { 78 return get(new Class [] { intrface, ProxyContext.class, DynamicMBean .class }, name, server); 79 } 80 81 83 public static Object get(ObjectName name, MBeanServer server) throws MBeanProxyCreationException 84 { 85 return get(new Class [] { ProxyContext.class, DynamicMBean .class }, name, server); 86 } 87 88 private static Object get(Class [] interfaces, ObjectName name, MBeanServer server) throws MBeanProxyCreationException 89 { 90 return Proxy.newProxyInstance( 91 Thread.currentThread().getContextClassLoader(), 92 interfaces, new JMXInvocationHandler(server, name) 93 ); 94 } 95 96 108 public static Object create(Class instance, Class intrface, ObjectName name, String agentID) throws MBeanProxyCreationException 109 { 110 return create(instance, intrface, name, (MBeanServer )MBeanServerFactory.findMBeanServer(agentID).get(0)); 111 } 112 113 123 public static Object create(Class instance, Class intrface, ObjectName name, MBeanServer server) throws MBeanProxyCreationException 124 { 125 try 126 { 127 server.createMBean(instance.getName(), name); 128 return get(intrface, name, server); 129 } 130 catch (ReflectionException e) { 131 throw new MBeanProxyCreationException("Creating the MBean failed: " + e.toString()); 132 } 133 catch (InstanceAlreadyExistsException e) { 134 throw new MBeanProxyCreationException("Instance already exists: " + name); 135 } 136 catch (MBeanRegistrationException e) { 137 throw new MBeanProxyCreationException("Error registering the MBean to the server: " + e.toString()); 138 } 139 catch (MBeanException e) { 140 throw new MBeanProxyCreationException(e.toString()); 141 } 142 catch (NotCompliantMBeanException e) { 143 throw new MBeanProxyCreationException("Not a compliant MBean " + instance.getClass().getName() + ": " + e.toString()); 144 } 145 } 146 147 } 148 149 150 151 | Popular Tags |