KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > remote > rmi > ClientInvoker


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.remote.rmi;
10
11 import java.io.IOException JavaDoc;
12 import java.io.NotSerializableException JavaDoc;
13 import java.rmi.MarshalledObject JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.management.Attribute JavaDoc;
16 import javax.management.AttributeList JavaDoc;
17 import javax.management.AttributeNotFoundException JavaDoc;
18 import javax.management.InstanceAlreadyExistsException JavaDoc;
19 import javax.management.InstanceNotFoundException JavaDoc;
20 import javax.management.IntrospectionException JavaDoc;
21 import javax.management.InvalidAttributeValueException JavaDoc;
22 import javax.management.ListenerNotFoundException JavaDoc;
23 import javax.management.MBeanException JavaDoc;
24 import javax.management.MBeanInfo JavaDoc;
25 import javax.management.MBeanRegistrationException JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27 import javax.management.NotCompliantMBeanException JavaDoc;
28 import javax.management.NotificationFilter JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.QueryExp JavaDoc;
33 import javax.management.ReflectionException JavaDoc;
34 import javax.management.remote.rmi.RMIConnection JavaDoc;
35 import javax.security.auth.Subject JavaDoc;
36
37 import mx4j.remote.NotificationTuple;
38 import mx4j.remote.RemoteNotificationClientHandler;
39
40 /**
41  * An MBeanServerConnection that "converts" the MBeanServerConnection calls to {@link RMIConnection} calls,
42  * performing wrapping of parameters and/or the needed actions.
43  *
44  * @version $Revision: 1.10 $
45  * @see mx4j.remote.rmi.RMIConnectionInvoker
46  */

