KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > notification > MBeanServerListenerRegistry


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mx.notification;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.management.JMException JavaDoc;
28 import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.NotificationBroadcaster JavaDoc;
30 import javax.management.NotificationFilter JavaDoc;
31 import javax.management.NotificationListener JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 /**
35  * A notification listener registry per ObjectName.
36  *
37  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
38  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>
39  *
40  * @version $Revision: 37459 $
41  *
42  * <p><b>Revisions:</b>
43  *
44  * <p><b>20030806 Juha Lindfors:</b>
45  * <ul>
46  * <li>
47  * Added removeAll()
48  * </ul>
49  *
50  */

51 public class MBeanServerListenerRegistry
52 {
53    // Attributes ----------------------------------------------------
54

55    /**
56     * A map of object names to listener registries.
57     */

58    private HashMap JavaDoc registries = new HashMap JavaDoc();
59
60    // Constructor ---------------------------------------------------
61

62    /**
63     * Create a notification listener registry
64     */

65    public MBeanServerListenerRegistry()
66    {
67    }
68
69    // Public --------------------------------------------------------
70

71    /**
72     * Adds a listener to the mbean
73     *
74     * @param name the object name
75     * @param broadcaster the broadcaster
76     * @param listener the listener to register
77     * @param filter filters the notifications in the broadcaster, can be null
78     * @param handback the object to include in the notification, can be null
79     * @exception IllegalArgumentException for a null object name or listener
80     */

81    public void add(ObjectName JavaDoc name, NotificationBroadcaster JavaDoc broadcaster,
82                    NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
83    {
84       if (name == null)
85          throw new IllegalArgumentException JavaDoc("Null name");
86       if (listener == null)
87          throw new IllegalArgumentException JavaDoc("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 JavaDoc e)
103       {
104          // This shouldn't happen
105
throw new RuntimeException JavaDoc(e.toString());
106       }
107    }
108
109    /**
110     * Removes all registrations for an mbean broadcaster.
111     *
112     * @param name the object name
113     * @exception IllegalArgumentException for a null object name
114     */

115    public void remove(ObjectName JavaDoc name)
116    {
117       if (name == null)
118          throw new IllegalArgumentException JavaDoc("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       // Remove the registrations with the MBean
129
for (ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator(); iterator.hasNext();)
130       {
131          ListenerRegistration registration = iterator.nextRegistration();
132          registration.removed();
133       }
134    }
135
136    /**
137     * Removes all registrations for a listener.
138     *
139     * @param name the object name
140     * @param listener the listener to remove
141     * @exception ListenerNotFoundException when the listener is not registered
142     * @exception IllegalArgumentException for a null object name
143     */

144    public void remove(ObjectName JavaDoc name, NotificationListener JavaDoc listener)
145       throws ListenerNotFoundException JavaDoc
146    {
147       if (name == null)
148          throw new IllegalArgumentException JavaDoc("Null name");
149
150       synchronized(registries)
151       {
152          ListenerRegistry registry = (ListenerRegistry) registries.get(name);
153          if (registry == null)
154             throw new ListenerNotFoundException JavaDoc("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    /**
163     * Removes only the registrations for a listener that match the filter and handback.
164     *
165     * @param name the object name
166     * @param listener the listener to remove
167     * @param filter the filter of the registration to remove
168     * @param handback the handback object of the registration to remove
169     * @exception ListenerNotFoundException when the listener is not registered
170     * @exception IllegalArgumentException for a null object name
171     */

172    public void remove(ObjectName JavaDoc name, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
173       throws ListenerNotFoundException JavaDoc
174    {
175       if (name == null)
176          throw new IllegalArgumentException JavaDoc("Null name");
177
178       synchronized(registries)
179       {
180          ListenerRegistry registry = (ListenerRegistry) registries.get(name);
181          if (registry == null)
182             throw new ListenerNotFoundException JavaDoc("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    /**
193     * Clears all listener registries from this registry.
194     */

195    public void removeAll()
196    {
197       synchronized (registries)
198       {
199          Iterator JavaDoc 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