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: }
|