1 8 9 package mx4j.tools.remote; 10 11 import java.io.IOException ; 12 import java.util.Collections ; 13 import java.util.Map ; 14 import javax.management.MBeanServer ; 15 import javax.management.remote.JMXConnectorServer ; 16 import javax.management.remote.JMXServiceURL ; 17 18 import mx4j.log.Log; 19 import mx4j.log.Logger; 20 import mx4j.remote.MX4JRemoteUtils; 21 22 30 public abstract class AbstractJMXConnectorServer extends JMXConnectorServer 31 { 32 private JMXServiceURL url; 33 private final Map environment; 34 private volatile boolean active; 35 private volatile boolean stopped; 36 37 public AbstractJMXConnectorServer(JMXServiceURL url, Map environment, MBeanServer server) 38 { 39 super(server); 40 this.url = url; 41 this.environment = environment; 42 } 43 44 public synchronized JMXServiceURL getAddress() 45 { 46 return url; 47 } 48 49 52 protected synchronized void setAddress(JMXServiceURL url) 53 { 54 this.url = url; 55 } 56 57 public synchronized Map getAttributes() 58 { 59 Map env = MX4JRemoteUtils.removeNonSerializableEntries(getEnvironment()); 60 return Collections.unmodifiableMap(env); 61 } 62 63 66 protected synchronized Map getEnvironment() 67 { 68 return environment; 69 } 70 71 public boolean isActive() 72 { 73 return active; 74 } 75 76 79 protected boolean isStopped() 80 { 81 return stopped; 82 } 83 84 public synchronized void start() throws IOException , IllegalStateException 85 { 86 Logger logger = getLogger(); 87 88 if (isActive()) 89 { 90 if (logger.isEnabledFor(Logger.TRACE)) logger.trace("This JMXConnectorServer has already been started"); 91 return; 92 } 93 if (isStopped()) 94 { 95 if (logger.isEnabledFor(Logger.TRACE)) logger.trace("This JMXConnectorServer has already been stopped"); 96 throw new IOException ("This RMIConnectorServer has already been stopped"); 97 } 98 99 doStart(); 100 101 active = true; 102 103 if (logger.isEnabledFor(Logger.INFO)) logger.info("JMXConnectorServer started at: " + getAddress()); 104 } 105 106 109 protected abstract void doStart() throws IOException , IllegalStateException ; 110 111 public synchronized void stop() throws IOException 112 { 113 if (!isActive() || isStopped()) return; 114 115 stopped = true; 116 active = false; 117 118 doStop(); 119 120 Logger logger = getLogger(); 121 if (logger.isEnabledFor(Logger.INFO)) logger.info("JMXConnectorServer stopped at: " + getAddress()); 122 } 123 124 127 protected abstract void doStop() throws IOException ; 128 129 protected Logger getLogger() 130 { 131 return Log.getLogger(getClass().getName()); 132 } 133 134 public void connectionOpened(String connectionId, String message, Object userData) 135 { 136 super.connectionOpened(connectionId, message, userData); 137 } 138 139 public void connectionClosed(String connectionId, String message, Object userData) 140 { 141 super.connectionClosed(connectionId, message, userData); 142 } 143 144 public void connectionFailed(String connectionId, String message, Object userData) 145 { 146 super.connectionFailed(connectionId, message, userData); 147 } 148 } 149 | Popular Tags |