KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > capability > ReflectedMBeanDispatcher


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.capability;
23
24 import org.jboss.mx.metadata.AttributeOperationResolver;
25
26 import javax.management.Attribute JavaDoc;
27 import javax.management.AttributeList JavaDoc;
28 import javax.management.AttributeNotFoundException JavaDoc;
29 import javax.management.DynamicMBean JavaDoc;
30 import javax.management.InvalidAttributeValueException JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.MBeanConstructorInfo JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanNotificationInfo JavaDoc;
36 import javax.management.MBeanOperationInfo JavaDoc;
37 import javax.management.NotificationBroadcaster JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39 import javax.management.RuntimeErrorException JavaDoc;
40 import javax.management.RuntimeMBeanException JavaDoc;
41 import javax.management.RuntimeOperationsException JavaDoc;
42 import java.lang.reflect.InvocationTargetException JavaDoc;
43 import java.lang.reflect.Method JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 /**
47  * Directs DynamicMBean calls to underlying resource via reflection. It's suitable
48  * for use as a StandardMBean or as the resource for a ModelMBean.
49  *
50  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
51  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>
52  */

53 public class ReflectedMBeanDispatcher implements DynamicMBean JavaDoc
54 {
55    private Object JavaDoc resource = null;
56
57    private AttributeOperationResolver resolver = null;
58    private MBeanConstructorInfo JavaDoc[] constructorInfo = null;
59    private MBeanAttributeInfo JavaDoc[] attributeInfo = null;
60    private MBeanOperationInfo JavaDoc[] operationInfo = null;
61
62    private Method JavaDoc[] operations = null;
63    private MethodPair[] attributes = null;
64
65    private boolean isBroadcaster = false;
66
67    private String JavaDoc resourceClassName = null;
68    private String JavaDoc resourceDescription = null;
69
70    public ReflectedMBeanDispatcher(MBeanInfo JavaDoc mbinfo, AttributeOperationResolver resolver, Object JavaDoc resource)
71    {
72       if (null == resource)
73       {
74          throw new IllegalArgumentException JavaDoc("resource cannot be null");
75       }
76
77       if (null == mbinfo)
78       {
79          throw new IllegalArgumentException JavaDoc("MBeanInfo cannot be null");
80       }
81
82       if (null == resolver)
83       {
84          throw new IllegalArgumentException JavaDoc("AOresolver cannot be null");
85       }
86
87
88       if (resource instanceof NotificationBroadcaster JavaDoc)
89       {
90          this.isBroadcaster = true;
91       }
92
93       this.resource = resource;
94       this.resolver = resolver;
95       this.resourceClassName = mbinfo.getClassName();
96       this.resourceDescription = mbinfo.getDescription();
97       this.constructorInfo = mbinfo.getConstructors();
98       this.attributeInfo = mbinfo.getAttributes();
99       this.operationInfo = mbinfo.getOperations();
100
101       this.operations = new Method JavaDoc[operationInfo.length];
102       this.attributes = new MethodPair[attributeInfo.length];
103    }
104
105    public void bindOperationAt(int position, Method JavaDoc method)
106    {
107       try
108       {
109          operations[position] = method;
110       }
111       catch (ArrayIndexOutOfBoundsException JavaDoc e)
112       {
113          throw new IllegalArgumentException JavaDoc("position out of bounds: " + position);
114       }
115    }
116
117    public void bindAttributeAt(int position, Method JavaDoc getter, Method JavaDoc setter)
118    {
119       try
120       {
121          attributes[position] = new MethodPair(getter, setter);
122       }
123       catch (ArrayIndexOutOfBoundsException JavaDoc e)
124       {
125          throw new IllegalArgumentException JavaDoc("position out of bounds: " + position);
126       }
127    }
128
129    public String JavaDoc getResourceClassName()
130    {
131       return resourceClassName;
132    }
133
134    public Object JavaDoc getAttribute(String JavaDoc attribute)
135       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
136    {
137       if (null == attribute)
138       {
139          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("attribute cannot be null"));
140       }
141
142       Method JavaDoc m = null;
143       try
144       {
145          m = attributes[resolver.lookup(attribute).intValue()].getter;
146          return m.invoke(resource, new Object JavaDoc[0]);
147       }
148       catch (NullPointerException JavaDoc e)
149       {
150          throw new AttributeNotFoundException JavaDoc("Readable attribute '" + attribute + "' not found");
151       }
152       catch (ArrayIndexOutOfBoundsException JavaDoc e)
153       {
154          throw new AttributeNotFoundException JavaDoc("Readable attribute '" + attribute + "' not found");
155       }
156       catch (InvocationTargetException JavaDoc e)
157       {
158          Throwable JavaDoc t = e.getTargetException();
159          if (t instanceof RuntimeException JavaDoc)
160          {
161             throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc) t, "RuntimeException in MBean when getting attribute '" + attribute + "'");
162          }
163          else if (t instanceof Exception JavaDoc)
164          {
165             throw new MBeanException JavaDoc((Exception JavaDoc) t, "Exception in MBean when getting attribute '" + attribute + "'");
166          }
167          else // it's an error
168
{
169             throw new RuntimeErrorException JavaDoc((Error JavaDoc) t, "Error in MBean when getting attribute '" + attribute + "'");
170          }
171       }
172       catch (IllegalArgumentException JavaDoc e)
173       {
174          throw new AttributeNotFoundException JavaDoc("Readable attribute '" + attribute + "' not found");
175       }
176       catch (Exception JavaDoc e) // assume all other exceptions are reflection related
177
{
178          throw new ReflectionException JavaDoc(e, "Exception in AttributeProvider for getting '" + attribute + "'");
179       }
180       catch (Error JavaDoc e)
181       {
182          throw new RuntimeErrorException JavaDoc(e, "Error in AttributeProvider for getting '" + attribute + "'");
183       }
184    }
185
186
187    public void setAttribute(Attribute JavaDoc attribute)
188       throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
189       MBeanException JavaDoc, ReflectionException JavaDoc
190    {
191       if (null == attribute)
192       {
193          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("attribute cannot be null"));
194       }
195
196       Method JavaDoc m = null;
197       try
198       {
199          m = attributes[resolver.lookup(attribute.getName()).intValue()].setter;
200          m.invoke(resource, new Object JavaDoc[]{attribute.getValue()});
201       }
202       catch (NullPointerException JavaDoc e)
203       {
204          throw new AttributeNotFoundException JavaDoc("Writable attribute '" + attribute.getName() + "' not found");
205       }
206       catch (ArrayIndexOutOfBoundsException JavaDoc e)
207       {
208          throw new AttributeNotFoundException JavaDoc("Writable attribute '" + attribute.getName() + "' not found");
209       }
210       catch (InvocationTargetException JavaDoc e)
211       {
212          Throwable JavaDoc t = e.getTargetException();
213          if (t instanceof RuntimeException JavaDoc)
214          {
215             throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc) t, "RuntimeException in MBean when setting attribute '" + attribute.getName() + "'");
216          }
217          else if (t instanceof Exception JavaDoc)
218          {
219             throw new MBeanException JavaDoc((Exception JavaDoc) t, "Exception in MBean when setting attribute '" + attribute.getName() + "'");
220          }
221          else // it's an error
222
{
223             throw new RuntimeErrorException JavaDoc((Error JavaDoc) t, "Error in MBean when setting attribute '" + attribute.getName() + "'");
224          }
225       }
226       catch (IllegalArgumentException JavaDoc e)
227       {
228          String JavaDoc valueType = (null == attribute.getValue()) ? "<null value>" : attribute.getValue().getClass().getName();
229          throw new InvalidAttributeValueException JavaDoc("Attribute value mismatch while setting '" + attribute.getName() + "': " + valueType);
230       }
231       catch (Exception JavaDoc e) // assume all other exceptions are reflection related
232
{
233          throw new ReflectionException JavaDoc(e, "Exception in AttributeProvider for setting '" + attribute.getName() + "'");
234       }
235       catch (Error JavaDoc e)
236       {
237          throw new RuntimeErrorException JavaDoc(e, "Error in AttributeProvider for setting '" + attribute.getName() + "'");
238       }
239    }
240
241
242    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
243    {
244       if (null == attributes)
245       {
246          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("attributes array cannot be null"));
247       }
248
249       AttributeList JavaDoc list = new AttributeList JavaDoc();
250       for (int i = 0; i < attributes.length; i++)
251       {
252          try
253          {
254             list.add(new Attribute JavaDoc(attributes[i], getAttribute(attributes[i])));
255          }
256          catch (Throwable JavaDoc e)
257          {
258             // QUERY - do we *really* just ignore all problems?
259
}
260       }
261
262       return list;
263    }
264
265    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
266    {
267       if (null == attributes)
268       {
269          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("attribute list cannot be null"));
270       }
271
272       AttributeList JavaDoc list = new AttributeList JavaDoc();
273       for (Iterator JavaDoc iterator = attributes.iterator(); iterator.hasNext();)
274       {
275          Attribute JavaDoc toSet = (Attribute JavaDoc) iterator.next();
276          try
277          {
278             setAttribute(toSet);
279             list.add(toSet);
280          }
281          catch (Throwable JavaDoc e)
282          {
283             // QUERY - do we *really* just ignore all problems?
284
}
285       }
286       return list;
287    }
288
289    public Object JavaDoc invoke(String JavaDoc actionName,
290                         Object JavaDoc[] params,
291                         String JavaDoc[] signature)
292       throws MBeanException JavaDoc, ReflectionException JavaDoc
293    {
294       Method JavaDoc m = null;
295       try
296       {
297          m = operations[resolver.lookup(actionName, signature).intValue()];
298          return m.invoke(resource, params);
299       }
300       catch (NullPointerException JavaDoc e)
301       {
302          throw new ReflectionException JavaDoc(new NoSuchMethodException JavaDoc("Unable to locate MBean operation for: " + opKeyString(actionName, signature)));
303       }
304       catch (ArrayIndexOutOfBoundsException JavaDoc e)
305       {
306          throw new ReflectionException JavaDoc(new NoSuchMethodException JavaDoc("Unable to locate MBean operation for: " + opKeyString(actionName, signature)));
307       }
308       catch (InvocationTargetException JavaDoc e)
309       {
310          Throwable JavaDoc t = e.getTargetException();
311          if (t instanceof RuntimeException JavaDoc)
312          {
313             throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc) t, "RuntimeException in MBean operation '" + opKeyString(actionName, signature) + "'");
314          }
315          else if (t instanceof Exception JavaDoc)
316          {
317             throw new MBeanException JavaDoc((Exception JavaDoc) t, "Exception in MBean operation '" + opKeyString(actionName, signature) + "'");
318          }
319          else // it's an error
320
{
321             throw new RuntimeErrorException JavaDoc((Error JavaDoc) t, "Error in MBean operation '" + opKeyString(actionName, signature) + "'");
322          }
323       }
324       catch (Exception JavaDoc e) // assume all other exceptions are reflection related
325
{
326          throw new ReflectionException JavaDoc(e, "Exception when calling method for '" + opKeyString(actionName, signature) + "'");
327       }
328       catch (Error JavaDoc e)
329       {
330          throw new RuntimeErrorException JavaDoc(e, "Error when calling method for '" + opKeyString(actionName, signature) + "'");
331       }
332
333    }
334
335    public MBeanInfo JavaDoc getMBeanInfo()
336    {
337       return new MBeanInfo JavaDoc(resourceClassName, resourceDescription,
338                            attributeInfo, constructorInfo,
339                            operationInfo, (isBroadcaster) ? this.getNotificationInfo() : new MBeanNotificationInfo JavaDoc[0]);
340
341    }
342
343    // Protected -----------------------------------------------------
344
protected MBeanNotificationInfo JavaDoc[] getNotificationInfo()
345    {
346       if (isBroadcaster)
347       {
348          return ((NotificationBroadcaster JavaDoc) resource).getNotificationInfo();
349       }
350       else
351       {
352          throw new RuntimeOperationsException JavaDoc(new UnsupportedOperationException JavaDoc("resource is not a NotificationBroadcaster"));
353       }
354    }
355
356    protected Object JavaDoc getResourceObject()
357    {
358       return resource;
359    }
360
361    // ONLY used for friendly exceptions!
362
protected final String JavaDoc opKeyString(String JavaDoc name, String JavaDoc[] signature)
363    {
364       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(name).append('(');
365       if (null != signature)
366       {
367          for (int i = 0; i < signature.length-1; i++)
368             buf.append(signature[i]).append(',');
369          if (signature.length > 0)
370             buf.append(signature[signature.length-1]);
371       }
372       return buf.append(')').toString();
373    }
374
375    public static class MethodPair
376    {
377       public Method JavaDoc getter = null;
378       public Method JavaDoc setter = null;
379
380       public MethodPair(Method JavaDoc getter, Method JavaDoc setter)
381       {
382          this.getter = getter;
383          this.setter = setter;
384       }
385    }
386 }
387
Popular Tags