KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > config > ConfigurationStartup


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.tools.config;
10
11 import java.io.BufferedReader JavaDoc;
12 import java.io.FileReader JavaDoc;
13 import java.io.Reader JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.MBeanServerFactory JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17
18 import mx4j.tools.config.ConfigurationLoader;
19
20 /**
21  * This example shows how to use the XML configuration files to load MBeans into
22  * an MBeanServer. <br />
23  * The main class is {@link ConfigurationLoader}, that is able to read the XML
24  * configuration format defined by the MX4J project (see the online documentation
25  * for details on the format).
26  * A <code>ConfigurationLoader</code> is an MBean itself, and loads information
27  * from one XML file into one MBeanServer. <br />
28  * This example runs by specifying the path of an XML configuration file as a
29  * program argument, such as
30  * <pre>
31  * java -classpath ... mx4j.examples.tools.config.ConfigurationStartup ./config.xml
32  * </pre>
33  * Refer to the documentation about the ConfigurationLoader for further information.
34  *
35  * @version $Revision: 1.1 $
36  * @see ConfigurationShutdown
37  */

38 public class ConfigurationStartup
39 {
40    public static void main(String JavaDoc[] args) throws Exception JavaDoc
41    {
42       // The MBeanServer
43
MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
44
45       // The configuration loader
46

47       /* Choice 1: as an external object */
48       // ConfigurationLoader loader = new ConfigurationLoader(server);
49

50       /* Choice 2: as a created MBean */
51       // server.createMBean(ConfigurationLoader.class.getName(), ObjectName.getInstance("config:service=loader"), null);
52

53       /* Choice 3: as a registered MBean */
54       ConfigurationLoader loader = new ConfigurationLoader();
55       server.registerMBean(loader, ObjectName.getInstance("config:service=loader"));
56
57       // The XML file
58

59       /* Choice 1: read it from classpath using classloaders
60          Note: the directory that contains the XML file must be in the classpath */

61       // InputStream stream = ConfigurationStartup.class.getClassLoader().getResourceAsStream("config.xml");
62
// Reader reader = new BufferedReader(new InputStreamReader(stream));
63

64       /* Choice 2: read it from a file
65          Note: requires file path to be passed as program argument */

66       String JavaDoc path = args[0];
67       Reader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(path));
68
69       // Read and execute the 'startup' section of the XML file
70
loader.startup(reader);
71
72       reader.close();
73
74       System.out.println("Application configured successfully");
75    }
76 }
77
Popular Tags