/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.proxy;

import java.lang.reflect.Proxy;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import javax.management.MBeanServerFactory;

import example.jmx.standard.SimpleStandard;
import example.jmx.standard.SimpleStandardMBean;

/**
 * test whether the MBean obj
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class MBeanProxy {

  /**
   * Test whether the MBean is registered in the MBeanServer, if not, register it.
   * then , return the MBean Proxy Interface.
   * if the MBean has registered, the method equals getMBeanProxy(MBeanServer server, Object mbeanObj, ObjectName name)
   * @param server
   * @param name
   * @param mbeanObj
   * @return
   * @throws InstanceAlreadyExistsException
   * @throws MBeanRegistrationException
   * @throws NotCompliantMBeanException
   */
  public static Object createMBean(MBeanServer server, ObjectName name, Object mbeanObj)
         throws InstanceAlreadyExistsException,
         MBeanRegistrationException,
         NotCompliantMBeanException {
    if(!server.isRegistered(name)) { // MBean not registered yet , register it
      server.registerMBean(mbeanObj,name);
    }
    return getMBeanProxy(server,mbeanObj,name);
  }

  /**
   * get the MBean Object's Proxy Object, use the MBeanServer and the ObjectName
   * as InvocationHandler's parameter
   * @param server
   * @param mbeanObj the MBean Object
   * @param name
   * @return MBean's interface Proxy Object
   */
  public static Object getMBeanProxy(MBeanServer server, Object mbeanObj, ObjectName name){
    return getMBeanProxy(server,mbeanObj.getClass(),name);
  }

  /**
   * get the MBean Object's Proxy Object, use the MBeanServer and the ObjectName
   * as InvocationHandler's parameter
   * @param server
   * @param mbeanCls MBean Object's class name
   * @param name
   * @return MBean's interface Proxy Object
   */
  public static Object getMBeanProxy(MBeanServer server, Class mbeanCls, ObjectName name){
    return Proxy.newProxyInstance(mbeanCls.getClassLoader(),
                                  mbeanCls.getInterfaces(),
                                  new MBeanInvocationHandler(server,name)
                                  );
  }

  public static void main(String[] args) throws Exception {
		MBeanServer server = MBeanServerFactory.createMBeanServer();
    ObjectName name  = new ObjectName(":name=simple");
    SimpleStandard simple = new SimpleStandard();
    SimpleStandardMBean mbean = (SimpleStandardMBean)MBeanProxy.createMBean(server,name,simple);
    System.out.println(mbean.getState());

    main2();
 	}
  // createMBean the getMBeanProxy
  public static void main2() throws Exception {
    MBeanServer server = MBeanServerFactory.createMBeanServer();
    ObjectName name  = new ObjectName(":name=simple2");
    server.createMBean("example.jmx.standard.SimpleStandard",name);
    SimpleStandardMBean simple = (SimpleStandardMBean)MBeanProxy.getMBeanProxy(server,SimpleStandard.class,name);
    System.out.println(simple.getState());
  }
}
