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

//import java.security.PrivilegedAction;
//import java.util.Iterator;
import java.util.Properties;

import javax.management.ObjectName;

import com.eactive.eai.rms.client.service.JMXMonitoring;
//import java.util.Set;
//
//import javax.management.Attribute;
//import javax.management.AttributeList;
//import javax.management.ObjectName;
//import javax.management.Query;
//import javax.management.QueryExp;
//import javax.security.auth.Subject;
//import javax.security.auth.callback.CallbackHandler;
//import javax.security.auth.login.LoginContext;
//
//import com.ibm.websphere.management.AdminClient;
//import com.ibm.websphere.management.AdminClientFactory;
//import com.ibm.websphere.security.auth.WSSubject;
//import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;
//
public class SingleServerMonitoringForWebSphere implements JMXMonitoring {
//	public static AdminClient ac = null;
//	
//	private static Boolean isSubjectLogin ; 
//	private static Properties properties ;

	private static SingleServerMonitoringForWebSphere monitoring;

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

	public void init(Properties properties) throws Exception {
//		if (ac == null) {
//			synchronized(SingleServerMonitoring.class){
//				this.properties = properties;
//				Object obj = properties.get("isSubjectLogin");
//				if (obj == null){
//					this.isSubjectLogin = false;
//				}else {
//					if (obj instanceof String){
//						this.isSubjectLogin = Boolean.parseBoolean((String)obj);
//					}
//				}
//				if (ac== null){
//					ac = AdminClientFactory.createAdminClient(properties);
//				}
//			}
//		}
	}
//	private static Object doAsAdmin(PrivilegedAction action) throws Exception {
//		return WSSubject.doAs(getLoginSubject(), action);
//	}
//
//	private static Subject getLoginSubject() throws Exception {
//		CallbackHandler callbackHandler = new WSCallbackHandlerImpl(properties.getProperty(AdminClient.USERNAME), null,
//				properties.getProperty(AdminClient.PASSWORD));
//		LoginContext loginContext;
//		try {
//			loginContext = new LoginContext("WSLogin", callbackHandler);
//			loginContext.login();
//		} catch (Exception e) {
//			throw new RuntimeException(e.getMessage());
//		}
//		return loginContext.getSubject();
//	}
//	private ObjectName getServerObjectNames(String serverName) throws Exception {
//		final ObjectName objectName = new ObjectName("WebSphere:type=Server,*");
//		final QueryExp queryExp = Query.eq(Query.attr("name"), Query.value(serverName));
//		
//		PrivilegedAction action = new PrivilegedAction(){
//			@Override
//			public Object run() {
//				try {
//					return ac.queryNames(objectName, queryExp);
//				} catch (Exception e) {
//					//e.printStackTrace();
//				}
//				return null;
//			}
//		};
//		
//		Set<ObjectName> objectNameSet = null;
//		if(isSubjectLogin){
//			objectNameSet = (Set) doAsAdmin(action);
//		}else{
//			objectNameSet = (Set) action.run();
//		}
//		
//		if (objectNameSet != null) {
//			for(ObjectName o : objectNameSet){
//				return o;
//			}
//		} 
//		return null;
//	}
//	private int getServerStatus(ObjectName objectName) throws Exception{
//		PrivilegedAction action = new PrivilegedAction(){
//			@Override
//			public Object run() {
//				try {
//					return ac.getAttributes(objectName, new String[] { "state" });
//				} catch (Exception e) {
//					//e.printStackTrace();
//				}
//				return null;
//			}
//		};
//		
//		AttributeList attrList = null;
//		if(isSubjectLogin){
//			attrList = (AttributeList) doAsAdmin(action);
//		}else{
//			attrList = (AttributeList) action.run();
//		}
//		if (attrList == null){
//			return 0;
//		}
//		Iterator it = attrList.iterator();
//		while(it.hasNext()){
//			Attribute a  = (Attribute)it.next();
//			if ("state".equals(a.getName())){
//				if (a.getValue() instanceof String ){
//					if ("STARTED".equals((String)a.getValue())){
//						return 2;
//					}
//				}
//			}		
//		}
//		return 0;
//	}
//	
	public Object getState(String serverName) throws Exception {
//		ObjectName objectName = getServerObjectNames(serverName);
//		if (objectName == null){
//			return 0;
//		}
//		try{
//			return getServerStatus(objectName);
//		}catch(Exception e){
//			
//		}
		return 0;
	}

//	public static void main(String[] args) throws Exception{
//		String serverIp = "192.168.68.21"; //개발서버-일괄
//		String port     = "9809";//RMI // 관리콘솔 -> System administration -> Deployment manager -> Ports -> Bootstrap Address
//		String loginId  = "wasadm"; // 관리콘솔 로그인 ID
//		String password = "system01"; // 콴리콘솔 로그인 PASSWORD
//		
//		Properties props = new Properties();
//		props.put(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_RMI);
//		props.put(AdminClient.CONNECTOR_SECURITY_ENABLED, false);
//		props.put(AdminClient.CONNECTOR_HOST, serverIp);
//		props.put(AdminClient.CONNECTOR_PORT, port);
//		props.put(AdminClient.USERNAME, loginId);
//		props.put(AdminClient.PASSWORD, password);
//		props.put("isSubjectLogin", false);
//		
//		SingleServerMonitoring m = new SingleServerMonitoring().getInstance();
//		m.init(props);
//		
//		System.out.println(m.getState("EAIDS02_i01"));
//		
//		SingleServerMonitoring m2 = new SingleServerMonitoring().getInstance();
//		m2.init(props);
//		System.out.println(m.getState("EAIBS01_i01"));
//		
//	}

}
