Source Code Cross Referenced for MBeanSkeleton.java in  » EJB-Server-resin-3.1.5 » resin » com » caucho » jmx » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server resin 3.1.5 » resin » com.caucho.jmx 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003:         *
004:         * This file is part of Resin(R) Open Source
005:         *
006:         * Each copy or derived work must preserve the copyright notice and this
007:         * notice unmodified.
008:         *
009:         * Resin Open Source is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU General Public License as published by
011:         * the Free Software Foundation; either version 2 of the License, or
012:         * (at your option) any later version.
013:         *
014:         * Resin Open Source is distributed in the hope that it will be useful,
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017:         * of NON-INFRINGEMENT.  See the GNU General Public License for more
018:         * details.
019:         *
020:         * You should have received a copy of the GNU General Public License
021:         * along with Resin Open Source; if not, write to the
022:         *   Free SoftwareFoundation, Inc.
023:         *   59 Temple Place, Suite 330
024:         *   Boston, MA 02111-1307  USA
025:         *
026:         * @author Scott Ferguson
027:         */
028:
029:        package com.caucho.jmx;
030:
031:        import com.caucho.util.L10N;
032:
033:        import javax.management.*;
034:        import java.lang.reflect.InvocationTargetException;
035:        import java.lang.reflect.Method;
036:        import java.lang.reflect.Modifier;
037:        import java.util.ArrayList;
038:        import java.util.HashMap;
039:
040:        /**
041:         * Skeleton for an standard MBean.
042:         */
043:        public class MBeanSkeleton {
044:            private static L10N L = new L10N(MBeanSkeleton.class);
045:
046:            private Class _mbeanClass;
047:
048:            private MBeanInfo _mbeanInfo;
049:
050:            private HashMap<String, Method> _getAttributes = new HashMap<String, Method>();
051:            private HashMap<String, Method> _setAttributes = new HashMap<String, Method>();
052:            private Method[] _operations;
053:
054:            /**
055:             * Creates the skeleton based on the MBean class.
056:             *
057:             * @param mbeanClass the _MBean class
058:             */
059:            MBeanSkeleton(Class mbeanClass) throws IntrospectionException {
060:                _mbeanClass = mbeanClass;
061:
062:                introspect();
063:            }
064:
065:            /**
066:             * Returns the mbean class.
067:             */
068:            public Class getMBeanClass() {
069:                return _mbeanClass;
070:            }
071:
072:            /**
073:             * Returns the MBeanInfo for the class.
074:             */
075:            public MBeanInfo getMBeanInfo() {
076:                return _mbeanInfo;
077:            }
078:
079:            /**
080:             * Returns the attribute value.
081:             */
082:            public Object getAttribute(Object object, String name)
083:                    throws AttributeNotFoundException, ReflectionException {
084:                Method method = _getAttributes.get(name);
085:
086:                if (method != null) {
087:                    try {
088:                        return method.invoke(object, (Object[]) null);
089:                    } catch (IllegalAccessException e) {
090:                        throw new ReflectionException(e);
091:                    } catch (InvocationTargetException e) {
092:                        throw new ReflectionException(e);
093:                    }
094:                } else if (name.equals("resin-api"))
095:                    return _mbeanClass.getName();
096:                else
097:                    throw new AttributeNotFoundException(L.l(
098:                            "MBean class `{0}' has no read attribute `{1}'.",
099:                            _mbeanClass.getName(), name));
100:
101:            }
102:
103:            /**
104:             * Sets the attribute value.
105:             */
106:            public void setAttribute(Object object, String name, Object value)
107:                    throws AttributeNotFoundException, ReflectionException {
108:                Method method = _setAttributes.get(name);
109:
110:                if (method == null)
111:                    throw new AttributeNotFoundException(L.l(
112:                            "MBean class `{0}' has no write attribute `{1}'.",
113:                            _mbeanClass.getName(), name));
114:
115:                try {
116:                    method.invoke(object, new Object[] { value });
117:                } catch (IllegalAccessException e) {
118:                    throw new ReflectionException(e);
119:                } catch (InvocationTargetException e) {
120:                    throw new ReflectionException(e);
121:                }
122:            }
123:
124:            /**
125:             * Invokes an operation.
126:             */
127:            public Object invoke(Object object, String name, Object[] args,
128:                    String[] sig) throws MBeanException, ReflectionException {
129:                Method method = findMethod(name, sig);
130:
131:                if (method == null)
132:                    throw new MBeanException(
133:                            null,
134:                            L
135:                                    .l(
136:                                            "MBean class `{0}' has no matching operation `{1}'.",
137:                                            _mbeanClass.getName(), name));
138:
139:                try {
140:                    return method.invoke(object, args);
141:                } catch (IllegalAccessException e) {
142:                    throw new ReflectionException(e);
143:                } catch (InvocationTargetException e) {
144:                    throw new ReflectionException(e);
145:                }
146:            }
147:
148:            private Method findMethod(String name, String[] sig) {
149:                loop: for (int i = 0; i < _operations.length; i++) {
150:                    Method method = _operations[i];
151:
152:                    if (!method.getName().equals(name))
153:                        continue;
154:
155:                    Class[] param = method.getParameterTypes();
156:
157:                    if (param.length != sig.length)
158:                        continue;
159:
160:                    for (int j = 0; j < sig.length; j++)
161:                        if (!param[j].getName().equals(sig[j]))
162:                            continue loop;
163:
164:                    return method;
165:                }
166:
167:                return null;
168:            }
169:
170:            /**
171:             * Introspects the class.
172:             */
173:            private void introspect() throws IntrospectionException {
174:                Method[] methods = _mbeanClass.getMethods();
175:
176:                ArrayList<Method> publicMethods = new ArrayList<Method>();
177:
178:                ArrayList<MBeanAttributeInfo> attributes;
179:                attributes = new ArrayList<MBeanAttributeInfo>();
180:                ArrayList<MBeanOperationInfo> operations;
181:                operations = new ArrayList<MBeanOperationInfo>();
182:
183:                for (int i = 0; i < methods.length; i++) {
184:                    Method method = methods[i];
185:
186:                    if (!Modifier.isPublic(method.getModifiers()))
187:                        continue;
188:
189:                    if (method.getDeclaringClass().getName().startsWith(
190:                            "javax.management."))
191:                        continue;
192:
193:                    String name = method.getName();
194:                    Class[] param = method.getParameterTypes();
195:                    Class retType = method.getReturnType();
196:
197:                    if (name.startsWith("get") && param.length == 0
198:                            && !void.class.equals(retType)) {
199:                        String attributeName = name.substring(3);
200:
201:                        Method setter = getSetter(attributeName, retType);
202:
203:                        _getAttributes.put(attributeName, method);
204:
205:                        attributes.add(new MBeanAttributeInfo(attributeName,
206:                                attributeName, method, setter));
207:                    } else if (name.startsWith("is")
208:                            && param.length == 0
209:                            && (boolean.class.equals(retType) || Boolean.class
210:                                    .equals(retType))) {
211:                        String attributeName = name.substring(2);
212:
213:                        Method setter = getSetter(attributeName, retType);
214:
215:                        attributes.add(new MBeanAttributeInfo(attributeName,
216:                                attributeName, method, setter));
217:
218:                        _getAttributes.put(attributeName, method);
219:                    } else if (name.startsWith("set") && param.length == 1
220:                            && void.class.equals(retType)) {
221:                        String attributeName = name.substring(3);
222:
223:                        _setAttributes.put(attributeName, method);
224:                    } else {
225:                        operations.add(new MBeanOperationInfo(method.getName(),
226:                                method));
227:                        publicMethods.add(method);
228:                    }
229:                }
230:
231:                _operations = new Method[publicMethods.size()];
232:                publicMethods.toArray(_operations);
233:
234:                MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[attributes
235:                        .size()];
236:                attributes.toArray(attrs);
237:
238:                MBeanConstructorInfo[] makers = new MBeanConstructorInfo[0];
239:
240:                MBeanOperationInfo[] ops = new MBeanOperationInfo[operations
241:                        .size()];
242:                operations.toArray(ops);
243:
244:                MBeanNotificationInfo[] notifs = new MBeanNotificationInfo[0];
245:
246:                _mbeanInfo = new MBeanInfo(_mbeanClass.getName(), _mbeanClass
247:                        .getName(), attrs, makers, ops, notifs);
248:
249:            }
250:
251:            private Method getSetter(String attributeName, Class type) {
252:                String setName = "set" + attributeName;
253:
254:                Method[] methods = _mbeanClass.getMethods();
255:
256:                for (int i = 0; i < methods.length; i++) {
257:                    Method method = methods[i];
258:
259:                    if (!Modifier.isPublic(method.getModifiers()))
260:                        continue;
261:
262:                    String name = method.getName();
263:
264:                    if (!setName.equals(name))
265:                        continue;
266:
267:                    Class[] param = method.getParameterTypes();
268:                    Class ret = method.getReturnType();
269:
270:                    if (param.length != 1)
271:                        continue;
272:
273:                    if (!void.class.equals(ret))
274:                        continue;
275:
276:                    if (param[0].equals(type))
277:                        return method;
278:                    else
279:                        return null; // XXX: s/b error?
280:                }
281:
282:                return null;
283:            }
284:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.