01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.examples.tools.config;
10:
11: import java.io.BufferedReader;
12: import java.io.FileReader;
13: import java.io.Reader;
14: import javax.management.MBeanServer;
15: import javax.management.MBeanServerFactory;
16: import javax.management.ObjectName;
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: public static void main(String[] args) throws Exception {
40: // The MBeanServer
41: MBeanServer server = MBeanServerFactory.newMBeanServer();
42:
43: // The configuration loader
44:
45: /* Choice 1: as an external object */
46: // ConfigurationLoader loader = new ConfigurationLoader(server);
47: /* Choice 2: as a created MBean */
48: // server.createMBean(ConfigurationLoader.class.getName(), ObjectName.getInstance("config:service=loader"), null);
49: /* Choice 3: as a registered MBean */
50: ConfigurationLoader loader = new ConfigurationLoader();
51: server.registerMBean(loader, ObjectName
52: .getInstance("config:service=loader"));
53:
54: // The XML file
55:
56: /* Choice 1: read it from classpath using classloaders
57: Note: the directory that contains the XML file must be in the classpath */
58: // InputStream stream = ConfigurationStartup.class.getClassLoader().getResourceAsStream("config.xml");
59: // Reader reader = new BufferedReader(new InputStreamReader(stream));
60: /* Choice 2: read it from a file
61: Note: requires file path to be passed as program argument */
62: String path = args[0];
63: Reader reader = new BufferedReader(new FileReader(path));
64:
65: // Read and execute the 'startup' section of the XML file
66: loader.startup(reader);
67:
68: reader.close();
69:
70: System.out.println("Application configured successfully");
71: }
72: }
|