1 package org.objectweb.celtix.bus.management; 2 3 4 import java.util.Iterator ; 5 import java.util.LinkedList ; 6 import java.util.List ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 9 10 import javax.management.MBeanServer ; 11 12 import org.objectweb.celtix.Bus; 13 import org.objectweb.celtix.BusEvent; 14 import org.objectweb.celtix.BusEventListener; 15 import org.objectweb.celtix.BusException; 16 import org.objectweb.celtix.bus.busimpl.CeltixBus; 17 import org.objectweb.celtix.bus.busimpl.CeltixBusInstrumentation; 18 import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent; 19 import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent; 20 21 import org.objectweb.celtix.bus.instrumentation.InstrumentationPolicyType; 22 import org.objectweb.celtix.bus.instrumentation.MBServerPolicyType; 23 import org.objectweb.celtix.bus.jaxws.EndpointImpl; 24 import org.objectweb.celtix.bus.jaxws.EndpointInstrumentation; 25 import org.objectweb.celtix.bus.management.jmx.JMXManagedComponentManager; 26 import org.objectweb.celtix.bus.transports.http.HTTPClientTransport; 27 import org.objectweb.celtix.bus.transports.http.HTTPClientTransportInstrumentation; 28 import org.objectweb.celtix.bus.transports.http.HTTPServerTransportInstrumentation; 29 import org.objectweb.celtix.bus.transports.http.JettyHTTPServerTransport; 30 import org.objectweb.celtix.bus.transports.jms.JMSClientTransport; 31 import org.objectweb.celtix.bus.transports.jms.JMSClientTransportInstrumentation; 32 import org.objectweb.celtix.bus.transports.jms.JMSServerTransport; 33 import org.objectweb.celtix.bus.transports.jms.JMSServerTransportInstrumentation; 34 import org.objectweb.celtix.bus.workqueue.WorkQueueInstrumentation; 35 import org.objectweb.celtix.bus.workqueue.WorkQueueManagerImpl; 36 import org.objectweb.celtix.bus.wsdl.WSDLManagerImpl; 37 import org.objectweb.celtix.bus.wsdl.WSDLManagerInstrumentation; 38 import org.objectweb.celtix.common.logging.LogUtils; 39 import org.objectweb.celtix.configuration.Configuration; 40 import org.objectweb.celtix.configuration.ConfigurationBuilder; 41 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory; 42 import org.objectweb.celtix.management.Instrumentation; 43 import org.objectweb.celtix.management.InstrumentationManager; 44 45 46 47 48 49 56 public class InstrumentationManagerImpl implements InstrumentationManager, BusEventListener { 57 static final Logger LOG = LogUtils.getL7dLogger(InstrumentationManagerImpl.class); 58 static final String INSTRUMENTATION_CONFIGURATION_URI = 59 "http://celtix.objectweb.org/bus/instrumentation/instrumentation-config"; 60 static final String INSTRUMENTATION_CONFIGURATION_ID = 61 "Instrumentation"; 62 private Bus bus; 63 private List <Instrumentation> instrumentations; 64 private JMXManagedComponentManager jmxManagedComponentManager; 65 private ComponentEventFilter componentEventFilter; 66 private boolean instrumentationEnabled; 67 private boolean jmxEnabled; 68 69 public InstrumentationManagerImpl(Bus b) throws BusException { 70 InstrumentationPolicyType instrumentation = null; 71 MBServerPolicyType mbserver = null; 72 73 bus = b; 74 75 Configuration itConfiguration = getConfiguration(bus.getConfiguration()); 76 77 instrumentation = (InstrumentationPolicyType) 78 itConfiguration.getObject("InstrumentationControl"); 79 80 if (instrumentation == null) { 81 instrumentation = new InstrumentationPolicyType(); 82 } 83 84 mbserver = (MBServerPolicyType) 85 itConfiguration.getObject("MBServer"); 86 87 if (mbserver == null) { 88 mbserver = new MBServerPolicyType(); 89 } 90 91 instrumentationEnabled = instrumentation.isInstrumentationEnabled(); 92 jmxEnabled = instrumentation.isJMXEnabled(); 93 94 if (LOG.isLoggable(Level.INFO)) { 95 LOG.info("Setting up InstrumentationManager for BUS"); 96 } 97 98 if (instrumentationEnabled) { 100 instrumentations = new LinkedList <Instrumentation>(); 101 componentEventFilter = new ComponentEventFilter(); 102 bus.addListener((BusEventListener)this, 103 componentEventFilter); 104 } 105 106 jmxManagedComponentManager = new JMXManagedComponentManager(bus); 107 108 if (jmxEnabled) { 109 110 jmxManagedComponentManager.init(mbserver); 111 112 bus.addListener((BusEventListener)jmxManagedComponentManager, 113 jmxManagedComponentManager.getManagementEventFilter()); 114 } 115 116 117 } 118 119 private Configuration getConfiguration(Configuration configuration) { 120 121 ConfigurationBuilder cb = ConfigurationBuilderFactory.getBuilder(null); 122 123 Configuration itCfg = cb.getConfiguration(INSTRUMENTATION_CONFIGURATION_URI, 124 INSTRUMENTATION_CONFIGURATION_ID, 125 configuration); 126 if (null == itCfg) { 127 itCfg = cb.buildConfiguration(INSTRUMENTATION_CONFIGURATION_URI, 128 INSTRUMENTATION_CONFIGURATION_ID, 129 configuration); 130 } 131 return itCfg; 132 } 133 134 public void shutdown() { 135 if (LOG.isLoggable(Level.INFO)) { 136 LOG.info("Shutdown InstrumentationManager "); 137 } 138 if (instrumentationEnabled) { 139 try { 140 bus.removeListener((BusEventListener)this); 141 } catch (BusException ex) { 142 LOG.log(Level.SEVERE, "REMOVE_LISTENER_FAILURE_MSG", ex); 143 } 144 } 145 146 if (jmxManagedComponentManager != null && jmxEnabled) { 147 try { 148 bus.removeListener((BusEventListener)jmxManagedComponentManager); 149 } catch (BusException ex) { 150 LOG.log(Level.SEVERE, "REMOVE_LISTENER_FAILURE_MSG", ex); 151 } 152 jmxManagedComponentManager.shutdown(); 153 } 154 } 155 156 public void register(Instrumentation it) { 157 if (it == null) { 158 return; 160 } else { 161 instrumentations.add(it); 162 bus.sendEvent(new InstrumentationCreatedEvent(it)); 164 } 165 } 166 167 public void unregister(Object component) { 168 for (Iterator <Instrumentation> i = instrumentations.iterator(); i.hasNext();) { 169 Instrumentation it = i.next(); 170 if (it.getComponent() == component) { 171 i.remove(); 172 if (it != null) { 173 bus.sendEvent(new InstrumentationRemovedEvent(it)); 175 } 176 } 177 } 178 } 179 180 public void processEvent(BusEvent e) throws BusException { 182 Instrumentation it; 183 if (e.getID().equals(ComponentCreatedEvent.COMPONENT_CREATED_EVENT)) { 184 it = createInstrumentation(e.getSource()); 185 if (LOG.isLoggable(Level.INFO)) { 186 LOG.info("Instrumentation register " + e.getSource().getClass().getName()); 187 } 188 register(it); 189 190 } else if (e.getID().equals(ComponentRemovedEvent.COMPONENT_REMOVED_EVENT)) { 191 if (LOG.isLoggable(Level.INFO)) { 192 LOG.info("Instrumentation unregister " + e.getSource().getClass().getName()); 193 } 194 unregister(e.getSource()); 195 } 196 } 197 198 199 private Instrumentation createInstrumentation(Object component) { 200 Instrumentation it = null; 201 204 if (Instrumentation.class.isAssignableFrom(component.getClass())) { 205 it = (Instrumentation)component; 206 return it; 207 } 208 209 if (CeltixBus.class.isAssignableFrom(component.getClass())) { 210 it = new CeltixBusInstrumentation( 211 (CeltixBus)component); 212 } 213 if (WSDLManagerImpl.class.isAssignableFrom(component.getClass())) { 214 it = new WSDLManagerInstrumentation( 215 (WSDLManagerImpl)component); 216 } 217 if (WorkQueueManagerImpl.class.isAssignableFrom(component.getClass())) { 218 it = new WorkQueueInstrumentation( 219 (WorkQueueManagerImpl)component); 220 } 221 if (HTTPClientTransport.class.isAssignableFrom(component.getClass())) { 222 it = new HTTPClientTransportInstrumentation( 223 (HTTPClientTransport)component); 224 } 225 if (JettyHTTPServerTransport.class.isAssignableFrom(component.getClass())) { 226 it = new HTTPServerTransportInstrumentation( 227 (JettyHTTPServerTransport)component); 228 } 229 if (JMSServerTransport.class.isAssignableFrom(component.getClass())) { 230 it = new JMSServerTransportInstrumentation( 231 (JMSServerTransport)component); 232 } 233 if (JMSClientTransport.class.isAssignableFrom(component.getClass())) { 234 it = new JMSClientTransportInstrumentation( 235 (JMSClientTransport)component); 236 } 237 if (EndpointImpl.class.isAssignableFrom(component.getClass())) { 238 it = new EndpointInstrumentation( 239 (EndpointImpl)component); 240 } 241 242 return it; 243 } 244 245 246 public List <Instrumentation> getAllInstrumentation() { 247 return instrumentations; 249 } 250 251 public MBeanServer getMBeanServer() { 252 return jmxManagedComponentManager.getMBeanServer(); 253 } 254 255 256 } 257 | Popular Tags |