KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > aspects > jmx > JMXIntroduction


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop.microcontainer.aspects.jmx;
23
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.NotCompliantMBeanException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.StandardMBean JavaDoc;
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 /**
36  * A JMXIntroduction.
37  *
38  * @TODO This is a very stupid implementation. It should be based on
39  * annotations but they are not there yet, either in the integration
40  * or even the basic xml parsing.
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 58387 $
43  */

44 public class JMXIntroduction implements Interceptor
45 {
46    private static final Logger log = Logger.getLogger(JMXIntroduction.class);
47    private MBeanServer JavaDoc server;
48    
49    public String JavaDoc getName()
50    {
51       return getClass().getName();
52    }
53    
54    public void setMbeanServer(MBeanServer JavaDoc server)
55    {
56       this.server = server;
57    }
58  
59    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
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 JavaDoc objectName = null;
67       if (jmx != null)
68       {
69          String JavaDoc jmxName = jmx.name();
70          if (jmxName != null && jmxName.length() > 0)
71             objectName = new ObjectName JavaDoc(jmxName);
72       }
73       
74       if (objectName == null)
75       {
76          // try to build one from the bean name
77
String JavaDoc name = (String JavaDoc) context.getName();
78          
79          if (name.contains(":"))
80          {
81             objectName = new ObjectName JavaDoc(name);
82          }
83          else
84          {
85             objectName = new ObjectName JavaDoc("test:name='" + name + "'");
86          }
87       }
88       
89       if (server == null)
90       {
91          throw new RuntimeException JavaDoc("No MBeanServer was injected");
92       }
93       
94       if ("setKernelControllerContext".equals(mi.getMethod().getName()))
95       {
96          Class JavaDoc intfClass = null;
97          boolean registerDirectly = false;
98          if (jmx != null)
99          {
100             intfClass = jmx.exposedInterface();
101             registerDirectly = jmx.registerDirectly();
102          }
103          Object JavaDoc mbean = (registerDirectly ? context.getTarget()
104                                           : new StandardMBean JavaDoc(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