Source Code Cross Referenced for Main.java in  » J2EE » enhydra » mx4j » examples » services » loading » 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 » J2EE » enhydra » mx4j.examples.services.loading 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */package mx4j.examples.services.loading;
008:
009:        import java.io.File;
010:        import java.net.URL;
011:        import java.util.Arrays;
012:        import java.util.Iterator;
013:        import java.util.Set;
014:        import javax.management.MBeanServer;
015:        import javax.management.MBeanServerFactory;
016:        import javax.management.ObjectInstance;
017:        import javax.management.ObjectName;
018:        import javax.management.ReflectionException;
019:        import javax.management.ServiceNotFoundException;
020:        import javax.management.loading.MLet;
021:
022:        /**
023:         * The starter class for loading MBeans via an MLET file. <br>
024:         * Modify at your wish.
025:         *
026:         * @version $Revision: 1.1 $
027:         */
028:        public class Main {
029:            public static void main(String[] args) throws Exception {
030:                // Create the MBeanServer
031:                MBeanServer server = MBeanServerFactory.createMBeanServer();
032:
033:                // Register the MLet in the MBeanServer
034:                MLet mlet = new MLet();
035:                ObjectName mletName = new ObjectName("system:mbean=loader");
036:                server.registerMBean(mlet, mletName);
037:
038:                // Set the MLet as context classloader
039:                // Can be useful for the loaded services that want to access this classloader.
040:                Thread.currentThread().setContextClassLoader(mlet);
041:
042:                // Resolve the file to load MBeans from
043:                // If we got a program argument, we load it from there, otherwise
044:                // we assume we have a 'mbeans.mlet' file in this example's directory
045:                URL mbeansURL = null;
046:                if (args.length == 1) {
047:                    String file = args[0];
048:                    mbeansURL = new File(file).toURL();
049:                } else {
050:                    mbeansURL = mlet
051:                            .getResource("examples/services/loading/mbeans.mlet");
052:                }
053:
054:                // If the URL is still null, abort
055:                if (mbeansURL == null)
056:                    throw new ServiceNotFoundException(
057:                            "Could not find MBeans to load");
058:
059:                // Load the MBeans
060:                Set mbeans = mlet.getMBeansFromURL(mbeansURL);
061:
062:                System.out.println("MLet has now the following classpath: "
063:                        + Arrays.asList(mlet.getURLs()));
064:
065:                // Now let's check everything is ok.
066:                checkMBeansLoadedSuccessfully(mbeans);
067:
068:                // Now the system is loaded, but maybe we should initialize and start them
069:                initializeMBeans(server, mbeans);
070:                startMBeans(server, mbeans);
071:
072:                // Now the system is up and running
073:                System.out.println("System up and running !");
074:
075:                // The program exits because none of the loaded MBeans in this example started a non-daemon thread.
076:            }
077:
078:            private static void checkMBeansLoadedSuccessfully(Set mbeans)
079:                    throws ServiceNotFoundException {
080:                // MLet.getMBeansFromURL returns a Set containing exceptions if an MBean could not be loaded
081:                boolean allLoaded = true;
082:                for (Iterator i = mbeans.iterator(); i.hasNext();) {
083:                    Object mbean = i.next();
084:                    if (mbean instanceof  Throwable) {
085:                        ((Throwable) mbean).printStackTrace();
086:                        allLoaded = false;
087:                        // And go on with the next
088:                    } else {
089:                        // Ok, the MBean was registered successfully
090:                        System.out.println("Registered MBean: " + mbean);
091:                    }
092:                }
093:
094:                if (!allLoaded)
095:                    throw new ServiceNotFoundException(
096:                            "Some MBean could not be loaded");
097:            }
098:
099:            private static void initializeMBeans(MBeanServer server, Set mbeans) {
100:                for (Iterator i = mbeans.iterator(); i.hasNext();) {
101:                    try {
102:                        ObjectInstance instance = (ObjectInstance) i.next();
103:                        if (server
104:                                .isInstanceOf(instance.getObjectName(),
105:                                        "org.apache.avalon.framework.activity.Initializable")) {
106:                            try {
107:                                server.invoke(instance.getObjectName(),
108:                                        "initialize", null, null);
109:                            } catch (ReflectionException ignored) {
110:                                // The initialize method is not part of the management interface, ignore
111:                            }
112:                        }
113:                    } catch (Exception x) {
114:                        x.printStackTrace();
115:                    }
116:                }
117:            }
118:
119:            private static void startMBeans(MBeanServer server, Set mbeans) {
120:                for (Iterator i = mbeans.iterator(); i.hasNext();) {
121:                    try {
122:                        ObjectInstance instance = (ObjectInstance) i.next();
123:                        if (server
124:                                .isInstanceOf(instance.getObjectName(),
125:                                        "org.apache.avalon.framework.activity.Startable")) {
126:                            try {
127:                                server.invoke(instance.getObjectName(),
128:                                        "start", null, null);
129:                            } catch (ReflectionException ignored) {
130:                                // The start method is not part of the management interface, ignore
131:                            }
132:                        }
133:                    } catch (Exception x) {
134:                        x.printStackTrace();
135:                    }
136:                }
137:            }
138:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.