1 package org.objectweb.celtix.bus.management.jmx; 2 3 4 5 import java.io.IOException ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 9 import javax.management.InstanceAlreadyExistsException ; 10 import javax.management.InstanceNotFoundException ; 11 import javax.management.JMException ; 12 import javax.management.MBeanException ; 13 import javax.management.MBeanRegistrationException ; 14 import javax.management.MBeanServer ; 15 import javax.management.MBeanServerFactory ; 16 17 18 import javax.management.NotCompliantMBeanException ; 19 import javax.management.ObjectName ; 20 import javax.management.ReflectionException ; 21 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 22 import javax.management.modelmbean.ModelMBeanInfo ; 23 import javax.management.modelmbean.RequiredModelMBean ; 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 40 41 42 public class JMXManagedComponentManager implements InstrumentationEventListener { 43 private static final Logger 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 busID; 51 private MBeanServer 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 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 ex) { 79 LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG", new Object []{ex}); 80 } 81 82 } 83 84 public MBeanServer getMBeanServer() { 85 return mbs; 86 } 87 88 public void shutdown() { 89 if (!platformMBeanServer) { 90 try { 91 mcf.destroy(); 92 } catch (IOException ex) { 93 LOG.log(Level.SEVERE, "STOP_CONNECTOR_FAILURE_MSG", new Object []{ex}); 94 } 95 } 96 } 97 98 public InstrumentationEventFilter getManagementEventFilter() { 99 return meFilter; 100 } 101 102 public void registerMBean(Object object, ObjectName name) { 103 try { 104 onRegister(name); 105 mbs.registerMBean(object, name); 106 } catch (InstanceAlreadyExistsException e) { 107 try { 110 mbs.unregisterMBean(name); 111 mbs.registerMBean(object, name); 112 } catch (Exception e1) { 113 LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object []{name, e1}); 114 } 115 } catch (MBeanRegistrationException e) { 116 LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object []{name, e}); 117 } catch (NotCompliantMBeanException e) { 118 LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object []{name, e}); 119 } 120 } 121 122 public void unregisterMBean(ObjectName name) { 123 124 try { 125 onUnregister(name); 126 mbs.unregisterMBean(name); 127 } catch (JMException e) { 128 LOG.log(Level.SEVERE, "UNREGISTER_FAILURE_MSG", new Object []{name, e}); 129 } 130 } 131 132 133 protected void onRegister(ObjectName objectName) { 134 135 } 136 137 protected void onUnregister(ObjectName objectName) { 138 if (LOG.isLoggable(Level.INFO)) { 139 LOG.info("unregistered the object to MBserver" 140 + objectName); 141 } 142 } 143 144 145 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 mbi = mbAssembler.getModelMbeanInfo(instrumentation.getClass()); 154 155 if (mbi != null) { 156 RequiredModelMBean rtMBean; 157 try { 158 159 rtMBean = (RequiredModelMBean )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 e) { 178 LOG.log(Level.SEVERE, "INSTANTIANTE_FAILURE_MSG", new Object []{e}); 179 } catch (MBeanException e) { 180 LOG.log(Level.SEVERE, "MBEAN_FAILURE_MSG", new Object []{e}); 181 } catch (InstanceNotFoundException e) { 182 LOG.log(Level.SEVERE, "SET_MANAGED_RESOURCE_FAILURE_MSG", new Object []{e}); 183 } catch (InvalidTargetObjectTypeException e) { 184 LOG.log(Level.SEVERE, "SET_MANAGED_RESOURCE_FAILURE_MSG", new Object []{e}); 185 } 186 } else { 187 LOG.log(Level.SEVERE, "GET_MANAGED_INFORMATION_FAILURE_MSG", 188 new Object []{instrumentation.getInstrumentationName()}); 189 } 190 } 191 192 193 if (meFilter.isRemovedEvent(event)) { 194 ObjectName 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 |