001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ModelSystemService.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.system;
030:
031: import com.sun.jbi.management.JbiNameInfo;
032: import com.sun.jbi.management.support.MBeanSet;
033: import com.sun.jbi.management.common.LoggerMBean;
034: import com.sun.jbi.management.registry.Registry;
035: import com.sun.jbi.StringTranslator;
036: import java.util.logging.Logger;
037: import javax.management.NotificationEmitter;
038: import javax.management.NotificationListener;
039: import javax.management.NotificationFilter;
040: import javax.management.MBeanNotificationInfo;
041: import javax.management.NotificationBroadcasterSupport;
042: import javax.management.ListenerNotFoundException;
043: import javax.management.MBeanServer;
044: import javax.management.ObjectName;
045: import javax.jbi.management.LifeCycleMBean;
046:
047: /**
048: * The ModelSystemService implements the management controls for a standard
049: * JBI Framework system service.
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public abstract class ModelSystemService implements LifeCycleMBean,
054: NotificationEmitter {
055: /** JMX object names for all JBI system "service" mbeans: */
056: protected ObjectName mAdminServiceMBeanName;
057: protected ObjectName mConfigServiceMBeanName;
058: protected ObjectName mInstallServiceMBeanName;
059: protected ObjectName mLoggingServiceMBeanName;
060: protected ObjectName mDeployServiceMBeanName;
061: protected ObjectName mMessageServiceMBeanName;
062:
063: protected com.sun.jbi.EnvironmentContext mEnv;
064:
065: //allocate mbean sets for INITIAL and START/STOP mbeans:
066: protected MBeanSet mInitialMBeans, mStartMBeans;
067:
068: /** Logger to be used by ModelSystemService methods: */
069: protected Logger mLogger;
070:
071: /** handle to our MBean Server: */
072: protected MBeanServer mMBeanServer;
073:
074: /** handle to our MBean Namer: */
075: protected com.sun.jbi.management.MBeanNames mMBeanNames;
076:
077: /** handle to jbi system or installed component identification: */
078: protected JbiNameInfo mJbiNameInfo;
079:
080: /** User-friendly display name to be used by ModelSystemService methods: */
081: protected String mDisplayName;
082:
083: /**
084: * The default logger implementation, all logger tasks are delegated to
085: * this MBean
086: */
087: protected LoggerMBean mLoggerImpl;
088:
089: /** JMX object names for standard mbeans: */
090: protected ObjectName mConfigMBeanName;
091: protected ObjectName mLifeCycleMBeanName;
092: protected ObjectName mLoggerMBeanName;
093: protected ObjectName mStatisticsMBeanName;
094:
095: protected final String mModelName = "ModelSystemService";
096:
097: /** Handle to StringTranslator for message translation: */
098: protected StringTranslator mTranslator;
099:
100: /** allocate a logger, and then initialize the ModelSystemService. */
101: protected void initModelSystemService(
102: com.sun.jbi.EnvironmentContext anEnv, JbiNameInfo aJbiName) {
103: //allocate a logger, since none was provided::
104: mLogger = Logger.getLogger(this .getClass().getName());
105:
106: //call the secondary form of init:
107: initModelSystemService(anEnv, mLogger, aJbiName);
108: }
109:
110: /** Initialize the ModelSystemService. This does not register any mbeans. */
111: protected void initModelSystemService(
112: com.sun.jbi.EnvironmentContext anEnv, Logger aLogger,
113: JbiNameInfo aJbiName) {
114: //get access to environment implementation, if different:
115: mEnv = (com.sun.jbi.EnvironmentContext) anEnv;
116:
117: //save handle to jbi system or installed component identification:
118: mJbiNameInfo = aJbiName;
119: mDisplayName = aJbiName.name();
120:
121: //save logger handle. Note that the logger's level is inherited from
122: // the main JBI system logger (com.sun.jbi) if no level has been
123: // explicitly set for this logger.
124: mLogger = aLogger;
125:
126: try {
127: mTranslator = (StringTranslator) mEnv
128: .getStringTranslator("com.sun.jbi.management");
129: } catch (Exception e) {
130: mLogger.warning(e.getMessage());
131: }
132:
133: // Instantiate the Default Logger Implementation
134: try {
135: mLoggerImpl = new com.sun.jbi.management.support.SystemServiceLoggerMBeanImpl(
136: mLogger, mDisplayName);
137: } catch (Exception ex) {
138: String[] params = new String[] { mDisplayName,
139: mLogger.getName(), ex.getMessage() };
140: String locMsg = mTranslator
141: .getString(
142: com.sun.jbi.management.LocalStringKeys.JBI_ADMIN_LOGGER_MBN_CREATE_FAILED,
143: params);
144:
145: mLogger.warning(locMsg);
146: }
147:
148: mMBeanServer = mEnv.getMBeanServer();
149:
150: try {
151: //why doesn't this work for model components????
152: //(throws a class-cast exception).
153: //mMBeanNames = (com.sun.jbi.management.MBeanNames) mEnv.getMBeanNames();
154:
155: //work-around for above:
156: com.sun.jbi.management.support.MBeanNamesImpl mbnamesImpl = new com.sun.jbi.management.support.MBeanNamesImpl(
157: mEnv.getMBeanNames().getJmxDomainName(), mEnv
158: .getPlatformContext().getInstanceName());
159: mMBeanNames = (com.sun.jbi.management.MBeanNames) mbnamesImpl;
160:
161: //create MBEAN sets for INITIAL and START states:
162: mInitialMBeans = new MBeanSet(mMBeanServer, mLogger);
163: mStartMBeans = new MBeanSet(mMBeanServer, mLogger);
164:
165: //create Object names for standard mbeans, and populate mbean sets:
166: initModelSystemMBeanNames();
167: } catch (Exception e) {
168: mLogger.warning(e.getMessage());
169: }
170:
171: mLogger
172: .fine("initModelSystemService: Initialization complete for "
173: + mJbiNameInfo.name());
174: }
175:
176: /** perform one-time startup tasks for this model */
177: protected void bootstrap() {
178: mLogger.fine("ModelSystemService: START bootstrap for '"
179: + mJbiNameInfo.name() + "'");
180:
181: //register INITIAL mbean set:
182: mInitialMBeans.register();
183: mLogger.fine("ModelSystemService: FINISH bootstrap for '"
184: + mJbiNameInfo.name() + "'");
185: }
186:
187: ////////////////
188: //implementation of LifeCycleMBean
189: ////////////////
190:
191: /**
192: * Start the item.
193: *
194: * @exception javax.jbi.JBIException if the item fails to start.
195: */
196: public void start() throws javax.jbi.JBIException {
197: //register START/STOP mbean set:
198: mStartMBeans.register();
199: }
200:
201: /**
202: * Stop the item. This suspends current messaging activities.
203: *
204: * @exception javax.jbi.JBIException if the item fails to stop.
205: */
206: public void stop() throws javax.jbi.JBIException {
207: //unregister START/STOP mbean set:
208: mStartMBeans.unregister();
209: }
210:
211: /**
212: * Shut down the item. This releases resources and returns the item
213: * to an uninitialized state.
214: *
215: * @exception javax.jbi.JBIException if the item fails to shut down.
216: */
217: public void shutDown() throws javax.jbi.JBIException {
218: //stop management mbean lifecycle:
219: try {
220: //make sure lifecycle is stopped:
221: mMBeanServer.invoke(mLifeCycleMBeanName, "stop",
222: new Object[0], new String[0]);
223: } catch (Exception e) {
224: mLogger.warning(e.toString());
225: e.printStackTrace();
226: }
227:
228: try {
229: //unregister INITIAL mbean set:
230: mInitialMBeans.unregister();
231: } catch (Exception e) {
232: e.printStackTrace();
233: }
234: }
235:
236: /**
237: * Get the current state of this managed compononent.
238: *
239: * @return the current state of this managed component (must be one of the
240: * string constants defined by this interface)
241: */
242: public String getCurrentState() {
243: // For now, just return "Unknown" until states are stored.
244: return UNKNOWN;
245: }
246:
247: /** ## These methods provide access to registry-related interfaces. ## */
248:
249: /**
250: * Get a reference to the JBI registry.
251: */
252: protected Registry getRegistry() {
253: return (com.sun.jbi.management.registry.Registry) mEnv
254: .getRegistry();
255: }
256:
257: /**
258: * Get a reference to ComponentQuery.
259: */
260: protected com.sun.jbi.ComponentQuery getComponentQuery() {
261: try {
262: return getRegistry().getComponentQuery();
263: } catch (com.sun.jbi.management.registry.RegistryException regEx) {
264: throw new RuntimeException(regEx.getMessage());
265: }
266: }
267:
268: /**
269: * create Object names for required ModelSystemService mbeans,
270: * and populate the INITIAL state mbean set.
271: */
272: private void initModelSystemMBeanNames() {
273: ////////////
274: //lifecycle:
275: ////////////
276: mLifeCycleMBeanName = mMBeanNames
277: .getSystemServiceMBeanName(mJbiNameInfo.name(),
278: mMBeanNames.CONTROL_TYPE_LIFECYCLE);
279: mInitialMBeans.add(mLifeCycleMBeanName,
280: javax.jbi.management.LifeCycleMBean.class, this );
281:
282: /////////
283: //logger:
284: /////////
285: if (mLoggerImpl != null) {
286: mLoggerMBeanName = mMBeanNames
287: .getSystemServiceLoggerMBeanName(mJbiNameInfo
288: .name(), mLogger);
289: mInitialMBeans.add(mLoggerMBeanName,
290: com.sun.jbi.management.common.LoggerMBean.class,
291: mLoggerImpl);
292: }
293:
294: /////////
295: //statistics:
296: /////////
297: mStatisticsMBeanName = mMBeanNames.getSystemServiceMBeanName(
298: mJbiNameInfo.name(),
299: mMBeanNames.CONTROL_TYPE_STATISTICS);
300:
301: initSystemServiceMBeanNames();
302: }
303:
304: /**
305: * create Object names for all System Service MBeans.
306: */
307: private void initSystemServiceMBeanNames() {
308: mAdminServiceMBeanName = mMBeanNames.getSystemServiceMBeanName(
309: mMBeanNames.SERVICE_NAME_ADMIN_SERVICE,
310: mMBeanNames.CONTROL_TYPE_ADMIN_SERVICE);
311: mConfigServiceMBeanName = mMBeanNames
312: .getSystemServiceMBeanName(
313: mMBeanNames.SERVICE_NAME_CONFIG_SERVICE,
314: mMBeanNames.CONTROL_TYPE_CONFIG_SERVICE);
315: mDeployServiceMBeanName = mMBeanNames
316: .getSystemServiceMBeanName(
317: mMBeanNames.SERVICE_NAME_DEPLOY_SERVICE,
318: mMBeanNames.CONTROL_TYPE_DEPLOY_SERVICE);
319: mInstallServiceMBeanName = mMBeanNames
320: .getSystemServiceMBeanName(
321: mMBeanNames.SERVICE_NAME_INSTALL_SERVICE,
322: mMBeanNames.CONTROL_TYPE_INSTALL_SERVICE);
323: mLoggingServiceMBeanName = mMBeanNames
324: .getSystemServiceMBeanName(
325: mMBeanNames.SERVICE_NAME_LOGGING_SERVICE,
326: mMBeanNames.CONTROL_TYPE_LOGGING_SERVICE);
327: mMessageServiceMBeanName = mMBeanNames
328: .getSystemServiceMBeanName(
329: mMBeanNames.SERVICE_NAME_MESSAGE_SERVICE,
330: mMBeanNames.CONTROL_TYPE_MESSAGE_SERVICE);
331: }
332:
333: //allocate our broadcast support class:
334: private NotificationBroadcasterSupport mEmitter = new NotificationBroadcasterSupport();
335:
336: //Implementation for NotificationBroadcaster, extended by NotificationEmitter
337: /**
338: * Adds a listener to this MBean.
339: *
340: * @param listener The listener object which will handle the
341: * notifications emitted by the broadcaster.
342: * @param filter The filter object. If filter is null, no
343: * filtering will be performed before handling notifications.
344: * @param handback An opaque object to be sent back to the
345: * listener when a notification is emitted. This object cannot be
346: * used by the Notification broadcaster object. It should be
347: * resent unchanged with the notification to the listener.
348: *
349: * @exception IllegalArgumentException Listener parameter is null.
350: *
351: * @see #removeNotificationListener
352: */
353: public void addNotificationListener(NotificationListener listener,
354: NotificationFilter filter, Object handback)
355: throws java.lang.IllegalArgumentException {
356: mEmitter.addNotificationListener(listener, filter, handback);
357: }
358:
359: /**
360: * Removes a listener from this MBean. If the listener
361: * has been registered with different handback objects or
362: * notification filters, all entries corresponding to the listener
363: * will be removed.
364: *
365: * @param listener A listener that was previously added to this
366: * MBean.
367: *
368: * @exception ListenerNotFoundException The listener is not
369: * registered with the MBean.
370: *
371: * @see #addNotificationListener
372: * @see NotificationEmitter#removeNotificationListener
373: */
374: public void removeNotificationListener(NotificationListener listener)
375: throws ListenerNotFoundException {
376: mEmitter.removeNotificationListener(listener);
377: }
378:
379: /**
380: * <p>Returns an array indicating, for each notification this
381: * MBean may send, the name of the Java class of the notification
382: * and the notification type.</p>
383: *
384: * <p>It is not illegal for the MBean to send notifications not
385: * described in this array. However, some clients of the MBean
386: * server may depend on the array being complete for their correct
387: * functioning.</p>
388: *
389: * @return the array of possible notifications.
390: */
391: public MBeanNotificationInfo[] getNotificationInfo() {
392: return (new MBeanNotificationInfo[0]);
393: }
394:
395: //Implementaion for NotificationEmitter, which extends NotificationBroadcaster
396:
397: /**
398: * <p>Removes a listener from this MBean. The MBean must have a
399: * listener that exactly matches the given <code>listener</code>,
400: * <code>filter</code>, and <code>handback</code> parameters. If
401: * there is more than one such listener, only one is removed.</p>
402: *
403: * <p>The <code>filter</code> and <code>handback</code> parameters
404: * may be null if and only if they are null in a listener to be
405: * removed.</p>
406: *
407: * @param listener A listener that was previously added to this
408: * MBean.
409: * @param filter The filter that was specified when the listener
410: * was added.
411: * @param handback The handback that was specified when the listener was
412: * added.
413: *
414: * @exception ListenerNotFoundException The listener is not
415: * registered with the MBean, or it is not registered with the
416: * given filter and handback.
417: */
418: public void removeNotificationListener(
419: NotificationListener listener, NotificationFilter filter,
420: Object handback) throws ListenerNotFoundException {
421: mEmitter.removeNotificationListener(listener, filter, handback);
422: }
423:
424: /**
425: * Getter for local NotificationBroadcaster.
426: *
427: * @return NotificationBroadcaster
428: */
429: public NotificationBroadcasterSupport getNotificationBroadcaster() {
430: return mEmitter;
431: }
432: }
|