1 22 package org.xsocket.stream; 23 24 import java.lang.management.ManagementFactory ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import javax.management.JMException ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 32 import org.xsocket.ILifeCycle; 33 import org.xsocket.IntrospectionBasedDynamicBean; 34 35 36 37 45 final class ConnectionPoolMBeanProxyFactory { 46 47 48 54 public static void createAndRegister(BlockingConnectionPool pool) throws JMException { 55 createAndRegister(pool, "org.xsocket.stream"); 56 } 57 58 64 public static void createAndRegister(NonBlockingConnectionPool pool) throws JMException { 65 createAndRegister(pool, "org.xsocket.stream"); 66 } 67 68 76 public static void createAndRegister(BlockingConnectionPool pool, String domain) throws JMException { 77 createAndRegister(ManagementFactory.getPlatformMBeanServer(), pool, domain); 78 } 79 80 81 89 public static void createAndRegister(NonBlockingConnectionPool pool, String domain) throws JMException { 90 createAndRegister(ManagementFactory.getPlatformMBeanServer(), pool, domain); 91 } 92 93 94 103 public static void createAndRegister(MBeanServer mbeanServer, BlockingConnectionPool pool, String domain) throws JMException { 104 ObjectName objectName = new ObjectName (domain + ":type=BlockingConnectionPool,name=" + pool.hashCode()); 105 ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(pool), objectName); 106 107 new BlockingConnectionPoolListener(pool, domain); 108 } 109 110 111 120 public static void createAndRegister(MBeanServer mbeanServer, NonBlockingConnectionPool pool, String domain) throws JMException { 121 ObjectName objectName = new ObjectName (domain + ":type=NonBlockingConnectionPool,name=" + pool.hashCode()); 122 ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(pool), objectName); 123 124 new NonBlockingConnectionPoolListener(pool, domain); 125 } 126 127 private static void unregister(BlockingConnectionPool pool, String domain) throws JMException { 128 ObjectName objectName = new ObjectName (domain + ":type=BlockingConnectionPool,name=" + pool.hashCode()); 129 ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName); 130 } 131 132 133 private static void unregister(NonBlockingConnectionPool pool, String domain) throws JMException { 134 ObjectName objectName = new ObjectName (domain + ":type=NonBlockingConnectionPool,name=" + pool.hashCode()); 135 ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName); 136 } 137 138 139 private static final class BlockingConnectionPoolListener implements ILifeCycle { 140 141 private static final Logger LOG = Logger.getLogger(BlockingConnectionPoolListener.class.getName()); 142 143 private BlockingConnectionPool pool = null; 144 private String domain = null; 145 146 BlockingConnectionPoolListener(BlockingConnectionPool pool, String domain) { 147 this.pool = pool; 148 this.domain = domain; 149 150 pool.addListener(this); 151 } 152 153 public void onInit() { 154 } 155 156 public void onDestroy() { 157 try { 158 unregister(pool, domain); 159 } catch (Exception ex) { 160 if (LOG.isLoggable(Level.FINE)) { 161 LOG.fine("error occured by deregistering the pool (domain=" + domain + "). reason: " + ex.toString()); 162 } 163 } 164 } 165 } 166 167 private static final class NonBlockingConnectionPoolListener implements ILifeCycle { 168 169 private static final Logger LOG = Logger.getLogger(BlockingConnectionPoolListener.class.getName()); 170 171 private NonBlockingConnectionPool pool = null; 172 private String domain = null; 173 174 NonBlockingConnectionPoolListener(NonBlockingConnectionPool pool, String domain) { 175 this.pool = pool; 176 this.domain = domain; 177 178 pool.addListener(this); 179 } 180 181 public void onInit() { 182 } 183 184 public void onDestroy() { 185 try { 186 unregister(pool, domain); 187 } catch (Exception ex) { 188 if (LOG.isLoggable(Level.FINE)) { 189 LOG.fine("error occured by deregistering the pool (domain=" + domain + "). reason: " + ex.toString()); 190 } 191 } 192 } 193 } 194 195 } 196 | Popular Tags |