1 22 package org.jboss.aop.microcontainer.aspects.jmx; 23 24 import javax.management.MBeanServer ; 25 import javax.management.NotCompliantMBeanException ; 26 import javax.management.ObjectName ; 27 import javax.management.StandardMBean ; 28 29 import org.jboss.aop.advice.Interceptor; 30 import org.jboss.aop.joinpoint.Invocation; 31 import org.jboss.aop.joinpoint.MethodInvocation; 32 import org.jboss.kernel.spi.dependency.KernelControllerContext; 33 import org.jboss.logging.Logger; 34 35 44 public class JMXIntroduction implements Interceptor 45 { 46 private static final Logger log = Logger.getLogger(JMXIntroduction.class); 47 private MBeanServer server; 48 49 public String getName() 50 { 51 return getClass().getName(); 52 } 53 54 public void setMbeanServer(MBeanServer server) 55 { 56 this.server = server; 57 } 58 59 public Object invoke(Invocation invocation) throws Throwable 60 { 61 MethodInvocation mi = (MethodInvocation) invocation; 62 KernelControllerContext context = (KernelControllerContext) mi.getArguments()[0]; 63 64 JMX jmx = (JMX)invocation.resolveClassAnnotation(JMX.class); 65 66 ObjectName objectName = null; 67 if (jmx != null) 68 { 69 String jmxName = jmx.name(); 70 if (jmxName != null && jmxName.length() > 0) 71 objectName = new ObjectName (jmxName); 72 } 73 74 if (objectName == null) 75 { 76 String name = (String ) context.getName(); 78 79 if (name.contains(":")) 80 { 81 objectName = new ObjectName (name); 82 } 83 else 84 { 85 objectName = new ObjectName ("test:name='" + name + "'"); 86 } 87 } 88 89 if (server == null) 90 { 91 throw new RuntimeException ("No MBeanServer was injected"); 92 } 93 94 if ("setKernelControllerContext".equals(mi.getMethod().getName())) 95 { 96 Class intfClass = null; 97 boolean registerDirectly = false; 98 if (jmx != null) 99 { 100 intfClass = jmx.exposedInterface(); 101 registerDirectly = jmx.registerDirectly(); 102 } 103 Object mbean = (registerDirectly ? context.getTarget() 104 : new StandardMBean (context.getTarget(), intfClass)); 105 server.registerMBean(mbean, objectName); 106 log.info("Registered MBean " + objectName); 107 } 108 else 109 { 110 log.info("Unregistering MBean " + objectName); 111 server.unregisterMBean(objectName); 112 } 113 114 return null; 115 } 116 } 117 | Popular Tags |