KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.management.AttributeList JavaDoc;
18 import javax.management.AttributeNotFoundException JavaDoc;
19 import javax.management.InstanceAlreadyExistsException JavaDoc;
20 import javax.management.InstanceNotFoundException JavaDoc;
21 import javax.management.IntrospectionException JavaDoc;
22 import javax.management.InvalidAttributeValueException JavaDoc;
23 import javax.management.ListenerNotFoundException JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.MBeanInfo JavaDoc;
26 import javax.management.MBeanRegistrationException JavaDoc;
27 import javax.management.NotCompliantMBeanException JavaDoc;
28 import javax.management.ObjectInstance JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.remote.JMXServiceURL JavaDoc;
32 import javax.management.remote.NotificationResult JavaDoc;
33 import javax.security.auth.Subject JavaDoc;
34
35 import mx4j.log.Log;
36 import mx4j.log.Logger;
37 import mx4j.tools.remote.Connection;
38 import mx4j.tools.remote.ConnectionManager;
39
40 /**
41  * @version $Revision: 1.4 $
42  */

43 public abstract class HTTPService implements HTTPConnection
44 {
45    private final Map JavaDoc connections = new HashMap JavaDoc();
46
47    protected Logger getLogger()
48    {
49       return Log.getLogger(getClass().getName());
50    }
51
52    public String JavaDoc connect(Object JavaDoc credentials) throws IOException JavaDoc, SecurityException JavaDoc
53    {
54       JMXServiceURL JavaDoc address = findJMXServiceURL();
55
56       // Lookup the ConnectionManager
57
ConnectionManager connectionManager = HTTPConnectorServer.find(address);
58       if (connectionManager == null) throw new IOException JavaDoc("Could not find ConnectionManager. Make sure a HTTPConnectorServer is in classloader scope and bound at this address " + address);
59
60       Connection connection = connectionManager.connect(credentials);
61       addConnection(connection);
62       return connection.getConnectionId();
63    }
64
65    protected JMXServiceURL JavaDoc findJMXServiceURL() throws MalformedURLException JavaDoc
66    {
67       String JavaDoc url = findRequestURL();
68       JMXServiceURL JavaDoc temp = new JMXServiceURL JavaDoc("service:jmx:" + url);
69       int port = temp.getPort();
70       if ("http".equals(temp.getProtocol()) && port == 0)
71       {
72          // Default HTTP port, set it to 80
73
port = 80;
74       }
75       else if ("https".equals(temp.getProtocol()) && port == 0)
76       {
77          // Default HTTPS port, set it to 443
78
port = 443;
79       }
80       return new JMXServiceURL JavaDoc(getProtocol(), temp.getHost(), port, temp.getURLPath());
81    }
82
83    protected abstract String JavaDoc findRequestURL();
84
85    protected abstract String JavaDoc getProtocol();
86
87    protected void addConnection(Connection connection) throws IOException JavaDoc
88    {
89       String JavaDoc connectionId = connection.getConnectionId();
90       synchronized (this)
91       {
92          if (connections.containsKey(connectionId)) throw new IOException JavaDoc("Connection '" + connection + "' already connected");
93          connections.put(connectionId, connection);
94
95          Logger logger = getLogger();
96          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Added connection '" + connectionId + "', known connections are " + connections.keySet());
97       }
98    }
99
100    protected void removeConnection(Connection connection) throws IOException JavaDoc
101    {
102       String JavaDoc connectionId = connection.getConnectionId();
103       synchronized (this)
104       {
105          if (!connections.containsKey(connectionId)) throw new IOException JavaDoc("Connection '" + connection + "' unknown");
106          connections.remove(connectionId);
107
108          Logger logger = getLogger();
109          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Removed connection '" + connectionId + "', known connections are " + connections.keySet());
110       }
111    }
112
113    protected Connection findConnection() throws IOException JavaDoc
114    {
115       String JavaDoc connectionId = findConnectionId();
116       synchronized (this)
117       {
118          Connection connection = (Connection)connections.get(connectionId);
119          if (connection != null) return connection;
120
121          Logger logger = getLogger();
122          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Unknown connection '" + connectionId + "', known connections are " + connections.keySet());
123          throw new IOException JavaDoc("Connection ID '" + connectionId + "' unknown");
124       }
125    }
126
127    protected abstract String JavaDoc findConnectionId();
128
129    public void close() throws IOException JavaDoc
130    {
131       Connection connection = findConnection();
132       removeConnection(connection);
133       connection.close();
134    }
135
136    public String JavaDoc getConnectionId() throws IOException JavaDoc
137    {
138       Connection connection = findConnection();
139       return connection.getConnectionId();
140    }
141
142    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, Object JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, IOException JavaDoc
143    {
144       HTTPConnection connection = (HTTPConnection)findConnection();
145       return connection.createMBean(className, name, params, signature, delegate);
146    }
147
148    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, ObjectName JavaDoc loaderName, Object JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc, IOException JavaDoc
149    {
150       HTTPConnection connection = (HTTPConnection)findConnection();
151       return connection.createMBean(className, name, loaderName, params, signature, delegate);
152    }
153
154    public void unregisterMBean(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc, IOException JavaDoc
155    {
156       HTTPConnection connection = (HTTPConnection)findConnection();
157       connection.unregisterMBean(name, delegate);
158    }
159
160    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
161    {
162       HTTPConnection connection = (HTTPConnection)findConnection();
163       return connection.getObjectInstance(name, delegate);
164    }
165
166    public Set JavaDoc queryMBeans(ObjectName JavaDoc name, Object JavaDoc query, Subject JavaDoc delegate) throws IOException JavaDoc
167    {
168       HTTPConnection connection = (HTTPConnection)findConnection();
169       return connection.queryMBeans(name, query, delegate);
170    }
171
172    public Set JavaDoc queryNames(ObjectName JavaDoc name, Object JavaDoc query, Subject JavaDoc delegate) throws IOException JavaDoc
173    {
174       HTTPConnection connection = (HTTPConnection)findConnection();
175       return connection.queryNames(name, query, delegate);
176    }
177
178    public boolean isRegistered(ObjectName JavaDoc name, Subject JavaDoc delegate) throws IOException JavaDoc
179    {
180       HTTPConnection connection = (HTTPConnection)findConnection();
181       return connection.isRegistered(name, delegate);
182    }
183
184    public Integer JavaDoc getMBeanCount(Subject JavaDoc delegate) throws IOException JavaDoc
185    {
186       HTTPConnection connection = (HTTPConnection)findConnection();
187       return connection.getMBeanCount(delegate);
188    }
189
190    public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute, Subject JavaDoc delegate) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
191    {
192       HTTPConnection connection = (HTTPConnection)findConnection();
193       return connection.getAttribute(name, attribute, delegate);
194    }
195
196    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
197    {
198       HTTPConnection connection = (HTTPConnection)findConnection();
199       return connection.getAttributes(name, attributes, delegate);
200    }
201
202    public void setAttribute(ObjectName JavaDoc name, Object JavaDoc attribute, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
203    {
204       HTTPConnection connection = (HTTPConnection)findConnection();
205       connection.setAttribute(name, attribute, delegate);
206    }
207
208    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, Object JavaDoc attributes, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
209    {
210       HTTPConnection connection = (HTTPConnection)findConnection();
211       return connection.setAttributes(name, attributes, delegate);
212    }
213
214    public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName, Object JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
215    {
216       HTTPConnection connection = (HTTPConnection)findConnection();
217       return connection.invoke(name, operationName, params, signature, delegate);
218    }
219
220    public String JavaDoc getDefaultDomain(Subject JavaDoc delegate) throws IOException JavaDoc
221    {
222       HTTPConnection connection = (HTTPConnection)findConnection();
223       return connection.getDefaultDomain(delegate);
224    }
225
226    public String JavaDoc[] getDomains(Subject JavaDoc delegate) throws IOException JavaDoc
227    {
228       HTTPConnection connection = (HTTPConnection)findConnection();
229       return connection.getDomains(delegate);
230    }
231
232    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
233    {
234       HTTPConnection connection = (HTTPConnection)findConnection();
235       return connection.getMBeanInfo(name, delegate);
236    }
237
238    public boolean isInstanceOf(ObjectName JavaDoc name, String JavaDoc className, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
239    {
240       HTTPConnection connection = (HTTPConnection)findConnection();
241       return connection.isInstanceOf(name, className, delegate);
242    }
243
244    public void addNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, Object JavaDoc filter, Object JavaDoc handback, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
245    {
246       HTTPConnection connection = (HTTPConnection)findConnection();
247       connection.addNotificationListener(name, listener, filter, handback, delegate);
248    }
249
250    public void removeNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
251    {
252       HTTPConnection connection = (HTTPConnection)findConnection();
253       connection.removeNotificationListener(name, listener, delegate);
254    }
255
256    public void removeNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, Object JavaDoc filter, Object JavaDoc handback, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
257    {
258       HTTPConnection connection = (HTTPConnection)findConnection();
259       connection.removeNotificationListener(name, listener, filter, handback, delegate);
260    }
261
262    public Integer JavaDoc addNotificationListener(ObjectName JavaDoc name, Object JavaDoc filter, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
263    {
264       HTTPConnection connection = (HTTPConnection)findConnection();
265       return connection.addNotificationListener(name, filter, delegate);
266    }
267
268    public void removeNotificationListeners(ObjectName JavaDoc name, Integer JavaDoc[] listenerIDs, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
269    {
270       HTTPConnection connection = (HTTPConnection)findConnection();
271       connection.removeNotificationListeners(name, listenerIDs, delegate);
272    }
273
274    public NotificationResult JavaDoc fetchNotifications(long clientSequenceNumber, int maxNotifications, long timeout) throws IOException JavaDoc
275    {
276       HTTPConnection connection = (HTTPConnection)findConnection();
277       return connection.fetchNotifications(clientSequenceNumber, maxNotifications, timeout);
278    }
279 }
280
Popular Tags