package com.eactive.eai.rms.client.service.weblogic;

import java.util.Hashtable;
import java.util.Properties;

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

import com.eactive.eai.rms.client.service.JMXMonitoring;

public class SingleServerMonitoringForWebLogic implements JMXMonitoring {
//	private static MBeanServerConnection connection;
//	private static JMXConnector connector;
	private String hostname ;
	private String portString ;
	private String username ;
	private String password ;
	
	private static SingleServerMonitoringForWebLogic monitoring;

	public static SingleServerMonitoringForWebLogic getInstance(){
        if(monitoring == null) {
            synchronized(SingleServerMonitoringForWebLogic.class) {
                if(monitoring == null) {
                	monitoring = new SingleServerMonitoringForWebLogic();   
                }   
            }
        }
        return monitoring;
	}

	@Override
	public void init(Properties properties) throws Exception {
		hostname   = properties.getProperty("LOCAL_MBEAN_SERVER_IP");
		portString = properties.getProperty("LOCAL_MBEAN_SERVER_PORT");
		username   = properties.getProperty("LOCAL_MBEAN_SERVER_ID");
		password   = properties.getProperty("LOCAL_MBEAN_SERVER_PASSWORD");
	}

	@Override
	public Object getState(String instName) throws Exception {
		JMXConnector connector = null;
		MBeanServerConnection connection =null;
		
    	try {
    		String protocol ="t3";
    		Integer portInteger = Integer.valueOf(portString);
    		int port = portInteger.intValue();
    		String jndiroot = "/jndi/";
    		String mserver = "weblogic.management.mbeanservers.domainruntime";
    		JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
    		Hashtable h = new Hashtable();
    		h.put(Context.SECURITY_PRINCIPAL, username);
    		h.put(Context.SECURITY_CREDENTIALS, password);
    		h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
    		connector = JMXConnectorFactory.connect(serviceURL,h);
    		connection = connector.getMBeanServerConnection();
    		
    		
			ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
	    	ObjectName[] servers = (ObjectName[])connection.getAttribute(service, "ServerRuntimes");
	    	for(ObjectName server : servers){
	    		String name = server.getKeyProperty("Name");
	    		if (name == null) continue;
	    		if (name.equals(instName)){
	    			return 2;
	    		}
	    	}
		}finally{
			if (connector != null){try{connector.close();}catch(Exception e){}}
		}
		return 0;
	}
	public static void main(String[] args)throws Exception{
		SingleServerMonitoringForWebLogic monitoring = new SingleServerMonitoringForWebLogic();
		Properties properties = new Properties();
		properties.put("LOCAL_MBEAN_SERVER_IP", "172.16.131.21");
		properties.put("LOCAL_MBEAN_SERVER_PORT", "30100");
		properties.put("LOCAL_MBEAN_SERVER_ID", "epostadm");
		properties.put("LOCAL_MBEAN_SERVER_PASSWORD", "welcome1!");
		
		monitoring.init(properties);
		// System.out.println(monitoring.getState("mcuSvr11"));
	}


}
