KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > management > jmx > JMXManagedComponentManager


1 package org.objectweb.celtix.bus.management.jmx;
2
3
4
5 import java.io.IOException JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.management.InstanceAlreadyExistsException JavaDoc;
10 import javax.management.InstanceNotFoundException JavaDoc;
11 import javax.management.JMException JavaDoc;
12 import javax.management.MBeanException JavaDoc;
13 import javax.management.MBeanRegistrationException JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.MBeanServerFactory JavaDoc;
16
17
18 import javax.management.NotCompliantMBeanException JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.ReflectionException JavaDoc;
21 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
22 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
23 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
24
25 import org.objectweb.celtix.Bus;
26 import org.objectweb.celtix.BusEvent;
27 import org.objectweb.celtix.BusException;
28 import org.objectweb.celtix.bus.instrumentation.MBServerPolicyType;
29 import org.objectweb.celtix.bus.management.InstrumentationEventFilter;
30 import org.objectweb.celtix.bus.management.InstrumentationEventListener;
31 import org.objectweb.celtix.bus.management.jmx.export.runtime.ModelMBeanAssembler;
32 import org.objectweb.celtix.common.logging.LogUtils;
33 import org.objectweb.celtix.management.Instrumentation;
34
35
36 /***
37  * The manager class for the JMXManagedComponent which host the JMXManagedComponent
38  * It implemenated the ManagementEventListener for the managed component register and unregister
39  */

40
41
42 public class JMXManagedComponentManager implements InstrumentationEventListener {
43     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(JMXManagedComponentManager.class);
44    
45     private boolean platformMBeanServer;
46     private InstrumentationEventFilter meFilter;
47     private ModelMBeanAssembler mbAssembler;
48     private MBServerConnectorFactory mcf;
49     private Bus bus;
50     private String JavaDoc busID;
51     private MBeanServer JavaDoc mbs;
52     
53     public JMXManagedComponentManager(Bus b) {
54         bus = b;
55         busID = bus.getBusID();
56         meFilter = new InstrumentationEventFilter();
57         mbAssembler = new ModelMBeanAssembler();
58         
59        
60     }
61     
62        
63     public void init(MBServerPolicyType mbpt) {
64         // get the init information from configuration
65

66         if (LOG.isLoggable(Level.INFO)) {
67             LOG.info("Setting up MBeanServer ");
68         }
69         
70         mbs = MBeanServerFactory.createMBeanServer(JMXUtils.DOMAIN_STRING);
71         mcf = MBServerConnectorFactory.getInstance();
72         mcf.setMBeanServer(mbs);
73         mcf.setThreaded(mbpt.getJMXConnector().isThreaded());
74         mcf.setDaemon(mbpt.getJMXConnector().isDaemon());
75         mcf.setServiceUrl(mbpt.getJMXConnector().getJMXServiceURL());
76         try {
77             mcf.createConnector();
78         } catch (IOException JavaDoc ex) {
79             LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG", new Object JavaDoc[]{ex});
80         }
81         
82     }
83     
84     public MBeanServer JavaDoc getMBeanServer() {
85         return mbs;
86     }
87     
88     public void shutdown() {
89         if (!platformMBeanServer) {
90             try {
91                 mcf.destroy();
92             } catch (IOException JavaDoc ex) {
93                 LOG.log(Level.SEVERE, "STOP_CONNECTOR_FAILURE_MSG", new Object JavaDoc[]{ex});
94             }
95         }
96     }
97     
98     public InstrumentationEventFilter getManagementEventFilter() {
99         return meFilter;
100     }
101    
102     public void registerMBean(Object JavaDoc object, ObjectName JavaDoc name) {
103         try {
104             onRegister(name);
105             mbs.registerMBean(object, name);
106         } catch (InstanceAlreadyExistsException JavaDoc e) {
107             //LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object[]{name, e});
108
//regist the new object instance
109
try {
110                 mbs.unregisterMBean(name);
111                 mbs.registerMBean(object, name);
112             } catch (Exception JavaDoc e1) {
113                 LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object JavaDoc[]{name, e1});
114             }
115         } catch (MBeanRegistrationException JavaDoc e) {
116             LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object JavaDoc[]{name, e});
117         } catch (NotCompliantMBeanException JavaDoc e) {
118             LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object JavaDoc[]{name, e});
119         }
120     }
121     
122     public void unregisterMBean(ObjectName JavaDoc name) {
123         
124         try {
125             onUnregister(name);
126             mbs.unregisterMBean(name);
127         } catch (JMException JavaDoc e) {
128             LOG.log(Level.SEVERE, "UNREGISTER_FAILURE_MSG", new Object JavaDoc[]{name, e});
129         }
130     }
131      
132     
133     protected void onRegister(ObjectName JavaDoc objectName) {
134         
135     }
136     
137     protected void onUnregister(ObjectName JavaDoc objectName) {
138         if (LOG.isLoggable(Level.INFO)) {
139             LOG.info("unregistered the object to MBserver"
140                                + objectName);
141         }
142     }
143     
144     
145     // find out the related JMX managed component do register and unregister things
146

