1 8 9 package mx4j.tools.remote.http; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.Map ; 14 15 import javax.management.MBeanServerConnection ; 16 import javax.management.remote.JMXServiceURL ; 17 import javax.security.auth.Subject ; 18 19 import mx4j.remote.ConnectionNotificationEmitter; 20 import mx4j.remote.ConnectionResolver; 21 import mx4j.remote.HeartBeat; 22 import mx4j.remote.RemoteNotificationClientHandler; 23 import mx4j.tools.remote.AbstractJMXConnector; 24 25 28 public class HTTPConnector extends AbstractJMXConnector 29 { 30 private transient HTTPConnection connection; 31 private transient String connectionId; 32 private transient HeartBeat heartbeat; 33 private transient RemoteNotificationClientHandler notificationHandler; 34 35 public HTTPConnector(JMXServiceURL address, Map environment) throws IOException 36 { 37 super(address); 38 } 39 40 protected void doConnect(Map environment) throws IOException , SecurityException 41 { 42 JMXServiceURL address = getAddress(); 43 String protocol = address.getProtocol(); 44 ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment); 45 if (resolver == null) throw new MalformedURLException ("Unsupported protocol: " + protocol); 46 47 HTTPConnection temp = (HTTPConnection)resolver.lookupClient(address, environment); 48 connection = (HTTPConnection)resolver.bindClient(temp, environment); 49 50 Object credentials = environment == null ? null : environment.get(CREDENTIALS); 51 connectionId = connection.connect(credentials); 52 53 this.heartbeat = createHeartBeat(connection, getConnectionNotificationEmitter(), environment); 54 this.notificationHandler = createRemoteNotificationClientHandler(connection, getConnectionNotificationEmitter(), heartbeat, environment); 55 56 this.heartbeat.start(); 57 this.notificationHandler.start(); 58 } 59 60 protected HeartBeat createHeartBeat(HTTPConnection connection, ConnectionNotificationEmitter emitter, Map environment) 61 { 62 return new HTTPHeartBeat(connection, emitter, environment); 63 } 64 65 protected RemoteNotificationClientHandler createRemoteNotificationClientHandler(HTTPConnection connection, ConnectionNotificationEmitter emitter, HeartBeat heartbeat, Map environment) 66 { 67 return new HTTPRemoteNotificationClientHandler(connection, emitter, heartbeat, environment); 68 } 69 70 protected MBeanServerConnection doGetMBeanServerConnection(Subject delegate) throws IOException 71 { 72 return new HTTPConnectionMBeanServerConnection(getHTTPConnection(), delegate, getRemoteNotificationClientHandler()); 73 } 74 75 protected void doClose() throws IOException 76 { 77 if (notificationHandler != null) notificationHandler.stop(); 78 if (heartbeat != null) heartbeat.stop(); 79 if (connection != null) connection.close(); 80 } 81 82 public String getConnectionId() throws IOException 83 { 84 return connectionId; 85 } 86 87 protected HTTPConnection getHTTPConnection() 88 { 89 return connection; 90 } 91 92 protected RemoteNotificationClientHandler getRemoteNotificationClientHandler() 93 { 94 return notificationHandler; 95 } 96 } 97 | Popular Tags |