001: package org.objectweb.celtix.bus.management.jmx;
002:
003: import java.io.IOException;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import javax.management.InstanceAlreadyExistsException;
008: import javax.management.InstanceNotFoundException;
009: import javax.management.JMException;
010: import javax.management.MBeanException;
011: import javax.management.MBeanRegistrationException;
012: import javax.management.MBeanServer;
013: import javax.management.MBeanServerFactory;
014:
015: import javax.management.NotCompliantMBeanException;
016: import javax.management.ObjectName;
017: import javax.management.ReflectionException;
018: import javax.management.modelmbean.InvalidTargetObjectTypeException;
019: import javax.management.modelmbean.ModelMBeanInfo;
020: import javax.management.modelmbean.RequiredModelMBean;
021:
022: import org.objectweb.celtix.Bus;
023: import org.objectweb.celtix.BusEvent;
024: import org.objectweb.celtix.BusException;
025: import org.objectweb.celtix.bus.instrumentation.MBServerPolicyType;
026: import org.objectweb.celtix.bus.management.InstrumentationEventFilter;
027: import org.objectweb.celtix.bus.management.InstrumentationEventListener;
028: import org.objectweb.celtix.bus.management.jmx.export.runtime.ModelMBeanAssembler;
029: import org.objectweb.celtix.common.logging.LogUtils;
030: import org.objectweb.celtix.management.Instrumentation;
031:
032: /***
033: * The manager class for the JMXManagedComponent which host the JMXManagedComponent
034: * It implemenated the ManagementEventListener for the managed component register and unregister
035: */
036:
037: public class JMXManagedComponentManager implements
038: InstrumentationEventListener {
039: private static final Logger LOG = LogUtils
040: .getL7dLogger(JMXManagedComponentManager.class);
041:
042: private boolean platformMBeanServer;
043: private InstrumentationEventFilter meFilter;
044: private ModelMBeanAssembler mbAssembler;
045: private MBServerConnectorFactory mcf;
046: private Bus bus;
047: private String busID;
048: private MBeanServer mbs;
049:
050: public JMXManagedComponentManager(Bus b) {
051: bus = b;
052: busID = bus.getBusID();
053: meFilter = new InstrumentationEventFilter();
054: mbAssembler = new ModelMBeanAssembler();
055:
056: }
057:
058: public void init(MBServerPolicyType mbpt) {
059: // get the init information from configuration
060:
061: if (LOG.isLoggable(Level.INFO)) {
062: LOG.info("Setting up MBeanServer ");
063: }
064:
065: mbs = MBeanServerFactory
066: .createMBeanServer(JMXUtils.DOMAIN_STRING);
067: mcf = MBServerConnectorFactory.getInstance();
068: mcf.setMBeanServer(mbs);
069: mcf.setThreaded(mbpt.getJMXConnector().isThreaded());
070: mcf.setDaemon(mbpt.getJMXConnector().isDaemon());
071: mcf.setServiceUrl(mbpt.getJMXConnector().getJMXServiceURL());
072: try {
073: mcf.createConnector();
074: } catch (IOException ex) {
075: LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG",
076: new Object[] { ex });
077: }
078:
079: }
080:
081: public MBeanServer getMBeanServer() {
082: return mbs;
083: }
084:
085: public void shutdown() {
086: if (!platformMBeanServer) {
087: try {
088: mcf.destroy();
089: } catch (IOException ex) {
090: LOG.log(Level.SEVERE, "STOP_CONNECTOR_FAILURE_MSG",
091: new Object[] { ex });
092: }
093: }
094: }
095:
096: public InstrumentationEventFilter getManagementEventFilter() {
097: return meFilter;
098: }
099:
100: public void registerMBean(Object object, ObjectName name) {
101: try {
102: onRegister(name);
103: mbs.registerMBean(object, name);
104: } catch (InstanceAlreadyExistsException e) {
105: //LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object[]{name, e});
106: //regist the new object instance
107: try {
108: mbs.unregisterMBean(name);
109: mbs.registerMBean(object, name);
110: } catch (Exception e1) {
111: LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG",
112: new Object[] { name, e1 });
113: }
114: } catch (MBeanRegistrationException e) {
115: LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object[] {
116: name, e });
117: } catch (NotCompliantMBeanException e) {
118: LOG.log(Level.SEVERE, "REGISTER_FAILURE_MSG", new Object[] {
119: name, e });
120: }
121: }
122:
123: public void unregisterMBean(ObjectName name) {
124:
125: try {
126: onUnregister(name);
127: mbs.unregisterMBean(name);
128: } catch (JMException e) {
129: LOG.log(Level.SEVERE, "UNREGISTER_FAILURE_MSG",
130: new Object[] { name, e });
131: }
132: }
133:
134: protected void onRegister(ObjectName objectName) {
135:
136: }
137:
138: protected void onUnregister(ObjectName objectName) {
139: if (LOG.isLoggable(Level.INFO)) {
140: LOG
141: .info("unregistered the object to MBserver"
142: + objectName);
143: }
144: }
145:
146: // find out the related JMX managed component do register and unregister things
147:
148: public void processEvent(BusEvent event) throws BusException {
149: if (meFilter.isEventEnabled(event)) {
150: Instrumentation instrumentation = (Instrumentation) event
151: .getSource();
152:
153: if (meFilter.isCreateEvent(event)) {
154:
155: ModelMBeanInfo mbi = mbAssembler
156: .getModelMbeanInfo(instrumentation.getClass());
157:
158: if (mbi != null) {
159: RequiredModelMBean rtMBean;
160: try {
161:
162: rtMBean = (RequiredModelMBean) mbs
163: .instantiate("javax.management.modelmbean.RequiredModelMBean");
164:
165: rtMBean.setModelMBeanInfo(mbi);
166:
167: rtMBean.setManagedResource(instrumentation,
168: "ObjectReference");
169:
170: registerMBean(
171: rtMBean,
172: JMXUtils
173: .getObjectName(
174: instrumentation
175: .getInstrumentationName(),
176: instrumentation
177: .getUniqueInstrumentationName(),
178: busID));
179:
180: if (LOG.isLoggable(Level.INFO)) {
181: LOG
182: .info("registered the object to MBserver "
183: + instrumentation
184: .getUniqueInstrumentationName());
185: }
186:
187: } catch (ReflectionException e) {
188: LOG.log(Level.SEVERE,
189: "INSTANTIANTE_FAILURE_MSG",
190: new Object[] { e });
191: } catch (MBeanException e) {
192: LOG.log(Level.SEVERE, "MBEAN_FAILURE_MSG",
193: new Object[] { e });
194: } catch (InstanceNotFoundException e) {
195: LOG.log(Level.SEVERE,
196: "SET_MANAGED_RESOURCE_FAILURE_MSG",
197: new Object[] { e });
198: } catch (InvalidTargetObjectTypeException e) {
199: LOG.log(Level.SEVERE,
200: "SET_MANAGED_RESOURCE_FAILURE_MSG",
201: new Object[] { e });
202: }
203: } else {
204: LOG.log(Level.SEVERE,
205: "GET_MANAGED_INFORMATION_FAILURE_MSG",
206: new Object[] { instrumentation
207: .getInstrumentationName() });
208: }
209: }
210:
211: if (meFilter.isRemovedEvent(event)) {
212: // unregist the component and distroy it.
213: ObjectName name;
214: name = JMXUtils.getObjectName(instrumentation
215: .getInstrumentationName(), instrumentation
216: .getUniqueInstrumentationName(), busID);
217: unregisterMBean(name);
218: if (LOG.isLoggable(Level.INFO)) {
219: LOG.info("unregistered the object to MBserver"
220: + instrumentation
221: .getUniqueInstrumentationName());
222: }
223:
224: }
225: }
226: }
227:
228: }
|