KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > MBeanTyperInvoker


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.util;
23
24 import javax.management.*;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * MBeanTyperInvoker handles method invocations against the MBeanTyper target object
34  * and forwards them to the MBeanServer and ObjectName for invocation.
35  *
36  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
37  */

38 final class MBeanTyperInvoker implements java.lang.reflect.InvocationHandler JavaDoc
39 {
40     private final MBeanServer server;
41     private final ObjectName mbean;
42     private final Map JavaDoc signatureCache = Collections.synchronizedMap(new HashMap JavaDoc());
43
44     MBeanTyperInvoker(MBeanServer server, ObjectName mbean)
45     {
46         this.server = server;
47         this.mbean = mbean;
48     }
49
50     private boolean isJMXAttribute(Method JavaDoc m)
51     {
52         String JavaDoc name = m.getName();
53         return (name.startsWith("get"));
54
55     }
56
57     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
58             throws Throwable JavaDoc
59     {
60         if (MBeanTyper.DEBUG)
61         {
62             System.err.println(" ++ method=" + method.getName() + ",args=" + args);
63         }
64         try
65         {
66             if (method.getDeclaringClass() == Object JavaDoc.class)
67             {
68                 String JavaDoc name = method.getName();
69                 if (name.equals("hashCode"))
70                 {
71                     return new Integer JavaDoc(this.hashCode());
72                 }
73                 else if (name.equals("toString"))
74                 {
75                     return this.toString();
76                 }
77                 else if (name.equals("equals"))
78                 {
79                     // FIXME: this needs to be reviewed - we should be
80
// smarter about this ...
81
return new Boolean JavaDoc(equals(args[0]));
82                 }
83             }
84             else if (isJMXAttribute(method) && (args == null || args.length <= 0))
85             {
86                 String JavaDoc name = method.getName().substring(3);
87                 return server.getAttribute(mbean, name);
88             }
89
90             String JavaDoc sig[] = (String JavaDoc[]) signatureCache.get(method);
91             if (sig == null)
92             {
93                 // get the method signature from the method argument directly
94
// vs. the arguments passed, since there may be primitives that
95
// are wrapped as objects in the arguments
96
Class JavaDoc _args[] = method.getParameterTypes();
97                 if (_args != null && _args.length > 0)
98                 {
99                     sig = new String JavaDoc[_args.length];
100                     for (int c = 0; c < sig.length; c++)
101                     {
102                         if (_args[c] != null)
103                         {
104                             sig[c] = _args[c].getName();
105                         }
106                     }
107                 }
108                 else
109                 {
110                     sig = new String JavaDoc[0];
111                 }
112                 signatureCache.put(method, sig);
113             }
114             return server.invoke(mbean, method.getName(), args, sig);
115         }
116         catch (Throwable JavaDoc t)
117         {
118             if (MBeanTyper.DEBUG)
119             {
120                 t.printStackTrace();
121             }
122             if (t instanceof UndeclaredThrowableException JavaDoc)
123             {
124                 UndeclaredThrowableException JavaDoc ut = (UndeclaredThrowableException JavaDoc) t;
125                 throw ut.getUndeclaredThrowable();
126             }
127             else if (t instanceof InvocationTargetException JavaDoc)
128             {
129                 InvocationTargetException JavaDoc it = (InvocationTargetException JavaDoc) t;
130                 throw it.getTargetException();
131             }
132             else if (t instanceof MBeanException)
133             {
134                 MBeanException me=(MBeanException)t;
135                 throw me.getTargetException();
136             }
137             else
138             {
139                 throw t;
140             }
141         }
142     }
143 }
144
Popular Tags