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.NotificationFilter;
15: import javax.management.NotificationListener;
16: import javax.management.ObjectName;
17: import javax.security.auth.Subject;
18:
19: import mx4j.remote.NotificationTuple;
20: import mx4j.remote.RemoteNotificationClientHandler;
21: import mx4j.tools.remote.JMXConnection;
22: import mx4j.tools.remote.JMXConnectionMBeanServerConnection;
23:
24: /**
25: * Implementation of an adapter that converts MBeanServerConnection calls
26: * to HTTPConnection calls.
27: * It handles remote notifications, but it does not handle unmarshalling of
28: * arguments (and all related classloading problems).
29: * NotificationFilters are always invoked on client side.
30: *
31: * @version $Revision: 1.3 $
32: */
33: public class HTTPConnectionMBeanServerConnection extends
34: JMXConnectionMBeanServerConnection {
35: private final RemoteNotificationClientHandler notificationHandler;
36:
37: public HTTPConnectionMBeanServerConnection(
38: JMXConnection connection, Subject delegate,
39: RemoteNotificationClientHandler notificationHandler) {
40: super (connection, delegate);
41: this .notificationHandler = notificationHandler;
42: }
43:
44: public void addNotificationListener(ObjectName observed,
45: NotificationListener listener, NotificationFilter filter,
46: Object handback) throws InstanceNotFoundException,
47: IOException {
48: NotificationTuple tuple = new NotificationTuple(observed,
49: listener, filter, handback);
50: // Filters are always invoked on client side, for now
51: tuple.setInvokeFilter(true);
52: if (notificationHandler.contains(tuple))
53: return;
54: Integer id = ((HTTPConnection) getConnection())
55: .addNotificationListener(observed, null,
56: getDelegateSubject());
57: notificationHandler.addNotificationListener(id, tuple);
58: }
59:
60: public void removeNotificationListener(ObjectName observed,
61: NotificationListener listener)
62: throws InstanceNotFoundException,
63: ListenerNotFoundException, IOException {
64: Integer[] ids = notificationHandler
65: .getNotificationListeners(new NotificationTuple(
66: observed, listener));
67: if (ids == null)
68: throw new ListenerNotFoundException(
69: "Could not find listener " + listener);
70: ((HTTPConnection) getConnection()).removeNotificationListeners(
71: observed, ids, getDelegateSubject());
72: notificationHandler.removeNotificationListeners(ids);
73: }
74:
75: public void removeNotificationListener(ObjectName observed,
76: NotificationListener listener, NotificationFilter filter,
77: Object handback) throws InstanceNotFoundException,
78: ListenerNotFoundException, IOException {
79: Integer id = notificationHandler
80: .getNotificationListener(new NotificationTuple(
81: observed, listener, filter, handback));
82: if (id == null)
83: throw new ListenerNotFoundException(
84: "Could not find listener " + listener
85: + " with filter " + filter
86: + " and handback " + handback);
87: Integer[] ids = new Integer[] { id };
88: ((HTTPConnection) getConnection()).removeNotificationListeners(
89: observed, ids, getDelegateSubject());
90: notificationHandler.removeNotificationListeners(ids);
91: }
92: }
|