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.IDispatcher; 33 import org.xsocket.IWorkerPool; 34 import org.xsocket.stream.io.impl.JmxIoProvider; 35 import org.xsocket.stream.io.spi.IServerIoJmxProvider; 36 37 38 39 47 public final class MultithreadedServerMBeanProxyFactory { 48 49 private static final Logger LOG = Logger.getLogger(MultithreadedServerMBeanProxyFactory.class.getName()); 50 51 private static IServerIoJmxProvider jmxProvider = null; 52 53 54 static { 55 String jmxIoProviderClassname = System.getProperty(IServerIoJmxProvider.PROVIDER_CLASSNAME_KEY); 56 if (jmxIoProviderClassname != null) { 57 try { 58 Class jmxProviderClass = Class.forName(jmxIoProviderClassname); 59 jmxProvider = (IServerIoJmxProvider) jmxProviderClass.newInstance(); 60 } catch (Exception e) { 61 LOG.warning("error occured by creating jmxProivder " + jmxIoProviderClassname + ": " + e.toString()); 62 } 63 } 64 65 if (jmxProvider == null) { 66 jmxProvider = new JmxIoProvider(); 67 } 68 } 69 70 71 72 73 79 public static void createAndRegister(MultithreadedServer server) throws JMException { 80 createAndRegister(server, "org.xsocket.stream"); 81 } 82 83 84 92 public static void createAndRegister(MultithreadedServer server, String domain) throws JMException { 93 createAndRegister(ManagementFactory.getPlatformMBeanServer(), server, domain); 94 } 95 96 97 106 public static void createAndRegister(MBeanServer mbeanServer, MultithreadedServer server, String domain) throws JMException { 107 ObjectName objectName = new ObjectName (domain + ":type=MultithreadedServer,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort()); 108 109 Object mbean = jmxProvider.createMBean(server, server.getAcceptor(), domain); 110 ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, objectName); 111 112 server.addListener(new Listener(server, domain)); 113 } 114 115 116 private static void unregister(IMultithreadedServer server, String domain) throws JMException { 117 ObjectName objectName = new ObjectName (domain + ":type=MultithreadedServer,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort()); 118 ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName); 119 } 120 121 122 private static final class Listener implements IMutlithreadedServerListener { 123 124 private static final Logger LOG = Logger.getLogger(Listener.class.getName()); 125 126 private MultithreadedServer server = null; 127 private String domain = null; 128 129 Listener(MultithreadedServer server, String domain) { 130 this.server = server; 131 this.domain = domain; 132 133 server.addListener(this); 134 } 135 136 public void onInit() { 137 } 138 139 public void onDestroy() { 140 try { 141 unregister(server, domain); 142 } catch (Exception ex) { 143 if (LOG.isLoggable(Level.FINE)) { 144 LOG.fine("error occured by deregistering the server (domain=" + domain + "). reason: " + ex.toString()); 145 } 146 } 147 } 148 149 public void onWorkerPoolUpdated(IWorkerPool oldWorkerPool, IWorkerPool newWorkerPool) { } 150 151 152 public void onDispatcherAdded(IDispatcher dispatcher) { } 153 154 public void onDispatcherRemoved(IDispatcher dispatcher) { } 155 } 156 } 157 | Popular Tags |