KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > server > interceptor > InvokerMBeanServerInterceptor


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.server.interceptor;
10
11 import java.lang.reflect.Constructor JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import javax.management.Attribute JavaDoc;
14 import javax.management.AttributeList JavaDoc;
15 import javax.management.AttributeNotFoundException JavaDoc;
16 import javax.management.DynamicMBean JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.JMRuntimeException JavaDoc;
19 import javax.management.ListenerNotFoundException JavaDoc;
20 import javax.management.MBeanException JavaDoc;
21 import javax.management.MBeanInfo JavaDoc;
22 import javax.management.MBeanRegistration JavaDoc;
23 import javax.management.MBeanRegistrationException JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.NotificationBroadcaster JavaDoc;
26 import javax.management.NotificationEmitter JavaDoc;
27 import javax.management.NotificationFilter JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.RuntimeErrorException JavaDoc;
32 import javax.management.RuntimeMBeanException JavaDoc;
33
34 import mx4j.ImplementationException;
35 import mx4j.log.Logger;
36 import mx4j.server.MBeanMetaData;
37 import mx4j.util.Utils;
38
39 /**
40  * The last MBeanServer --$gt; MBean interceptor in the chain.
41  * It calls the MBean instance; if the MBean is a dynamic MBean, the call is direct, otherwise the call is delegated
42  * to an {@link mx4j.server.MBeanInvoker MBeanInvoker}.
43  *
44  * @version $Revision: 1.23 $
45  */

