KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > jmx > JmxIntrospectingMixin


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.aspects.jmx;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.AttributeList JavaDoc;
26 import javax.management.AttributeNotFoundException JavaDoc;
27 import javax.management.DynamicMBean JavaDoc;
28 import javax.management.InvalidAttributeValueException JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanException JavaDoc;
31 import javax.management.MBeanInfo JavaDoc;
32 import javax.management.MBeanOperationInfo JavaDoc;
33 import javax.management.ReflectionException JavaDoc;
34
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * Introspects a class to provide the JMX management interface.
42  * The class does not have to follow any pattern.
43  * <p/>
44  * public get/set methods will create mbean attributes, all other public
45  * methods will be operations.
46  *
47  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 37406 $
49  */

50 public class JmxIntrospectingMixin implements DynamicMBean JavaDoc
51 {
52    public class OpsKey
53    {
54       private final String JavaDoc methodName;
55       private final String JavaDoc[] signature;
56       private final int hash;
57
58       public OpsKey(String JavaDoc methodName, String JavaDoc[] signature)
59       {
60          this.methodName = methodName;
61          this.signature = signature;
62          int tmp = methodName.hashCode();
63          for (int i = 0; i < signature.length; i++)
64          {
65             tmp += signature[i].hashCode();
66          }
67          hash = tmp;
68       }
69
70       public int hashCode()
71       {
72          return hash;
73       };
74       public String JavaDoc getMethodName()
75       {
76          return methodName;
77       }
78
79       public String JavaDoc[] getSignature()
80       {
81          return signature;
82       }
83
84       public boolean equals(Object JavaDoc obj)
85       {
86          if (obj == this) return true;
87          OpsKey key = (OpsKey) obj;
88          if (key.hash != hash) return false;
89          if (!key.methodName.equals(methodName)) return false;
90          if (key.signature.length != signature.length) return false;
91          for (int i = 0; i < signature.length; i++)
92          {
93             if (!signature[i].equals(key.signature[i])) return false;
94          }
95          return true;
96       }
97    }
98
99    private final Object JavaDoc target;
100    private final HashMap JavaDoc ops = new HashMap JavaDoc();
101    private final HashMap JavaDoc gets = new HashMap JavaDoc();
102    private final HashMap JavaDoc sets = new HashMap JavaDoc();
103    private MBeanInfo JavaDoc mbeanInfo;
104
105
106    public JmxIntrospectingMixin(Object JavaDoc target)
107    {
108       this.target = target;
109    }
110
111    public Object JavaDoc getAttribute(String JavaDoc attribute) throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
112    {
113       Method JavaDoc get = (Method JavaDoc) gets.get(attribute);
114       if (get == null) throw new AttributeNotFoundException JavaDoc(attribute);
115       try
116       {
117          return get.invoke(target, null);
118       }
119       catch (IllegalAccessException JavaDoc e)
120       {
121          throw new ReflectionException JavaDoc(e);
122       }
123       catch (InvocationTargetException JavaDoc e)
124       {
125          if (e.getTargetException() instanceof Exception JavaDoc) throw new MBeanException JavaDoc((Exception JavaDoc) e.getTargetException());
126          throw new MBeanException JavaDoc(new Exception JavaDoc(e.getTargetException().getMessage()));
127       }
128    }
129
130    public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
131    {
132       Method JavaDoc set = (Method JavaDoc) sets.get(attribute.getName());
133       if (set == null) throw new AttributeNotFoundException JavaDoc(attribute.getName());
134       try
135       {
136          Object JavaDoc[] args = {attribute.getValue()};
137          set.invoke(target, args);
138       }
139       catch (IllegalAccessException JavaDoc e)
140       {
141          throw new ReflectionException JavaDoc(e);
142       }
143       catch (InvocationTargetException JavaDoc e)
144       {
145          if (e.getTargetException() instanceof Exception JavaDoc) throw new MBeanException JavaDoc((Exception JavaDoc) e.getTargetException());
146          throw new MBeanException JavaDoc(new Exception JavaDoc(e.getTargetException().getMessage()));
147       }
148    }
149
150    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
151    {
152       AttributeList JavaDoc list = new AttributeList JavaDoc();
153       for (int i = 0; i < attributes.length; i++)
154       {
155          try
156          {
157             list.add(0, new Attribute JavaDoc(attributes[i], getAttribute(attributes[i])));
158          }
159          catch (AttributeNotFoundException JavaDoc e)
160          {
161             throw new RuntimeException JavaDoc(e);
162          }
163          catch (MBeanException JavaDoc e)
164          {
165             throw new RuntimeException JavaDoc(e);
166          }
167          catch (ReflectionException JavaDoc e)
168          {
169             throw new RuntimeException JavaDoc(e);
170          }
171       }
172       return list;
173    }
174
175    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
176    {
177       for (int i = 0; i < attributes.size(); i++)
178       {
179          try
180          {
181             Attribute JavaDoc attr = (Attribute JavaDoc) attributes.get(i);
182             setAttribute(attr);
183          }
184          catch (InvalidAttributeValueException JavaDoc inv)
185          {
186             throw new RuntimeException JavaDoc(inv);
187          }
188          catch (AttributeNotFoundException JavaDoc e)
189          {
190             throw new RuntimeException JavaDoc(e);
191          }
192          catch (MBeanException JavaDoc e)
193          {
194             throw new RuntimeException JavaDoc(e);
195          }
196          catch (ReflectionException JavaDoc e)
197          {
198             throw new RuntimeException JavaDoc(e);
199          }
200       }
201       return attributes;
202    }
203
204    public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc params[], String JavaDoc signature[]) throws MBeanException JavaDoc, ReflectionException JavaDoc
205    {
206       OpsKey key = new OpsKey(actionName, signature);
207       Method JavaDoc m = (Method JavaDoc) ops.get(key);
208       if (m == null) throw new NoSuchMethodError JavaDoc(actionName);
209       try
210       {
211          return m.invoke(target, params);
212       }
213       catch (IllegalAccessException JavaDoc e)
214       {
215          throw new ReflectionException JavaDoc(e);
216       }
217       catch (InvocationTargetException JavaDoc e)
218       {
219          Throwable JavaDoc cause = e.getTargetException();
220          if (cause instanceof Exception JavaDoc) throw new MBeanException JavaDoc((Exception JavaDoc) cause);
221          throw new MBeanException JavaDoc(new Exception JavaDoc(e.getMessage()));
222       }
223    }
224
225    public MBeanInfo JavaDoc getMBeanInfo()
226    {
227       //System.out.println("******************* MBEAN INFO **********************");
228
if (mbeanInfo != null)
229       {
230          //System.out.println("****mbeanInfo already exists***");
231
return mbeanInfo;
232       }
233
234
235       try
236       {
237          Method JavaDoc[] methods = target.getClass().getMethods();
238
239          //System.out.println("**** introspect attributes ****");
240
for (int i = 0; i < methods.length; i++)
241          {
242             if (methods[i].getName().equals("_getInstanceAdvisor")) continue;
243             if (methods[i].getName().equals("_setInstanceAdvisor")) continue;
244             if (methods[i].getName().equals("_getAdvisor")) continue;
245             if (methods[i].getName().equals("setAttribute")) continue;
246             if (methods[i].getName().equals("getMBeanInfo")) continue;
247             if (methods[i].getName().equals("getAttribute")) continue;
248             if (methods[i].getName().equals("setAttributes")) continue;
249             if (methods[i].getName().equals("getAttributes")) continue;
250             if (methods[i].getName().equals("invoke")) continue;
251             if (methods[i].getName().indexOf("$aop") != -1) continue;
252             if (methods[i].getDeclaringClass().equals(java.lang.Object JavaDoc.class)) continue;
253             if (methods[i].getName().startsWith("get") && methods[i].getParameterTypes().length == 0)
254             {
255                //System.out.println("adding get attribute: " + methods[i].getName().substring(3));
256
gets.put(methods[i].getName().substring(3), methods[i]);
257             }
258             else if (methods[i].getName().startsWith("is") && methods[i].getParameterTypes().length == 0 &&
259                     (methods[i].getReturnType().equals(Boolean JavaDoc.class) || methods[i].getReturnType().equals(boolean.class)))
260             {
261                //System.out.println("adding is attribute: " + methods[i].getName().substring(2));
262
gets.put(methods[i].getName().substring(2), methods[i]);
263             }
264             else if (methods[i].getName().startsWith("set") && methods[i].getParameterTypes().length == 1)
265             {
266                //System.out.println("adding set attribute: " + methods[i].getName().substring(3));
267
sets.put(methods[i].getName().substring(3), methods[i]);
268             }
269             else
270             {
271                //System.out.println("adding operation: " + methods[i].getName());
272
String JavaDoc[] signature = new String JavaDoc[methods[i].getParameterTypes().length];
273                for (int j = 0; j < methods[i].getParameterTypes().length; j++)
274                {
275                   signature[j] = methods[i].getParameterTypes()[j].getName();
276                }
277                ops.put(new OpsKey(methods[i].getName(), signature), methods[i]);
278             }
279          }
280
281          HashMap JavaDoc attributes = new HashMap JavaDoc();
282          Iterator JavaDoc it = gets.keySet().iterator();
283          while (it.hasNext())
284          {
285             String JavaDoc attribute = (String JavaDoc) it.next();
286             Method JavaDoc m = (Method JavaDoc) gets.get(attribute);
287             //System.out.println("*** creating get: " + attribute);
288
boolean isWritable = sets.containsKey(attribute);
289             boolean isIs = m.getName().startsWith("is");
290             MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc(attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, true, isWritable, isIs);
291             attributes.put(attribute, info);
292          }
293          it = sets.keySet().iterator();
294          while (it.hasNext())
295          {
296             String JavaDoc attribute = (String JavaDoc) it.next();
297             if (gets.containsKey(attribute)) continue;
298             //System.out.println("*** creating set: " + attribute);
299
Method JavaDoc m = (Method JavaDoc) sets.get(attribute);
300             MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc(attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, false, true, false);
301             attributes.put(attribute, info);
302          }
303
304          MBeanOperationInfo JavaDoc[] operations = new MBeanOperationInfo JavaDoc[ops.size()];
305          it = ops.values().iterator();
306          int i = 0;
307          while (it.hasNext())
308          {
309             Method JavaDoc m = (Method JavaDoc) it.next();
310             operations[i++] = new MBeanOperationInfo JavaDoc(m.toString(), m);
311          }
312          MBeanAttributeInfo JavaDoc[] attrs = (MBeanAttributeInfo JavaDoc[]) attributes.values().toArray(new MBeanAttributeInfo JavaDoc[attributes.size()]);
313
314          mbeanInfo = new MBeanInfo JavaDoc(target.getClass().getName(), target.getClass().getName(),
315                  attrs, null, operations, null);
316          //System.out.println("***returning MBeanInfo****");
317
return mbeanInfo;
318       }
319
320       catch (Exception JavaDoc e)
321       {
322          e.printStackTrace();
323          throw new RuntimeException JavaDoc(e);
324       }
325    }
326 }
327
Popular Tags