KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > persister > PersisterTest


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 package mx4j.examples.tools.persister;
9
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.loading.MLet JavaDoc;
15
16
17 /**
18  * Our agent which instantiates 2 MLets to load the MBeans contained in their separate jars
19  *
20  * @version $Revision: 1.3 $
21  */

22 public class PersisterTest
23 {
24    private MBeanServer JavaDoc m_server = null;
25
26    public PersisterTest()
27    {
28
29    }
30
31    public void endExample()
32    {
33       System.out.println("----- example completed -----");
34       MBeanServerFactory.releaseMBeanServer(m_server);
35       System.exit(0);
36    }
37
38    public void doDemo(String JavaDoc[] args)
39    {
40       String JavaDoc jarPath1 = args[0];
41       String JavaDoc jarPath2 = args[1];
42       String JavaDoc storePath = args[2];
43       String JavaDoc filename = args[3];
44
45       m_server = MBeanServerFactory.createMBeanServer("test");
46       try
47       {
48          // register the mlet used to load the MBeans MLet one
49
ObjectName JavaDoc mName1 = new ObjectName JavaDoc("loading:test=mlet1");
50          MLet JavaDoc mlet1 = new MLet JavaDoc();
51          m_server.registerMBean(mlet1, mName1);
52          mlet1.addURL(jarPath1);
53 // mlet1.addURL(new File("one.jar").toURL());
54

55          ObjectName JavaDoc mName2 = new ObjectName JavaDoc("loading:test=mlet2");
56          MLet JavaDoc mlet2 = new MLet JavaDoc();
57          m_server.registerMBean(mlet2, mName2);
58          mlet2.addURL(jarPath2);
59 // mlet2.addURL(new File("two.jar").toURL());
60

61          String JavaDoc mbeanClass1 = "mx4j.examples.tools.persister.MBeanOne";
62          ObjectName JavaDoc mbeanName1 = new ObjectName JavaDoc("test:name=MBeanOne");
63          m_server.createMBean(mbeanClass1, mbeanName1, mName1,
64                               new Object JavaDoc[]{storePath, filename}, new String JavaDoc[]{"java.lang.String", "java.lang.String"});
65
66          String JavaDoc mbeanClass2 = "mx4j.examples.tools.persister.MBeanTwo";
67          ObjectName JavaDoc mbeanName2 = new ObjectName JavaDoc("test:name=MBeanTwo");
68          m_server.createMBean(mbeanClass2, mbeanName2, mName2, new Object JavaDoc[]{new Integer JavaDoc(15)},
69                               new String JavaDoc[]{"java.lang.Integer"});
70
71          m_server.invoke(mbeanName2, "storeIt", new Object JavaDoc[]{m_server, mbeanName1},
72                          new String JavaDoc[]{"javax.management.MBeanServer", "javax.management.ObjectName"});
73
74          Object JavaDoc a = m_server.invoke(mbeanName2, "loadIt", new Object JavaDoc[]{m_server, mbeanName1},
75                                     new String JavaDoc[]{"javax.management.MBeanServer", "javax.management.ObjectName"});
76
77          if (a.getClass().getName() == mbeanClass2) System.out.println("Objects are equal and the same");
78       }
79       catch (Exception JavaDoc ex)
80       {
81          ex.printStackTrace();
82       }
83       finally
84       {
85          endExample();
86       }
87    }
88
89    public static void usage()
90    {
91       System.out.println("Four arguments are needed to run this example:");
92       System.out.println("arg[0] = <path to jar containing MBeanOne> eg: file:C:/dev/one.jar");
93       System.out.println("arg[1] = <path to jar containing MBeanTwo> eg: file:C:/dev/two.jar");
94       System.out.println("arg[2] = <path store file> eg: C:/dev");
95       System.out.println("arg[3] = <name of file> eg: myMBean.ser");
96
97       System.out.println("Program is exiting.......");
98       System.exit(1);
99    }
100
101    public static void main(String JavaDoc[] args)
102    {
103       PersisterTest test = new PersisterTest();
104       if (args.length < 4)
105       {
106          usage();
107       }
108
109       test.doDemo(args);
110    }
111 }
112
Popular Tags