46 public class InvokerMBeanServerInterceptor extends DefaultMBeanServerInterceptor implements InvokerMBeanServerInterceptorMBean
47 {
48    private MBeanServer JavaDoc outerServer;
49
50    /**
51     * Instantiates a new interceptor instance.
52     *
53     * @param outerServer the {@link MBeanServer} instance that is passed to
54     * {@link MBeanRegistration#preRegister(MBeanServer, ObjectName)}.
55     */

56    public InvokerMBeanServerInterceptor(MBeanServer JavaDoc outerServer)
57    {
58       this.outerServer = outerServer;
59    }
60
61    /**
62     * Returns the type of this interceptor
63     */

64    public String JavaDoc getType()
65    {
66       return "invoker";
67    }
68
69    /**
70     * This interceptor is always enabled
71     */

72    public boolean isEnabled()
73    {
74       return true;
75    }
76
77    public void addNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
78    {
79       ((NotificationBroadcaster JavaDoc)metadata.getMBean()).addNotificationListener(listener, filter, handback);
80    }
81
82    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
83    {
84       ((NotificationBroadcaster JavaDoc)metadata.getMBean()).removeNotificationListener(listener);
85    }
86
87    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
88            throws ListenerNotFoundException JavaDoc
89    {
90       ((NotificationEmitter JavaDoc)metadata.getMBean()).removeNotificationListener(listener, filter, handback);
91    }
92
93    public void instantiate(MBeanMetaData metadata, String JavaDoc className, String JavaDoc[] params, Object JavaDoc[] args) throws ReflectionException JavaDoc, MBeanException JavaDoc
94    {
95       try
96       {
97          ClassLoader JavaDoc loader = metadata.getClassLoader();
98          if (loader == null) loader = Thread.currentThread().getContextClassLoader();
99          Class JavaDoc cls = loader.loadClass(className);
100
101          Class JavaDoc[] signature = Utils.loadClasses(loader, params);
102
103          Constructor JavaDoc ctor = cls.getConstructor(signature);
104
105          metadata.setMBean(ctor.newInstance(args));
106       }
107       catch (ClassNotFoundException JavaDoc x)
108       {
109          throw new ReflectionException JavaDoc(x);
110       }
111       catch (NoSuchMethodException JavaDoc x)
112       {
113          throw new ReflectionException JavaDoc(x);
114       }
115       catch (InstantiationException JavaDoc x)
116       {
117          throw new ReflectionException JavaDoc(x);
118       }
119       catch (IllegalAccessException JavaDoc x)
120       {
121          throw new ReflectionException JavaDoc(x);
122       }
123       catch (IllegalArgumentException JavaDoc x)
124       {
125          throw new ReflectionException JavaDoc(x);
126       }
127       catch (InvocationTargetException JavaDoc x)
128       {
129          Throwable JavaDoc t = x.getTargetException();
130          if (t instanceof Error JavaDoc)
131          {
132             throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
133          }
134          else if (t instanceof RuntimeException JavaDoc)
135          {
136             throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc)t);
137          }
138          else
139          {
140             throw new MBeanException JavaDoc((Exception JavaDoc)t);
141          }
142       }
143    }
144
145    public void registration(MBeanMetaData metadata, int operation) throws MBeanRegistrationException JavaDoc
146    {
147       Object JavaDoc mbean = metadata.getMBean();
148       if (!(mbean instanceof MBeanRegistration JavaDoc)) return;
149
150       MBeanRegistration JavaDoc registrable = (MBeanRegistration JavaDoc)mbean;
151
152       try
153       {
154          switch (operation)
155          {
156             case PRE_REGISTER:
157                ObjectName JavaDoc objName = registrable.preRegister(outerServer, metadata.getObjectName());
158                metadata.setObjectName(objName);
159                break;
160             case POST_REGISTER_TRUE:
161                registrable.postRegister(Boolean.TRUE);
162                break;
163             case POST_REGISTER_FALSE:
164                registrable.postRegister(Boolean.FALSE);
165                break;
166             case PRE_DEREGISTER:
167                registrable.preDeregister();
168                break;
169             case POST_DEREGISTER:
170                registrable.postDeregister();
171                break;
172             default:
173                throw new ImplementationException();
174          }
175       }
176       catch (RuntimeException JavaDoc x)
177       {
178          throw new RuntimeMBeanException JavaDoc(x);
179       }
180       catch (Exception JavaDoc x)
181       {
182          if (x instanceof MBeanRegistrationException JavaDoc)
183          {
184             throw (MBeanRegistrationException JavaDoc)x;
185          }
186          throw new MBeanRegistrationException JavaDoc(x);
187       }
188       catch (Error JavaDoc x)
189       {
190          throw new RuntimeErrorException JavaDoc(x);
191       }
192    }
193
194    public MBeanInfo JavaDoc getMBeanInfo(MBeanMetaData metadata)
195    {
196       if (metadata.isMBeanDynamic())
197       {
198          // From JMX 1.1 the MBeanInfo may be dynamically changed at every time, let's refresh it
199
MBeanInfo JavaDoc info = null;
200          try
201          {
202             info = ((DynamicMBean JavaDoc)metadata.getMBean()).getMBeanInfo();
203          }
204          catch (RuntimeException JavaDoc x)
205          {
206             throw new RuntimeMBeanException JavaDoc(x);
207          }
208          if (info == null) return null;
209          metadata.setMBeanInfo(info);
210       }
211
212       return (MBeanInfo JavaDoc)metadata.getMBeanInfo().clone();
213    }
214
215    public Object JavaDoc invoke(MBeanMetaData metadata, String JavaDoc method, String JavaDoc[] params, Object JavaDoc[] args) throws MBeanException JavaDoc, ReflectionException JavaDoc
216    {
217       if (metadata.isMBeanDynamic())
218       {
219          try
220          {
221             return ((DynamicMBean JavaDoc)metadata.getMBean()).invoke(method, args, params);
222          }
223          catch (JMRuntimeException JavaDoc x)
224          {
225             throw x;
226          }
227          catch (RuntimeException JavaDoc x)
228          {
229             throw new RuntimeMBeanException JavaDoc(x);
230          }
231          catch (Error JavaDoc x)
232          {
233             throw new RuntimeErrorException JavaDoc(x);
234          }
235       }
236       else
237       {
238          return metadata.getMBeanInvoker().invoke(metadata, method, params, args);
239       }
240    }
241
242    public Object JavaDoc getAttribute(MBeanMetaData metadata, String JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, ReflectionException JavaDoc
243    {
244       if (metadata.isMBeanDynamic())
245       {
246          try
247          {
248             return ((DynamicMBean JavaDoc)metadata.getMBean()).getAttribute(attribute);
249          }
250          catch (JMRuntimeException JavaDoc x)
251          {
252             throw x;
253          }
254          catch (RuntimeException JavaDoc x)
255          {
256             throw new RuntimeMBeanException JavaDoc(x);
257          }
258          catch (Error JavaDoc x)
259          {
260             throw new RuntimeErrorException JavaDoc(x);
261          }
262       }
263       else
264       {
265          return metadata.getMBeanInvoker().getAttribute(metadata, attribute);
266       }
267    }
268
269    public void setAttribute(MBeanMetaData metadata, Attribute JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, ReflectionException JavaDoc
270    {
271       if (metadata.isMBeanDynamic())
272       {
273          try
274          {
275             ((DynamicMBean JavaDoc)metadata.getMBean()).setAttribute(attribute);
276          }
277          catch (JMRuntimeException JavaDoc x)
278          {
279             throw x;
280          }
281          catch (RuntimeException JavaDoc x)
282          {
283             throw new RuntimeMBeanException JavaDoc(x);
284          }
285          catch (Error JavaDoc x)
286          {
287             throw new RuntimeErrorException JavaDoc(x);
288          }
289       }
290       else
291       {
292          metadata.getMBeanInvoker().setAttribute(metadata, attribute);
293       }
294    }
295
296    public AttributeList JavaDoc getAttributes(MBeanMetaData metadata, String JavaDoc[] attributes)
297    {
298       if (metadata.isMBeanDynamic())
299       {
300          try
301          {
302             return ((DynamicMBean JavaDoc)metadata.getMBean()).getAttributes(attributes);
303          }
304          catch (JMRuntimeException JavaDoc x)
305          {
306             throw x;
307          }
308          catch (RuntimeException JavaDoc x)
309          {
310             throw new RuntimeMBeanException JavaDoc(x);
311          }
312          catch (Error JavaDoc x)
313          {
314             throw new RuntimeErrorException JavaDoc(x);
315          }
316       }
317       else
318       {
319          AttributeList JavaDoc list = new AttributeList JavaDoc();
320          for (int i = 0; i < attributes.length; ++i)
321          {
322             String JavaDoc name = attributes[i];
323             try
324             {
325                Object JavaDoc value = getAttribute(metadata, name);
326                Attribute JavaDoc attr = new Attribute JavaDoc(name, value);
327                list.add(attr);
328             }
329             catch (Exception JavaDoc ignored)
330             {
331                Logger logger = getLogger();
332                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Exception caught from getAttributes(), ignoring attribute " + name);
333             }
334          }
335          return list;
336       }
337    }
338
339    public AttributeList JavaDoc setAttributes(MBeanMetaData metadata, AttributeList JavaDoc attributes)
340    {
341       if (metadata.isMBeanDynamic())
342       {
343          try
344          {
345             return ((DynamicMBean JavaDoc)metadata.getMBean()).setAttributes(attributes);
346          }
347          catch (JMRuntimeException JavaDoc x)
348          {
349             throw x;
350          }
351          catch (RuntimeException JavaDoc x)
352          {
353             throw new RuntimeMBeanException JavaDoc(x);
354          }
355          catch (Error JavaDoc x)
356          {
357             throw new RuntimeErrorException JavaDoc(x);
358          }
359       }
360       else
361       {
362          AttributeList JavaDoc list = new AttributeList JavaDoc();
363          for (int i = 0; i < attributes.size(); ++i)
364          {
365             Attribute JavaDoc attr = (Attribute JavaDoc)attributes.get(i);
366             try
367             {
368                setAttribute(metadata, attr);
369                list.add(attr);
370             }
371             catch (Exception JavaDoc ignored)
372             {
373                Logger logger = getLogger();
374                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Exception caught from setAttributes(), ignoring attribute " + attr, ignored);
375             }
376          }
377          return list;
378       }
379    }
380 }
381
Popular Tags