KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > alert > MBeanRegistrationEventListener


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 package com.sun.enterprise.admin.alert;
24
25 import java.util.logging.ErrorManager JavaDoc;
26
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.MBeanServerNotification JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31
32 import com.sun.enterprise.admin.server.core.AdminService;
33
34
35 import java.util.List JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Class MBeanRegistrationEventListener listens to MBeanRegistered event.
40  * The main purpose of this class is to introspect the 'name' element in
41  * objectName of the MBean (or Monitor) registered and check to see if it
42  * matches with any of the names listed for alert-subscription. If it matches
43  * then the listener will be subscribed with the MBean.
44  *
45  * @AUTHOR: Hemanth Puttaswamy
46  */

47 public class MBeanRegistrationEventListener implements NotificationListener JavaDoc {
48     // All the alert subscriptions from domain.xml
49
private List JavaDoc alertSubscriptions;
50
51     private MBeanServer JavaDoc mbeanServer;
52
53     private static final String JavaDoc NAME_PROPERTY_KEY = "name";
54  
55     /**
56      * Constructor which accepts a list of AlertSubscriptions based on
57      * domain.xml entries.
58      */

59     public MBeanRegistrationEventListener( List JavaDoc alertSubscriptions ) {
60         this.alertSubscriptions = alertSubscriptions;
61         mbeanServer =
62             AdminService.getAdminService().getAdminContext().getMBeanServer();
63     }
64
65
66     /**
67      * If the event type is REGISTRATION_NOTIFICATION and if the 'name' element
68      * in registered MBean's ObjectName matches one of names in the alert
69      * subscriptions then we will add the Notification Listener and the Filter
70      * to the MBean.
71      */

72     public void handleNotification( Notification JavaDoc notification,
73         Object JavaDoc handBack )
74     {
75         if( !notification.getType().equals(
76             MBeanServerNotification.REGISTRATION_NOTIFICATION ) )
77         {
78             // We are only interested in Registration event
79
return;
80         }
81         try {
82             MBeanServerNotification JavaDoc mbeanServerNotification =
83                 (MBeanServerNotification JavaDoc) notification;
84             String JavaDoc registeredMbeanName =
85                 mbeanServerNotification.getMBeanName().getKeyProperty(
86                     NAME_PROPERTY_KEY );
87             // Registered ObjectName doesn't have the 'name' key value. So,
88
// no action needs to be taken.
89
if( registeredMbeanName == null ) return;
90             Iterator JavaDoc iterator = alertSubscriptions.iterator( );
91             while ( iterator.hasNext( ) ) {
92                 AlertSubscriptionInfo subscription =
93                     (AlertSubscriptionInfo)iterator.next();
94                 if( matches( subscription.getMonitorNames(),
95                     registeredMbeanName ))
96                 {
97                     mbeanServer.addNotificationListener(
98                         mbeanServerNotification.getMBeanName(),
99                             subscription.getNotificationListener(),
100                             subscription.getNotificationFilter(), null );
101                 }
102             }
103         } catch( Exception JavaDoc e ) {
104              new ErrorManager JavaDoc().error( "Error In " +
105                   " MBeanServerRegistrationEventListener ", e,
106                   ErrorManager.GENERIC_FAILURE );
107         }
108     }
109
110     /**
111      * A simple utility method to check to see if the registeredMBeanName
112      * matches with one of the monitorNames for alert subscription.
113      */

114     private boolean matches( List JavaDoc monitorNames, String JavaDoc registeredMBeanName ) {
115         Iterator JavaDoc iterator = monitorNames.iterator( );
116         while( iterator.hasNext( ) ) {
117             if( registeredMBeanName.equals( (String JavaDoc) iterator.next() ) ) {
118                 return true;
119             }
120         }
121         return false;
122     }
123 }
124                      
125
126     
127
Popular Tags