001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.tools.remote.http;
010:
011: import java.io.IOException;
012: import java.net.MalformedURLException;
013: import java.util.Map;
014:
015: import javax.management.MBeanServerConnection;
016: import javax.management.remote.JMXServiceURL;
017: import javax.security.auth.Subject;
018:
019: import mx4j.remote.ConnectionNotificationEmitter;
020: import mx4j.remote.ConnectionResolver;
021: import mx4j.remote.HeartBeat;
022: import mx4j.remote.RemoteNotificationClientHandler;
023: import mx4j.tools.remote.AbstractJMXConnector;
024:
025: /**
026: * @version $
027: */
028: public class HTTPConnector extends AbstractJMXConnector {
029: private transient HTTPConnection connection;
030: private transient String connectionId;
031: private transient HeartBeat heartbeat;
032: private transient RemoteNotificationClientHandler notificationHandler;
033:
034: public HTTPConnector(JMXServiceURL address, Map environment)
035: throws IOException {
036: super (address);
037: }
038:
039: protected void doConnect(Map environment) throws IOException,
040: SecurityException {
041: JMXServiceURL address = getAddress();
042: String protocol = address.getProtocol();
043: ConnectionResolver resolver = ConnectionResolver
044: .newConnectionResolver(protocol, environment);
045: if (resolver == null)
046: throw new MalformedURLException("Unsupported protocol: "
047: + protocol);
048:
049: HTTPConnection temp = (HTTPConnection) resolver.lookupClient(
050: address, environment);
051: connection = (HTTPConnection) resolver.bindClient(temp,
052: environment);
053:
054: Object credentials = environment == null ? null : environment
055: .get(CREDENTIALS);
056: connectionId = connection.connect(credentials);
057:
058: this .heartbeat = createHeartBeat(connection,
059: getConnectionNotificationEmitter(), environment);
060: this .notificationHandler = createRemoteNotificationClientHandler(
061: connection, getConnectionNotificationEmitter(),
062: heartbeat, environment);
063:
064: this .heartbeat.start();
065: this .notificationHandler.start();
066: }
067:
068: protected HeartBeat createHeartBeat(HTTPConnection connection,
069: ConnectionNotificationEmitter emitter, Map environment) {
070: return new HTTPHeartBeat(connection, emitter, environment);
071: }
072:
073: protected RemoteNotificationClientHandler createRemoteNotificationClientHandler(
074: HTTPConnection connection,
075: ConnectionNotificationEmitter emitter, HeartBeat heartbeat,
076: Map environment) {
077: return new HTTPRemoteNotificationClientHandler(connection,
078: emitter, heartbeat, environment);
079: }
080:
081: protected MBeanServerConnection doGetMBeanServerConnection(
082: Subject delegate) throws IOException {
083: return new HTTPConnectionMBeanServerConnection(
084: getHTTPConnection(), delegate,
085: getRemoteNotificationClientHandler());
086: }
087:
088: protected void doClose() throws IOException {
089: if (notificationHandler != null)
090: notificationHandler.stop();
091: if (heartbeat != null)
092: heartbeat.stop();
093: if (connection != null)
094: connection.close();
095: }
096:
097: public String getConnectionId() throws IOException {
098: return connectionId;
099: }
100:
101: protected HTTPConnection getHTTPConnection() {
102: return connection;
103: }
104:
105: public RemoteNotificationClientHandler getRemoteNotificationClientHandler() {
106: return notificationHandler;
107: }
108: }
|