47 public class ClientInvoker implements MBeanServerConnection JavaDoc
48 {
49    private final RMIConnection JavaDoc connection;
50    private final Subject JavaDoc delegate;
51    private final RemoteNotificationClientHandler notificationHandler;
52
53    public ClientInvoker(RMIConnection JavaDoc rmiConnection, RemoteNotificationClientHandler notificationHandler, Subject JavaDoc delegate)
54    {
55       this.connection = rmiConnection;
56       this.delegate = delegate;
57       this.notificationHandler = notificationHandler;
58    }
59
60    public void addNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
61            throws InstanceNotFoundException JavaDoc, IOException JavaDoc
62    {
63       NotificationTuple tuple = new NotificationTuple(observed, listener, filter, handback);
64       if (notificationHandler.contains(tuple)) return;
65
66       MarshalledObject JavaDoc f = null;
67       try
68       {
69          f = RMIMarshaller.marshal(filter);
70       }
71       catch (NotSerializableException JavaDoc x)
72       {
73          // Invoke the filter on client side
74
tuple.setInvokeFilter(true);
75       }
76       Integer JavaDoc[] ids = connection.addNotificationListeners(new ObjectName JavaDoc[]{observed}, new MarshalledObject JavaDoc[]{f}, new Subject JavaDoc[]{delegate});
77       notificationHandler.addNotificationListener(ids[0], tuple);
78    }
79
80    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener)
81            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
82    {
83       Integer JavaDoc[] ids = notificationHandler.getNotificationListeners(new NotificationTuple(observed, listener));
84       if (ids == null) throw new ListenerNotFoundException JavaDoc("Could not find listener " + listener);
85       try
86       {
87          connection.removeNotificationListeners(observed, ids, delegate);
88          notificationHandler.removeNotificationListeners(ids);
89       }
90       catch (InstanceNotFoundException JavaDoc x)
91       {
92          notificationHandler.removeNotificationListeners(ids);
93          throw x;
94       }
95       catch (ListenerNotFoundException JavaDoc x)
96       {
97          notificationHandler.removeNotificationListeners(ids);
98          throw x;
99       }
100    }
101
102    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
103            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
104    {
105       Integer JavaDoc id = notificationHandler.getNotificationListener(new NotificationTuple(observed, listener, filter, handback));
106       if (id == null) throw new ListenerNotFoundException JavaDoc("Could not find listener " + listener + " with filter " + filter + " and handback " + handback);
107       Integer JavaDoc[] ids = new Integer JavaDoc[]{id};
108       try
109       {
110          connection.removeNotificationListeners(observed, ids, delegate);
111          notificationHandler.removeNotificationListeners(ids);
112       }
113       catch (InstanceNotFoundException JavaDoc x)
114       {
115          notificationHandler.removeNotificationListeners(ids);
116          throw x;
117       }
118       catch (ListenerNotFoundException JavaDoc x)
119       {
120          notificationHandler.removeNotificationListeners(ids);
121          throw x;
122       }
123    }
124
125    public void addNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
126            throws InstanceNotFoundException JavaDoc, IOException JavaDoc
127    {
128       MarshalledObject JavaDoc f = RMIMarshaller.marshal(filter);
129       MarshalledObject JavaDoc h = RMIMarshaller.marshal(handback);
130       connection.addNotificationListener(observed, listener, f, h, delegate);
131    }
132
133    public void removeNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener)
134            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
135    {
136       connection.removeNotificationListener(observed, listener, delegate);
137    }
138
139    public void removeNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
140            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
141    {
142       MarshalledObject JavaDoc f = RMIMarshaller.marshal(filter);
143       MarshalledObject JavaDoc h = RMIMarshaller.marshal(handback);
144       connection.removeNotificationListener(observed, listener, f, h, delegate);
145    }
146
147    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc objectName)
148            throws InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
149    {
150       return connection.getMBeanInfo(objectName, delegate);
151    }
152
153    public boolean isInstanceOf(ObjectName JavaDoc objectName, String JavaDoc className)
154            throws InstanceNotFoundException JavaDoc, IOException JavaDoc
155    {
156       return connection.isInstanceOf(objectName, className, delegate);
157    }
158
159    public String JavaDoc[] getDomains()
160            throws IOException JavaDoc
161    {
162       return connection.getDomains(delegate);
163    }
164
165    public String JavaDoc getDefaultDomain()
166            throws IOException JavaDoc
167    {
168       return connection.getDefaultDomain(delegate);
169    }
170
171    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName)
172            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, IOException JavaDoc
173    {
174       return connection.createMBean(className, objectName, delegate);
175    }
176
177    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, Object JavaDoc[] args, String JavaDoc[] parameters)
178            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, IOException JavaDoc
179    {
180       MarshalledObject JavaDoc arguments = RMIMarshaller.marshal(args);
181       return connection.createMBean(className, objectName, arguments, parameters, delegate);
182    }
183
184    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, ObjectName JavaDoc loaderName)
185            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc, IOException JavaDoc
186    {
187       return connection.createMBean(className, objectName, loaderName, delegate);
188    }
189
190    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, ObjectName JavaDoc loaderName, Object JavaDoc[] args, String JavaDoc[] parameters)
191            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc, IOException JavaDoc
192    {
193       MarshalledObject JavaDoc arguments = RMIMarshaller.marshal(args);
194       return connection.createMBean(className, objectName, loaderName, arguments, parameters, delegate);
195    }
196
197    public void unregisterMBean(ObjectName JavaDoc objectName)
198            throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc, IOException JavaDoc
199    {
200       connection.unregisterMBean(objectName, delegate);
201    }
202
203    public Object JavaDoc getAttribute(ObjectName JavaDoc objectName, String JavaDoc attribute)
204            throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
205    {
206       return connection.getAttribute(objectName, attribute, delegate);
207    }
208
209    public void setAttribute(ObjectName JavaDoc objectName, Attribute JavaDoc attribute)
210            throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
211    {
212       MarshalledObject JavaDoc attrib = RMIMarshaller.marshal(attribute);
213       connection.setAttribute(objectName, attrib, delegate);
214    }
215
216    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc objectName, String JavaDoc[] attributes)
217            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
218    {
219       return connection.getAttributes(objectName, attributes, delegate);
220    }
221
222    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc objectName, AttributeList JavaDoc attributes)
223            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
224    {
225       MarshalledObject JavaDoc attribs = RMIMarshaller.marshal(attributes);
226       return connection.setAttributes(objectName, attribs, delegate);
227    }
228
229    public Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc methodName, Object JavaDoc[] args, String JavaDoc[] parameters)
230            throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
231    {
232       MarshalledObject JavaDoc arguments = RMIMarshaller.marshal(args);
233       return connection.invoke(objectName, methodName, arguments, parameters, delegate);
234    }
235
236    public Integer JavaDoc getMBeanCount()
237            throws IOException JavaDoc
238    {
239       return connection.getMBeanCount(delegate);
240    }
241
242    public boolean isRegistered(ObjectName JavaDoc objectName)
243            throws IOException JavaDoc
244    {
245       return connection.isRegistered(objectName, delegate);
246    }
247
248    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc objectName)
249            throws InstanceNotFoundException JavaDoc, IOException JavaDoc
250    {
251       return connection.getObjectInstance(objectName, delegate);
252    }
253
254    public Set JavaDoc queryMBeans(ObjectName JavaDoc patternName, QueryExp JavaDoc filter)
255            throws IOException JavaDoc
256    {
257       MarshalledObject JavaDoc query = RMIMarshaller.marshal(filter);
258       return connection.queryMBeans(patternName, query, delegate);
259    }
260
261    public Set JavaDoc queryNames(ObjectName JavaDoc patternName, QueryExp JavaDoc filter)
262            throws IOException JavaDoc
263    {
264       MarshalledObject JavaDoc query = RMIMarshaller.marshal(filter);
265       return connection.queryNames(patternName, query, delegate);
266    }
267 }
268
Popular Tags