Source Code Cross Referenced for DefaultDynamicMBean.java in  » JMX » XMOJO » com » adventnet » 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 » JMX » XMOJO » com.adventnet.jmx 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * The XMOJO Project 5
0003:         * Copyright © 2003 XMOJO.org. All rights reserved.
0004:
0005:         * NO WARRANTY
0006:
0007:         * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
0008:         * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
0009:         * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
0010:         * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
0011:         * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0012:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
0013:         * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
0014:         * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
0015:         * REPAIR OR CORRECTION.
0016:
0017:         * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
0018:         * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
0019:         * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
0020:         * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
0021:         * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
0022:         * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
0023:         * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
0024:         * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
0025:         * SUCH DAMAGES.
0026:         **/package com.adventnet.jmx;
0027:
0028:        import java.lang.reflect.Constructor;
0029:        import java.lang.reflect.Method;
0030:        import java.lang.reflect.InvocationTargetException;
0031:        import java.util.Arrays;
0032:        import java.util.ArrayList;
0033:        import java.util.Vector;
0034:
0035:        import javax.management.Attribute;
0036:        import javax.management.AttributeList;
0037:        import javax.management.DynamicMBean;
0038:        import javax.management.ObjectName;
0039:        import javax.management.MBeanServer;
0040:        import javax.management.MBeanServerFactory;
0041:        import javax.management.MBeanRegistration;
0042:        import javax.management.MBeanInfo;
0043:        import javax.management.MBeanAttributeInfo;
0044:        import javax.management.MBeanConstructorInfo;
0045:        import javax.management.MBeanOperationInfo;
0046:        import javax.management.MBeanParameterInfo;
0047:        import javax.management.MBeanNotificationInfo;
0048:        import javax.management.Notification;
0049:        import javax.management.NotificationListener;
0050:        import javax.management.NotificationFilter;
0051:        import javax.management.NotificationBroadcaster;
0052:        import javax.management.AttributeNotFoundException;
0053:        import javax.management.ListenerNotFoundException;
0054:        import javax.management.MBeanException;
0055:        import javax.management.NotCompliantMBeanException;
0056:        import javax.management.ReflectionException;
0057:        import javax.management.RuntimeErrorException;
0058:        import javax.management.RuntimeMBeanException;
0059:        import javax.management.RuntimeOperationsException;
0060:        import javax.management.InvalidAttributeValueException;
0061:
0062:        /**
0063:         * This class acts as the wrapper dynamic mbean for standard mbeans
0064:         * that intends to register with the MBeanServer. This class gets
0065:         * instantiated from the server's registerMBean method, whenever the
0066:         * incoming object is a standard mbean.
0067:         */
0068:        public class DefaultDynamicMBean implements  DynamicMBean,
0069:                NotificationBroadcaster, NotificationListener,
0070:                NotificationFilter, MBeanRegistration {
0071:            public MBeanInfo mbeanInfo = null;
0072:            public Object object = null;
0073:            public Class clazz = null;
0074:            public Class iclazz = null;
0075:            public Method[] super InterfaceMeths = null;
0076:            public Vector readMeths = null;
0077:            public Vector writeMeths = null;
0078:            public Vector isIsMeths = null;
0079:
0080:            public DefaultDynamicMBean(Object object) throws Exception {
0081:                this .object = object;
0082:                clazz = object.getClass();
0083:
0084:                Class[] interfaces = clazz.getInterfaces();
0085:
0086:                boolean flag = false;
0087:
0088:                if (interfaces == null) {
0089:                    clazz = clazz.getSuperclass();
0090:                    if (clazz == null)
0091:                        throw new Exception("NonJmxMBeanRegistrationException");
0092:                    if ((interfaces = clazz.getInterfaces()) == null)
0093:                        throw new Exception("NonJmxMBeanRegistrationException");
0094:                } else {
0095:                    for (int i = 0; i < interfaces.length; i++) {
0096:                        if (interfaces[i].getName().equals(
0097:                                clazz.getName() + "MBean")) {
0098:                            iclazz = interfaces[i];
0099:                            flag = true;
0100:                            /** This will be used for Attribute's get, set and is methods overloading */
0101:                            Class[] interClass = interfaces[i].getInterfaces();
0102:                            if (interClass != null) {
0103:                                if (interClass.length > 0) {
0104:                                    super InterfaceMeths = interClass[0]
0105:                                            .getMethods();
0106:                                }
0107:                            }
0108:                            break;
0109:                        }
0110:                    }
0111:
0112:                    if (!flag) {
0113:                        clazz = clazz.getSuperclass();
0114:                        if (clazz == null)
0115:                            throw new Exception(
0116:                                    "NonJmxMBeanRegistrationException");
0117:                        if ((interfaces = clazz.getInterfaces()) == null)
0118:                            throw new Exception(
0119:                                    "NonJmxMBeanRegistrationException");
0120:                    }
0121:                }
0122:
0123:                if (!flag) {
0124:                    for (int i = 0; i < interfaces.length; i++) {
0125:                        if (interfaces[i].getName().equals(
0126:                                clazz.getName() + "MBean")) {
0127:                            iclazz = interfaces[i];
0128:                            flag = true;
0129:                            break;
0130:                        }
0131:                    }
0132:
0133:                    while (!flag) {
0134:                        flag = getFlagValue(clazz, flag);
0135:                        if (clazz == null)
0136:                            break;
0137:                    }
0138:                }
0139:
0140:                if (!flag)
0141:                    throw new Exception("NonJmxMBeanRegistrationException");
0142:
0143:                clazz = object.getClass();
0144:                makeMBeanInfo();
0145:            }
0146:
0147:            public Object getStandardMBeanObject() {
0148:                return object;
0149:            }
0150:
0151:            protected void makeMBeanInfo() throws Exception {
0152:                String className = clazz.getName();
0153:                String description = className + " MBean";
0154:
0155:                mbeanInfo = new MBeanInfo(className, description,
0156:                        getAttributes(), getConstructors(), getOperations(),
0157:                        getNotifications());
0158:            }
0159:
0160:            private MBeanAttributeInfo[] getAttributes() throws Exception {
0161:                int length = 0;
0162:                int size = 0;
0163:                int pos = 0;
0164:                //Method[] meths = iclazz.getDeclaredMethods();
0165:                //Method[] meths = iclazz.getDeclaredMethods();
0166:                Method[] meths = iclazz.getMethods();
0167:                readMeths = new Vector();
0168:                writeMeths = new Vector();
0169:                isIsMeths = new Vector();
0170:                Vector readableVec = new Vector();
0171:                Vector writableVec = new Vector();
0172:                Vector isWriteStatus = new Vector();
0173:                String[] types = null;
0174:                String[] names = null;
0175:
0176:                /** The Changes have been done for making standard MBean TCK compliance.
0177:                    However the restrictions are
0178:                    1. Overlaoaded methods are not handled.
0179:                    2.If set method with multiple parameter comes it is treated as Attribute
0180:                    ----> The restictions are to be fixed.
0181:                 */
0182:
0183:                /** This loop is used to identify the attribute. Seperates "get", "set" and "is" **/
0184:
0185:                for (int i = 0; i < meths.length; i++) {
0186:                    ///test
0187:                    String methName = meths[i].getName();
0188:                    ////////
0189:                    /** This is to avoid the method by named "get" "set" and "is" getting added in
0190:                        attribute list. */
0191:                    if (methName.equals("get") || methName.equals("set")
0192:                            || methName.equals("is")) {
0193:                        continue;
0194:                    }
0195:                    if ((methName.startsWith("get") && meths[i]
0196:                            .getParameterTypes().length == 0)
0197:                            && !(meths[i].getReturnType().getName()
0198:                                    .equals("void"))
0199:                            || (methName.startsWith("is")
0200:                                    && (meths[i].getReturnType().getName()
0201:                                            .equals("boolean") || meths[i]
0202:                                            .getReturnType().getName().equals(
0203:                                                    "java.lang.Boolean")) && meths[i]
0204:                                    .getParameterTypes().length == 0)) {
0205:                        //System.out.println(" READ METH IS "+meths[i].getName());
0206:                        if (methName.startsWith("get")) {
0207:                            /*if(isAttributeMethodOverloaded(meths[i])){
0208:                              throw new NotCompliantMBeanException();
0209:                              }*/
0210:                            readMeths.add(meths[i]);
0211:                        } else {
0212:                            isIsMeths.add(meths[i]);
0213:                            /*if(isAttributeMethodOverloaded(meths[i])){
0214:                              throw new NotCompliantMBeanException();
0215:                              }*/
0216:                        }
0217:                    }
0218:                    if (methName.startsWith("set")
0219:                            && meths[i].getParameterTypes().length == 1
0220:                            && meths[i].getReturnType().getName()
0221:                                    .equals("void")) {
0222:                        writeMeths.add(meths[i]);
0223:                        /*if(isAttributeMethodOverloaded(meths[i])){
0224:                          throw new NotCompliantMBeanException();
0225:                          }*/
0226:                    }
0227:
0228:                }
0229:
0230:                /** Comparison is done between get and set attribute to fine read-write access */
0231:                for (int i = 0; i < readMeths.size(); i++) {
0232:                    boolean isMatched = false;
0233:                    Method read = (Method) readMeths.get(i);
0234:                    /** This remove and add is done to avoid checking the same method */
0235:                    readMeths.remove(i);
0236:                    if (isAttributeMethodOverloaded(read)) {
0237:                        throw new NotCompliantMBeanException();
0238:                    }
0239:                    readMeths.add(i, read);
0240:                    for (int j = 0; j < writeMeths.size(); j++) {
0241:                        Method write = (Method) writeMeths.get(j);
0242:                        writeMeths.remove(j);
0243:                        if (isAttributeMethodOverloaded(write)) {
0244:                            throw new NotCompliantMBeanException();
0245:                        }
0246:                        writeMeths.add(j, write);
0247:                        if (((Method) readMeths.get(i)).getName().substring(3)
0248:                                .equals(
0249:                                        ((Method) writeMeths.get(j)).getName()
0250:                                                .substring(3))) {
0251:                            if (!(read.getReturnType().getName().equals(write
0252:                                    .getParameterTypes()[0].getName()))) {
0253:                                throw new NotCompliantMBeanException();
0254:                            }
0255:                            readableVec.add(new Boolean(true));
0256:                            writableVec.add(new Boolean(true));
0257:                            writeMeths.remove(j);
0258:                            isMatched = true;
0259:                            break;
0260:                        }
0261:                    }
0262:                    if (!isMatched) {
0263:                        readableVec.add(new Boolean(true));
0264:                        writableVec.add(new Boolean(false));
0265:                    }
0266:                }
0267:                /** Comparison is done between is and set attribute to find read-write access */
0268:                for (int i = 0; i < isIsMeths.size(); i++) {
0269:                    boolean isMatched = false;
0270:                    Method is = (Method) isIsMeths.get(i);
0271:                    isIsMeths.remove(i);
0272:                    if (isAttributeMethodOverloaded(is)) {
0273:                        throw new NotCompliantMBeanException();
0274:                    }
0275:                    isIsMeths.add(i, is);
0276:                    for (int j = 0; j < writeMeths.size(); j++) {
0277:                        Method write = (Method) writeMeths.get(j);
0278:                        if (is.getName().substring(2).equals(
0279:                                write.getName().substring(3))) {
0280:                            if (!(is.getReturnType().getName().equals(write
0281:                                    .getParameterTypes()[0].getName()))) {
0282:                                throw new NotCompliantMBeanException();
0283:                            }
0284:                            /*readableVec.add(new Boolean(true));
0285:                              writableVec.add(new Boolean(true));
0286:                              writeMeths.remove(j);*/
0287:                            isWriteStatus.add(new Boolean(true));
0288:                            isMatched = true;
0289:                            writeMeths.remove(j);
0290:                            break;
0291:                        }
0292:                    }
0293:                    if (!isMatched) {
0294:                        //readableVec.add(new Boolean(true));
0295:                        //writableVec.add(new Boolean(false));
0296:                        isWriteStatus.add(new Boolean(false));
0297:                    }
0298:                }
0299:
0300:                /** Only the write access attribute */
0301:                for (int i = 0; i < writeMeths.size(); i++) {
0302:                    Method meth = (Method) writeMeths.get(i);
0303:                    writeMeths.remove(i);
0304:                    if (isAttributeMethodOverloaded(meth)) {
0305:                        throw new NotCompliantMBeanException();
0306:                    }
0307:                    writeMeths.add(i, meth);
0308:                    readableVec.add(new Boolean(false));
0309:                    writableVec.add(new Boolean(true));
0310:                    //System.out.println("222222222222222222222 readable is "+readableVec);
0311:                    //System.out.println("333333333333333333333 writable is "+writableVec);
0312:                }
0313:                size = readMeths.size() + writeMeths.size();
0314:                types = new String[size];
0315:                names = new String[readMeths.size() + writeMeths.size()
0316:                        + isIsMeths.size()];
0317:                pos = 0;
0318:
0319:                /** Find the types of attribute and split the attributes name */
0320:                for (int i = 0; i < readMeths.size(); i++) {
0321:                    names[i] = ((Method) readMeths.get(i)).getName().substring(
0322:                            3);
0323:                    //types[i] = convertToJmxArrayType(((Method)readMeths.get(i)).getReturnType().getName());
0324:                    types[i] = (((Method) readMeths.get(i)).getReturnType()
0325:                            .getName());
0326:                }
0327:                for (int i = readMeths.size(); i < size; i++) {
0328:                    //types[i] = convertToJmxArrayType(((Method)writeMeths.get(pos)).getParameterTypes()[0].getName());
0329:                    types[i] = (((Method) writeMeths.get(pos))
0330:                            .getParameterTypes()[0].getName());
0331:                    names[i] = ((Method) writeMeths.get(pos)).getName()
0332:                            .substring(3);
0333:                    pos++;
0334:                }
0335:
0336:                MBeanAttributeInfo[] toRet = new MBeanAttributeInfo[names.length];
0337:                String des = "This attribute is present in a Standard MBean";
0338:                for (int i = 0; i < size; i++) {
0339:
0340:                    if (clazz.getName().equals(
0341:                            "javax.management.MBeanServerDelegate")) {
0342:                        if (names[i].equals("MBeanServerId"))
0343:                            des = "Specifies the ID of this JMX MBean Server";
0344:                        else if (names[i].equals("ImplementationName"))
0345:                            des = "Specifies the JMX implementation name (the name of this product).";
0346:                        else if (names[i].equals("ImplementationVendor"))
0347:                            des = "Specifies the JMX implementation vendor (the vendor of this product)";
0348:                        else if (names[i].equals("ImplementationVersion"))
0349:                            des = "Specifies the JMX implementation version (the version of this product). ";
0350:                        else if (names[i].equals("SpecificationName"))
0351:                            des = "Specifies the full name of the JMX specification implemented by this product.";
0352:                        else if (names[i].equals("SpecificationVendor"))
0353:                            des = "Specifies the vendor of the JMX specification implemented by this product.";
0354:                        else if (names[i].equals("SpecificationVersion"))
0355:                            des = "Specifies the version of the JMX specification implemented by this product.";
0356:                    } else if (clazz.getName().equals(
0357:                            "javax.management.loading.MLet")) {
0358:                        if (names[i].equals("URLs"))
0359:                            des = "specifies the search path of URLs for loading classes and resources.";
0360:                    }
0361:
0362:                    toRet[i] = new MBeanAttributeInfo(names[i], types[i], des,
0363:                            ((Boolean) readableVec.get(i)).booleanValue(),
0364:                            ((Boolean) writableVec.get(i)).booleanValue(),
0365:                            false);
0366:                }
0367:                /** isIs artributes are handled here */
0368:                pos = 0;
0369:                //int writePos = size;
0370:                for (int i = size; i < toRet.length; i++) {
0371:                    Method meth = (Method) isIsMeths.get(pos);
0372:                    //toRet[i] = new MBeanAttributeInfo(meth.getName().substring(2),meth.getReturnType().getName(),des,true,((Boolean)writableVec.get(writePos)).booleanValue(),true);
0373:                    toRet[i] = new MBeanAttributeInfo(meth.getName().substring(
0374:                            2), meth.getReturnType().getName(), des, true,
0375:                            ((Boolean) isWriteStatus.get(pos)).booleanValue(),
0376:                            true);
0377:                    pos++;
0378:                    //writePos++;
0379:                }
0380:
0381:                return toRet;
0382:            }
0383:
0384:            private boolean isAttributeMethodOverloaded(Method toCheckMeth) {
0385:                String toCheckStr = toCheckMeth.getName();
0386:                String methStr = null;
0387:                if (toCheckStr.startsWith("is")) {
0388:                    toCheckStr = toCheckStr.substring(2);
0389:                } else if (toCheckStr.startsWith("get")) {
0390:                    toCheckStr = toCheckStr.substring(3);
0391:                }
0392:                if (toCheckMeth.getName().startsWith("is")
0393:                        || toCheckMeth.getName().startsWith("get")) {
0394:                    for (int i = 0; i < readMeths.size(); i++) {
0395:                        Method meth = (Method) readMeths.get(i);
0396:                        methStr = meth.getName();
0397:                        if (toCheckStr.equals(methStr.substring(3))) {
0398:                            if (!(toCheckMeth.getReturnType().getName()
0399:                                    .equals(meth.getReturnType().getName()))
0400:                                    || (toCheckMeth.getDeclaringClass()
0401:                                            .getName().equals(meth
0402:                                            .getDeclaringClass().getName()))) {
0403:                                //readMeths.remove(i);
0404:                                return true;
0405:                            }
0406:                            readMeths.remove(i);
0407:                            //return true;
0408:                        }
0409:                    }
0410:                    for (int i = 0; i < isIsMeths.size(); i++) {
0411:                        Method meth = (Method) isIsMeths.get(i);
0412:                        methStr = meth.getName();
0413:                        if (toCheckStr.equals(methStr.substring(2))) {
0414:                            if (!(toCheckMeth.getReturnType().getName()
0415:                                    .equals(meth.getReturnType().getName()))
0416:                                    || (toCheckMeth.getDeclaringClass()
0417:                                            .getName().equals(meth
0418:                                            .getDeclaringClass().getName()))) {
0419:                                //isIsMeths.remove(i);
0420:                                return true;
0421:                            }
0422:                            isIsMeths.remove(i);
0423:                            //return true;
0424:                        }
0425:                    }
0426:                } else if (toCheckMeth.getName().startsWith("set")) {
0427:                    toCheckStr = toCheckStr.substring(3);
0428:                    for (int i = 0; i < writeMeths.size(); i++) {
0429:                        Method meth = (Method) writeMeths.get(i);
0430:                        methStr = ((Method) writeMeths.get(i)).getName();
0431:                        if (toCheckStr.equals(methStr.substring(3))) {
0432:                            if (!(toCheckMeth.getParameterTypes()[0].getName()
0433:                                    .equals(meth.getParameterTypes()[0]
0434:                                            .getName()))
0435:                                    || (toCheckMeth.getDeclaringClass()
0436:                                            .getName().equals(meth
0437:                                            .getDeclaringClass().getName()))) {
0438:                                //writeMeths.remove(i);
0439:                                return true;
0440:                            }
0441:                            writeMeths.remove(i);
0442:                            //return true;
0443:                        }
0444:                    }
0445:                }
0446:                return false;
0447:            }
0448:
0449:            public MBeanConstructorInfo[] getConstructors() {
0450:                Constructor[] constrs = clazz.getConstructors();
0451:                MBeanConstructorInfo[] toRet = new MBeanConstructorInfo[constrs.length];
0452:
0453:                for (int i = 0; i < toRet.length; i++) {
0454:                    Class[] cparams = constrs[i].getParameterTypes();
0455:                    MBeanParameterInfo[] params = new MBeanParameterInfo[cparams.length];
0456:
0457:                    for (int j = 0; j < params.length; j++) {
0458:                        params[j] = new MBeanParameterInfo(("param" + j),
0459:                                cparams[j].getName(), "param to constructor");
0460:                    }
0461:
0462:                    toRet[i] = new MBeanConstructorInfo(constrs[i].getName(),
0463:                            constrs[i].toString(), params);
0464:                }
0465:
0466:                return toRet;
0467:            }
0468:
0469:            public MBeanOperationInfo[] getOperations() {
0470:                int length = 0;
0471:                int pos = 0;
0472:
0473:                Method[] meths = iclazz.getMethods();
0474:
0475:                //MBeanOperationInfo[] toRet = new MBeanOperationInfo[length];
0476:                Vector operInfo = new Vector();
0477:                MBeanOperationInfo mbOperInfo = null;
0478:
0479:                for (int i = 0; i < meths.length; i++) {
0480:                    String methName = meths[i].getName();
0481:                    String returnType = meths[i].getReturnType().getName();
0482:                    Class[] mparams = meths[i].getParameterTypes();
0483:                    if (methName.startsWith("get") && !methName.equals("get")
0484:                            && (mparams.length == 0)
0485:                            && !(returnType.equals("void"))) {
0486:                        continue;
0487:                    }
0488:                    if (methName.startsWith("is")
0489:                            && !methName.equals("is")
0490:                            && (mparams.length == 0)
0491:                            && (returnType.equals("boolean") || returnType
0492:                                    .equals("java.lang.Boolean"))) {
0493:                        continue;
0494:                    }
0495:                    if (methName.startsWith("set") && !methName.equals("set")
0496:                            && (mparams.length == 1)
0497:                            && returnType.equals("void")) {
0498:                        continue;
0499:                    }
0500:
0501:                    MBeanParameterInfo[] params = new MBeanParameterInfo[mparams.length];
0502:                    //commentSystem.out.println("\n\n** DEFAULT DYNAMIC MBEAN = "+meths[i].getName());
0503:                    for (int j = 0; j < params.length; j++) {
0504:                        //commentSystem.out.println("** PARAMETER = "+mparams[j].getName());
0505:                        //params[j] = new MBeanParameterInfo( ("param" + j ), convertToJmxArrayType(mparams[j].getName()),"param to method");
0506:                        params[j] = new MBeanParameterInfo(("param" + j),
0507:                                mparams[j].getName(), "param to method");
0508:                    }
0509:                    String des = "Operation exposed for management";
0510:                    if (clazz.getName().equals("javax.management.loading.MLet")) {
0511:                        if (meths[i].getName().equals("addURL"))
0512:                            des = "Appends the specified URL to the list of URLs to search for classes and resources.";
0513:                        else if (meths[i].getName().equals("getMBeansFromURL"))
0514:                            des = "Loads a text file containing MLET tags that define the MBeans to be added to the agent.";
0515:                    }
0516:
0517:                    mbOperInfo = new MBeanOperationInfo(meths[i].getName(),
0518:                            des, params, meths[i].getReturnType().getName(),
0519:                            MBeanOperationInfo.ACTION);
0520:                    operInfo.add(mbOperInfo);
0521:                }
0522:
0523:                int size = operInfo.size();
0524:                MBeanOperationInfo[] toRet = new MBeanOperationInfo[size];
0525:                for (int i = 0; i < size; i++) {
0526:                    toRet[i] = (MBeanOperationInfo) operInfo.get(i);
0527:                }
0528:                return toRet;
0529:            }
0530:
0531:            public MBeanNotificationInfo[] getNotifications() {
0532:                //currently not supported
0533:                return null;
0534:            }
0535:
0536:            public MBeanInfo getMBeanInfo() {
0537:                return mbeanInfo;
0538:            }
0539:
0540:            public Object getAttribute(String attribute)
0541:                    //throws Exception
0542:                    throws javax.management.AttributeNotFoundException,
0543:                    javax.management.MBeanException,
0544:                    javax.management.ReflectionException {
0545:                try {
0546:
0547:                    Class e = null;
0548:                    e = object.getClass();
0549:                    Method attrMethod = null;
0550:                    Object[] arguments = new Object[0];
0551:
0552:                    try {
0553:                        //String[] attributes = getAttributes();
0554:                        MBeanAttributeInfo[] attrInfo = getMBeanInfo()
0555:                                .getAttributes();
0556:                        boolean isAttributeFound = false;
0557:                        for (int i = 0; i < attrInfo.length; i++) {
0558:                            if (attribute.equals(attrInfo[i].getName())) {
0559:                                isAttributeFound = true;
0560:                            }
0561:                        }
0562:                        if (!isAttributeFound) {
0563:                            throw new AttributeNotFoundException();
0564:                        }
0565:
0566:                        attrMethod = e.getMethod("get" + attribute, null);
0567:                        /*if(attrMethod == null){
0568:                                throw new AttributeNotFoundException();
0569:                        }*/
0570:                        return attrMethod.invoke(object, arguments);
0571:                    } catch (Exception ee) {
0572:                        /** For TCK */
0573:                        if (ee instanceof  NoSuchMethodException) {
0574:                            attrMethod = e.getMethod("is" + attribute, null);
0575:                            return attrMethod.invoke(object, arguments);
0576:                        }
0577:                        throw ee;
0578:                    }
0579:                } catch (Exception e) {
0580:                    if (e instanceof  AttributeNotFoundException) {
0581:                        throw (AttributeNotFoundException) e;
0582:                    }
0583:                    if (e instanceof  NoSuchMethodException)
0584:                        throw new AttributeNotFoundException();
0585:
0586:                    if (e instanceof  InvocationTargetException) {
0587:                        /** For TCK */
0588:                        InvocationTargetException ite = (InvocationTargetException) e;
0589:                        Throwable th = ite.getTargetException();
0590:                        if (th instanceof  Error) {
0591:                            Error er = (Error) th;
0592:                            RuntimeErrorException ree = new RuntimeErrorException(
0593:                                    er);
0594:                            throw ree;
0595:                        }
0596:                        Exception e1 = (Exception) ((InvocationTargetException) e)
0597:                                .getTargetException();
0598:                        throw new ReflectionException(e1);
0599:                    }
0600:                    throw new ReflectionException(e);
0601:                }
0602:            }
0603:
0604:            public AttributeList getAttributes(String[] attributes)
0605:            //throws Exception
0606:            {
0607:                AttributeList toRet = null;
0608:                if (attributes == null)
0609:                    return toRet;
0610:
0611:                toRet = new AttributeList();
0612:                for (int i = 0; i < attributes.length; i++) {
0613:                    Attribute attr = null;
0614:
0615:                    try {
0616:                        attr = new Attribute(attributes[i],
0617:                                getAttribute(attributes[i]));
0618:                    } catch (Exception e) {
0619:                        attr = new Attribute(attributes[i], e);
0620:                    }
0621:                    toRet.add(attr);
0622:                }
0623:
0624:                return toRet;
0625:            }
0626:
0627:            public Object invoke(String actionName, Object[] params,
0628:                    String[] signature) throws javax.management.MBeanException,
0629:                    javax.management.ReflectionException {
0630:                Class e = null;
0631:                try {
0632:                    e = object.getClass();
0633:
0634:                    MBeanOperationInfo[] opers = mbeanInfo.getOperations();
0635:
0636:                    MBeanOperationInfo oper = null;
0637:                    for (int i = 0; i < opers.length; i++) {
0638:                        if (opers[i].getName().equals(actionName)) {
0639:                            MBeanParameterInfo[] pars = opers[i].getSignature();
0640:                            if (pars == null)
0641:                                break;
0642:
0643:                            if (params != null) {
0644:                                if (!(pars.length == params.length)) {
0645:                                    if (i != (opers.length - 1)) {
0646:                                        continue;
0647:                                    } else {
0648:                                        throw new RuntimeOperationsException(
0649:                                                new IllegalArgumentException());
0650:                                    }
0651:                                }
0652:                            }
0653:
0654:                            if (params != null && signature != null) {
0655:                                if (params.length != signature.length) {
0656:                                    throw new ReflectionException(
0657:                                            new IllegalArgumentException());
0658:                                }
0659:                            }
0660:
0661:                            int j = 0;
0662:                            for (; j < pars.length; j++) {
0663:                                if (params[j] != null) {
0664:                                    String jmxParam = convertToJmxArrayType(signature[j]);
0665:
0666:                                    if (!pars[j].getType().equals(jmxParam)) {
0667:                                        String temp = convertToWrapperType(pars[j]
0668:                                                .getType());
0669:
0670:                                        if ((temp == null)
0671:                                                || (!(temp.equals(jmxParam))))
0672:                                            continue;
0673:                                    }
0674:                                }
0675:
0676:                            }
0677:                            if (j < pars.length)
0678:                                continue;
0679:
0680:                            oper = opers[i];
0681:                            break;
0682:                        }
0683:                    }
0684:
0685:                    if (oper == null) {
0686:                        /* Provision for handling Attribute */
0687:                        /*if(actionName.startsWith("get") || actionName.startsWith("set")){
0688:                          actionName = actionName.substring(3);
0689:                          }
0690:                          else if(actionName.startsWith("is")){
0691:                          actionName = actionName.substring(2);
0692:                          }
0693:                          Object attribute = getAttribute(actionName);
0694:                          if(attribute == null){
0695:                          throw new MBeanException(new Exception());
0696:                          }*/
0697:                        /** This is to execute Attributes .Setter will have only one parameters*/
0698:                        Class[] paramClass = null;
0699:                        if (params != null) {
0700:                            if (params.length == 0 || params.length == 1) {
0701:                                paramClass = new Class[params.length];
0702:                                for (int i = 0; i < params.length; i++) {
0703:                                    paramClass[i] = params[i].getClass();
0704:                                }
0705:                            } else {
0706:                                //throw new MBeanException(new Exception());
0707:                                throw new RuntimeOperationsException(
0708:                                        new IllegalArgumentException());
0709:                            }
0710:                        }
0711:                        Method attrMethod = null;
0712:                        try {
0713:                            attrMethod = e.getMethod(actionName, paramClass);
0714:                        } catch (NoSuchMethodException ne) {
0715:                            if (paramClass.length == 1) {
0716:                                Method[] meths = e.getMethods();
0717:                                for (int i = 0; i < meths.length; i++) {
0718:                                    if (meths[i].getName().equals(actionName)) {
0719:                                        Class[] parameters = meths[i]
0720:                                                .getParameterTypes();
0721:                                        if (parameters[0]
0722:                                                .isAssignableFrom(paramClass[0])) {
0723:                                            attrMethod = meths[i];
0724:                                            return attrMethod.invoke(object,
0725:                                                    params);
0726:                                        }
0727:                                    }
0728:                                }
0729:                                if (attrMethod == null) {
0730:                                    paramClass[0] = getPrimitiveClass(paramClass[0]
0731:                                            .getName());
0732:                                    attrMethod = e.getMethod(actionName,
0733:                                            paramClass);
0734:                                }
0735:                            } else {
0736:                                throw new MBeanException(new Exception());
0737:                            }
0738:                        }
0739:                        if (attrMethod == null) {
0740:                            throw new MBeanException(new Exception());
0741:                        }
0742:                        return attrMethod.invoke(object, params);
0743:                        //throw new MBeanException(new Exception());
0744:                    }
0745:                    MBeanParameterInfo[] pars = oper.getSignature();
0746:                    Class parameterTypes[] = null;
0747:                    if (params != null) {
0748:                        parameterTypes = new Class[params.length];
0749:                        for (int i = 0; i < params.length; i++) {
0750:                            Class clazz = getProperClass(pars[i].getType());
0751:                            if (clazz == null)
0752:                                clazz = Thread.currentThread()
0753:                                        .getContextClassLoader().loadClass(
0754:                                                signature[i]);
0755:
0756:                            parameterTypes[i] = clazz;
0757:                        }
0758:                    }
0759:
0760:                    Method attrMethod = e.getMethod(actionName, parameterTypes);
0761:                    return attrMethod.invoke(object, params);
0762:                } catch (ReflectionException re) {
0763:                    throw re;
0764:                } catch (Exception ex) {
0765:                    if (ex instanceof  InvocationTargetException) {
0766:                        /** For TCK */
0767:                        Exception ee = null;
0768:                        InvocationTargetException ite = (InvocationTargetException) ex;
0769:                        Throwable th = ite.getTargetException();
0770:                        if (th instanceof  Error) {
0771:                            Error er = (Error) th;
0772:                            RuntimeErrorException ree = new RuntimeErrorException(
0773:                                    er);
0774:                            throw ree;
0775:                        } else {
0776:                            //ee = (Exception)th;
0777:                            if (th instanceof  RuntimeException) {
0778:                                throw new RuntimeMBeanException(
0779:                                        (RuntimeException) th);
0780:                            }
0781:                            if (th instanceof  ReflectionException) {
0782:                                throw new MBeanException(
0783:                                        (ReflectionException) th);
0784:                            }
0785:                        }
0786:                        throw new MBeanException((Exception) th);
0787:                    }
0788:                    if (ex instanceof  RuntimeOperationsException) {
0789:                        throw (RuntimeOperationsException) ex;
0790:                    }
0791:                    if (ex instanceof  RuntimeException) {
0792:                        throw new RuntimeMBeanException((RuntimeException) ex);
0793:                    }
0794:
0795:                    if (ex instanceof  ClassNotFoundException) {
0796:
0797:                        try {
0798:                            Class[] classParams = new Class[params.length];
0799:                            for (int i = 0; i < params.length; i++) {
0800:                                if (isPrimitiveDataType(signature[i]))
0801:                                    classParams[i] = getProperClass(signature[i]);
0802:                                else
0803:                                    classParams[i] = params[i].getClass();
0804:                            }
0805:                            //try{
0806:                            Method meth = e.getMethod(actionName, classParams);
0807:                            return meth.invoke(object, params);
0808:                            /*}catch(NoSuchMethodException cnfe){
0809:                              Class[] cParams = new Class[params.length];
0810:                              for(int i=0; i<params.length; i++){
0811:                              try{
0812:                              cParams[i] = DefaultLoaderRepositoryExt.loadClass(signature[i]);
0813:                              }catch(Exception exc){
0814:                              //when classnotfound in default loader repository, there is a chance it to be
0815:                              //system class like java.*
0816:                              if(isPrimitiveDataType(signature[i]))
0817:                              cParams[i] = getProperClass(signature[i]);
0818:                              else
0819:                              cParams[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
0820:                              }
0821:                              }
0822:                              Method meth = e.getMethod(actionName,cParams);
0823:                              return meth.invoke(object,params);
0824:                              }*/
0825:                        } catch (NoSuchMethodException ne) {
0826:                            try {
0827:                                Class[] cParams = new Class[params.length];
0828:                                for (int i = 0; i < params.length; i++) {
0829:                                    try {
0830:                                        cParams[i] = DefaultLoaderRepositoryExt
0831:                                                .loadClass(signature[i]);
0832:                                    } catch (Exception exc) {
0833:                                        //when classnotfound in default loader repository, there is a chance it to be
0834:                                        //system class like java.*
0835:                                        if (isPrimitiveDataType(signature[i]))
0836:                                            cParams[i] = getProperClass(signature[i]);
0837:                                        else
0838:                                            cParams[i] = Thread.currentThread()
0839:                                                    .getContextClassLoader()
0840:                                                    .loadClass(signature[i]);
0841:                                    }
0842:                                }
0843:                                Method meth = e.getMethod(actionName, cParams);
0844:                                return meth.invoke(object, params);
0845:                            } catch (NoSuchMethodException nee) {
0846:                                throw new ReflectionException(ne);
0847:                            } catch (Exception eee) {
0848:                                throw new MBeanException(eee);
0849:                            }
0850:                        } catch (Exception ene) {
0851:                            if (ene instanceof  MBeanException) {
0852:                                throw (MBeanException) ene;
0853:                            }
0854:
0855:                            throw new MBeanException(ene);
0856:                        }
0857:                    }
0858:                    throw new MBeanException(ex);
0859:                    //throw new ReflectionException(e);
0860:                }
0861:            }
0862:
0863:            private boolean isPrimitiveDataType(String type) {
0864:                if (type.endsWith("int"))
0865:                    return true;
0866:                else if (type.endsWith("long"))
0867:                    return true;
0868:                else if (type.endsWith("byte"))
0869:                    return true;
0870:                else if (type.endsWith("float"))
0871:                    return true;
0872:                else if (type.endsWith("char"))
0873:                    return true;
0874:                else if (type.endsWith("short"))
0875:                    return true;
0876:                else if (type.endsWith("double"))
0877:                    return true;
0878:                else if (type.endsWith("boolean"))
0879:                    return true;
0880:
0881:                return false;
0882:
0883:            }
0884:
0885:            private boolean isPrimitiveArrayDataType(String type) {
0886:                if (type.endsWith("int[]"))
0887:                    return true;
0888:                else if (type.endsWith("long[]"))
0889:                    return true;
0890:                else if (type.endsWith("byte[]"))
0891:                    return true;
0892:                else if (type.endsWith("float[]"))
0893:                    return true;
0894:                else if (type.endsWith("char[]"))
0895:                    return true;
0896:                else if (type.endsWith("short[]"))
0897:                    return true;
0898:                else if (type.endsWith("double[]"))
0899:                    return true;
0900:                else if (type.endsWith("boolean[]"))
0901:                    return true;
0902:
0903:                return false;
0904:
0905:            }
0906:
0907:            private String convertToWrapperType(String type) {
0908:                if (type.endsWith("int"))
0909:                    return "java.lang.Integer";
0910:                else if (type.endsWith("int[]"))
0911:                    return "[Ljava.lang.Integer;";
0912:                else if (type.endsWith("long"))
0913:                    return "java.lang.Long";
0914:                else if (type.endsWith("long[]"))
0915:                    return "[Ljava.lang.Long;";
0916:                else if (type.endsWith("byte"))
0917:                    return "java.lang.Byte";
0918:                else if (type.endsWith("byte[]"))
0919:                    return "[Ljava.lang.Byte;";
0920:                else if (type.endsWith("float"))
0921:                    return "java.lang.Float";
0922:                else if (type.endsWith("float[]"))
0923:                    return "[Ljava.lang.Float;";
0924:                else if (type.endsWith("char"))
0925:                    return "java.lang.Character";
0926:                else if (type.endsWith("char[]"))
0927:                    return "[Ljava.lang.Character;";
0928:                else if (type.endsWith("short"))
0929:                    return "java.lang.Short";
0930:                else if (type.endsWith("short[]"))
0931:                    return "[Ljava.lang.Short;";
0932:                else if (type.endsWith("double"))
0933:                    return "java.lang.Double";
0934:                else if (type.endsWith("double[]"))
0935:                    return "[Ljava.lang.Double;";
0936:                else if (type.endsWith("boolean"))
0937:                    return "java.lang.Boolean";
0938:                else if (type.endsWith("boolean[]"))
0939:                    return "[Ljava.lang.Boolean;";
0940:
0941:                return null;
0942:
0943:            }
0944:
0945:            private Class getProperClass(String type) {
0946:                if (type.endsWith("int[]"))
0947:                    return (new int[0]).getClass();
0948:                else if (type.endsWith("long[]"))
0949:                    return (new long[0]).getClass();
0950:                else if (type.endsWith("byte[]"))
0951:                    return (new byte[0]).getClass();
0952:                else if (type.endsWith("float[]"))
0953:                    return (new float[0]).getClass();
0954:                else if (type.endsWith("char[]"))
0955:                    return (new char[0]).getClass();
0956:                else if (type.endsWith("short[]"))
0957:                    return (new short[0]).getClass();
0958:                else if (type.endsWith("double[]"))
0959:                    return (new double[0]).getClass();
0960:                else if (type.endsWith("boolean[]"))
0961:                    return (new boolean[0]).getClass();
0962:                else if (type.endsWith("String[]")) {
0963:                    return (new String[0]).getClass();
0964:                }
0965:
0966:                if (type.endsWith("int"))
0967:                    return int.class;
0968:                else if (type.endsWith("long"))
0969:                    return long.class;
0970:                else if (type.endsWith("byte"))
0971:                    return byte.class;
0972:                else if (type.endsWith("float"))
0973:                    return float.class;
0974:                else if (type.endsWith("char"))
0975:                    return char.class;
0976:                else if (type.endsWith("short"))
0977:                    return short.class;
0978:                else if (type.endsWith("double"))
0979:                    return double.class;
0980:                else if (type.endsWith("boolean"))
0981:                    return boolean.class;
0982:
0983:                return null;
0984:            }
0985:
0986:            private Class getPrimitiveClass(String className) {
0987:                if (className.equals("java.lang.Integer")) {
0988:                    return Integer.TYPE;
0989:                } else if (className.equals("java.lang.Long")) {
0990:                    return Long.TYPE;
0991:                } else if (className.equals("java.lang.Short")) {
0992:                    return Short.TYPE;
0993:                } else if (className.equals("java.lang.Float")) {
0994:                    return Float.TYPE;
0995:                } else if (className.equals("java.lang.Double")) {
0996:                    return Double.TYPE;
0997:                } else if (className.equals("java.lang.Character")) {
0998:                    return Character.TYPE;
0999:                } else if (className.equals("java.lang.Boolean")) {
1000:                    return Boolean.TYPE;
1001:                }
1002:
1003:                return null;
1004:            }
1005:
1006:            private String convertToJmxArrayType(String type) {
1007:                if (type.equals("[I"))
1008:                    return "int[]";
1009:                else if (type.equals("[J"))
1010:                    return "long[]";
1011:                else if (type.equals("[B"))
1012:                    return "byte[]";
1013:                else if (type.equals("[F"))
1014:                    return "float[]";
1015:                else if (type.equals("[C"))
1016:                    return "char[]";
1017:                else if (type.equals("[S"))
1018:                    return "short[]";
1019:                else if (type.equals("[D"))
1020:                    return "double[]";
1021:                else if (type.equals("[Z"))
1022:                    return "boolean[]";
1023:
1024:                if (type.startsWith("[L") && type.endsWith(";"))
1025:                    return (type.substring(2, type.length() - 1) + "[]");
1026:
1027:                return type;
1028:            }
1029:
1030:            public void setAttribute(Attribute attribute)
1031:                    //throws Exception
1032:                    throws javax.management.AttributeNotFoundException,
1033:                    javax.management.InvalidAttributeValueException,
1034:                    javax.management.MBeanException,
1035:                    javax.management.ReflectionException {
1036:
1037:                if (attribute == null)
1038:                    throw new RuntimeOperationsException(
1039:                            new IllegalArgumentException(
1040:                                    "Null values not possible"));
1041:
1042:                try {
1043:                    Class e = null;
1044:                    try {
1045:                        e = Thread.currentThread().getContextClassLoader()
1046:                                .loadClass(mbeanInfo.getClassName());
1047:                    } catch (ClassNotFoundException cce) {
1048:                        MBeanServerImpl server = null;
1049:                        ArrayList list = MBeanServerFactory
1050:                                .findMBeanServer(null);
1051:                        if (list != null)
1052:                            server = (MBeanServerImpl) list.toArray()[0];
1053:                        else
1054:                            throw cce;
1055:
1056:                        Object loader = server.getLoaderTable().get(
1057:                                mbeanInfo.getClassName());
1058:                        if (loader == null)
1059:                            throw cce;
1060:
1061:                        e = Class.forName(mbeanInfo.getClassName(), true,
1062:                                (ClassLoader) loader);
1063:                    }
1064:
1065:                    MBeanAttributeInfo[] attrs = mbeanInfo.getAttributes();
1066:                    MBeanAttributeInfo attr = null;
1067:                    for (int i = 0; i < attrs.length; i++) {
1068:
1069:                        if (attrs[i].getName().equals(attribute.getName())) {
1070:                            attr = attrs[i];
1071:                            break;
1072:                        }
1073:                    }
1074:
1075:                    if (attr == null)
1076:                        throw new AttributeNotFoundException(attribute
1077:                                .getName());
1078:
1079:                    Class parameterTypes[] = new Class[1];
1080:
1081:                    Class clazz = getProperClass(attr.getType());
1082:                    if (clazz == null) {
1083:                        clazz = attribute.getValue().getClass();
1084:                    }
1085:                    parameterTypes[0] = clazz;
1086:
1087:                    Method attrMethod = null;
1088:                    ArrayList methodList = new ArrayList();
1089:                    ArrayList paramList = new ArrayList();
1090:                    try {
1091:                        Method[] meths = e.getMethods();
1092:                        Class[] params = null;
1093:                        parameterTypes[0] = null;
1094:                        for (int i = 0; i < meths.length; i++) {
1095:                            Method meth = meths[i];
1096:                            if (meth.getName().equals(
1097:                                    "set" + attribute.getName())) {
1098:                                params = meth.getParameterTypes();
1099:                                //Attribute will have only one param
1100:                                if (params != null) {
1101:                                    if (params[0].isAssignableFrom(clazz)) {
1102:                                        parameterTypes[0] = params[0];
1103:                                        methodList.add(meth);
1104:                                        paramList.add(parameterTypes[0]);
1105:                                    }
1106:                                }
1107:                            }
1108:
1109:                            if (params != null && params.length > 0
1110:                                    && params[0] != null) {
1111:                                if (parameterTypes[0] == null) {
1112:                                    throw new InvalidAttributeValueException();
1113:                                }
1114:                            }
1115:                        }
1116:                        int size = methodList.size();
1117:                        if (size == 0) {
1118:                            throw new NoSuchMethodException();
1119:                        } else if (size == 1) {
1120:                            attrMethod = (Method) methodList.get(0);
1121:                        } else {
1122:                            MBeanInfo info = getMBeanInfo();
1123:                            //String className = info.getClassName()+"MBean";
1124:                            //Class mbeanClass = Class.forName(className);
1125:                            Method[] mbeanMeths = iclazz.getMethods();
1126:                            int mbeanMethsSize = mbeanMeths.length;
1127:                            boolean isFound = false;
1128:                            for (int i = 0; i < size; i++) {
1129:                                Method meth = (Method) methodList.get(i);
1130:                                for (int j = 0; j < mbeanMethsSize; j++) {
1131:                                    if (meth.getName().equals(
1132:                                            mbeanMeths[j].getName())) {
1133:                                        if (Arrays.equals(meth
1134:                                                .getExceptionTypes(),
1135:                                                mbeanMeths[j]
1136:                                                        .getExceptionTypes())) {
1137:                                            attrMethod = meth;
1138:                                            isFound = true;
1139:                                            break;
1140:                                        }
1141:                                    }
1142:                                }
1143:                                if (isFound) {
1144:                                    break;
1145:                                }
1146:                            }
1147:                        }
1148:                        if (attrMethod == null) {
1149:                            throw new NoSuchMethodException();
1150:                        }
1151:                    } catch (Exception ee) {
1152:                        if (ee instanceof  InvalidAttributeValueException) {
1153:                            throw (InvalidAttributeValueException) ee;
1154:                        }
1155:                        if (ee instanceof  NoSuchMethodException) {
1156:                            throw new AttributeNotFoundException();
1157:                        }
1158:                        //ee.printStackTrace();
1159:                        throw new MBeanException(ee);
1160:                    }
1161:
1162:                    try {
1163:                        attrMethod.invoke(object, new Object[] { attribute
1164:                                .getValue() });
1165:                    } catch (Exception e2) {
1166:                        if (e2 instanceof  AttributeNotFoundException) {
1167:                            throw (AttributeNotFoundException) e2;
1168:                        }
1169:                        if (e2 instanceof  InvocationTargetException) {
1170:                            /** For TCK */
1171:                            InvocationTargetException ite = (InvocationTargetException) e2;
1172:                            Throwable th = ite.getTargetException();
1173:                            if (th instanceof  Error) {
1174:                                Error er = (Error) th;
1175:                                RuntimeErrorException ree = new RuntimeErrorException(
1176:                                        er);
1177:                                throw ree;
1178:                            } else {
1179:                                if (th instanceof  ReflectionException) {
1180:                                    throw (ReflectionException) th;
1181:                                }
1182:
1183:                                if (th instanceof  RuntimeException) {
1184:                                    throw new RuntimeMBeanException(
1185:                                            (RuntimeException) th);
1186:                                } else {
1187:                                    throw new MBeanException((Exception) th);
1188:                                }
1189:                            }
1190:                        }
1191:                    }
1192:                } catch (ReflectionException e) {
1193:                    throw e;
1194:                } catch (Exception e) {
1195:                    if (e instanceof  InvalidAttributeValueException) {
1196:                        throw (InvalidAttributeValueException) e;
1197:                    }
1198:                    if (e instanceof  AttributeNotFoundException) {
1199:                        throw (AttributeNotFoundException) e;
1200:                    }
1201:
1202:                    if (e instanceof  InvocationTargetException) {
1203:                        /** For TCK */
1204:                        InvocationTargetException ite = (InvocationTargetException) e;
1205:                        Throwable th = ite.getTargetException();
1206:                        if (th instanceof  Error) {
1207:                            Error er = (Error) th;
1208:                            RuntimeErrorException ree = new RuntimeErrorException(
1209:                                    er);
1210:                            throw ree;
1211:                        } else {
1212:                            if (th instanceof  ReflectionException) {
1213:                                throw (ReflectionException) th;
1214:                            }
1215:
1216:                            if (th instanceof  RuntimeException) {
1217:                                throw new RuntimeMBeanException(
1218:                                        (RuntimeException) th);
1219:                            } else {
1220:                                throw new MBeanException((Exception) th);
1221:                            }
1222:                        }
1223:                    }
1224:
1225:                    if (e instanceof  RuntimeMBeanException) {
1226:                        throw (RuntimeMBeanException) e;
1227:                    }
1228:                    if (e instanceof  MBeanException) {
1229:                        throw (MBeanException) e;
1230:                    }
1231:                }
1232:            }
1233:
1234:            public AttributeList setAttributes(AttributeList attributes) {
1235:                if (attributes == null)
1236:                    throw new RuntimeOperationsException(
1237:                            new IllegalArgumentException(
1238:                                    "Null values not possible"));
1239:
1240:                Object[] array = attributes.toArray();
1241:
1242:                if (array == null)
1243:                    return attributes;
1244:
1245:                for (int i = 0; i < array.length; i++) {
1246:                    Attribute attr = null;
1247:                    try {
1248:                        attr = (Attribute) array[i];
1249:                    } catch (ClassCastException ce) {
1250:                        continue;
1251:                    }
1252:
1253:                    try {
1254:                        setAttribute(attr);
1255:                    } catch (Exception e) {
1256:                        int index = attributes.indexOf(attr);
1257:                        attributes.remove(index);
1258:                        attr = new Attribute(attr.getName(), e);
1259:                        attributes.add(index, attr);
1260:                    }
1261:                }
1262:
1263:                return attributes;
1264:            }
1265:
1266:            //Implementing NotificationBroadcaster
1267:            public void addNotificationListener(NotificationListener listener,
1268:                    NotificationFilter filter, Object handback)
1269:                    throws IllegalArgumentException {
1270:                if (!(object instanceof  NotificationBroadcaster)) {
1271:                    throw new RuntimeOperationsException(
1272:                            new IllegalArgumentException(
1273:                                    "The MBean does not implement NotificationBroadCaster"));
1274:                }
1275:
1276:                ((NotificationBroadcaster) object).addNotificationListener(
1277:                        listener, filter, handback);
1278:            }
1279:
1280:            public void removeNotificationListener(NotificationListener listener)
1281:                    throws ListenerNotFoundException {
1282:                if (!(object instanceof  NotificationBroadcaster)) {
1283:                    throw new RuntimeOperationsException(
1284:                            new IllegalArgumentException(
1285:                                    "The MBean does not implement NotificationBroadCaster"));
1286:                }
1287:
1288:                ((NotificationBroadcaster) object)
1289:                        .removeNotificationListener(listener);
1290:            }
1291:
1292:            public MBeanNotificationInfo[] getNotificationInfo() {
1293:                if (!(object instanceof  NotificationBroadcaster))
1294:                    return null;
1295:
1296:                return ((NotificationBroadcaster) object).getNotificationInfo();
1297:            }
1298:
1299:            //Implementing for NotificationFilter
1300:            public boolean isNotificationEnabled(Notification notification) {
1301:                if (!(object instanceof  NotificationFilter))
1302:                    return false;
1303:
1304:                return ((NotificationFilter) object)
1305:                        .isNotificationEnabled(notification);
1306:            }
1307:
1308:            //IMplementing for NotificationListener
1309:            public void handleNotification(Notification notification,
1310:                    Object handback) {
1311:                if (!(object instanceof  NotificationListener))
1312:                    return;
1313:
1314:                ((NotificationListener) object).handleNotification(
1315:                        notification, handback);
1316:            }
1317:
1318:            //IMplementing for MBeanRegistration
1319:            public ObjectName preRegister(MBeanServer server, ObjectName name)
1320:                    throws Exception {
1321:                if (!(object instanceof  MBeanRegistration))
1322:                    return null;
1323:
1324:                return ((MBeanRegistration) object).preRegister(server, name);
1325:            }
1326:
1327:            public void postRegister(java.lang.Boolean registrationDone) {
1328:                if (!(object instanceof  MBeanRegistration))
1329:                    return;
1330:
1331:                ((MBeanRegistration) object).postRegister(registrationDone);
1332:            }
1333:
1334:            public void preDeregister() throws Exception {
1335:                if (!(object instanceof  MBeanRegistration))
1336:                    return;
1337:                try {
1338:                    ((MBeanRegistration) object).preDeregister();
1339:                } catch (Exception e) {
1340:                    throw e;
1341:                }
1342:            }
1343:
1344:            public void postDeregister() {
1345:                if (!(object instanceof  MBeanRegistration))
1346:                    return;
1347:
1348:                ((MBeanRegistration) object).postDeregister();
1349:            }
1350:
1351:            private boolean getFlagValue(Class mainClass, boolean value) {
1352:                clazz = mainClass.getSuperclass();
1353:
1354:                Class[] interfaces = clazz.getInterfaces();
1355:                for (int i = 0; i < interfaces.length; i++) {
1356:                    if (interfaces[i].getName().equals(
1357:                            clazz.getName() + "MBean")) {
1358:                        iclazz = interfaces[i];
1359:                        value = true;
1360:                        break;
1361:                    }
1362:
1363:                }
1364:
1365:                return value;
1366:            }
1367:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.