KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.MarshalledObject JavaDoc;
13 import java.security.AccessController JavaDoc;
14 import java.security.PrivilegedAction JavaDoc;
15 import java.security.PrivilegedActionException JavaDoc;
16 import java.security.PrivilegedExceptionAction JavaDoc;
17 import java.security.SecureClassLoader JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import javax.management.Attribute JavaDoc;
22 import javax.management.AttributeList JavaDoc;
23 import javax.management.AttributeNotFoundException JavaDoc;
24 import javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.IntrospectionException JavaDoc;
27 import javax.management.InvalidAttributeValueException JavaDoc;
28 import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanInfo JavaDoc;
31 import javax.management.MBeanRegistrationException JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.NotCompliantMBeanException JavaDoc;
34 import javax.management.NotificationFilter JavaDoc;
35 import javax.management.NotificationListener JavaDoc;
36 import javax.management.ObjectInstance JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.QueryExp JavaDoc;
39 import javax.management.ReflectionException JavaDoc;
40 import javax.management.loading.ClassLoaderRepository JavaDoc;
41 import javax.management.remote.NotificationResult JavaDoc;
42 import javax.management.remote.rmi.RMIConnection JavaDoc;
43 import javax.security.auth.Subject JavaDoc;
44
45 import mx4j.remote.NotificationTuple;
46 import mx4j.remote.RemoteNotificationServerHandler;
47
48 /**
49  * An RMIConnection that "converts" remote calls to {@link MBeanServer} calls,
50  * performing unwrapping of parameters and/or the needed actions.
51  *
52  * @version $Revision: 1.12 $
53  * @see mx4j.remote.rmi.ClientInvoker
54  */

