KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > NotificationServiceMgrImpl


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.support;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Collections JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.MBeanRegistrationException JavaDoc;
33 import javax.management.Notification JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35 import javax.management.MBeanServerNotification JavaDoc;
36
37 import com.sun.appserv.management.base.NotificationServiceMgr;
38 import com.sun.appserv.management.base.NotificationService;
39 import com.sun.appserv.management.base.XTypes;
40 import com.sun.appserv.management.base.AMX;
41 import com.sun.appserv.management.util.jmx.JMXUtil;
42 import com.sun.appserv.management.base.Util;
43
44 import com.sun.enterprise.management.support.UniqueIDGenerator;
45
46
47 /**
48  */

49 public class NotificationServiceMgrImpl extends AMXImplBase
50     implements NotificationListener JavaDoc
51 {
52     private final Map JavaDoc<ObjectName JavaDoc,NotificationServiceImpl> mServices;
53     private final UniqueIDGenerator mUniqueIDs;
54     
55         public
56     NotificationServiceMgrImpl()
57     {
58         mServices = Collections.synchronizedMap( new HashMap JavaDoc<ObjectName JavaDoc,NotificationServiceImpl>() );
59         
60         mUniqueIDs = new UniqueIDGenerator( "notif-service-" );
61     }
62     
63         public void
64     handleNotification(
65         final Notification JavaDoc notifIn,
66         final Object JavaDoc handback)
67     {
68         final String JavaDoc type = notifIn.getType();
69         
70         // ensure that if a NotificationService is unregistered that we remove
71
// it from our list
72
if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION) )
73         {
74             final MBeanServerNotification JavaDoc notif = (MBeanServerNotification JavaDoc)notifIn;
75             final ObjectName JavaDoc objectName = notif.getMBeanName();
76             
77             if ( Util.getJ2EEType( objectName ).
78                     equals( XTypes.NOTIFICATION_SERVICE ) )
79             {
80                 mServices.remove( objectName );
81             }
82         }
83     }
84
85         public String JavaDoc
86     getGroup()
87     {
88         return( AMX.GROUP_UTILITY );
89     }
90     
91     /*
92     
93         public Map
94     getNotificationServiceObjectNameMap()
95     {
96         return( getContaineeObjectNameMap( XTypes.NOTIFICATION_SERVICE ) );
97     }
98     */

99     
100         public void
101     preRegisterDone()
102         throws Exception JavaDoc
103     {
104         JMXUtil.listenToMBeanServerDelegate( getMBeanServer(), this, null, null );
105     }
106     
107     
108         public ObjectName JavaDoc
109     createNotificationService(
110         final Object JavaDoc userData,
111         final int bufferSize )
112     {
113         final NotificationServiceImpl service =
114             new NotificationServiceImpl( userData, bufferSize );
115         
116         final ObjectName JavaDoc self = getObjectName();
117         
118         final String JavaDoc domain = self.getDomain();
119         final String JavaDoc childName = mUniqueIDs.createID().toString();
120         final String JavaDoc requiredProps =
121             Util.makeRequiredProps( XTypes.NOTIFICATION_SERVICE, childName );
122         
123         final ObjectName JavaDoc tempName = JMXUtil.newObjectName( domain, requiredProps );
124         
125         ObjectName JavaDoc objectName = null;
126         try
127         {
128             objectName = registerMBean( service, tempName );
129             mServices.put( objectName, service );
130         }
131         catch( Exception JavaDoc e )
132         {
133             throw new RuntimeException JavaDoc( e );
134         }
135         
136         return( objectName );
137     }
138     
139         public ObjectName JavaDoc
140     getNotificationServiceObjectName( final String JavaDoc name )
141     {
142         return( getContaineeObjectName( XTypes.NOTIFICATION_SERVICE, name ) );
143     }
144     
145         public synchronized void
146     removeNotificationService( final String JavaDoc name )
147         throws InstanceNotFoundException JavaDoc
148     {
149         final ObjectName JavaDoc objectName = getNotificationServiceObjectName( name );
150         if ( objectName == null )
151         {
152             throw new IllegalArgumentException JavaDoc( name );
153         }
154         
155         if ( ! mServices.containsKey( objectName ) )
156         {
157             throw new InstanceNotFoundException JavaDoc( objectName.toString() );
158         }
159         
160         try
161         {
162             getMBeanServer().unregisterMBean( objectName );
163         }
164         catch( MBeanRegistrationException JavaDoc e )
165         {
166             throw new RuntimeException JavaDoc( e );
167         }
168         
169         // remove it after unregistering it, in case an exception is thrown
170
mServices.remove( objectName );
171     }
172     
173 }
174
175
176
177
178
179
180
181
182
183
184
185
Popular Tags