KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > MBeanRegistrationListener


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.appserv.management.util.jmx;
24
25 import java.io.IOException JavaDoc;
26
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.NotificationFilter JavaDoc;
31 import javax.management.MBeanServerNotification JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.InstanceNotFoundException JavaDoc;
34 import com.sun.appserv.management.base.AMXRootLogger;
35
36 /**
37     Convenience base class for listening to
38     {@link MBeanServerNotification} notifications.
39     A class extending this class must implement {@link #mbeanRegistered}
40     and {@link #mbeanUnregistered}.
41     <p>
42     The class is designed to start listening upon creation.
43     The caller should call cleanup() when listening is no longer
44     desired. Once cleanup() is called, no further listening can
45     be done; a new MBeanRegistrationListener should be instantiated
46     if further listening is desired.
47  */

48 public abstract class MBeanRegistrationListener extends NotificationListenerBase
49 {
50     private final ObjectName JavaDoc mRegUnregFilter;
51     private final String JavaDoc mDefaultDomain;
52     
53     /**
54         If 'constrain' is non-null, then all registration and unregistration
55         events will be filtered through it. Only those MBeans
56         matching will be passed through to {@link #mbeanRegistered}
57         and {@link #mbeanUnregistered}.
58         
59         @param conn
60         @param constrain optional fixed or pattern ObjectName
61      */

62         public
63     MBeanRegistrationListener(
64         final MBeanServerConnection JavaDoc conn,
65         final ObjectName JavaDoc constrain )
66         throws InstanceNotFoundException JavaDoc, IOException JavaDoc
67     {
68         super( conn,
69             JMXUtil.getMBeanServerDelegateObjectName() );
70         mRegUnregFilter = constrain;
71         
72         mDefaultDomain = conn.getDefaultDomain();
73     }
74     
75     /**
76         Calls this( conn, null ).
77         @param conn
78      */

79        public
80     MBeanRegistrationListener( final MBeanServerConnection JavaDoc conn)
81         throws InstanceNotFoundException JavaDoc, IOException JavaDoc
82     {
83         this( conn, (ObjectName JavaDoc)null );
84     }
85     
86     protected abstract void mbeanRegistered( final ObjectName JavaDoc objectName );
87     protected abstract void mbeanUnregistered( final ObjectName JavaDoc objectName );
88         
89         public void
90     handleNotification( final Notification JavaDoc notifIn, final Object JavaDoc handback)
91     {
92         if ( ! (notifIn instanceof MBeanServerNotification JavaDoc) )
93         {
94             throw new IllegalArgumentException JavaDoc( notifIn.toString() );
95         }
96         
97         final MBeanServerNotification JavaDoc notif = (MBeanServerNotification JavaDoc)notifIn;
98         final ObjectName JavaDoc objectName = notif.getMBeanName();
99         final String JavaDoc type = notif.getType();
100         
101         final boolean matchesFilter = (mRegUnregFilter == null) ||
102             JMXUtil.matchesPattern( mDefaultDomain, mRegUnregFilter, objectName );
103             
104         if ( matchesFilter )
105         {
106             if ( type.equals( MBeanServerNotification.REGISTRATION_NOTIFICATION ) )
107             {
108                 mbeanRegistered( objectName );
109             }
110             else if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION ) )
111             {
112                 mbeanUnregistered( objectName );
113             }
114         }
115     }
116 }
117
118
119
120
121
Popular Tags