001: package org.objectweb.celtix.bus.management;
002:
003: import java.util.Iterator;
004: import java.util.LinkedList;
005: import java.util.List;
006: import java.util.logging.Level;
007: import java.util.logging.Logger;
008:
009: import javax.management.MBeanServer;
010:
011: import org.objectweb.celtix.Bus;
012: import org.objectweb.celtix.BusEvent;
013: import org.objectweb.celtix.BusEventListener;
014: import org.objectweb.celtix.BusException;
015: import org.objectweb.celtix.bus.busimpl.CeltixBus;
016: import org.objectweb.celtix.bus.busimpl.CeltixBusInstrumentation;
017: import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent;
018: import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent;
019:
020: import org.objectweb.celtix.bus.instrumentation.InstrumentationPolicyType;
021: import org.objectweb.celtix.bus.instrumentation.MBServerPolicyType;
022: import org.objectweb.celtix.bus.jaxws.EndpointImpl;
023: import org.objectweb.celtix.bus.jaxws.EndpointInstrumentation;
024: import org.objectweb.celtix.bus.management.jmx.JMXManagedComponentManager;
025: import org.objectweb.celtix.bus.transports.http.HTTPClientTransport;
026: import org.objectweb.celtix.bus.transports.http.HTTPClientTransportInstrumentation;
027: import org.objectweb.celtix.bus.transports.http.HTTPServerTransportInstrumentation;
028: import org.objectweb.celtix.bus.transports.http.JettyHTTPServerTransport;
029: import org.objectweb.celtix.bus.transports.jms.JMSClientTransport;
030: import org.objectweb.celtix.bus.transports.jms.JMSClientTransportInstrumentation;
031: import org.objectweb.celtix.bus.transports.jms.JMSServerTransport;
032: import org.objectweb.celtix.bus.transports.jms.JMSServerTransportInstrumentation;
033: import org.objectweb.celtix.bus.workqueue.WorkQueueInstrumentation;
034: import org.objectweb.celtix.bus.workqueue.WorkQueueManagerImpl;
035: import org.objectweb.celtix.bus.wsdl.WSDLManagerImpl;
036: import org.objectweb.celtix.bus.wsdl.WSDLManagerInstrumentation;
037: import org.objectweb.celtix.common.logging.LogUtils;
038: import org.objectweb.celtix.configuration.Configuration;
039: import org.objectweb.celtix.configuration.ConfigurationBuilder;
040: import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
041: import org.objectweb.celtix.management.Instrumentation;
042: import org.objectweb.celtix.management.InstrumentationManager;
043:
044: /** The basic manager information center for common management model
045: * Instrumentation components will be registed to InstrumenationManager.
046: * The Instrumentation mananger will send event to notifier the management
047: * layer to expose the managed component.
048: * Instrumentation manager also provider a qurey interface for the instrumentation.
049: * The JMX layer could query the detail information for instrumentation.
050: */
051: public class InstrumentationManagerImpl implements
052: InstrumentationManager, BusEventListener {
053: static final Logger LOG = LogUtils
054: .getL7dLogger(InstrumentationManagerImpl.class);
055: static final String INSTRUMENTATION_CONFIGURATION_URI = "http://celtix.objectweb.org/bus/instrumentation/instrumentation-config";
056: static final String INSTRUMENTATION_CONFIGURATION_ID = "Instrumentation";
057: private Bus bus;
058: private List<Instrumentation> instrumentations;
059: private JMXManagedComponentManager jmxManagedComponentManager;
060: private ComponentEventFilter componentEventFilter;
061: private boolean instrumentationEnabled;
062: private boolean jmxEnabled;
063:
064: public InstrumentationManagerImpl(Bus b) throws BusException {
065: InstrumentationPolicyType instrumentation = null;
066: MBServerPolicyType mbserver = null;
067:
068: bus = b;
069:
070: Configuration itConfiguration = getConfiguration(bus
071: .getConfiguration());
072:
073: instrumentation = (InstrumentationPolicyType) itConfiguration
074: .getObject("InstrumentationControl");
075:
076: if (instrumentation == null) {
077: instrumentation = new InstrumentationPolicyType();
078: }
079:
080: mbserver = (MBServerPolicyType) itConfiguration
081: .getObject("MBServer");
082:
083: if (mbserver == null) {
084: mbserver = new MBServerPolicyType();
085: }
086:
087: instrumentationEnabled = instrumentation
088: .isInstrumentationEnabled();
089: jmxEnabled = instrumentation.isJMXEnabled();
090:
091: if (LOG.isLoggable(Level.INFO)) {
092: LOG.info("Setting up InstrumentationManager for BUS");
093: }
094:
095: // get the configuration
096: if (instrumentationEnabled) {
097: instrumentations = new LinkedList<Instrumentation>();
098: componentEventFilter = new ComponentEventFilter();
099: bus.addListener((BusEventListener) this ,
100: componentEventFilter);
101: }
102:
103: jmxManagedComponentManager = new JMXManagedComponentManager(bus);
104:
105: if (jmxEnabled) {
106:
107: jmxManagedComponentManager.init(mbserver);
108:
109: bus.addListener(
110: (BusEventListener) jmxManagedComponentManager,
111: jmxManagedComponentManager
112: .getManagementEventFilter());
113: }
114:
115: }
116:
117: private Configuration getConfiguration(Configuration configuration) {
118:
119: ConfigurationBuilder cb = ConfigurationBuilderFactory
120: .getBuilder(null);
121:
122: Configuration itCfg = cb.getConfiguration(
123: INSTRUMENTATION_CONFIGURATION_URI,
124: INSTRUMENTATION_CONFIGURATION_ID, configuration);
125: if (null == itCfg) {
126: itCfg = cb.buildConfiguration(
127: INSTRUMENTATION_CONFIGURATION_URI,
128: INSTRUMENTATION_CONFIGURATION_ID, configuration);
129: }
130: return itCfg;
131: }
132:
133: public void shutdown() {
134: if (LOG.isLoggable(Level.INFO)) {
135: LOG.info("Shutdown InstrumentationManager ");
136: }
137: if (instrumentationEnabled) {
138: try {
139: bus.removeListener((BusEventListener) this );
140: } catch (BusException ex) {
141: LOG
142: .log(Level.SEVERE,
143: "REMOVE_LISTENER_FAILURE_MSG", ex);
144: }
145: }
146:
147: if (jmxManagedComponentManager != null && jmxEnabled) {
148: try {
149: bus
150: .removeListener((BusEventListener) jmxManagedComponentManager);
151: } catch (BusException ex) {
152: LOG
153: .log(Level.SEVERE,
154: "REMOVE_LISTENER_FAILURE_MSG", ex);
155: }
156: jmxManagedComponentManager.shutdown();
157: }
158: }
159:
160: public void register(Instrumentation it) {
161: if (it == null) {
162: // can't find the right instrumentation ,just return
163: return;
164: } else {
165: instrumentations.add(it);
166: //create the instrumentation creation event
167: bus.sendEvent(new InstrumentationCreatedEvent(it));
168: }
169: }
170:
171: public void unregister(Object component) {
172: for (Iterator<Instrumentation> i = instrumentations.iterator(); i
173: .hasNext();) {
174: Instrumentation it = i.next();
175: if (it.getComponent() == component) {
176: i.remove();
177: if (it != null) {
178: //create the instrumentation remove event
179: bus.sendEvent(new InstrumentationRemovedEvent(it));
180: }
181: }
182: }
183: }
184:
185: // get the instance and create the right component
186: public void processEvent(BusEvent e) throws BusException {
187: Instrumentation it;
188: if (e.getID().equals(
189: ComponentCreatedEvent.COMPONENT_CREATED_EVENT)) {
190: it = createInstrumentation(e.getSource());
191: if (LOG.isLoggable(Level.INFO)) {
192: LOG.info("Instrumentation register "
193: + e.getSource().getClass().getName());
194: }
195: register(it);
196:
197: } else if (e.getID().equals(
198: ComponentRemovedEvent.COMPONENT_REMOVED_EVENT)) {
199: if (LOG.isLoggable(Level.INFO)) {
200: LOG.info("Instrumentation unregister "
201: + e.getSource().getClass().getName());
202: }
203: unregister(e.getSource());
204: }
205: }
206:
207: private Instrumentation createInstrumentation(Object component) {
208: Instrumentation it = null;
209: // if the componenet implements the instrumentation interface,
210: // just return the object itself
211:
212: if (Instrumentation.class
213: .isAssignableFrom(component.getClass())) {
214: it = (Instrumentation) component;
215: return it;
216: }
217:
218: if (CeltixBus.class.isAssignableFrom(component.getClass())) {
219: it = new CeltixBusInstrumentation((CeltixBus) component);
220: }
221: if (WSDLManagerImpl.class
222: .isAssignableFrom(component.getClass())) {
223: it = new WSDLManagerInstrumentation(
224: (WSDLManagerImpl) component);
225: }
226: if (WorkQueueManagerImpl.class.isAssignableFrom(component
227: .getClass())) {
228: it = new WorkQueueInstrumentation(
229: (WorkQueueManagerImpl) component);
230: }
231: if (HTTPClientTransport.class.isAssignableFrom(component
232: .getClass())) {
233: it = new HTTPClientTransportInstrumentation(
234: (HTTPClientTransport) component);
235: }
236: if (JettyHTTPServerTransport.class.isAssignableFrom(component
237: .getClass())) {
238: it = new HTTPServerTransportInstrumentation(
239: (JettyHTTPServerTransport) component);
240: }
241: if (JMSServerTransport.class.isAssignableFrom(component
242: .getClass())) {
243: it = new JMSServerTransportInstrumentation(
244: (JMSServerTransport) component);
245: }
246: if (JMSClientTransport.class.isAssignableFrom(component
247: .getClass())) {
248: it = new JMSClientTransportInstrumentation(
249: (JMSClientTransport) component);
250: }
251: if (EndpointImpl.class.isAssignableFrom(component.getClass())) {
252: it = new EndpointInstrumentation((EndpointImpl) component);
253: }
254:
255: return it;
256: }
257:
258: public List<Instrumentation> getAllInstrumentation() {
259: // TODO need to add more qurey interface
260: return instrumentations;
261: }
262:
263: public MBeanServer getMBeanServer() {
264: return jmxManagedComponentManager.getMBeanServer();
265: }
266:
267: }
|