KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > http > HTTPConnector


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.http;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.management.MBeanServerConnection JavaDoc;
16 import javax.management.remote.JMXServiceURL JavaDoc;
17 import javax.security.auth.Subject JavaDoc;
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 /**
26  * @version $
27  */

28 public class HTTPConnector extends AbstractJMXConnector
29 {
30    private transient HTTPConnection connection;
31    private transient String JavaDoc connectionId;
32    private transient HeartBeat heartbeat;
33    private transient RemoteNotificationClientHandler notificationHandler;
34
35    public HTTPConnector(JMXServiceURL JavaDoc address, Map JavaDoc environment) throws IOException JavaDoc
36    {
37       super(address);
38    }
39
40    protected void doConnect(Map JavaDoc environment) throws IOException JavaDoc, SecurityException JavaDoc
41    {
42       JMXServiceURL JavaDoc address = getAddress();
43       String JavaDoc protocol = address.getProtocol();
44       ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment);
45       if (resolver == null) throw new MalformedURLException JavaDoc("Unsupported protocol: " + protocol);
46
47       HTTPConnection temp = (HTTPConnection)resolver.lookupClient(address, environment);
48       connection = (HTTPConnection)resolver.bindClient(temp, environment);
49
50       Object JavaDoc 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 JavaDoc environment)
61    {
62       return new HTTPHeartBeat(connection, emitter, environment);
63    }
64
65    protected RemoteNotificationClientHandler createRemoteNotificationClientHandler(HTTPConnection connection, ConnectionNotificationEmitter emitter, HeartBeat heartbeat, Map JavaDoc environment)
66    {
67       return new HTTPRemoteNotificationClientHandler(connection, emitter, heartbeat, environment);
68    }
69
70    protected MBeanServerConnection JavaDoc doGetMBeanServerConnection(Subject JavaDoc delegate) throws IOException JavaDoc
71    {
72       return new HTTPConnectionMBeanServerConnection(getHTTPConnection(), delegate, getRemoteNotificationClientHandler());
73    }
74
75    protected void doClose() throws IOException JavaDoc
76    {
77       if (notificationHandler != null) notificationHandler.stop();
78       if (heartbeat != null) heartbeat.stop();
79       if (connection != null) connection.close();
80    }
81
82    public String JavaDoc getConnectionId() throws IOException JavaDoc
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