KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > CreateRemoveListener


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.management;
24
25 import java.util.Map JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ListenerNotFoundException JavaDoc;
32
33 import com.sun.appserv.management.base.Util;
34 import com.sun.appserv.management.base.Container;
35 import com.sun.appserv.management.config.AMXConfig;
36 import com.sun.appserv.management.util.stringifier.SmartStringifier;
37 import com.sun.appserv.management.util.misc.TypeCast;
38
39     
40 /**
41     A NotificationListener which expects to receive a
42     CONFIG_CREATED_NOTIFICATION_TYPE and CONFIG_REMOVED_NOTIFICATION_TYPE
43     from an MBean with a particular j2eeType and name.
44  */

45 public final class CreateRemoveListener implements NotificationListener JavaDoc
46 {
47     private final String JavaDoc mNameExpected;
48     private final String JavaDoc mJ2EETypeExpected;
49     private final Container mSource;
50     
51     private Notification JavaDoc mCreateNotif;
52     private Notification JavaDoc mRemoveNotif;
53     
54         public
55     CreateRemoveListener(
56         final Container source,
57         final String JavaDoc j2eeTypeExpected,
58         final String JavaDoc nameExpected )
59     {
60         mSource = source;
61         mNameExpected = nameExpected;
62         mJ2EETypeExpected = j2eeTypeExpected;
63         
64         mSource.addNotificationListener( this, null, null );
65     }
66     
67         public void
68     handleNotification(
69         final Notification JavaDoc notifIn,
70         final Object JavaDoc handback)
71     {
72         final String JavaDoc type = notifIn.getType();
73
74         //final Map<String,Serializable> m = getAMXNotificationData * notifIn );
75
final ObjectName JavaDoc objectName =
76             Util.getAMXNotificationValue( notifIn, AMXConfig.CONFIG_OBJECT_NAME_KEY, ObjectName JavaDoc.class );
77         
78         //trace( "CreateRemoveListener:\n" + SmartStringifier.toString( notifIn ) + ":\n" + objectName );
79

80         if ( Util.getJ2EEType( objectName ).equals( mJ2EETypeExpected ) &&
81             Util.getName( objectName ).equals( mNameExpected ) )
82         {
83             if ( type.equals( AMXConfig.CONFIG_CREATED_NOTIFICATION_TYPE ) )
84             {
85                 mCreateNotif = notifIn;
86             }
87             else if ( type.equals( AMXConfig.CONFIG_REMOVED_NOTIFICATION_TYPE ) )
88             {
89                 mRemoveNotif = notifIn;
90             }
91         }
92     }
93             protected void
94     trace( Object JavaDoc o )
95     {
96         System.out.println( SmartStringifier.toString( o ) );
97     }
98     
99         public void
100     waitCreate()
101     {
102         long millis = 10;
103         
104         while ( mCreateNotif == null )
105         {
106             AMXTestBase.mySleep( millis );
107             trace( "waiting " + millis + "ms for CONFIG_CREATED_NOTIFICATION_TYPE for " + mNameExpected);
108             millis *= 2;
109         }
110     }
111     
112         public void
113     waitRemove()
114     {
115         long millis = 10;
116         while ( mRemoveNotif == null )
117         {
118             AMXTestBase.mySleep( millis );
119             trace( "waiting " + millis + "ms for CONFIG_REMOVED_NOTIFICATION_TYPE for " + mNameExpected);
120             millis *= 2;
121         }
122     }
123     
124     
125         public void
126     waitNotifs()
127     {
128         waitCreate();
129         waitRemove();
130         
131         try
132         {
133             mSource.removeNotificationListener( (NotificationListener JavaDoc)this, null, null );
134         }
135         catch( ListenerNotFoundException JavaDoc e )
136         {
137             throw new RuntimeException JavaDoc( e );
138         }
139     }
140 }
Popular Tags