KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > management > ReconfigDispatcher


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ReconfigDispatcher.java,v 1.11 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.management;
26
27 // JMX import
28
import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.MBeanNotificationInfo JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.NotificationBroadcasterSupport JavaDoc;
32 import javax.management.NotificationFilter JavaDoc;
33 import javax.management.NotificationListener JavaDoc;
34
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.Logger;
37
38 /**
39  * This MBean implementation has to be extended by MBeans associated to reconfigurable JOnAS services
40  * and to reconfigurable resources managed by JOnAS (DataSources and MailFactories).
41  * These MBeans can add and remove a notification listener, and emit notifications towards this listener.
42  * Currently only one listner may register as listener: the ReconfigManager MBean created at the server start-up.
43  * @author Adriana Danes
44  * Contributor(s):
45  */

46 public class ReconfigDispatcher extends NotificationBroadcasterSupport JavaDoc implements ReconfigDispatcherMBean {
47
48     private static Logger slogger = null;
49
50     /**
51      * Management notification type for <i>reconfiguration</i> events (notify that the management application made a reconfiguration operation)
52      */

53     public static final String JavaDoc RECONFIG_TYPE = "jonas.management.reconfiguration";
54     /**
55      * Management notification type for <i>save configuration</i> events (notify that the management application asks for save changes)
56      */

57     public static final String JavaDoc SAVE_RECONFIG_TYPE = "jonas.management.reconfiguration.save";
58     /**
59      * Class implementing the management notifications.
60      */

61     final String JavaDoc RECONFIG_NOTIF_CLASS = "jmx.management.Notification";
62     final String JavaDoc SAVE_RECONFIG_NOTIF_CLASS = "jmx.management.Notification";
63
64     /**
65      * There may be only one listener : the ReconfigManager MBean.
66      */

67     ListenerJavaBean myListener = null;
68
69     /**
70      * Initialize the logger with a Logger provided by the reconfigurable resource
71      * or service.
72      * @param mylogger the logger provided by a sub-class
73      */

74     public void initLogger(Logger mylogger) {
75         slogger = mylogger;
76     }
77
78     /**
79      * This method is called by the ReconfigManager.
80      * @listner the ReconfigManager
81      * @filter null (not used)
82      * @handback null (not used
83      */

84     public void addNotificationListener(NotificationListener JavaDoc listner,
85                                         NotificationFilter JavaDoc filter,
86                                         java.lang.Object JavaDoc handback)
87         throws java.lang.IllegalArgumentException JavaDoc {
88
89         if (slogger.isLoggable(BasicLevel.DEBUG)) {
90             slogger.log(BasicLevel.DEBUG, "handback: " + handback);
91             if (myListener == null) {
92                 slogger.log(BasicLevel.DEBUG, "Register the listener");
93             } else {
94                 slogger.log(BasicLevel.DEBUG, "Caution : Re-register a listener !!! ");
95             }
96         }
97         myListener = new ListenerJavaBean(listner, filter, handback);
98     }
99     /**
100      * This method is called by the ReconfigManager when it no longer needs to receive management notifications.
101      * @listner the ReconfigManager
102      */

103     public void removeNotificationListener(NotificationListener JavaDoc listner) throws ListenerNotFoundException JavaDoc {
104         NotificationListener JavaDoc registeredListener = myListener.getListener();
105         if (registeredListener.equals(listner))
106             myListener = null;
107     }
108     /**
109      * Returns information about management notifications sent by this object
110      * @return information about management notifications sent by this object
111      */

112     public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
113         String JavaDoc[] notifsType = new String JavaDoc[2];
114         notifsType[0] = new String JavaDoc(RECONFIG_TYPE);
115         notifsType[1] = new String JavaDoc(SAVE_RECONFIG_TYPE);
116
117         MBeanNotificationInfo JavaDoc[] myNotifInfos = new MBeanNotificationInfo JavaDoc[2];
118         MBeanNotificationInfo JavaDoc myNotifInfo = new MBeanNotificationInfo JavaDoc(notifsType, RECONFIG_NOTIF_CLASS, "Notify service reconfiguration events");
119         myNotifInfos[0] = myNotifInfo;
120
121         myNotifInfo = new MBeanNotificationInfo JavaDoc(notifsType, SAVE_RECONFIG_NOTIF_CLASS, "Notify save reconfiguretion events");
122         myNotifInfos[1] = myNotifInfo;
123
124         return myNotifInfos;
125     }
126
127     /**
128      * Returns the listener reference (the ReconfigManager)
129      * @return the ReconfigManager reference
130      */

131     public NotificationListener JavaDoc getListener() {
132         if (myListener != null)
133             return myListener.getListener();
134         else
135             return null;
136     }
137     /**
138      * Returns the listner's filter
139      * @return the filter object
140      */

141     public NotificationFilter JavaDoc getFilter() {
142         if (myListener != null)
143             return myListener.getFilter();
144         else
145             return null;
146     }
147     /**
148      * Returns the listner's handback
149      * @return the handback object
150      */

151     public java.lang.Object JavaDoc getHandback() {
152         if (myListener != null)
153             return myListener.getHandback();
154         else
155             return null;
156     }
157
158     /**
159      * Send a <i>save configuration</i> notification to the registerd listener. If no listener,
160      * then log an error message.
161      * @param sequenceNumber notification attribute
162      * @param resourceName the name of a reconfigurable resource or of a JOnAS service
163      */

164     public void sendSaveNotification(long sequenceNumber, String JavaDoc resourceName) {
165         // create notification
166
Notification JavaDoc saveNotif = new Notification JavaDoc(SAVE_RECONFIG_TYPE, this, sequenceNumber, resourceName);
167         if (slogger.isLoggable(BasicLevel.DEBUG)) {
168             slogger.log(BasicLevel.DEBUG, saveNotif.getType() + " notification object created");
169         }
170
171         // get listener and emit notification to it
172
NotificationListener JavaDoc listenerMBean = getListener();
173         Object JavaDoc handback = getHandback();
174         if (listenerMBean != null) {
175             listenerMBean.handleNotification(saveNotif, handback);
176         } else {
177             if (slogger.isLoggable(BasicLevel.DEBUG)) {
178                 slogger.log(BasicLevel.ERROR, "MBean can't send management notification as no listener registered !!");
179             }
180         }
181     }
182
183     /**
184      * Send a <i>reconfiguration</i> notification to the registerd listener. If no listener,
185      * then log an error message.
186      * @param sequenceNumber notification attribute
187      * @param resourceName the name of a reconfigurable resource or of a JOnAS service
188      * @param userData data containing the name and the value of the reconfigured property(or properties)
189      */

190     public void sendReconfigNotification(long sequenceNumber, String JavaDoc resourceName, Object JavaDoc userData) {
191         // create notification and set the userData
192
Notification JavaDoc configNotif = new Notification JavaDoc(RECONFIG_TYPE, this, sequenceNumber, resourceName);
193         configNotif.setUserData(userData);
194         if (slogger.isLoggable(BasicLevel.DEBUG)) {
195             slogger.log(BasicLevel.DEBUG, configNotif.getType() + " notification object created");
196         }
197
198         // get listener and emit notification to it
199
NotificationListener JavaDoc listenerMBean = getListener();
200         Object JavaDoc handback = getHandback();
201         if (listenerMBean != null) {
202             listenerMBean.handleNotification(configNotif, handback);
203         } else {
204             if (slogger.isLoggable(BasicLevel.DEBUG)) {
205                 slogger.log(BasicLevel.ERROR, "MBean can't send management notification as no listener registered !!");
206             }
207         }
208     }
209 }
210
Popular Tags