1 22 package org.jboss.mx.notification; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 27 import javax.management.JMException ; 28 import javax.management.ListenerNotFoundException ; 29 import javax.management.NotificationBroadcaster ; 30 import javax.management.NotificationFilter ; 31 import javax.management.NotificationListener ; 32 import javax.management.ObjectName ; 33 34 51 public class MBeanServerListenerRegistry 52 { 53 55 58 private HashMap registries = new HashMap (); 59 60 62 65 public MBeanServerListenerRegistry() 66 { 67 } 68 69 71 81 public void add(ObjectName name, NotificationBroadcaster broadcaster, 82 NotificationListener listener, NotificationFilter filter, Object handback) 83 { 84 if (name == null) 85 throw new IllegalArgumentException ("Null name"); 86 if (listener == null) 87 throw new IllegalArgumentException ("Null listener"); 88 89 ListenerRegistry registry = null; 90 synchronized(registries) 91 { 92 registry = (ListenerRegistry) registries.get(name); 93 if (registry == null) 94 registry = new ListenerRegistry(new MBeanServerListenerRegistrationFactory(name, broadcaster)); 95 registries.put(name, registry); 96 } 97 98 try 99 { 100 registry.add(listener, filter, handback); 101 } 102 catch (JMException e) 103 { 104 throw new RuntimeException (e.toString()); 106 } 107 } 108 109 115 public void remove(ObjectName name) 116 { 117 if (name == null) 118 throw new IllegalArgumentException ("Null name"); 119 120 ListenerRegistry registry = null; 121 synchronized(registries) 122 { 123 registry = (ListenerRegistry) registries.remove(name); 124 if (registry == null) 125 return; 126 } 127 128 for (ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator(); iterator.hasNext();) 130 { 131 ListenerRegistration registration = iterator.nextRegistration(); 132 registration.removed(); 133 } 134 } 135 136 144 public void remove(ObjectName name, NotificationListener listener) 145 throws ListenerNotFoundException 146 { 147 if (name == null) 148 throw new IllegalArgumentException ("Null name"); 149 150 synchronized(registries) 151 { 152 ListenerRegistry registry = (ListenerRegistry) registries.get(name); 153 if (registry == null) 154 throw new ListenerNotFoundException ("Listener not found " + listener + " for object name " + name); 155 156 registry.remove(listener); 157 if (registry.isEmpty()) 158 registries.remove(name); 159 } 160 } 161 162 172 public void remove(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) 173 throws ListenerNotFoundException 174 { 175 if (name == null) 176 throw new IllegalArgumentException ("Null name"); 177 178 synchronized(registries) 179 { 180 ListenerRegistry registry = (ListenerRegistry) registries.get(name); 181 if (registry == null) 182 throw new ListenerNotFoundException ("Listener not found listener=" + listener + 183 " filter=" + filter + " handback=" + handback + 184 " for object name " + name); 185 186 registry.remove(listener, filter, handback); 187 if (registry.isEmpty()) 188 registries.remove(name); 189 } 190 } 191 192 195 public void removeAll() 196 { 197 synchronized (registries) 198 { 199 Iterator it = registries.keySet().iterator(); 200 201 while (it.hasNext()) 202 { 203 ListenerRegistry registry = (ListenerRegistry)registries.get(it.next()); 204 registry.removeAll(); 205 } 206 } 207 } 208 209 } 210 | Popular Tags |