001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ReconfigDispatcher.java 6661 2005-04-28 08:43:27Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.management;
025:
026: // JMX import
027: import javax.management.ListenerNotFoundException;
028: import javax.management.MBeanNotificationInfo;
029: import javax.management.Notification;
030: import javax.management.NotificationBroadcasterSupport;
031: import javax.management.NotificationFilter;
032: import javax.management.NotificationListener;
033:
034: import org.objectweb.util.monolog.api.BasicLevel;
035: import org.objectweb.util.monolog.api.Logger;
036:
037: /**
038: * This MBean implementation has to be extended by MBeans associated to reconfigurable JOnAS services
039: * and to reconfigurable resources managed by JOnAS (DataSources and MailFactories).
040: * These MBeans can add and remove a notification listener, and emit notifications towards this listener.
041: * Currently only one listner may register as listener: the ReconfigManager MBean created at the server start-up.
042: * @author Adriana Danes
043: * Contributor(s):
044: */
045: public class ReconfigDispatcher extends NotificationBroadcasterSupport
046: implements ReconfigDispatcherMBean {
047:
048: private static Logger slogger = null;
049:
050: /**
051: * Management notification type for <i>reconfiguration</i> events (notify that the management application made a reconfiguration operation)
052: */
053: public static final String RECONFIG_TYPE = "jonas.management.reconfiguration";
054: /**
055: * Management notification type for <i>save configuration</i> events (notify that the management application asks for save changes)
056: */
057: public static final String SAVE_RECONFIG_TYPE = "jonas.management.reconfiguration.save";
058: /**
059: * Class implementing the management notifications.
060: */
061: final String RECONFIG_NOTIF_CLASS = "jmx.management.Notification";
062: final String SAVE_RECONFIG_NOTIF_CLASS = "jmx.management.Notification";
063:
064: /**
065: * There may be only one listener : the ReconfigManager MBean.
066: */
067: ListenerJavaBean myListener = null;
068:
069: /**
070: * Initialize the logger with a Logger provided by the reconfigurable resource
071: * or service.
072: * @param mylogger the logger provided by a sub-class
073: */
074: public void initLogger(Logger mylogger) {
075: slogger = mylogger;
076: }
077:
078: /**
079: * This method is called by the ReconfigManager.
080: * @listner the ReconfigManager
081: * @filter null (not used)
082: * @handback null (not used
083: */
084: public void addNotificationListener(NotificationListener listner,
085: NotificationFilter filter, java.lang.Object handback)
086: throws java.lang.IllegalArgumentException {
087:
088: if (slogger.isLoggable(BasicLevel.DEBUG)) {
089: slogger.log(BasicLevel.DEBUG, "handback: " + handback);
090: if (myListener == null) {
091: slogger.log(BasicLevel.DEBUG, "Register the listener");
092: } else {
093: slogger.log(BasicLevel.DEBUG,
094: "Caution : Re-register a listener !!! ");
095: }
096: }
097: myListener = new ListenerJavaBean(listner, filter, handback);
098: }
099:
100: /**
101: * This method is called by the ReconfigManager when it no longer needs to receive management notifications.
102: * @listner the ReconfigManager
103: */
104: public void removeNotificationListener(NotificationListener listner)
105: throws ListenerNotFoundException {
106: NotificationListener registeredListener = myListener
107: .getListener();
108: if (registeredListener.equals(listner))
109: myListener = null;
110: }
111:
112: /**
113: * Returns information about management notifications sent by this object
114: * @return information about management notifications sent by this object
115: */
116: public MBeanNotificationInfo[] getNotificationInfo() {
117: String[] notifsType = new String[2];
118: notifsType[0] = new String(RECONFIG_TYPE);
119: notifsType[1] = new String(SAVE_RECONFIG_TYPE);
120:
121: MBeanNotificationInfo[] myNotifInfos = new MBeanNotificationInfo[2];
122: MBeanNotificationInfo myNotifInfo = new MBeanNotificationInfo(
123: notifsType, RECONFIG_NOTIF_CLASS,
124: "Notify service reconfiguration events");
125: myNotifInfos[0] = myNotifInfo;
126:
127: myNotifInfo = new MBeanNotificationInfo(notifsType,
128: SAVE_RECONFIG_NOTIF_CLASS,
129: "Notify save reconfiguretion events");
130: myNotifInfos[1] = myNotifInfo;
131:
132: return myNotifInfos;
133: }
134:
135: /**
136: * Returns the listener reference (the ReconfigManager)
137: * @return the ReconfigManager reference
138: */
139: public NotificationListener getListener() {
140: if (myListener != null)
141: return myListener.getListener();
142: else
143: return null;
144: }
145:
146: /**
147: * Returns the listner's filter
148: * @return the filter object
149: */
150: public NotificationFilter getFilter() {
151: if (myListener != null)
152: return myListener.getFilter();
153: else
154: return null;
155: }
156:
157: /**
158: * Returns the listner's handback
159: * @return the handback object
160: */
161: public java.lang.Object getHandback() {
162: if (myListener != null)
163: return myListener.getHandback();
164: else
165: return null;
166: }
167:
168: /**
169: * Send a <i>save configuration</i> notification to the registerd listener. If no listener,
170: * then log an error message.
171: * @param sequenceNumber notification attribute
172: * @param resourceName the name of a reconfigurable resource or of a JOnAS service
173: */
174: public void sendSaveNotification(long sequenceNumber,
175: String resourceName) {
176: // create notification
177: Notification saveNotif = new Notification(SAVE_RECONFIG_TYPE,
178: this , sequenceNumber, resourceName);
179: if (slogger.isLoggable(BasicLevel.DEBUG)) {
180: slogger.log(BasicLevel.DEBUG, saveNotif.getType()
181: + " notification object created");
182: }
183:
184: // get listener and emit notification to it
185: NotificationListener listenerMBean = getListener();
186: Object handback = getHandback();
187: if (listenerMBean != null) {
188: listenerMBean.handleNotification(saveNotif, handback);
189: } else {
190: if (slogger.isLoggable(BasicLevel.DEBUG)) {
191: slogger
192: .log(BasicLevel.ERROR,
193: "MBean can't send management notification as no listener registered !!");
194: }
195: }
196: }
197:
198: /**
199: * Send a <i>reconfiguration</i> notification to the registerd listener. If no listener,
200: * then log an error message.
201: * @param sequenceNumber notification attribute
202: * @param resourceName the name of a reconfigurable resource or of a JOnAS service
203: * @param userData data containing the name and the value of the reconfigured property(or properties)
204: */
205: public void sendReconfigNotification(long sequenceNumber,
206: String resourceName, Object userData) {
207: // create notification and set the userData
208: Notification configNotif = new Notification(RECONFIG_TYPE,
209: this , sequenceNumber, resourceName);
210: configNotif.setUserData(userData);
211: if (slogger.isLoggable(BasicLevel.DEBUG)) {
212: slogger.log(BasicLevel.DEBUG, configNotif.getType()
213: + " notification object created");
214: }
215:
216: // get listener and emit notification to it
217: NotificationListener listenerMBean = getListener();
218: Object handback = getHandback();
219: if (listenerMBean != null) {
220: listenerMBean.handleNotification(configNotif, handback);
221: } else {
222: if (slogger.isLoggable(BasicLevel.DEBUG)) {
223: slogger
224: .log(BasicLevel.ERROR,
225: "MBean can't send management notification as no listener registered !!");
226: }
227: }
228: }
229: }
|