package zx.common.workassistant;

import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JmxClientGs {

	static String hostName = "10.0.30.133";
	static int port = 10002;
	static String userName = "cyydtest";
	static String password = "test";
	
	public int connect(){
		try { 
			Map<String, String[]> prop = new HashMap<String, String[]>();
			String[] credentials = new String[] {userName, password};
			prop.put(JMXConnector.CREDENTIALS, credentials);
			
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/jmxrmi");
			final JMXConnector conn = JMXConnectorFactory.connect(url, prop); 
			if(conn == null){
				System.out.println("connect to jmx failed, url=" + url);
				return -1;
			}
			
			System.out.println("JXMConnector=" + conn.toString());
			
			Object[] params = new Object[2];
			String[] signatures = new String[2];
			params[0] = -1;
			signatures[0] = int.class.getName();
			params[1] = 0;
			signatures[1] = int.class.getName();
			
			MBeanServerConnection mbsc = conn.getMBeanServerConnection();
			Object ret = mbsc.invoke(new ObjectName("com:type=gwebtop,name=com.gwebtop.utils.SystemMXBeanImp"), "getSeasonStatus", params, signatures);
	        
			System.out.println("res: " + ret.toString());
            conn.close();
            
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		} 
		return 0;
	}
	
	
	public int connect1(){
		try { 
			Map<String, String[]> prop = new HashMap<String, String[]>();
			String[] credentials = new String[] {userName, password};
			prop.put(JMXConnector.CREDENTIALS, credentials);
			
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/jmxrmi");
			final JMXConnector conn = JMXConnectorFactory.connect(url, prop); 
			if(conn == null){
				return -1;
			}
			
			
			Object[] params = new Object[0];
			String[] signatures = new String[0];
			
			MBeanServerConnection mbsc = conn.getMBeanServerConnection();
			Object ret = mbsc.invoke(new ObjectName("com:type=gwebtop,name=com.gwebtop.utils.SystemMXBeanImp"), "reparseConfigData", params, signatures);
	        
			conn.close();
            
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		} 
		return 0;
	}
	
	public int connect2(){
		try { 
			Map<String, String[]> prop = new HashMap<String, String[]>();
			String[] credentials = new String[] {userName, password};
			prop.put(JMXConnector.CREDENTIALS, credentials);
			
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/jmxrmi");
			final JMXConnector conn = JMXConnectorFactory.connect(url, prop); 
			if(conn == null){
				return -1;
			}
			
			
			Object[] params = new Object[0];
			String[] signatures = new String[0];
			
			MBeanServerConnection mbsc = conn.getMBeanServerConnection();
			Object ret = mbsc.invoke(new ObjectName("com:type=gwebtop,name=com.gwebtop.CrossMXBeanImp"), "reparse", params, signatures);
	        
            conn.close();
            
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		} 
		return 0;
	}
	
	
	public int connect3(){
		try { 
			Map<String, String[]> prop = new HashMap<String, String[]>();
			String[] credentials = new String[] {userName, password};
			prop.put(JMXConnector.CREDENTIALS, credentials);
			
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/jmxrmi");
			final JMXConnector conn = JMXConnectorFactory.connect(url, prop); 
			if(conn == null){
				return -1;
			}
			
			
			Object[] params = new Object[0];
			String[] signatures = new String[0];
			
			MBeanServerConnection mbsc = conn.getMBeanServerConnection();
			Object ret = mbsc.invoke(new ObjectName("com:type=gwebtop,name=com.gwebtop.CenterMXBeanImp"), "reparse", params, signatures);
	        
            conn.close();
            
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		} 
		return 0;
	}
	
}
