Source Code Cross Referenced for DynamicAgent.java in  » JMX » jfoxmx » example » jmx » dynamic » 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.dynamic 
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.dynamic;
009:
010:        import java.io.IOException; //import java.net.UnknownHostException;
011:
012:        import javax.management.MBeanServer;
013:        import javax.management.ObjectName;
014:        import javax.management.MBeanNotificationInfo;
015:        import javax.management.MBeanOperationInfo;
016:        import javax.management.MBeanConstructorInfo;
017:        import javax.management.MBeanAttributeInfo;
018:        import javax.management.MBeanInfo;
019:        import javax.management.Attribute;
020:        import javax.management.MalformedObjectNameException;
021:        import javax.management.MBeanServerFactory;
022:
023:        /**
024:         * 
025:         * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
026:         */
027:
028:        public class DynamicAgent {
029:            /*
030:             * ------------------------------------------
031:             *  PRIVATE VARIABLES
032:             * ------------------------------------------
033:             */
034:
035:            private MBeanServer server = null;
036:
037:            /*
038:             * ------------------------------------------
039:             *  CONSTRUCTORS
040:             * ------------------------------------------
041:             */
042:
043:            public DynamicAgent() {
044:                echo("\n\tCREATE the MBeanServer.");
045:                server = MBeanServerFactory.createMBeanServer();
046:            }
047:
048:            /*
049:             * ------------------------------------------
050:             *  PUBLIC METHODS
051:             * ------------------------------------------
052:             */
053:
054:            public static void main(String[] args) {
055:
056:                // START
057:                //
058:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
059:                echo("\n>>> CREATE the agent...");
060:                DynamicAgent agent = new DynamicAgent();
061:                echo("\npress <Enter> to continue...\n");
062:                waitForEnterPressed();
063:
064:                // DO THE DEMO
065:                //
066:                agent.doSimpleDemo();
067:
068:                // END
069:                //
070:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
071:                echo("\n>>> END of the SimpleDynamic example:\n");
072:                //    String localHost = null;
073:                //    try {
074:                //      localHost = java.net.InetAddress.getLocalHost().getHostName();
075:                //    } catch (UnknownHostException e) {
076:                //      localHost = "localhost" ;
077:                //    }
078:                echo("\n\tpress <Enter> to doStop the agent...\n");
079:                waitForEnterPressed();
080:                System.exit(0);
081:            }
082:
083:            /*
084:             * ------------------------------------------
085:             *  PRIVATE METHODS
086:             * ------------------------------------------
087:             */
088:
089:            private void doSimpleDemo() {
090:
091:                // build the simple MBean ObjectName
092:                //
093:                ObjectName mbeanObjectName = null;
094:                String domain = server.getDefaultDomain();
095:                String mbeanName = "example.jmx.dynamic.SimpleDynamic";
096:                try {
097:                    mbeanObjectName = new ObjectName(domain + ":type="
098:                            + mbeanName);
099:                } catch (MalformedObjectNameException e) {
100:                    echo("\t!!! Could not doCreate the MBean ObjectName !!!");
101:                    e.printStackTrace();
102:                    echo("\nEXITING...\n");
103:                    System.exit(1);
104:                }
105:                // doCreate and register the MBean
106:                //
107:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
108:                createSimpleBean(mbeanObjectName, mbeanName);
109:                echo("\npress <Enter> to continue...\n");
110:                waitForEnterPressed();
111:
112:                // get and display the management information exposed by the MBean
113:                //
114:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
115:                printMBeanInfo(mbeanObjectName, mbeanName);
116:                echo("\npress <Enter> to continue...\n");
117:                waitForEnterPressed();
118:
119:                // manage the MBean
120:                //
121:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
122:                manageSimpleBean(mbeanObjectName, mbeanName);
123:                echo("\npress <Enter> to continue...\n");
124:                waitForEnterPressed();
125:
126:                // trying to do illegal management actions...
127:                //
128:                echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
129:                goTooFar(mbeanObjectName);
130:                echo("\npress <Enter> to continue...\n");
131:                waitForEnterPressed();
132:            }
133:
134:            private void createSimpleBean(ObjectName mbeanObjectName,
135:                    String mbeanName) {
136:
137:                echo("\n>>> CREATE the " + mbeanName
138:                        + " MBean within the MBeanServer:");
139:                //String mbeanClassName = this.getClass().getPackage().getNameSpace() + mbeanName;
140:                String mbeanClassName = mbeanName;
141:                echo("\tOBJECT NAME           = " + mbeanObjectName);
142:                try {
143:                    server.createMBean(mbeanClassName, mbeanObjectName);
144:                } catch (Exception e) {
145:                    echo("\t!!! Could not doCreate the " + mbeanName
146:                            + " MBean !!!");
147:                    e.printStackTrace();
148:                    echo("\nEXITING...\n");
149:                    System.exit(1);
150:                }
151:            }
152:
153:            private void manageSimpleBean(ObjectName mbeanObjectName,
154:                    String mbeanName) {
155:
156:                echo("\n>>> MANAGING the " + mbeanName + " MBean");
157:                echo("    using its attributes and operations exposed for management");
158:
159:                try {
160:                    // Get attribute values
161:                    sleep(1000);
162:                    printSimpleAttributes(mbeanObjectName);
163:
164:                    // Change State attribute
165:                    sleep(1000);
166:                    echo("\n    Setting State attribute to value \"new state\"...");
167:                    Attribute stateAttribute = new Attribute("State",
168:                            "new state");
169:                    server.setAttribute(mbeanObjectName, stateAttribute);
170:
171:                    // Get attribute values
172:                    sleep(1000);
173:                    printSimpleAttributes(mbeanObjectName);
174:
175:                    // Invoking reset operation
176:                    sleep(1000);
177:                    echo("\n    Invoking reset operation...");
178:                    server.invoke(mbeanObjectName, "reset", null, null);
179:
180:                    // Get attribute values
181:                    sleep(1000);
182:                    printSimpleAttributes(mbeanObjectName);
183:
184:                } catch (Exception e) {
185:                    e.printStackTrace();
186:                    return;
187:                }
188:            }
189:
190:            private void goTooFar(ObjectName mbeanObjectName) {
191:
192:                echo("\n>>> Trying to set the NbChanges attribute (read-only)!");
193:                echo("\n... We should get an AttributeNotFoundException:\n");
194:                sleep(1000);
195:                // Try to set the NbChanges attribute
196:                Attribute nbChangesAttribute = new Attribute("NbChanges",
197:                        new Integer(1));
198:                try {
199:                    server.setAttribute(mbeanObjectName, nbChangesAttribute);
200:                } catch (Exception e) {
201:                    e.printStackTrace();
202:                }
203:                echo("\n\n>>> Trying to access the NbResets property (not exposed for management)!");
204:                echo("\n... We should get an AttributeNotFoundException:\n");
205:                sleep(1000);
206:                // Try to access the NbResets property
207:                try {
208:                    server.getAttribute(mbeanObjectName, "NbResets");
209:                } catch (Exception e) {
210:                    e.printStackTrace();
211:                }
212:                return;
213:            }
214:
215:            private void printMBeanInfo(ObjectName mbeanObjectName,
216:                    String mbeanName) {
217:
218:                echo("\n>>> Getting the management information for the "
219:                        + mbeanName + " MBean");
220:                echo("    using the getMBeanInfo method of the MBeanServer");
221:                sleep(1000);
222:                MBeanInfo info = null;
223:                try {
224:                    info = server.getMBeanInfo(mbeanObjectName);
225:                } catch (Exception e) {
226:                    echo("\t!!! Could not get MBeanInfo object for "
227:                            + mbeanName + " !!!");
228:                    e.printStackTrace();
229:                    return;
230:                }
231:                echo("\nCLASSNAME: \t" + info.getClassName());
232:                echo("\nDESCRIPTION: \t" + info.getDescription());
233:                echo("\nATTRIBUTES");
234:                MBeanAttributeInfo[] attrInfo = info.getAttributes();
235:                if (attrInfo.length > 0) {
236:                    for (int i = 0; i < attrInfo.length; i++) {
237:                        echo(" ** NAME: \t" + attrInfo[i].getName());
238:                        echo("    DESCR: \t" + attrInfo[i].getDescription());
239:                        echo("    TYPE: \t" + attrInfo[i].getType()
240:                                + "\tREAD: " + attrInfo[i].isReadable()
241:                                + "\tWRITE: " + attrInfo[i].isWritable());
242:                    }
243:                } else
244:                    echo(" ** No attributes **");
245:                echo("\nCONSTRUCTORS");
246:                MBeanConstructorInfo[] constrInfo = info.getConstructors();
247:                for (int i = 0; i < constrInfo.length; i++) {
248:                    echo(" ** NAME: \t" + constrInfo[i].getName());
249:                    echo("    DESCR: \t" + constrInfo[i].getDescription());
250:                    echo("    PARAM: \t" + constrInfo[i].getSignature().length
251:                            + " parameter(s)");
252:                }
253:                echo("\nOPERATIONS");
254:                MBeanOperationInfo[] opInfo = info.getOperations();
255:                if (opInfo.length > 0) {
256:                    for (int i = 0; i < opInfo.length; i++) {
257:                        echo(" ** NAME: \t" + opInfo[i].getName());
258:                        echo("    DESCR: \t" + opInfo[i].getDescription());
259:                        echo("    PARAM: \t" + opInfo[i].getSignature().length
260:                                + " parameter(s)");
261:                    }
262:                } else
263:                    echo(" ** No operations ** ");
264:                echo("\nNOTIFICATIONS");
265:                MBeanNotificationInfo[] notifInfo = info.getNotifications();
266:                if (notifInfo.length > 0) {
267:                    for (int i = 0; i < notifInfo.length; i++) {
268:                        echo(" ** NAME: \t" + notifInfo[i].getName());
269:                        echo("    DESCR: \t" + notifInfo[i].getDescription());
270:                    }
271:                } else
272:                    echo(" ** No notifications **");
273:            }
274:
275:            private void printSimpleAttributes(ObjectName mbeanObjectName) {
276:
277:                try {
278:                    echo("\n    Getting attribute values:");
279:                    String State = (String) server.getAttribute(
280:                            mbeanObjectName, "State");
281:                    Integer NbChanges = (Integer) server.getAttribute(
282:                            mbeanObjectName, "NbChanges");
283:                    echo("\tState     = \"" + State + "\"");
284:                    echo("\tNbChanges = " + NbChanges);
285:                } catch (Exception e) {
286:                    echo("\t!!! Could not read attributes !!!");
287:                    e.printStackTrace();
288:                    return;
289:                }
290:            }
291:
292:            private static void echo(String msg) {
293:                System.out.println(msg);
294:            }
295:
296:            private static void sleep(int millis) {
297:                try {
298:                    Thread.sleep(millis);
299:                } catch (InterruptedException e) {
300:                    return;
301:                }
302:            }
303:
304:            private static void waitForEnterPressed() {
305:                try {
306:                    System.in.read();
307:                } catch (IOException e) {
308:                    e.printStackTrace();
309:                }
310:            }
311:
312:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.