KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.InstanceNotFoundException JavaDoc;
13 import javax.management.ListenerNotFoundException JavaDoc;
14 import javax.management.NotificationFilter JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.security.auth.Subject JavaDoc;
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.4 $
32  */

33 public class HTTPConnectionMBeanServerConnection extends JMXConnectionMBeanServerConnection
34 {
35    private final RemoteNotificationClientHandler notificationHandler;
36
37    public HTTPConnectionMBeanServerConnection(JMXConnection connection, Subject JavaDoc delegate, RemoteNotificationClientHandler notificationHandler)
38    {
39       super(connection, delegate);
40       this.notificationHandler = notificationHandler;
41    }
42
43    public void addNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
44    {
45       NotificationTuple tuple = new NotificationTuple(observed, listener, filter, handback);
46       // Filters are always invoked on client side, for now
47
tuple.setInvokeFilter(true);
48       if (notificationHandler.contains(tuple)) return;
49       Integer JavaDoc id = ((HTTPConnection)getConnection()).addNotificationListener(observed, null, getDelegateSubject());
50       notificationHandler.addNotificationListener(id, tuple);
51    }
52
53    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
54    {
55       Integer JavaDoc[] ids = notificationHandler.getNotificationListeners(new NotificationTuple(observed, listener));
56       if (ids == null) throw new ListenerNotFoundException JavaDoc("Could not find listener " + listener);
57       try
58       {
59          ((HTTPConnection)getConnection()).removeNotificationListeners(observed, ids, getDelegateSubject());
60          notificationHandler.removeNotificationListeners(ids);
61       }
62       catch (InstanceNotFoundException JavaDoc x)
63       {
64          notificationHandler.removeNotificationListeners(ids);
65          throw x;
66       }
67       catch (ListenerNotFoundException JavaDoc x)
68       {
69          notificationHandler.removeNotificationListeners(ids);
70          throw x;
71       }
72    }
73
74    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
75    {
76       Integer JavaDoc id = notificationHandler.getNotificationListener(new NotificationTuple(observed, listener, filter, handback));
77       if (id == null) throw new ListenerNotFoundException JavaDoc("Could not find listener " + listener + " with filter " + filter + " and handback " + handback);
78       Integer JavaDoc[] ids = new Integer JavaDoc[]{id};
79       try
80       {
81          ((HTTPConnection)getConnection()).removeNotificationListeners(observed, ids, getDelegateSubject());
82          notificationHandler.removeNotificationListeners(ids);
83       }
84       catch (InstanceNotFoundException JavaDoc x)
85       {
86          notificationHandler.removeNotificationListeners(ids);
87          throw x;
88       }
89       catch (ListenerNotFoundException JavaDoc x)
90       {
91          notificationHandler.removeNotificationListeners(ids);
92          throw x;
93       }
94    }
95 }
96
Popular Tags