KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > services > loading > Main


1 /**
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.services.loading;
10
11 import java.io.File JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.ObjectInstance JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.ReflectionException JavaDoc;
21 import javax.management.ServiceNotFoundException JavaDoc;
22 import javax.management.loading.MLet JavaDoc;
23
24 /**
25  * The starter class for loading MBeans via an MLET file. <br>
26  * Modify at your wish.
27  *
28  * @version $Revision: 1.3 $
29  */

30 public class Main
31 {
32    public static void main(String JavaDoc[] args) throws Exception JavaDoc
33    {
34       // Create the MBeanServer
35
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
36
37       // Register the MLet in the MBeanServer
38
MLet JavaDoc mlet = new MLet JavaDoc();
39       ObjectName JavaDoc mletName = new ObjectName JavaDoc("system:mbean=loader");
40       server.registerMBean(mlet, mletName);
41
42       // Set the MLet as context classloader
43
// Can be useful for the loaded services that want to access this classloader.
44
Thread.currentThread().setContextClassLoader(mlet);
45
46       // Resolve the file to load MBeans from
47
// If we got a program argument, we load it from there, otherwise
48
// we assume we have a 'mbeans.mlet' file in this example's directory
49
URL JavaDoc mbeansURL = null;
50       if (args.length == 1)
51       {
52          String JavaDoc file = args[0];
53          mbeansURL = new File JavaDoc(file).toURL();
54       }
55       else
56       {
57          mbeansURL = mlet.getResource("examples/services/loading/mbeans.mlet");
58       }
59
60       // If the URL is still null, abort
61
if (mbeansURL == null) throw new ServiceNotFoundException JavaDoc("Could not find MBeans to load");
62
63       // Load the MBeans
64
Set JavaDoc mbeans = mlet.getMBeansFromURL(mbeansURL);
65
66       System.out.println("MLet has now the following classpath: " + Arrays.asList(mlet.getURLs()));
67
68       // Now let's check everything is ok.
69
checkMBeansLoadedSuccessfully(mbeans);
70
71       // Now the system is loaded, but maybe we should initialize and start them
72
initializeMBeans(server, mbeans);
73       startMBeans(server, mbeans);
74
75       // Now the system is up and running
76
System.out.println("System up and running !");
77
78       // The program exits because none of the loaded MBeans in this example started a non-daemon thread.
79
}
80
81    private static void checkMBeansLoadedSuccessfully(Set JavaDoc mbeans) throws ServiceNotFoundException JavaDoc
82    {
83       // MLet.getMBeansFromURL returns a Set containing exceptions if an MBean could not be loaded
84
boolean allLoaded = true;
85       for (Iterator JavaDoc i = mbeans.iterator(); i.hasNext();)
86       {
87          Object JavaDoc mbean = i.next();
88          if (mbean instanceof Throwable JavaDoc)
89          {
90             ((Throwable JavaDoc)mbean).printStackTrace();
91             allLoaded = false;
92             // And go on with the next
93
}
94          else
95          {
96             // Ok, the MBean was registered successfully
97
System.out.println("Registered MBean: " + mbean);
98          }
99       }
100
101       if (!allLoaded) throw new ServiceNotFoundException JavaDoc("Some MBean could not be loaded");
102    }
103
104    private static void initializeMBeans(MBeanServer JavaDoc server, Set JavaDoc mbeans)
105    {
106       for (Iterator JavaDoc i = mbeans.iterator(); i.hasNext();)
107       {
108          try
109          {
110             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)i.next();
111             if (server.isInstanceOf(instance.getObjectName(), "org.apache.avalon.framework.activity.Initializable"))
112             {
113                try
114                {
115                   server.invoke(instance.getObjectName(), "initialize", null, null);
116                }
117                catch (ReflectionException JavaDoc ignored)
118                {
119                   // The initialize method is not part of the management interface, ignore
120
}
121             }
122          }
123          catch (Exception JavaDoc x)
124          {
125             x.printStackTrace();
126          }
127       }
128    }
129
130    private static void startMBeans(MBeanServer JavaDoc server, Set JavaDoc mbeans)
131    {
132       for (Iterator JavaDoc i = mbeans.iterator(); i.hasNext();)
133       {
134          try
135          {
136             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc)i.next();
137             if (server.isInstanceOf(instance.getObjectName(), "org.apache.avalon.framework.activity.Startable"))
138             {
139                try
140                {
141                   server.invoke(instance.getObjectName(), "start", null, null);
142                }
143                catch (ReflectionException JavaDoc ignored)
144                {
145                   // The start method is not part of the management interface, ignore
146
}
147             }
148          }
149          catch (Exception JavaDoc x)
150          {
151             x.printStackTrace();
152          }
153       }
154    }
155 }
156
Popular Tags