147     public void processEvent(BusEvent event) throws BusException {
148         if (meFilter.isEventEnabled(event)) {
149             Instrumentation instrumentation = (Instrumentation)event.getSource();
150                 
151             if (meFilter.isCreateEvent(event)) {
152                     
153                 ModelMBeanInfo JavaDoc mbi = mbAssembler.getModelMbeanInfo(instrumentation.getClass());
154                 
155                 if (mbi != null) {
156                     RequiredModelMBean JavaDoc rtMBean;
157                     try {
158                                                 
159                         rtMBean = (RequiredModelMBean JavaDoc)mbs.instantiate(
160                             "javax.management.modelmbean.RequiredModelMBean");
161                                        
162                         rtMBean.setModelMBeanInfo(mbi);
163                         
164                         rtMBean.setManagedResource(instrumentation, "ObjectReference");
165                                                
166                         registerMBean(rtMBean,
167                             JMXUtils.getObjectName(instrumentation.getInstrumentationName(),
168                                                    instrumentation.getUniqueInstrumentationName(),
169                                                    busID));
170                                                
171                         if (LOG.isLoggable(Level.INFO)) {
172                             LOG.info("registered the object to MBserver "
173                                                + instrumentation.getUniqueInstrumentationName());
174                         }
175                             
176                            
177                     } catch (ReflectionException JavaDoc e) {
178                         LOG.log(Level.SEVERE, "INSTANTIANTE_FAILURE_MSG", new Object JavaDoc[]{e});
179                     } catch (MBeanException JavaDoc e) {
180                         LOG.log(Level.SEVERE, "MBEAN_FAILURE_MSG", new Object JavaDoc[]{e});
181                     } catch (InstanceNotFoundException JavaDoc e) {
182                         LOG.log(Level.SEVERE, "SET_MANAGED_RESOURCE_FAILURE_MSG", new Object JavaDoc[]{e});
183                     } catch (InvalidTargetObjectTypeException JavaDoc e) {
184                         LOG.log(Level.SEVERE, "SET_MANAGED_RESOURCE_FAILURE_MSG", new Object JavaDoc[]{e});
185                     }
186                 } else {
187                     LOG.log(Level.SEVERE, "GET_MANAGED_INFORMATION_FAILURE_MSG",
188                             new Object JavaDoc[]{instrumentation.getInstrumentationName()});
189                 }
190             }
191
192            
193             if (meFilter.isRemovedEvent(event)) {
194                // unregist the component and distroy it.
195
ObjectName JavaDoc name;
196                 name = JMXUtils.getObjectName(instrumentation.getInstrumentationName(),
197                     instrumentation.getUniqueInstrumentationName(),
198                     busID);
199                 unregisterMBean(name);
200                 if (LOG.isLoggable(Level.INFO)) {
201                     LOG.info("unregistered the object to MBserver"
202                                        + instrumentation.getUniqueInstrumentationName());
203                 }
204                     
205             }
206         }
207     }
208
209     
210
211 }
212
Popular Tags