KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > examples > configuration > MBeanConfigurator


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.jmx.examples.configuration;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.system.ServiceMBeanSupport;
26
27 import javax.management.*;
28 import java.io.Serializable JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
32  */

33 public class MBeanConfigurator extends ServiceMBeanSupport implements MBeanConfiguratorMBean, NotificationListener
34 {
35
36    private static final Logger log = Logger.getLogger(MBeanConfigurator.class.getName());
37
38    /**
39     * Use the short class name as the default for the service name.
40     */

41    public String JavaDoc getName()
42    {
43       return "MBeanConfigurator";
44    }
45
46    public void startService()
47    {
48       try
49       {
50          log.debug("Starting MBeanConfigurator service.");
51          ObjectName mbeanserver = new ObjectName("JMImplementation:type=MBeanServerDelegate");
52          RegistrationNotificationFilter mbeanServerNotificationFilter = new RegistrationNotificationFilter();
53          getServer().addNotificationListener(mbeanserver, this, mbeanServerNotificationFilter, null);
54       }
55       catch (Exception JavaDoc e)
56       {
57          log.error("Could not start MBeanConfigurator service.", e);
58       }
59    }
60
61
62    public void mbeanRegistered(ObjectName objectName)
63          throws ReflectionException, InstanceNotFoundException, MBeanException, MalformedObjectNameException
64    {
65       log.debug("MBean " + objectName + " registered. Will see if any attribute bindings present.");
66       // Hard coded for now. We need someplace for standard service names...
67
ObjectName serviceBindingMgr = new ObjectName("jboss.system:service=ServiceBindingManager");
68       Object JavaDoc[] args = {objectName};
69       String JavaDoc[] sig = {"javax.management.ObjectName"};
70       MBeanServer server = getServer();
71       if(server != null)
72       {
73          server.invoke(serviceBindingMgr, "applyServiceConfig", args, sig);
74       }
75    }
76
77    /**
78     * Callback method from the broadcaster MBean this listener implementation
79     * is registered to.
80     *
81     * @param notification the notification object
82     * @param handback the handback object given to the broadcaster
83     * upon listener registration
84     */

85    public void handleNotification(Notification notification, Object JavaDoc handback)
86    {
87       //Should be a mbean registration notification due to RegistrationNotificationFilter being used.
88
if (notification instanceof MBeanServerNotification)
89       {
90          if (notification.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
91          {
92             MBeanServerNotification serverNotification = (MBeanServerNotification) notification;
93             try
94             {
95                mbeanRegistered(serverNotification.getMBeanName());
96             }
97             catch (Exception JavaDoc e)
98             {
99                log.error("Error configuring mbean " + serverNotification.getMBeanName(), e);
100             }
101          }
102       }
103
104    }
105
106    public class RegistrationNotificationFilter implements NotificationFilter, Serializable JavaDoc
107    {
108
109       /**
110        * This method is called before a notification is sent to see whether
111        * the listener wants the notification.
112        *
113        * @param notification the notification to be sent.
114        * @return true if the listener wants the notification, false otherwise
115        */

116       public boolean isNotificationEnabled(Notification notification)
117       {
118          boolean processNotification = false;
119          if (notification instanceof MBeanServerNotification)
120          {
121             if (notification.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
122             {
123                processNotification = true;
124             }
125          }
126          return processNotification;
127       }
128    }
129 }
Popular Tags