Source Code Cross Referenced for Demo.java in  » Library » Apache-commons-modeler-2.0.1-src » org » apache » commons » modeler » demo » 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 » Library » Apache commons modeler 2.0.1 src » org.apache.commons.modeler.demo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.modeler.demo;
019:
020:        import java.io.InputStream;
021:        import java.net.URL;
022:        import java.util.Iterator;
023:
024:        import javax.management.Attribute;
025:        import javax.management.Descriptor;
026:        import javax.management.MBeanAttributeInfo;
027:        import javax.management.MBeanConstructorInfo;
028:        import javax.management.MBeanException;
029:        import javax.management.MBeanNotificationInfo;
030:        import javax.management.MBeanOperationInfo;
031:        import javax.management.MBeanServer;
032:        import javax.management.MBeanServerFactory;
033:        import javax.management.Notification;
034:        import javax.management.NotificationListener;
035:        import javax.management.ObjectInstance;
036:        import javax.management.ObjectName;
037:        import javax.management.modelmbean.ModelMBean;
038:        import javax.management.modelmbean.ModelMBeanInfo;
039:
040:        import org.apache.commons.modeler.ManagedBean;
041:        import org.apache.commons.modeler.Registry;
042:
043:        /**
044:         * <p>Demonstration program for the Modeller package.  Instantiates a set
045:         * of simple managed objects, and a set of corresponding MBeans, then
046:         * manipulates the objects through the MBean interfaces.
047:         *
048:         * @author Craig R. McClanahan
049:         * @version $Revision: 480402 $ $Date: 2006-11-29 04:43:23 +0000 (Wed, 29 Nov 2006) $
050:         */
051:
052:        public class Demo implements  NotificationListener {
053:
054:            // ----------------------------------------------------- Instance Variables
055:
056:            // ------------------------------------------------------- Instance Methods
057:
058:            /**
059:             * Handle the notification of a JMX event.
060:             *
061:             * @param notification The event that has occurred
062:             * @param handback The handback object for this event
063:             */
064:            public void handleNotification(Notification notification,
065:                    Object handback) {
066:
067:                System.out.println("NOTIFICATION=" + notification
068:                        + ", HANDBACK=" + handback);
069:
070:            }
071:
072:            // ------------------------------------------------------- Static Variables
073:
074:            /**
075:             * The attribute notification listener.
076:             */
077:            private static Demo demo = null;
078:
079:            /**
080:             * The configuration information registry for our managed beans.
081:             */
082:            private static Registry registry = null;
083:
084:            /**
085:             * The <code>MBeanServer</code> for this application.
086:             */
087:            private static MBeanServer server = null;
088:
089:            /**
090:             * The managed object tree.
091:             */
092:            private static Server tree = null;
093:
094:            // ----------------------------------------------------------- Main Program
095:
096:            /**
097:             * The main program for this demonstration.
098:             *
099:             * @param args Command line arguments
100:             */
101:            public static void main(String arg[]) {
102:
103:                createRegistry();
104:                createServer();
105:                createTree();
106:                createMBeans();
107:                listMBeans();
108:                dumpServer();
109:                updateServer();
110:
111:            }
112:
113:            // -------------------------------------------------------- Support Methods
114:
115:            /**
116:             * Create the MBeans that correspond to every node of our tree.
117:             */
118:            private static void createMBeans() {
119:
120:                try {
121:
122:                    // NOTE:  JMXRI crashes on server.setAttribute() unless there has
123:                    // been an attribute change listener registered at some point
124:                    // on every ModelMBean. :-(
125:                    // NOTE:  Despite the documentation, you cannot register an
126:                    // attribute change listener for all attributes.  :-(
127:                    Demo demo = new Demo();
128:
129:                    System.out.println("Creating MBeans ...");
130:
131:                    // Create the MBean for the top-level Server object
132:                    String domain = server.getDefaultDomain();
133:                    ManagedBean managed = registry
134:                            .findManagedBean("StandardServer");
135:                    ModelMBean mm = managed.createMBean(tree);
136:                    mm.addAttributeChangeNotificationListener(demo, "shutdown",
137:                            tree);
138:                    mm.addAttributeChangeNotificationListener(demo, "port",
139:                            tree);
140:                    server.registerMBean(mm, createName(domain, tree));
141:
142:                    // Create the MBean for each associated Service and friendes
143:                    Service services[] = tree.findServices();
144:                    for (int i = 0; i < services.length; i++) {
145:
146:                        // The MBean for the Service itself
147:                        managed = registry.findManagedBean("StandardService");
148:                        mm = managed.createMBean(services[i]);
149:                        mm.addAttributeChangeNotificationListener(demo, "name",
150:                                services[i]);
151:                        server.registerMBean(mm,
152:                                createName(domain, services[i]));
153:
154:                        // The MBean for the corresponding Engine
155:                        managed = registry.findManagedBean("StandardEngine");
156:                        Engine container = (Engine) services[i].getContainer();
157:                        mm = managed.createMBean(container);
158:                        mm.addAttributeChangeNotificationListener(demo, "name",
159:                                container);
160:                        server.registerMBean(mm, createName(domain, container));
161:
162:                        // The MBeans for the corresponding Connectors
163:                        managed = registry.findManagedBean("HttpConnector");
164:                        Connector connectors[] = services[i].findConnectors();
165:                        for (int j = 0; j < connectors.length; j++) {
166:                            mm = managed.createMBean(connectors[j]);
167:                            mm.addAttributeChangeNotificationListener(demo,
168:                                    "port", connectors[j]);
169:                            server.registerMBean(mm, createName(domain,
170:                                    connectors[j]));
171:                        }
172:
173:                    }
174:
175:                } catch (MBeanException t) {
176:
177:                    Exception e = t.getTargetException();
178:                    if (e == null)
179:                        e = t;
180:                    System.out.println(e.getMessage());
181:                    e.printStackTrace(System.out);
182:
183:                } catch (Throwable t) {
184:
185:                    System.out.println(t.getMessage());
186:                    t.printStackTrace(System.out);
187:
188:                }
189:
190:            }
191:
192:            /**
193:             * Create an <code>ObjectName</code> for this object.
194:             *
195:             * @param domain Domain in which this name is to be created
196:             * @param connector The Connector to be named
197:             */
198:            private static ObjectName createName(String domain,
199:                    Connector connector) {
200:
201:                ObjectName name = null;
202:                try {
203:                    name = new ObjectName(domain + ":type=Connector,port="
204:                            + connector.getPort() + ",service="
205:                            + connector.getService().getName());
206:                } catch (Throwable t) {
207:                    System.out.println("Creating name for " + connector);
208:                    t.printStackTrace(System.out);
209:                    System.exit(1);
210:                }
211:                return (name);
212:
213:            }
214:
215:            /**
216:             * Create an <code>ObjectName</code> for this object.
217:             *
218:             * @param domain Domain in which this name is to be created
219:             * @param engine The Engine to be named
220:             */
221:            private static ObjectName createName(String domain, Engine engine) {
222:
223:                ObjectName name = null;
224:                try {
225:                    name = new ObjectName(domain + ":type=Engine,service="
226:                            + engine.getService().getName());
227:                } catch (Throwable t) {
228:                    System.out.println("Creating name for " + engine);
229:                    t.printStackTrace(System.out);
230:                    System.exit(1);
231:                }
232:                return (name);
233:
234:            }
235:
236:            /**
237:             * Create an <code>ObjectName</code> for this object.
238:             *
239:             * @param domain Domain in which this name is to be created
240:             * @param server The Server to be named
241:             */
242:            private static ObjectName createName(String domain, Server server) {
243:
244:                ObjectName name = null;
245:                try {
246:                    name = new ObjectName(domain + ":type=Server");
247:                } catch (Throwable t) {
248:                    System.out.println("Creating name for " + server);
249:                    t.printStackTrace(System.out);
250:                    System.exit(1);
251:                }
252:                return (name);
253:
254:            }
255:
256:            /**
257:             * Create an <code>ObjectName</code> for this object.
258:             *
259:             * @param domain Domain in which this name is to be created
260:             * @param service The Service to be named
261:             */
262:            private static ObjectName createName(String domain, Service service) {
263:
264:                ObjectName name = null;
265:                try {
266:                    name = new ObjectName(domain + ":type=Service,name="
267:                            + service.getName());
268:                } catch (Throwable t) {
269:                    System.out.println("Creating name for " + server);
270:                    t.printStackTrace(System.out);
271:                    System.exit(1);
272:                }
273:                return (name);
274:
275:            }
276:
277:            /**
278:             * Create and configure the registry of managed objects.
279:             */
280:            private static void createRegistry() {
281:
282:                System.out.println("Create configuration registry ...");
283:                try {
284:                    URL url = Demo.class
285:                            .getResource("/org/apache/commons/modeler/demo/mbeans-descriptors.xml");
286:                    InputStream stream = url.openStream();
287:                    Registry.loadRegistry(stream);
288:                    stream.close();
289:                    registry = Registry.getRegistry();
290:                } catch (Throwable t) {
291:                    t.printStackTrace(System.out);
292:                    System.exit(1);
293:                }
294:
295:            }
296:
297:            /**
298:             * Create the <code>MBeanServer</code> with which we will be
299:             * registering our <code>ModelMBean</code> implementations.
300:             */
301:            private static void createServer() {
302:
303:                System.out.println("Creating MBeanServer ...");
304:                try {
305:                    //            System.setProperty("LEVEL_TRACE", "true");
306:                    server = MBeanServerFactory.createMBeanServer();
307:                } catch (Throwable t) {
308:                    t.printStackTrace(System.out);
309:                    System.exit(1);
310:                }
311:
312:            }
313:
314:            /**
315:             * Create the tree of managed objects.
316:             */
317:            private static void createTree() {
318:
319:                System.out.println("Create managed object tree ...");
320:                tree = new Server(8005, "SHUTDOWN");
321:
322:                Service service = new Service("Standard Service", tree);
323:                tree.addService(service);
324:
325:                Engine engine = new Engine("Standard Engine", "localhost",
326:                        service);
327:                service.setContainer(engine);
328:
329:                Connector conn1 = new Connector(8080, "http", false, service,
330:                        engine);
331:                service.addConnector(conn1);
332:                Connector conn2 = new Connector(8443, "https", true, service,
333:                        engine);
334:                service.addConnector(conn2);
335:
336:            }
337:
338:            /**
339:             * Dump known information about the "Server" we are managing.
340:             */
341:            private static void dumpServer() {
342:
343:                try {
344:
345:                    System.out.println("Dump ModelMBeanInfo for Server:");
346:                    ObjectName name = new ObjectName(server.getDefaultDomain()
347:                            + ":type=Server");
348:
349:                    // Return static ModelMBeanInfo information
350:                    ModelMBeanInfo info = (ModelMBeanInfo) server
351:                            .getMBeanInfo(name);
352:                    System.out.println("  className=" + info.getClassName());
353:                    System.out
354:                            .println("  description=" + info.getDescription());
355:                    System.out.println("  mbeanDescriptor="
356:                            + info.getMBeanDescriptor());
357:                    MBeanAttributeInfo attrs[] = info.getAttributes();
358:                    for (int i = 0; i < attrs.length; i++)
359:                        System.out.println("  AttributeInfo=" + attrs[i]);
360:                    MBeanConstructorInfo consts[] = info.getConstructors();
361:                    for (int i = 0; i < consts.length; i++)
362:                        System.out.println("  ConstructorInfo=" + consts[i]);
363:                    Descriptor descs[] = info.getDescriptors(null);
364:                    for (int i = 0; i < descs.length; i++)
365:                        System.out.println("  Descriptor=" + descs[i]);
366:                    MBeanNotificationInfo notifs[] = info.getNotifications();
367:                    for (int i = 0; i < notifs.length; i++)
368:                        System.out.println("  Notification=" + notifs[i]);
369:                    MBeanOperationInfo opers[] = info.getOperations();
370:                    for (int i = 0; i < opers.length; i++)
371:                        System.out.println("  Operation=" + opers[i]);
372:
373:                } catch (MBeanException t) {
374:
375:                    Exception e = t.getTargetException();
376:                    if (e == null)
377:                        e = t;
378:                    System.out.println(e.getMessage());
379:                    e.printStackTrace(System.out);
380:
381:                } catch (Throwable t) {
382:
383:                    System.out.println(t.getMessage());
384:                    t.printStackTrace(System.out);
385:
386:                }
387:
388:            }
389:
390:            /**
391:             * List information about all registered MBeans.
392:             */
393:            private static void listMBeans() {
394:
395:                System.out.println("There are "
396:                        + server.getMBeanCount().intValue()
397:                        + " registered MBeans");
398:                Iterator instances = server.queryMBeans(null, null).iterator();
399:                while (instances.hasNext()) {
400:                    ObjectInstance instance = (ObjectInstance) instances.next();
401:                    System.out.println("  objectName="
402:                            + instance.getObjectName() + ", className="
403:                            + instance.getClassName());
404:                }
405:
406:            }
407:
408:            /**
409:             * Test updating an attribute through the JMX interfaces.
410:             */
411:            private static void updateServer() {
412:
413:                try {
414:
415:                    System.out
416:                            .println("===========================================");
417:
418:                    System.out.println("Test updating Server properties ...");
419:                    ObjectName name = new ObjectName(server.getDefaultDomain()
420:                            + ":type=Server");
421:
422:                    System.out
423:                            .println("  Retrieving current value of 'shutdown'");
424:                    String value = (String) server.getAttribute(name,
425:                            "shutdown");
426:                    if (!"SHUTDOWN".equals(value))
427:                        throw new IllegalStateException(
428:                                "Current shutdown value is '" + value + "'");
429:
430:                    System.out.println("  Setting new value of 'shutdown'");
431:                    server.setAttribute(name, new Attribute("shutdown",
432:                            "NEW VALUE"));
433:
434:                    System.out.println("  Checking new value of 'shutdown'");
435:                    value = (String) server.getAttribute(name, "shutdown");
436:                    if (!"NEW VALUE".equals(value))
437:                        throw new IllegalStateException(
438:                                "New shutdown value is '" + value + "'");
439:
440:                    System.out
441:                            .println("===========================================");
442:
443:                    System.out.println("Test updating Server properties ...");
444:
445:                    System.out.println("  Retrieving current value of 'port'");
446:                    Integer ivalue = (Integer) server
447:                            .getAttribute(name, "port");
448:                    if (ivalue.intValue() != 8005)
449:                        throw new IllegalStateException(
450:                                "Current port value is '" + ivalue + "'");
451:
452:                    System.out.println("  Setting new value of 'port'");
453:                    server.setAttribute(name, new Attribute("port",
454:                            new Integer(8765)));
455:                    /*
456:                    server.invoke(name, "setPort",
457:                                  new Object[] { new java.lang.Integer(8765) },
458:                                  new String[] { "int" });
459:
460:                     */
461:                    System.out.println("  Checking new value of 'port'");
462:                    ivalue = (Integer) server.getAttribute(name, "port");
463:                    if (ivalue.intValue() != 8765)
464:                        throw new IllegalStateException("New port value is '"
465:                                + ivalue + "'");
466:
467:                } catch (MBeanException t) {
468:
469:                    Exception e = t.getTargetException();
470:                    if (e == null)
471:                        e = t;
472:                    System.out.println(e.getMessage());
473:                    e.printStackTrace(System.out);
474:
475:                } catch (Throwable t) {
476:
477:                    System.out.println(t.getMessage());
478:                    t.printStackTrace(System.out);
479:
480:                }
481:
482:            }
483:
484:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.