01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.com
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.proxy;
09:
10: import java.lang.reflect.Proxy;
11: import javax.management.MBeanServer;
12: import javax.management.ObjectName;
13: import javax.management.InstanceAlreadyExistsException;
14: import javax.management.MBeanRegistrationException;
15: import javax.management.NotCompliantMBeanException;
16: import javax.management.MBeanServerFactory;
17:
18: import example.jmx.standard.SimpleStandard;
19: import example.jmx.standard.SimpleStandardMBean;
20:
21: /**
22: * test whether the MBean obj
23: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
24: */
25:
26: public class MBeanProxy {
27:
28: /**
29: * Test whether the MBean is registered in the MBeanServer, if not, register it.
30: * then , return the MBean Proxy Interface.
31: * if the MBean has registered, the method equals getMBeanProxy(MBeanServer server, Object mbeanObj, ObjectName name)
32: * @param server
33: * @param name
34: * @param mbeanObj
35: * @return
36: * @throws InstanceAlreadyExistsException
37: * @throws MBeanRegistrationException
38: * @throws NotCompliantMBeanException
39: */
40: public static Object createMBean(MBeanServer server,
41: ObjectName name, Object mbeanObj)
42: throws InstanceAlreadyExistsException,
43: MBeanRegistrationException, NotCompliantMBeanException {
44: if (!server.isRegistered(name)) { // MBean not registered yet , register it
45: server.registerMBean(mbeanObj, name);
46: }
47: return getMBeanProxy(server, mbeanObj, name);
48: }
49:
50: /**
51: * get the MBean Object's Proxy Object, use the MBeanServer and the ObjectName
52: * as InvocationHandler's parameter
53: * @param server
54: * @param mbeanObj the MBean Object
55: * @param name
56: * @return MBean's interface Proxy Object
57: */
58: public static Object getMBeanProxy(MBeanServer server,
59: Object mbeanObj, ObjectName name) {
60: return getMBeanProxy(server, mbeanObj.getClass(), name);
61: }
62:
63: /**
64: * get the MBean Object's Proxy Object, use the MBeanServer and the ObjectName
65: * as InvocationHandler's parameter
66: * @param server
67: * @param mbeanCls MBean Object's class name
68: * @param name
69: * @return MBean's interface Proxy Object
70: */
71: public static Object getMBeanProxy(MBeanServer server,
72: Class mbeanCls, ObjectName name) {
73: return Proxy.newProxyInstance(mbeanCls.getClassLoader(),
74: mbeanCls.getInterfaces(), new MBeanInvocationHandler(
75: server, name));
76: }
77:
78: public static void main(String[] args) throws Exception {
79: MBeanServer server = MBeanServerFactory.createMBeanServer();
80: ObjectName name = new ObjectName(":name=simple");
81: SimpleStandard simple = new SimpleStandard();
82: SimpleStandardMBean mbean = (SimpleStandardMBean) MBeanProxy
83: .createMBean(server, name, simple);
84: System.out.println(mbean.getState());
85:
86: main2();
87: }
88:
89: // createMBean the getMBeanProxy
90: public static void main2() throws Exception {
91: MBeanServer server = MBeanServerFactory.createMBeanServer();
92: ObjectName name = new ObjectName(":name=simple2");
93: server.createMBean("example.jmx.standard.SimpleStandard", name);
94: SimpleStandardMBean simple = (SimpleStandardMBean) MBeanProxy
95: .getMBeanProxy(server, SimpleStandard.class, name);
96: System.out.println(simple.getState());
97: }
98: }
|