Source Code Cross Referenced for ModelAgent.java in  » JMX » jfoxmx » example » jmx » modelmbean » 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 » jfoxmx » example.jmx.modelmbean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* JFox, the OpenSource J2EE Application Server
002:         * 
003:         * Copyright (C) 2002 huihoo.org
004:         * Distributable under GNU LGPL license
005:         * See the GNU Lesser General Public License for more details.
006:         */
007:
008:        package example.jmx.modelmbean;
009:
010:        import java.io.IOException;
011:        import java.lang.reflect.Constructor;
012:
013:        import javax.management.NotificationListener;
014:        import javax.management.Descriptor;
015:        import javax.management.MBeanServerFactory;
016:        import javax.management.MalformedObjectNameException;
017:        import javax.management.ObjectName;
018:        import javax.management.Attribute;
019:        import javax.management.MBeanNotificationInfo;
020:        import javax.management.MBeanConstructorInfo;
021:        import javax.management.MBeanAttributeInfo;
022:        import javax.management.MBeanOperationInfo;
023:        import javax.management.MBeanServer;
024:        import javax.management.MBeanParameterInfo;
025:        import javax.management.MBeanInfo;
026:        import javax.management.modelmbean.ModelMBeanAttributeInfo;
027:        import javax.management.modelmbean.ModelMBeanConstructorInfo;
028:        import javax.management.modelmbean.ModelMBeanOperationInfo;
029:        import javax.management.modelmbean.ModelMBeanNotificationInfo;
030:        import javax.management.modelmbean.ModelMBeanInfo;
031:        import javax.management.modelmbean.RequiredModelMBean;
032:        import javax.management.modelmbean.ModelMBeanInfoSupport;
033:        import javax.management.modelmbean.DescriptorSupport;
034:
035:        /**
036:         *
037:         * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
038:         */
039:
040:        public class ModelAgent {
041:            /*
042:             * -----------------------------------------------------
043:             * PRIVATE VARIABLES
044:             * -----------------------------------------------------
045:             */
046:
047:            private String dClassName = "example.jmx.modelmbean.TestBean";
048:            private String dDescription = "Simple implementation of a test app Bean.";
049:
050:            private ModelMBeanAttributeInfo[] dAttributes = new ModelMBeanAttributeInfo[3];
051:            private ModelMBeanConstructorInfo[] dConstructors = new ModelMBeanConstructorInfo[1];
052:            private ModelMBeanOperationInfo[] dOperations = new ModelMBeanOperationInfo[6];
053:            private ModelMBeanNotificationInfo[] dNotifications = new ModelMBeanNotificationInfo[1];
054:            private Descriptor mmbDesc = null;
055:            private ModelMBeanInfo dMBeanInfo = null;
056:
057:            NotificationListener attrListener = null;
058:
059:            /*
060:             * ------------------------------------------
061:             *  CONSTRUCTORS
062:             * ------------------------------------------
063:             */
064:
065:            public ModelAgent() {
066:
067:                // Parse system properties to check if LEVEL_TRACE and/or LEVEL_DEBUG are set
068:                // and enable the TRACE level accordingly
069:                /*
070:                 try
071:                 {
072:                 TraceManager.parseTraceProperties();
073:                 } catch (IOException e)
074:                 {
075:                 e.printStackTrace();
076:                 }
077:                 */
078:                // CREATE the MBeanServer
079:                //
080:                echo("\n\tCREATE the MBeanServer.");
081:                server = MBeanServerFactory.createMBeanServer();
082:            }
083:
084:            /*
085:             * ------------------------------------------
086:             *  PUBLIC METHODS
087:             * ------------------------------------------
088:             */
089:
090:            public static void main(String[] args) {
091:
092:                // START
093:                //
094:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
095:                echo("\n>>> CREATE the agent...");
096:                ModelAgent agent = new ModelAgent();
097:
098:                // DO THE DEMO
099:                //
100:                agent.doSimpleDemo();
101:
102:                // END
103:                //
104:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
105:                echo("\n>>> END of the SimpleModel example:\n");
106:                //    String localHost = null;
107:                //    try {
108:                //      localHost = java.net.InetAddress.getLocalHost().getHostName();
109:                //    } catch (UnknownHostException e) {
110:                //      localHost = "localhost";
111:                //    }
112:                echo("\n\tpress <Enter> to doStop the agent...\n");
113:                waitForEnterPressed();
114:                System.exit(0);
115:            }
116:
117:            /*
118:             * ------------------------------------------
119:             *  PRIVATE METHODS
120:             * ------------------------------------------
121:             */
122:
123:            private void doSimpleDemo() {
124:
125:                // build the simple MBean ObjectName
126:                //
127:                ObjectName mbeanObjectName = null;
128:                String domain = server.getDefaultDomain();
129:                //    String mbeanName = "ModelSample";
130:                String mbeanName = "example.jmx.ModelAgent";
131:
132:                attrListener = new TestBeanAttributeChangeListener();
133:
134:                try {
135:                    mbeanObjectName = new ObjectName(domain + ":type="
136:                            + mbeanName);
137:                } catch (MalformedObjectNameException e) {
138:                    echo("\t!!! Could not doCreate the MBean ObjectName !!!");
139:                    e.printStackTrace();
140:                    echo("\nEXITING...\n");
141:                    System.exit(1);
142:                }
143:                // doCreate and register the MBean
144:                //
145:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
146:                createMBean(mbeanObjectName, mbeanName);
147:                echo("\npress <Enter> to continue...\n");
148:                waitForEnterPressed();
149:
150:                // get and display the management information exposed by the MBean
151:                //
152:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
153:                echo("\nPrinting Descriptors from MBeanInfo");
154:                printMBeanInfo(mbeanObjectName, mbeanName);
155:                echo("\npress <Enter> to continue...\n");
156:                waitForEnterPressed();
157:
158:                // doCreate an event listener
159:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
160:                echo("\nCreate event listeners");
161:                createEventListeners(mbeanObjectName, attrListener);
162:                echo("\npress <Enter> to continue...\n");
163:                waitForEnterPressed();
164:                // manage the MBean
165:                //
166:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
167:                manageSimpleBean(mbeanObjectName, mbeanName);
168:
169:                // trying to do illegal management actions...
170:                //
171:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
172:                goTooFar(mbeanObjectName);
173:                echo("\npress <Enter> to continue...\n");
174:                waitForEnterPressed();
175:
176:                //issue a notification
177:
178:            }
179:
180:            private void createMBean(ObjectName mbeanObjectName,
181:                    String mbeanName) {
182:
183:                echo("\n>>> CREATE the " + mbeanName
184:                        + " MBean within the MBeanServer:");
185:                //    String mbeanClassName = "javax.management.modelmbean.RequiredModelMBean";
186:                echo("\tOBJECT NAME = " + mbeanObjectName);
187:
188:                // set management interface in ModelMBean - attributes, operations, notifications
189:                buildDynamicMBeanInfo(mbeanObjectName, mbeanName);
190:                try {
191:                    System.out.println("====>HERE");
192:                    RequiredModelMBean modelmbean = new RequiredModelMBean(
193:                            dMBeanInfo);
194:                    // Set the managed resource for ModelMBean instance
195:                    System.out.println("====>HERE");
196:                    modelmbean.setManagedResource(new TestBean(),
197:                            "objectReference");
198:                    // register the ModelMBean in the MBean Server
199:
200:                    server.registerMBean(modelmbean, mbeanObjectName);
201:                } catch (Exception e) {
202:                    echo("\t!!! ModelAgent: Could not doCreate the "
203:                            + mbeanName + " MBean !!!");
204:                    e.printStackTrace();
205:                    echo("\nEXITING...\n");
206:                    System.exit(1);
207:                }
208:
209:                echo("\n\tModelMBean has been successfully created.\n");
210:            }
211:
212:            private void createEventListeners(ObjectName mbeanObjectName,
213:                    NotificationListener aListener) {
214:                try {
215:
216:                    server.invoke(mbeanObjectName,
217:                            "addAttributeChangeNotificationListener",
218:                            (new Object[] { aListener, "State", null }),
219:                            (new String[] {
220:                                    "javax.management.NotificationListener",
221:                                    "java.lang.String", "java.lang.Object" }));
222:                    echo("\n\tEvent listener created successfully\n");
223:                } catch (Exception e) {
224:                    echo("Error! Creating Event listener with reflectInvoke failed with message:\n");
225:                    echo(e.getMessage() + "\n");
226:                    echo("\nEXITING...\n");
227:                    e.printStackTrace();
228:                    System.exit(1);
229:                }
230:            }
231:
232:            private void manageSimpleBean(ObjectName mbeanObjectName,
233:                    String mbeanName) {
234:
235:                echo("\n>>> MANAGING the " + mbeanName + " MBean ");
236:                echo("using its attributes and operations exposed for management");
237:
238:                try {
239:                    // Get attribute values
240:                    sleep(1000);
241:                    echo("\n>> Printing attributes from ModelMBean \n");
242:                    printSimpleAttributes(mbeanObjectName);
243:
244:                    sleep(1000);
245:                    echo("\n>> Printing attributes from instance cache \n");
246:                    printSimpleAttributes(mbeanObjectName);
247:
248:                    // Change State attribute
249:                    sleep(1000);
250:                    echo("\n>>  Setting State attribute to value \"new state\"...");
251:                    Attribute stateAttribute = new Attribute("State",
252:                            "new state");
253:                    server.setAttribute(mbeanObjectName, stateAttribute);
254:
255:                    // Get attribute values
256:                    sleep(1000);
257:                    printSimpleAttributes(mbeanObjectName);
258:
259:                    echo("\n>> The NbChanges attribute is still \"0\" as its cached value is valid for 5 seconds (currencyTimeLimit=5s)");
260:
261:                    echo("\n>> Wait for 5 seconds and print new attributes values ...");
262:                    echo("\npress <Enter> to continue...\n");
263:                    waitForEnterPressed();
264:                    sleep(5000);
265:                    printSimpleAttributes(mbeanObjectName);
266:
267:                    // Invoking reset operation
268:                    sleep(1000);
269:                    echo("\n>>  Invoking reset operation...");
270:                    server.invoke(mbeanObjectName, "reset", null, null);
271:
272:                    // Get attribute values          1
273:                    echo("\n>>  Printing reset attribute values");
274:                    sleep(1000);
275:                    printSimpleAttributes(mbeanObjectName);
276:
277:                    echo("\n>> The State and NbChanges attributes are still \"1\" and \"new state\" as their cached value is valid for 5 seconds (currencyTimeLimit=5s)");
278:
279:                    echo("\n>> Wait for 5 seconds and print new attributes values ...");
280:                    echo("\npress <Enter> to continue...\n");
281:
282:                    waitForEnterPressed();
283:                    sleep(5000);
284:                    printSimpleAttributes(mbeanObjectName);
285:
286:                    // Getting Notifications list
287:                    echo("\n>> Printing Notifications Broadcasted");
288:                    sleep(1000);
289:                    MBeanNotificationInfo[] myNotifys = (MBeanNotificationInfo[]) server
290:                            .invoke(mbeanObjectName, "getNotificationInfo",
291:                                    null, null);
292:                    echo("\n\tSupported notifications are:");
293:                    for (int i = 0; i < myNotifys.length; i++) {
294:                        echo("\n\t\t" + myNotifys[i].toString());
295:                    }
296:
297:                    // Accesssing and printing Procol Map for NbChanges
298:                    echo("\n>>  Exercising Protocol map for NbChanges");
299:                    sleep(1000);
300:                    ModelMBeanInfo myMMBI = (ModelMBeanInfo) server.invoke(
301:                            mbeanObjectName, "getMBeanInfo", null, null);
302:
303:                    Descriptor myDesc = myMMBI.getDescriptor("NbChanges",
304:                            "attribute");
305:
306:                    echo("\n\tRetrieving specific protocols:");
307:                    //          echo("Descriptor: " + myDesc.toString());
308:                    Descriptor pm = (Descriptor) myDesc
309:                            .getFieldValue("protocolMap");
310:                    //          echo("ProtocolMap *"+pm.toString()+"*");
311:                    echo("\tProtocolMap lookup SNMP is "
312:                            + pm.getFieldValue("SNMP"));
313:                    echo("\tProtocolMap lookup CIM is "
314:                            + pm.getFieldValue("CIM"));
315:
316:                    echo("\n\tDynamically updating Protocol Map:");
317:                    pm.setField("CIM", "ManagedResource.LongVersion");
318:                    pm.setField("CMIP", "SwitchData");
319:
320:                    echo("\n\tPrinting Protocol Map");
321:                    String[] pmKeys = pm.getFieldNames();
322:                    Object[] pmEntries = pm.getFieldValues(null);
323:                    for (int i = 0; i < pmKeys.length; i++) {
324:                        echo("\tProtocol Map Name " + i + ": Name: "
325:                                + pmKeys[i] + ": Entry:"
326:                                + pmEntries[i].toString());
327:                    }
328:
329:                    echo("\n>>  Testing operation caching");
330:                    echo("\n>>  Invoking getNbResets");
331:                    Integer numResets = (Integer) server.invoke(
332:                            mbeanObjectName, "getNbResets", null, null);
333:                    echo("\n\tReceived " + numResets
334:                            + " from getNbResets first time");
335:
336:                    echo("\n>>  Invoking second reset operation...");
337:                    server.invoke(mbeanObjectName, "reset", null, null);
338:                    Integer numResets2 = (Integer) server.invoke(
339:                            mbeanObjectName, "getNbResets", null, null);
340:                    echo("\n\tReceived "
341:                            + numResets2
342:                            + " from getNbResets second time (from operation cache)");
343:
344:                    echo("\n>>  Invoking get of attribute ONLY provided through ModelMBeanAttributeInfo (should be 99)...");
345:                    Integer respHardValue = (Integer) server.getAttribute(
346:                            mbeanObjectName, "HardValue");
347:                    echo("\n\tReceived " + respHardValue
348:                            + " from getAttributeInfo of hardValue");
349:
350:                } catch (Exception e) {
351:                    echo("\nManageSimpleBean failed with " + e.getMessage());
352:                    e.printStackTrace();
353:                    return;
354:                }
355:            }
356:
357:            private void goTooFar(ObjectName mbeanObjectName) {
358:
359:                echo("\n>>> Trying to set the NbChanges attribute (read-only)!");
360:                echo("\n... We should get an AttributeNotFoundException:\n");
361:                sleep(1000);
362:                // Try to set the NbChanges attribute
363:                Attribute nbChangesAttribute = new Attribute("NbChanges",
364:                        new Integer(1));
365:                try {
366:                    server.setAttribute(mbeanObjectName, nbChangesAttribute);
367:                } catch (Exception e) {
368:                    e.printStackTrace();
369:                }
370:                echo("\n\n>>> Trying to access the NbResets property (not exposed for management)!");
371:                echo("\n... We should get an AttributeNotFoundException:\n");
372:                sleep(1000);
373:                // Try to access the NbResets property
374:                try {
375:                    //      Integer NbResets = (Integer)
376:                    server.getAttribute(mbeanObjectName, "NbResets");
377:                } catch (Exception e) {
378:                    e.printStackTrace();
379:                }
380:                return;
381:            }
382:
383:            private void printMBeanInfo(ObjectName mbeanObjectName,
384:                    String mbeanName) {
385:
386:                echo("\n>>> Getting the management information for the "
387:                        + mbeanName + ":" + mbeanObjectName + " MBean");
388:                echo("    using the getMBeanInfo method of the MBeanServer");
389:                sleep(1000);
390:                ModelMBeanInfo info = null;
391:                try {
392:                    info = (ModelMBeanInfo) (server
393:                            .getMBeanInfo(mbeanObjectName));
394:                    if (info == null) {
395:                        echo("\nModelMBeanInfo from JMX Agent is null!");
396:                    }
397:                } catch (Exception e) {
398:                    echo("\t!!! ModelAgent:printMBeanInfo: Could not get MBeanInfo object for "
399:                            + mbeanName
400:                            + " exception type "
401:                            + e.getClass().toString()
402:                            + ":"
403:                            + e.getMessage()
404:                            + "!!!");
405:                    e.printStackTrace();
406:                    return;
407:                }
408:                echo("\nCLASSNAME: \t" + info.getClassName());
409:                echo("\nDESCRIPTION: \t" + info.getDescription());
410:                try {
411:                    echo("\nMBEANDESCRIPTOR: \t"
412:                            + (info.getMBeanDescriptor()).toString());
413:                } catch (Exception e) {
414:                    echo("\nMBEANDESCRIPTOR: \tNone");
415:                }
416:
417:                echo("\nATTRIBUTES");
418:                MBeanAttributeInfo[] attrInfo = (info.getAttributes());
419:                if (attrInfo.length > 0) {
420:                    for (int i = 0; i < attrInfo.length; i++) {
421:                        echo("\n ** NAME: \t" + attrInfo[i].getName());
422:                        echo("    DESCR: \t" + attrInfo[i].getDescription());
423:                        echo("    TYPE: \t" + attrInfo[i].getType()
424:                                + "\tREAD: " + attrInfo[i].isReadable()
425:                                + "\tWRITE: " + attrInfo[i].isWritable());
426:                        echo("    DESCRIPTOR: \t"
427:                                + (((ModelMBeanAttributeInfo) attrInfo[i])
428:                                        .getDescriptor()).toString());
429:                    }
430:                } else
431:                    echo(" ** No attributes **");
432:
433:                MBeanConstructorInfo[] constrInfo = info.getConstructors();
434:                echo("\nCONSTRUCTORS");
435:                if (constrInfo.length > 0) {
436:                    for (int i = 0; i < constrInfo.length; i++) {
437:                        echo("\n ** NAME: \t" + constrInfo[i].getName());
438:                        echo("    DESCR: \t" + constrInfo[i].getDescription());
439:                        echo("    PARAM: \t"
440:                                + constrInfo[i].getSignature().length
441:                                + " parameter(s)");
442:                        echo("    DESCRIPTOR: \t"
443:                                + (((ModelMBeanConstructorInfo) constrInfo[i])
444:                                        .getDescriptor()).toString());
445:                    }
446:                } else
447:                    echo(" ** No Constructors **");
448:
449:                echo("\nOPERATIONS");
450:                MBeanOperationInfo[] opInfo = info.getOperations();
451:                if (opInfo.length > 0) {
452:                    for (int i = 0; i < opInfo.length; i++) {
453:                        echo("\n ** NAME: \t" + opInfo[i].getName());
454:                        echo("    DESCR: \t" + opInfo[i].getDescription());
455:                        echo("    PARAM: \t" + opInfo[i].getSignature().length
456:                                + " parameter(s)");
457:                        echo("    DESCRIPTOR: \t"
458:                                + (((ModelMBeanOperationInfo) opInfo[i])
459:                                        .getDescriptor()).toString());
460:                    }
461:                } else
462:                    echo(" ** No operations ** ");
463:
464:                echo("\nNOTIFICATIONS");
465:                MBeanNotificationInfo[] notifInfo = info.getNotifications();
466:                if (notifInfo.length > 0) {
467:                    for (int i = 0; i < notifInfo.length; i++) {
468:                        echo("\n ** NAME: \t" + notifInfo[i].getName());
469:                        echo("    DESCR: \t" + notifInfo[i].getDescription());
470:                        echo("    DESCRIPTOR: \t"
471:                                + (((ModelMBeanNotificationInfo) notifInfo[i])
472:                                        .getDescriptor()).toString());
473:                    }
474:                } else
475:                    echo(" ** No notifications **");
476:                echo("\nEnd of MBeanInfo print");
477:            }
478:
479:            private void printSimpleAttributes(ObjectName mbeanObjectName) {
480:
481:                try {
482:                    echo("\n\tGetting attribute values:");
483:                    String State = (String) server.getAttribute(
484:                            mbeanObjectName, "State");
485:                    Integer NbChanges = (Integer) server.getAttribute(
486:                            mbeanObjectName, "NbChanges");
487:                    echo("\n\t\tState     = \"" + State + "\"");
488:                    echo("\t\tNbChanges = \"" + NbChanges.toString() + "\"");
489:                } catch (Exception e) {
490:                    echo("\tModelAgent:printSimpleAttributes: !!! Could not read attributes !!!");
491:                    e.printStackTrace();
492:                    return;
493:                }
494:            }
495:
496:            private static void echo(String msg) {
497:                System.out.println(msg);
498:            }
499:
500:            private static void sleep(int millis) {
501:                try {
502:                    Thread.sleep(millis);
503:                } catch (InterruptedException e) {
504:                    return;
505:                }
506:            }
507:
508:            private static void waitForEnterPressed() {
509:                try {
510:                    boolean done = false;
511:                    while (!done) {
512:                        char ch = (char) System.in.read();
513:                        if (ch < 0 || ch == '\n') {
514:                            done = true;
515:                        }
516:                    }
517:                } catch (IOException e) {
518:                    e.printStackTrace();
519:                }
520:            }
521:
522:            /*
523:             * ------------------------------------------
524:             *  PRIVATE VARIABLES
525:             * ------------------------------------------
526:             */
527:
528:            private MBeanServer server = null;
529:
530:            /*
531:             * -----------------------------------------------------
532:             * PRIVATE METHODS
533:             * -----------------------------------------------------
534:             */
535:
536:            /*
537:             private void loadDynamicMBeanInfo(ObjectName inMbeanObjectName, String inMbeanName) {
538:             try {
539:
540:             //      Class appBean = Class.forName(dClassName);
541:
542:
543:             //echo("\nSetting mbeanDescriptor " + mmbDesc);
544:
545:             dMBeanInfo = new ModelMBeanInfoSupport(dClassName,
546:             dDescription,
547:             dAttributes,
548:             dConstructors,
549:             dOperations,
550:             dNotifications,
551:             mmbDesc);
552:
553:
554:             } catch (Exception e) {
555:             echo("\nException in loadDynamicMBeanInfo : " + e.getMessage());
556:             e.printStackTrace();
557:             }
558:
559:
560:             }
561:             */
562:            /**
563:             * Build the private dMBeanInfo field,
564:             * which represents the management interface exposed by the MBean;
565:             * that is, the set of attributes, constructors, operations and notifications
566:             * which are available for management.
567:             *
568:             * A reference to the dMBeanInfo object is returned by the getMBeanInfo() method
569:             * of the DynamicMBean interface. Note that, once constructed, an MBeanInfo object is i
570:             */
571:            private void buildDynamicMBeanInfo(ObjectName inMbeanObjectName,
572:                    String inMbeanName) {
573:                try {
574:
575:                    Class appBean = Class.forName(dClassName);
576:
577:                    mmbDesc = new DescriptorSupport(new String[] {
578:                            ("name=" + inMbeanName), "descriptorType=mbean",
579:                            ("displayName=" + inMbeanObjectName), "log=T",
580:                            "logfile=jmxmain.log", "currencyTimeLimit=5" });
581:
582:                    Descriptor stateDesc = new DescriptorSupport();
583:                    stateDesc.setField("name", "State");
584:                    stateDesc.setField("descriptorType", "attribute");
585:                    stateDesc.setField("displayName", "MyState");
586:                    stateDesc.setField("getMethod", "getStatus");
587:                    stateDesc.setField("setMethod", "setState");
588:                    stateDesc.setField("currencyTimeLimit", "20");
589:                    // store enable
590:                    stateDesc.setField("persistPolicy", "OnUpdate");
591:
592:                    //          echo("\nbuildModelMBeanInfo: State descriptor is " + stateDesc.toString());
593:                    dAttributes[0] = new ModelMBeanAttributeInfo("State",
594:                            "java.lang.String", "State: state string.", true,
595:                            true, false, stateDesc);
596:
597:                    Descriptor nbChangesDesc = new DescriptorSupport();
598:                    nbChangesDesc.setField("name", "NbChanges");
599:                    nbChangesDesc.setField("descriptorType", "attribute");
600:                    nbChangesDesc.setField("default", "0");
601:                    nbChangesDesc.setField("displayName", "MyChangesCount");
602:                    nbChangesDesc.setField("getMethod", "getNbChanges");
603:                    nbChangesDesc.setField("setMethod", "setNbChanges");
604:                    Descriptor nbChangesMap = new DescriptorSupport(
605:                            new String[] { "SNMP=1.3.6.9.12.15.18.21.0",
606:                                    "CIM=ManagedResource.Version" });
607:
608:                    nbChangesDesc.setField("protocolMap", (nbChangesMap));
609:
610:                    dAttributes[1] = new ModelMBeanAttributeInfo("NbChanges",
611:                            "java.lang.Integer",
612:                            "NbChanges: number of times the State string",
613:                            true, false, false, nbChangesDesc);
614:
615:                    Descriptor hardValueDesc = new DescriptorSupport();
616:                    hardValueDesc.setField("name", "HardValue");
617:                    hardValueDesc.setField("descriptorType", "attribute");
618:                    hardValueDesc.setField("value", new Integer("99"));
619:                    hardValueDesc.setField("displayName", "HardCodedValue");
620:                    hardValueDesc.setField("currencyTimeLimit", "-1");
621:
622:                    dAttributes[2] = new ModelMBeanAttributeInfo(
623:                            "HardValue",
624:                            "java.lang.Integer",
625:                            "HardValue: static value in ModelMBeanInfo and not in TestBean",
626:                            true, false, false, hardValueDesc);
627:
628:                    Constructor[] constructors = appBean.getConstructors();
629:
630:                    Descriptor testBeanDesc = new DescriptorSupport();
631:                    testBeanDesc.setField("name",
632:                            "example.jmx.modelmbean.TestBean");
633:                    testBeanDesc.setField("descriptorType", "operation");
634:                    testBeanDesc.setField("role", "constructor");
635:
636:                    dConstructors[0] = new ModelMBeanConstructorInfo(
637:                            "TestBean(): Constructs a TestBean App",
638:                            constructors[0], testBeanDesc);
639:
640:                    MBeanParameterInfo[] params = null;
641:
642:                    Descriptor resetDesc = new DescriptorSupport();
643:                    resetDesc.setField("name", "reset");
644:                    resetDesc.setField("descriptorType", "operation");
645:                    resetDesc.setField("class",
646:                            "example.jmx.modelmbean.TestBean");
647:                    resetDesc.setField("role", "operation");
648:
649:                    dOperations[0] = new ModelMBeanOperationInfo("reset",
650:                            "reset(): reset State and NbChanges", params,
651:                            "void", MBeanOperationInfo.ACTION, resetDesc);
652:
653:                    Descriptor getNbResetsDesc = new DescriptorSupport(
654:                            new String[] {
655:                                    "name=getNbResets",
656:                                    "class=example.jmx.modelmbean.TestBeanFriend",
657:                                    "descriptorType=operation",
658:                                    "role=operation" });
659:                    TestBeanFriend tbf = new TestBeanFriend();
660:                    getNbResetsDesc.setField("targetObject", tbf);
661:                    getNbResetsDesc.setField("targetType", "objectReference");
662:
663:                    dOperations[1] = new ModelMBeanOperationInfo("getNbResets",
664:                            "getNbResets(): get number of resets done", params,
665:                            "java.lang.Integer", MBeanOperationInfo.INFO,
666:                            getNbResetsDesc);
667:
668:                    Descriptor getStateDesc = new DescriptorSupport(
669:                            new String[] { "name=getStatus",
670:                                    "descriptorType=operation",
671:                                    "class=example.jmx.modelmbean.TestBean",
672:                                    "role=getter" });
673:
674:                    dOperations[2] = new ModelMBeanOperationInfo("getStatus",
675:                            "get state attribute", params, "java.lang.String",
676:                            MBeanOperationInfo.ACTION, getStateDesc);
677:
678:                    Descriptor setStateDesc = new DescriptorSupport(
679:                            new String[] { "name=setState",
680:                                    "descriptorType=operation",
681:                                    "class=example.jmx.modelmbean.TestBean",
682:                                    "role=setter" });
683:
684:                    MBeanParameterInfo[] setStateParms = new MBeanParameterInfo[] { (new MBeanParameterInfo(
685:                            "newState", "java.lang.String", "new State value")) };
686:
687:                    dOperations[3] = new ModelMBeanOperationInfo("setState",
688:                            "set State attribute", setStateParms, "void",
689:                            MBeanOperationInfo.ACTION, setStateDesc);
690:
691:                    Descriptor getNbChangesDesc = new DescriptorSupport(
692:                            new String[] { "name=getNbChanges",
693:                                    "descriptorType=operation",
694:                                    "class=example.jmx.modelmbean.TestBean",
695:                                    "role=getter" });
696:
697:                    dOperations[4] = new ModelMBeanOperationInfo(
698:                            "getNbChanges", "get NbChanges attribute", params,
699:                            "java.lang.Integer", MBeanOperationInfo.INFO,
700:                            getNbChangesDesc);
701:
702:                    Descriptor setNbChangesDesc = new DescriptorSupport(
703:                            new String[] { "name=setNbChanges",
704:                                    "descriptorType=operation",
705:                                    "class=example.jmx.modelmbean.TestBean",
706:                                    "role=setter" });
707:
708:                    MBeanParameterInfo[] setNbChangesParms = new MBeanParameterInfo[] { (new MBeanParameterInfo(
709:                            "newNbChanges", "java.lang.Integer",
710:                            "new value for Number of Changes")) };
711:
712:                    dOperations[5] = new ModelMBeanOperationInfo(
713:                            "setNbChanges", "set NbChanges attribute",
714:                            setNbChangesParms, "void",
715:                            MBeanOperationInfo.ACTION, setNbChangesDesc);
716:
717:                    //dNotifications[0] = new MBeanNotificationInfo((new String[] {"jmx.attribute.change"}),
718:                    //                        "AttributeChange","ModelMBean Attribute Change Event");
719:
720:                    Descriptor genEventDesc = new DescriptorSupport(
721:                            new String[] { "descriptorType=notification",
722:                                    "name=jmx.ModelMBean.General",
723:                                    "severity=4", "MessageId=MA001", "log=T",
724:                                    "logfile=jmx.log" });
725:                    String[] genTypes = new String[1];
726:                    genTypes[0] = new String("jmx.ModelMBean.General");
727:                    dNotifications[0] = new ModelMBeanNotificationInfo(
728:                            genTypes, "jmx.ModelMBean.General", // was Generic
729:                            "Generic Event", genEventDesc); // test event
730:
731:                    dMBeanInfo = new ModelMBeanInfoSupport(dClassName,
732:                            dDescription, dAttributes, dConstructors,
733:                            dOperations, dNotifications);
734:
735:                    dMBeanInfo.setMBeanDescriptor(mmbDesc);
736:
737:                } catch (Exception e) {
738:                    echo("\nException in buildDynamicMBeanInfo : "
739:                            + e.getMessage());
740:                    e.printStackTrace();
741:                }
742:            }
743:
744:            public void printLocalMBeanInfo(MBeanInfo info) {
745:                echo("\nCLASSNAME: \t" + info.getClassName());
746:                echo("\nDESCRIPTION: \t" + info.getDescription());
747:                echo("\nATTRIBUTES");
748:                MBeanAttributeInfo[] attrInfo = info.getAttributes();
749:                if (attrInfo.length > 0) {
750:                    for (int i = 0; i < attrInfo.length; i++) {
751:                        echo(" ** NAME: \t" + attrInfo[i].getName());
752:                        echo("    DESCR: \t" + attrInfo[i].getDescription());
753:                        echo("    TYPE: \t" + attrInfo[i].getType()
754:                                + "\tREAD: " + attrInfo[i].isReadable()
755:                                + "\tWRITE: " + attrInfo[i].isWritable());
756:                        echo("    DESCRIPTOR: \t"
757:                                + (((ModelMBeanAttributeInfo) attrInfo[i])
758:                                        .getDescriptor()).toString());
759:                    }
760:                } else
761:                    echo(" ** No attributes **");
762:                echo("\nCONSTRUCTORS");
763:                MBeanConstructorInfo[] constrInfo = info.getConstructors();
764:                for (int i = 0; i < constrInfo.length; i++) {
765:                    echo(" ** NAME: \t" + constrInfo[i].getName());
766:                    echo("    DESCR: \t" + constrInfo[i].getDescription());
767:                    echo("    PARAM: \t" + constrInfo[i].getSignature().length
768:                            + " parameter(s)");
769:                    echo("    DESCRIPTOR: \t"
770:                            + (((ModelMBeanConstructorInfo) constrInfo[i])
771:                                    .getDescriptor()).toString());
772:                }
773:                echo("\nOPERATIONS");
774:                MBeanOperationInfo[] opInfo = info.getOperations();
775:                if (opInfo.length > 0) {
776:                    for (int i = 0; i < opInfo.length; i++) {
777:                        echo(" ** NAME: \t" + opInfo[i].getName());
778:                        echo("    DESCR: \t" + opInfo[i].getDescription());
779:                        echo("    PARAM: \t" + opInfo[i].getSignature().length
780:                                + " parameter(s)");
781:                        echo("    DESCRIPTOR: \t"
782:                                + (((ModelMBeanOperationInfo) opInfo[i])
783:                                        .getDescriptor()).toString());
784:                    }
785:                } else
786:                    echo(" ** No operations ** ");
787:                echo("\nNOTIFICATIONS");
788:                MBeanNotificationInfo[] notifInfo = info.getNotifications();
789:                if (notifInfo.length > 0) {
790:                    for (int i = 0; i < notifInfo.length; i++) {
791:                        echo(" ** NAME: \t" + notifInfo[i].getName());
792:                        echo("    DESCR: \t" + notifInfo[i].getDescription());
793:                        echo("    DESCRIPTOR: \t"
794:                                + (((ModelMBeanNotificationInfo) notifInfo[i])
795:                                        .getDescriptor()).toString());
796:                    }
797:                } else
798:                    echo(" ** No notifications **");
799:
800:            }
801:
802:            public void printLocalDescriptors(MBeanInfo mbi)
803:                    throws javax.management.MBeanException {
804:                echo(mbi.getDescription() + "Descriptors:\n");
805:                echo("Attribute Descriptors:\n");
806:                Descriptor[] dArray;
807:                dArray = ((ModelMBeanInfo) mbi).getDescriptors("attribute");
808:                for (int i = 0; i < dArray.length; i++) {
809:                    String[] afields = dArray[i].getFields();
810:                    for (int j = 0; j < afields.length; j++) {
811:                        echo(afields[j] + "\n");
812:                    }
813:                }
814:
815:                echo("Operation Descriptors:\n");
816:                dArray = ((ModelMBeanInfo) mbi).getDescriptors("operation");
817:                for (int i = 0; i < dArray.length; i++) {
818:                    echo("\n*Operation****************************");
819:                    String[] ofields = dArray[i].getFields();
820:                    for (int j = 0; j < ofields.length; j++) {
821:                        echo(ofields[j] + "\n");
822:                    }
823:                }
824:
825:                echo("Notification Descriptors:\n");
826:                dArray = ((ModelMBeanInfo) mbi).getDescriptors("notification");
827:                for (int i = 0; i < dArray.length; i++) {
828:                    System.out
829:                            .println("**Notification****************************");
830:                    String[] nfields = dArray[i].getFields();
831:                    for (int j = 0; j < nfields.length; j++) {
832:                        System.out.println(nfields[j] + "\n");
833:                    }
834:                }
835:            }
836:
837:            public void printModelMBeanDescriptors(ObjectName mbeanObjectName) {
838:                sleep(1000);
839:
840:                Descriptor[] dArray = new DescriptorSupport[0];
841:                try {
842:                    dArray = (Descriptor[]) (server.invoke(mbeanObjectName,
843:                            "getDescriptors", new Object[] {}, new String[] {}));
844:                    if (dArray == null) {
845:                        echo("\nDescriptor list is null!");
846:                    }
847:                } catch (Exception e) {
848:                    echo("\t!!! Could not get descriptors for mbeanName ");
849:                    e.printStackTrace();
850:                    return;
851:                }
852:
853:                echo("Descriptors: (");
854:                echo(dArray.length + ")\n");
855:                for (int i = 0; i < dArray.length; i++) {
856:                    echo("\n**Descriptor***********************");
857:                    String[] dlfields = dArray[i].getFields();
858:                    for (int j = 0; j < dlfields.length; j++) {
859:                        echo(dlfields[j] + "\n");
860:                    }
861:                }
862:            }
863:
864:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.