55 public class RMIConnectionInvoker implements RMIConnection JavaDoc
56 {
57    private final MBeanServer JavaDoc server;
58    private final ClassLoader JavaDoc defaultLoader;
59    private final RemoteNotificationServerHandler notificationHandler;
60
61    public RMIConnectionInvoker(MBeanServer JavaDoc server, ClassLoader JavaDoc defaultLoader, Map JavaDoc environment)
62    {
63       this.server = server;
64       this.defaultLoader = defaultLoader;
65       // TODO: here we hardcoded the handler for notifications. Maybe worth to make it pluggable ?
66
this.notificationHandler = new RMIRemoteNotificationServerHandler(environment);
67    }
68
69    public String JavaDoc getConnectionId() throws IOException JavaDoc
70    {
71       throw new Error JavaDoc("getConnectionId() must not be propagated along the invocation chain");
72    }
73
74    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, Subject JavaDoc delegate)
75            throws ReflectionException JavaDoc,
76                   InstanceAlreadyExistsException JavaDoc,
77                   MBeanRegistrationException JavaDoc,
78                   MBeanException JavaDoc,
79                   NotCompliantMBeanException JavaDoc,
80                   IOException JavaDoc
81    {
82       return server.createMBean(className, name);
83    }
84
85    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, ObjectName JavaDoc loaderName, Subject JavaDoc delegate)
86            throws ReflectionException JavaDoc,
87                   InstanceAlreadyExistsException JavaDoc,
88                   MBeanRegistrationException JavaDoc,
89                   MBeanException JavaDoc,
90                   NotCompliantMBeanException JavaDoc,
91                   InstanceNotFoundException JavaDoc,
92                   IOException JavaDoc
93    {
94       return server.createMBean(className, name, loaderName);
95    }
96
97    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, MarshalledObject JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate)
98            throws ReflectionException JavaDoc,
99                   InstanceAlreadyExistsException JavaDoc,
100                   MBeanRegistrationException JavaDoc,
101                   MBeanException JavaDoc,
102                   NotCompliantMBeanException JavaDoc,
103                   IOException JavaDoc
104    {
105       RepositoryClassLoader loader = (RepositoryClassLoader)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
106       {
107          public Object JavaDoc run()
108          {
109             return new RepositoryClassLoader(server.getClassLoaderRepository());
110          }
111       });
112       Object JavaDoc[] args = (Object JavaDoc[])RMIMarshaller.unmarshal(params, loader, defaultLoader);
113       return server.createMBean(className, name, args, signature);
114    }
115
116    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name, final ObjectName JavaDoc loaderName, MarshalledObject JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate)
117            throws ReflectionException JavaDoc,
118                   InstanceAlreadyExistsException JavaDoc,
119                   MBeanRegistrationException JavaDoc,
120                   MBeanException JavaDoc,
121                   NotCompliantMBeanException JavaDoc,
122                   InstanceNotFoundException JavaDoc,
123                   IOException JavaDoc
124    {
125       try
126       {
127          ClassLoader JavaDoc loader = (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
128          {
129             public Object JavaDoc run() throws InstanceNotFoundException JavaDoc
130             {
131                return server.getClassLoader(loaderName);
132             }
133          });
134          Object JavaDoc[] args = (Object JavaDoc[])RMIMarshaller.unmarshal(params, loader, defaultLoader);
135          return server.createMBean(className, name, loaderName, args, signature);
136       }
137       catch (PrivilegedActionException JavaDoc x)
138       {
139          throw (InstanceNotFoundException JavaDoc)x.getException();
140       }
141    }
142
143    public void unregisterMBean(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc, IOException JavaDoc
144    {
145       server.unregisterMBean(name);
146    }
147
148    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
149    {
150       return server.getObjectInstance(name);
151    }
152
153    public Set JavaDoc queryMBeans(ObjectName JavaDoc name, MarshalledObject JavaDoc query, Subject JavaDoc delegate) throws IOException JavaDoc
154    {
155       QueryExp JavaDoc filter = (QueryExp JavaDoc)RMIMarshaller.unmarshal(query, null, defaultLoader);
156       return server.queryMBeans(name, filter);
157    }
158
159    public Set JavaDoc queryNames(ObjectName JavaDoc name, MarshalledObject JavaDoc query, Subject JavaDoc delegate) throws IOException JavaDoc
160    {
161       QueryExp JavaDoc filter = (QueryExp JavaDoc)RMIMarshaller.unmarshal(query, null, defaultLoader);
162       return server.queryNames(name, filter);
163    }
164
165    public boolean isRegistered(ObjectName JavaDoc name, Subject JavaDoc delegate) throws IOException JavaDoc
166    {
167       return server.isRegistered(name);
168    }
169
170    public Integer JavaDoc getMBeanCount(Subject JavaDoc delegate) throws IOException JavaDoc
171    {
172       return server.getMBeanCount();
173    }
174
175    public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute, Subject JavaDoc delegate)
176            throws MBeanException JavaDoc,
177                   AttributeNotFoundException JavaDoc,
178                   InstanceNotFoundException JavaDoc,
179                   ReflectionException JavaDoc,
180                   IOException JavaDoc
181    {
182       return server.getAttribute(name, attribute);
183    }
184
185    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes, Subject JavaDoc delegate)
186            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
187    {
188       return server.getAttributes(name, attributes);
189    }
190
191    public void setAttribute(ObjectName JavaDoc name, MarshalledObject JavaDoc attribute, Subject JavaDoc delegate)
192            throws InstanceNotFoundException JavaDoc,
193                   AttributeNotFoundException JavaDoc,
194                   InvalidAttributeValueException JavaDoc,
195                   MBeanException JavaDoc,
196                   ReflectionException JavaDoc,
197                   IOException JavaDoc
198    {
199       Attribute JavaDoc attrib = (Attribute JavaDoc)RMIMarshaller.unmarshal(attribute, getClassLoaderFor(name), defaultLoader);
200       server.setAttribute(name, attrib);
201    }
202
203    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, MarshalledObject JavaDoc attributes, Subject JavaDoc delegate)
204            throws InstanceNotFoundException JavaDoc,
205                   ReflectionException JavaDoc,
206                   IOException JavaDoc
207    {
208       AttributeList JavaDoc attribs = (AttributeList JavaDoc)RMIMarshaller.unmarshal(attributes, getClassLoaderFor(name), defaultLoader);
209       return server.setAttributes(name, attribs);
210    }
211
212    public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName, MarshalledObject JavaDoc params, String JavaDoc[] signature, Subject JavaDoc delegate)
213            throws InstanceNotFoundException JavaDoc,
214                   MBeanException JavaDoc,
215                   ReflectionException JavaDoc,
216                   IOException JavaDoc
217    {
218       Object JavaDoc[] args = (Object JavaDoc[])RMIMarshaller.unmarshal(params, getClassLoaderFor(name), defaultLoader);
219       return server.invoke(name, operationName, args, signature);
220    }
221
222    public String JavaDoc getDefaultDomain(Subject JavaDoc delegate) throws IOException JavaDoc
223    {
224       return server.getDefaultDomain();
225    }
226
227    public String JavaDoc[] getDomains(Subject JavaDoc delegate) throws IOException JavaDoc
228    {
229       return server.getDomains();
230    }
231
232    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc
233    {
234       return server.getMBeanInfo(name);
235    }
236
237    public boolean isInstanceOf(ObjectName JavaDoc name, String JavaDoc className, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
238    {
239       return server.isInstanceOf(name, className);
240    }
241
242    public void addNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, MarshalledObject JavaDoc filter, MarshalledObject JavaDoc handback, Subject JavaDoc delegate)
243            throws InstanceNotFoundException JavaDoc, IOException JavaDoc
244    {
245       ClassLoader JavaDoc loader = getClassLoaderFor(name);
246       NotificationFilter JavaDoc f = (NotificationFilter JavaDoc)RMIMarshaller.unmarshal(filter, loader, defaultLoader);
247       Object JavaDoc h = RMIMarshaller.unmarshal(handback, loader, defaultLoader);
248       server.addNotificationListener(name, listener, f, h);
249    }
250
251    public void removeNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, Subject JavaDoc delegate)
252            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
253    {
254       server.removeNotificationListener(name, listener);
255    }
256
257    public void removeNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener, MarshalledObject JavaDoc filter, MarshalledObject JavaDoc handback, Subject JavaDoc delegate)
258            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
259    {
260       ClassLoader JavaDoc loader = getClassLoaderFor(name);
261       NotificationFilter JavaDoc f = (NotificationFilter JavaDoc)RMIMarshaller.unmarshal(filter, loader, defaultLoader);
262       Object JavaDoc h = RMIMarshaller.unmarshal(handback, loader, defaultLoader);
263       server.removeNotificationListener(name, listener, f, h);
264    }
265
266    public Integer JavaDoc[] addNotificationListeners(ObjectName JavaDoc[] names, MarshalledObject JavaDoc[] filters, Subject JavaDoc[] delegates) throws InstanceNotFoundException JavaDoc, IOException JavaDoc
267    {
268       ArrayList JavaDoc ids = new ArrayList JavaDoc();
269       for (int i = 0; i < names.length; ++i)
270       {
271          ObjectName JavaDoc name = names[i];
272          MarshalledObject JavaDoc filter = filters[i];
273          NotificationFilter JavaDoc f = (NotificationFilter JavaDoc)RMIMarshaller.unmarshal(filter, getClassLoaderFor(name), defaultLoader);
274          Integer JavaDoc id = notificationHandler.generateListenerID(name, f);
275          NotificationListener JavaDoc listener = notificationHandler.getServerNotificationListener();
276          server.addNotificationListener(name, listener, f, id);
277          notificationHandler.addNotificationListener(id, new NotificationTuple(name, listener, f, id));
278          ids.add(id);
279       }
280       return (Integer JavaDoc[])ids.toArray(new Integer JavaDoc[ids.size()]);
281    }
282
283    public void removeNotificationListeners(ObjectName JavaDoc name, Integer JavaDoc[] listenerIDs, Subject JavaDoc delegate) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc, IOException JavaDoc
284    {
285       for (int i = 0; i < listenerIDs.length; ++i)
286       {
287          Integer JavaDoc id = listenerIDs[i];
288          // Remove anyway, even if the server fails
289
NotificationTuple tuple = notificationHandler.removeNotificationListener(id);
290          if (tuple != null) server.removeNotificationListener(name, tuple.getNotificationListener(), tuple.getNotificationFilter(), tuple.getHandback());
291       }
292    }
293
294    public void close() throws IOException JavaDoc
295    {
296       NotificationTuple[] tuples = notificationHandler.close();
297       for (int i = 0; i < tuples.length; ++i)
298       {
299          NotificationTuple tuple = tuples[i];
300          try
301          {
302             server.removeNotificationListener(tuple.getObjectName(), tuple.getNotificationListener(), tuple.getNotificationFilter(), tuple.getHandback());
303          }
304          catch (InstanceNotFoundException JavaDoc ignored)
305          {
306          }
307          catch (ListenerNotFoundException JavaDoc ignored)
308          {
309          }
310       }
311    }
312
313    public NotificationResult JavaDoc fetchNotifications(long clientSequenceNumber, int maxNotifications, long timeout) throws IOException JavaDoc
314    {
315       return notificationHandler.fetchNotifications(clientSequenceNumber, maxNotifications, timeout);
316    }
317
318    private ClassLoader JavaDoc getClassLoaderFor(final ObjectName JavaDoc name) throws InstanceNotFoundException JavaDoc
319    {
320       if (System.getSecurityManager() == null)
321       {
322          return server.getClassLoaderFor(name);
323       }
324       else
325       {
326          try
327          {
328             return (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
329             {
330                public Object JavaDoc run() throws InstanceNotFoundException JavaDoc
331                {
332                   return server.getClassLoaderFor(name);
333                }
334             });
335          }
336          catch (PrivilegedActionException JavaDoc x)
337          {
338             throw (InstanceNotFoundException JavaDoc)x.getException();
339          }
340       }
341    }
342
343    private static class RepositoryClassLoader extends SecureClassLoader JavaDoc
344    {
345       private final ClassLoaderRepository JavaDoc repository;
346
347       private RepositoryClassLoader(ClassLoaderRepository JavaDoc repository)
348       {
349          this.repository = repository;
350       }
351
352       public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
353       {
354          return repository.loadClass(name);
355       }
356    }
357 }
358
Popular Tags