KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > notification > ClientNotificationManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.jmx.remote.notification;
25
26 import java.io.IOException JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.net.InetAddress JavaDoc;
33 import javax.management.*;
34
35 import com.sun.enterprise.admin.jmx.remote.notification.SimpleQueue;
36 import com.sun.enterprise.admin.jmx.remote.notification.NotificationWrapper;
37 import com.sun.enterprise.admin.jmx.remote.notification.ListenerInfo;
38 import com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress;
39 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
40
41
42 /**
43  * A NotificationManager class used on the client-side to mainly
44  * call the NotificationListeners appropriately
45  * whenever a notification is received from the server-side.
46  * The ClientNotificationManager uses a NotificationReceiver running on a
47  * separate thread to listen for notifications.
48  * The ClientNotificationManager when constructed will make a connection
49  * to the server-side with an unique id identifying this ClientNotificationManager.
50  */

51 public class ClientNotificationManager implements Runnable JavaDoc {
52     private Map JavaDoc env = null;
53     private HashMap JavaDoc listenerMap = null;
54     private String JavaDoc mgrId = null;
55     private SimpleQueue que = null;
56     private NotificationReceiver receiver = null;
57     private Thread JavaDoc eventThread = null;
58     private boolean exit = false;
59     
60     private static final Logger JavaDoc logger = Logger.getLogger(
61         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
62         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

63
64     public ClientNotificationManager(HttpConnectorAddress ad, Map JavaDoc env)
65             throws IOException JavaDoc {
66         this.env = env;
67         que = new SimpleQueue();
68         listenerMap = new HashMap JavaDoc();
69         
70         String JavaDoc hname="null";
71         try {
72             hname = InetAddress.getLocalHost().getHostName();
73         } catch (Exception JavaDoc ex) { /*ignore*/ }
74         mgrId = (new java.rmi.server.UID JavaDoc()).toString() + ":" + hname;
75
76         eventThread = new Thread JavaDoc(this);
77         eventThread.start();
78         receiver = new NotificationReceiver(ad, this);
79     }
80
81     public String JavaDoc getId() {
82         return mgrId;
83     }
84
85     /**
86      * A method to reinitialize the connection to the server-side in case
87      * the connection gets dropped.
88      * This method is called for every MBeanServerConnection method call.
89      */

90     public boolean reinit() throws IOException JavaDoc {
91         return receiver.reinit();
92     }
93
94     /**
95      * Registers the notification listener so that they may be called
96      * in case a notification is received.
97      */

98     public String JavaDoc addNotificationListener( ObjectName objname,
99                                             NotificationListener listener,
100                                             NotificationFilter filter,
101                                             Object JavaDoc handback) {
102         ListenerInfo info = new ListenerInfo();
103
104         info.listener = listener;
105         info.filter = filter;
106         info.handback = handback;
107         info.id = info.computeId();
108
109         ArrayList JavaDoc list = (ArrayList JavaDoc)listenerMap.get(objname);
110         if (list == null) {
111             list = new ArrayList JavaDoc();
112         }
113         list.add(info);
114
115         listenerMap.put(objname, list);
116
117         return info.id;
118     }
119
120     public String JavaDoc[] removeNotificationListener(
121                             ObjectName mbean,
122                             NotificationListener listener) {
123         String JavaDoc[] strs = removeNotificationListener(mbean, listener, null, null, true);
124         return strs;
125     }
126
127     public String JavaDoc[] removeNotificationListener(
128                             ObjectName mbean,
129                             NotificationListener listener,
130                             NotificationFilter filter,
131                             Object JavaDoc handback) {
132         String JavaDoc[] strs = removeNotificationListener( mbean,
133                                                     listener,
134                                                     filter,
135                                                     handback, false);
136         return strs;
137     }
138
139     /**
140      * Unregisters the notification listeners as per the logic defined by
141      * MBeanServer.removeNotificationListener
142      * 1. <mbean, listener> -- all the registrations for the listener to the specified mbean
143      * are removed.
144      * 2. <mbean, listener, filter, handback>
145      * -- only that registration, for the listener, which was registered
146      * with this filter and handback will be removed.
147      */

148     private String JavaDoc[] removeNotificationListener(
149                             ObjectName mbean,
150                             NotificationListener listener,
151                             NotificationFilter filter,
152                             Object JavaDoc handback,
153                             boolean listenerOnly) {
154         ArrayList JavaDoc idlist = new ArrayList JavaDoc();
155         ArrayList JavaDoc list = (ArrayList JavaDoc) listenerMap.get(mbean);
156         if (list == null) {
157             return (new String JavaDoc[0]);
158         }
159
160         ListenerInfo info1 = new ListenerInfo();
161         info1.listener = listener;
162         info1.filter = filter;
163         info1.handback = handback;
164         info1.id = info1.computeId();
165
166         Iterator JavaDoc itr = list.iterator();
167         // Because updating the list when we are iterating the list throws an exception,
168
// unless we return immediately
169
ArrayList JavaDoc list1 = (ArrayList JavaDoc) list.clone();
170         while (itr.hasNext()) {
171             ListenerInfo info = (ListenerInfo) itr.next();
172             if (!listenerOnly && info.id.equals(info1.id)) {
173                 list1.remove(list1.indexOf(info));
174                 idlist.add(info.id);
175             } else if (listenerOnly && info.listener == listener) {
176                 list1.remove(list1.indexOf(info));
177                 idlist.add(info.id);
178             }
179         }
180
181         listenerMap.put(mbean, list1);
182
183         String JavaDoc[] ids = new String JavaDoc[idlist.size()];
184         ids = (String JavaDoc[]) idlist.toArray(ids);
185         return ids;
186     }
187
188     public void raiseEvent(NotificationWrapper wrapr) {
189         synchronized (que) {
190             que.add(wrapr);
191             que.notify();
192         }
193     }
194
195     private boolean isExiting() {
196         return exit;
197     }
198
199     public void close() throws Exception JavaDoc {
200         exit = true;
201         try {
202             receiver.exit();
203         } finally {
204             synchronized (que) {
205                 que.notify();
206             }
207             eventThread.join();
208         }
209     }
210
211     /**
212      * This is the event dispatch thread, which calls the notification listeners
213      * as and when a notification is received for that listener.
214      */

215     public void run() {
216         while (!isExiting()) {
217             synchronized (que) {
218                 while (que.isEmpty() && !isExiting()) {
219                     try {
220                         que.wait();
221                     } catch (InterruptedException JavaDoc intre) {
222                     }
223                 }
224             }
225             if (isExiting())
226                 break;
227             while (!que.isEmpty() && !isExiting()) {
228                 NotificationWrapper wrapr = (NotificationWrapper) que.remove();
229                 ObjectName source = wrapr.getSource();
230                 Notification notif = wrapr.getNotification();
231
232                 ArrayList JavaDoc listeners = (ArrayList JavaDoc) listenerMap.get(source);
233                 Iterator JavaDoc itr = listeners.iterator();
234                 while (itr.hasNext() && !isExiting()) {
235                     ListenerInfo info = (ListenerInfo) itr.next();
236                     boolean callListener = true;
237                     if (info.filter != null)
238                         callListener = info.filter.isNotificationEnabled(notif);
239                     if (callListener)
240                         info.listener.handleNotification(notif, info.handback);
241                 }
242             }
243         }
244     }
245 }
246
Popular Tags