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: */
008: package mx4j.examples.tools.persister;
009:
010: import javax.management.MBeanServer;
011: import javax.management.MBeanServerFactory;
012: import javax.management.ObjectName;
013: import javax.management.loading.MLet;
014:
015: /**
016: * Our agent which instantiates 2 MLets to load the MBeans contained in their separate jars
017: *
018: * @version $Revision: 1.1 $
019: */
020: public class PersisterTest {
021: private MBeanServer m_server = null;
022:
023: public PersisterTest() {
024:
025: }
026:
027: public void endExample() {
028: System.out.println("----- example completed -----");
029: MBeanServerFactory.releaseMBeanServer(m_server);
030: System.exit(0);
031: }
032:
033: public void doDemo(String[] args) {
034: String jarPath1 = args[0];
035: String jarPath2 = args[1];
036: String storePath = args[2];
037: String filename = args[3];
038:
039: m_server = MBeanServerFactory.createMBeanServer("test");
040: try {
041: // register the mlet used to load the MBeans MLet one
042: ObjectName mName1 = new ObjectName("loading:test=mlet1");
043: MLet mlet1 = new MLet();
044: m_server.registerMBean(mlet1, mName1);
045: mlet1.addURL(jarPath1);
046: // mlet1.addURL(new File("one.jar").toURL());
047:
048: ObjectName mName2 = new ObjectName("loading:test=mlet2");
049: MLet mlet2 = new MLet();
050: m_server.registerMBean(mlet2, mName2);
051: mlet2.addURL(jarPath2);
052: // mlet2.addURL(new File("two.jar").toURL());
053:
054: String mbeanClass1 = "mx4j.examples.tools.persister.MBeanOne";
055: ObjectName mbeanName1 = new ObjectName("test:name=MBeanOne");
056: m_server.createMBean(mbeanClass1, mbeanName1, mName1,
057: new Object[] { storePath, filename }, new String[] {
058: "java.lang.String", "java.lang.String" });
059:
060: String mbeanClass2 = "mx4j.examples.tools.persister.MBeanTwo";
061: ObjectName mbeanName2 = new ObjectName("test:name=MBeanTwo");
062: m_server.createMBean(mbeanClass2, mbeanName2, mName2,
063: new Object[] { new Integer(15) },
064: new String[] { "java.lang.Integer" });
065:
066: m_server.invoke(mbeanName2, "storeIt", new Object[] {
067: m_server, mbeanName1 }, new String[] {
068: "javax.management.MBeanServer",
069: "javax.management.ObjectName" });
070:
071: Object a = m_server.invoke(mbeanName2, "loadIt",
072: new Object[] { m_server, mbeanName1 },
073: new String[] { "javax.management.MBeanServer",
074: "javax.management.ObjectName" });
075:
076: if (a.getClass().getName() == mbeanClass2)
077: System.out.println("Objects are equal and the same");
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: } finally {
081: endExample();
082: }
083: }
084:
085: public static void usage() {
086: System.out
087: .println("Four arguments are needed to run this example:");
088: System.out
089: .println("arg[0] = <path to jar containing MBeanOne> eg: file:C:/dev/one.jar");
090: System.out
091: .println("arg[1] = <path to jar containing MBeanTwo> eg: file:C:/dev/two.jar");
092: System.out.println("arg[2] = <path store file> eg: C:/dev");
093: System.out.println("arg[3] = <name of file> eg: myMBean.ser");
094:
095: System.out.println("Program is exiting.......");
096: System.exit(1);
097: }
098:
099: public static void main(String[] args) {
100: PersisterTest test = new PersisterTest();
101: if (args.length < 4) {
102: usage();
103: }
104:
105: test.doDemo(args);
106: }
107: }
|