KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > listener > NotificationListener


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.test.jmx.listener;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.management.Notification JavaDoc;
29
30 import org.jboss.system.ListenerServiceMBeanSupport;
31
32 /**
33  * A test service that subscribes and stores JMX Notifications.
34  *
35  * The subscription are specified declaratively using the
36  * ListenerServiceMBean SubscriptionList attribute.
37  *
38  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
39  * @version $Revision: 37406 $
40  */

41 public class NotificationListener extends ListenerServiceMBeanSupport
42    implements NotificationListenerMBean
43 {
44    // Private Data --------------------------------------------------
45

46   /** Stored notifications */
47   private List JavaDoc notifList;
48   
49   // Constructors --------------------------------------------------
50

51   /**
52    * CTOR
53   **/

54   public NotificationListener()
55   {
56      notifList = Collections.synchronizedList(new ArrayList JavaDoc());
57   }
58   
59   // NotificationListenerMBean implementation -----------------------
60

61    /**
62     * Number of notifications received.
63     */

64    public int getNotificationCount()
65    {
66       return notifList.size();
67    }
68    
69    /**
70     * @return a list of Notifications received
71     */

72    public List JavaDoc retrieveNotifications()
73    {
74       return new ArrayList JavaDoc(notifList);
75    }
76
77    /**
78     * Clear the stored notifications, reset the counter
79     */

80    public void clearNotifications()
81    {
82       notifList.clear();
83    }
84    
85    // Lifecycle -----------------------------------------------------
86

87    protected void startService() throws Exception JavaDoc
88    {
89       clearNotifications();
90       super.subscribe(false);
91    }
92    
93    protected void stopService()
94    {
95       super.unsubscribe();
96       clearNotifications();
97    }
98    
99    // ListenerServiceMBeanSupport -----------------------------------
100

101    public void handleNotification2(Notification JavaDoc notification, Object JavaDoc handback)
102    {
103       notifList.add(notification);
104       log.debug("Received notification #" + notifList.size() + ": " + notification);
105    }
106 }
107
Popular Tags