1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Collections ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanRegistrationException ; 33 import javax.management.Notification ; 34 import javax.management.NotificationListener ; 35 import javax.management.MBeanServerNotification ; 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 49 public class NotificationServiceMgrImpl extends AMXImplBase 50 implements NotificationListener 51 { 52 private final Map <ObjectName ,NotificationServiceImpl> mServices; 53 private final UniqueIDGenerator mUniqueIDs; 54 55 public 56 NotificationServiceMgrImpl() 57 { 58 mServices = Collections.synchronizedMap( new HashMap <ObjectName ,NotificationServiceImpl>() ); 59 60 mUniqueIDs = new UniqueIDGenerator( "notif-service-" ); 61 } 62 63 public void 64 handleNotification( 65 final Notification notifIn, 66 final Object handback) 67 { 68 final String type = notifIn.getType(); 69 70 if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION) ) 73 { 74 final MBeanServerNotification notif = (MBeanServerNotification )notifIn; 75 final ObjectName 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 86 getGroup() 87 { 88 return( AMX.GROUP_UTILITY ); 89 } 90 91 99 100 public void 101 preRegisterDone() 102 throws Exception 103 { 104 JMXUtil.listenToMBeanServerDelegate( getMBeanServer(), this, null, null ); 105 } 106 107 108 public ObjectName 109 createNotificationService( 110 final Object userData, 111 final int bufferSize ) 112 { 113 final NotificationServiceImpl service = 114 new NotificationServiceImpl( userData, bufferSize ); 115 116 final ObjectName self = getObjectName(); 117 118 final String domain = self.getDomain(); 119 final String childName = mUniqueIDs.createID().toString(); 120 final String requiredProps = 121 Util.makeRequiredProps( XTypes.NOTIFICATION_SERVICE, childName ); 122 123 final ObjectName tempName = JMXUtil.newObjectName( domain, requiredProps ); 124 125 ObjectName objectName = null; 126 try 127 { 128 objectName = registerMBean( service, tempName ); 129 mServices.put( objectName, service ); 130 } 131 catch( Exception e ) 132 { 133 throw new RuntimeException ( e ); 134 } 135 136 return( objectName ); 137 } 138 139 public ObjectName 140 getNotificationServiceObjectName( final String name ) 141 { 142 return( getContaineeObjectName( XTypes.NOTIFICATION_SERVICE, name ) ); 143 } 144 145 public synchronized void 146 removeNotificationService( final String name ) 147 throws InstanceNotFoundException 148 { 149 final ObjectName objectName = getNotificationServiceObjectName( name ); 150 if ( objectName == null ) 151 { 152 throw new IllegalArgumentException ( name ); 153 } 154 155 if ( ! mServices.containsKey( objectName ) ) 156 { 157 throw new InstanceNotFoundException ( objectName.toString() ); 158 } 159 160 try 161 { 162 getMBeanServer().unregisterMBean( objectName ); 163 } 164 catch( MBeanRegistrationException e ) 165 { 166 throw new RuntimeException ( e ); 167 } 168 169 mServices.remove( objectName ); 171 } 172 173 } 174 175 176 177 178 179 180 181 182 183 184 185 | Popular Tags |