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