01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.tools.remote.http;
10:
11: import java.io.IOException;
12: import javax.management.InstanceNotFoundException;
13: import javax.management.ListenerNotFoundException;
14: import javax.management.MBeanServerConnection;
15: import javax.management.NotificationListener;
16: import javax.management.ObjectName;
17: import javax.management.remote.NotificationResult;
18: import javax.security.auth.Subject;
19:
20: import mx4j.remote.NotificationTuple;
21: import mx4j.remote.RemoteNotificationServerHandler;
22: import mx4j.tools.remote.AbstractServerInvoker;
23:
24: /**
25: * Implementation of the HTTPConnector interface that forwards the calls
26: * to an MBeanServerConnection object.
27: * It handles remote notifications, but it does not handle unmarshalling of
28: * arguments (and all related classloading problems).
29: *
30: * @version $Revision: 1.3 $
31: */
32: public class HTTPServerInvoker extends AbstractServerInvoker implements
33: HTTPConnection {
34: private final RemoteNotificationServerHandler notificationHandler;
35:
36: public HTTPServerInvoker(MBeanServerConnection server,
37: RemoteNotificationServerHandler handler) {
38: super (server);
39: this .notificationHandler = handler;
40: }
41:
42: public String connect(Object credentials) throws IOException,
43: SecurityException {
44: return null;
45: }
46:
47: public void close() throws IOException {
48: NotificationTuple[] tuples = notificationHandler.close();
49: for (int i = 0; i < tuples.length; ++i) {
50: NotificationTuple tuple = tuples[i];
51: try {
52: getServer().removeNotificationListener(
53: tuple.getObjectName(),
54: tuple.getNotificationListener(),
55: tuple.getNotificationFilter(),
56: tuple.getHandback());
57: } catch (InstanceNotFoundException ignored) {
58: } catch (ListenerNotFoundException ignored) {
59: }
60: }
61: }
62:
63: public Integer addNotificationListener(ObjectName name,
64: Object filter, Subject delegate)
65: throws InstanceNotFoundException, IOException {
66: Integer id = notificationHandler.generateListenerID(name, null);
67: NotificationListener listener = notificationHandler
68: .getServerNotificationListener();
69: getServer().addNotificationListener(name, listener, null, id);
70: notificationHandler.addNotificationListener(id,
71: new NotificationTuple(name, listener, null, id));
72: return id;
73: }
74:
75: public void removeNotificationListeners(ObjectName name,
76: Integer[] listenerIDs, Subject delegate)
77: throws InstanceNotFoundException,
78: ListenerNotFoundException, IOException {
79: for (int i = 0; i < listenerIDs.length; ++i) {
80: Integer id = listenerIDs[i];
81: NotificationTuple tuple = notificationHandler
82: .removeNotificationListener(id);
83: getServer().removeNotificationListener(name,
84: tuple.getNotificationListener(),
85: tuple.getNotificationFilter(), tuple.getHandback());
86: }
87: }
88:
89: public NotificationResult fetchNotifications(
90: long clientSequenceNumber, int maxNotifications,
91: long timeout) throws IOException {
92: return notificationHandler.fetchNotifications(
93: clientSequenceNumber, maxNotifications, timeout);
94: }
95: }
|