1 23 24 package com.sun.enterprise.admin.jmx.remote.notification; 25 26 import java.io.IOException ; 27 import java.util.logging.Logger ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.net.InetAddress ; 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 51 public class ClientNotificationManager implements Runnable { 52 private Map env = null; 53 private HashMap listenerMap = null; 54 private String mgrId = null; 55 private SimpleQueue que = null; 56 private NotificationReceiver receiver = null; 57 private Thread eventThread = null; 58 private boolean exit = false; 59 60 private static final Logger logger = Logger.getLogger( 61 DefaultConfiguration.JMXCONNECTOR_LOGGER); 63 64 public ClientNotificationManager(HttpConnectorAddress ad, Map env) 65 throws IOException { 66 this.env = env; 67 que = new SimpleQueue(); 68 listenerMap = new HashMap (); 69 70 String hname="null"; 71 try { 72 hname = InetAddress.getLocalHost().getHostName(); 73 } catch (Exception ex) { } 74 mgrId = (new java.rmi.server.UID ()).toString() + ":" + hname; 75 76 eventThread = new Thread (this); 77 eventThread.start(); 78 receiver = new NotificationReceiver(ad, this); 79 } 80 81 public String getId() { 82 return mgrId; 83 } 84 85 90 public boolean reinit() throws IOException { 91 return receiver.reinit(); 92 } 93 94 98 public String addNotificationListener( ObjectName objname, 99 NotificationListener listener, 100 NotificationFilter filter, 101 Object 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 list = (ArrayList )listenerMap.get(objname); 110 if (list == null) { 111 list = new ArrayList (); 112 } 113 list.add(info); 114 115 listenerMap.put(objname, list); 116 117 return info.id; 118 } 119 120 public String [] removeNotificationListener( 121 ObjectName mbean, 122 NotificationListener listener) { 123 String [] strs = removeNotificationListener(mbean, listener, null, null, true); 124 return strs; 125 } 126 127 public String [] removeNotificationListener( 128 ObjectName mbean, 129 NotificationListener listener, 130 NotificationFilter filter, 131 Object handback) { 132 String [] strs = removeNotificationListener( mbean, 133 listener, 134 filter, 135 handback, false); 136 return strs; 137 } 138 139 148 private String [] removeNotificationListener( 149 ObjectName mbean, 150 NotificationListener listener, 151 NotificationFilter filter, 152 Object handback, 153 boolean listenerOnly) { 154 ArrayList idlist = new ArrayList (); 155 ArrayList list = (ArrayList ) listenerMap.get(mbean); 156 if (list == null) { 157 return (new String [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 itr = list.iterator(); 167 ArrayList list1 = (ArrayList ) 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 [] ids = new String [idlist.size()]; 184 ids = (String []) 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 { 200 exit = true; 201 try { 202 receiver.exit(); 203 } finally { 204 synchronized (que) { 205 que.notify(); 206 } 207 eventThread.join(); 208 } 209 } 210 211 215 public void run() { 216 while (!isExiting()) { 217 synchronized (que) { 218 while (que.isEmpty() && !isExiting()) { 219 try { 220 que.wait(); 221 } catch (InterruptedException 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 listeners = (ArrayList ) listenerMap.get(source); 233 Iterator 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 |