package com.eactive.eai.adapter.http;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.common.testcall.TestCallException;
import com.eactive.eai.common.testcall.TestCallSupport;
import com.eactive.eai.common.util.Logger;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

//HttpQuicsClientAdapter의 통신상태를 체크하는 클래스
public class HttpExtOrgClientAdapterTestCall extends TestCallSupport
{ 

    static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
    
    public boolean isAlive() throws TestCallException
    {
    	logger.debug("HttpExtOrgClientAdapterTestCall started");
        
        try {
	        AdapterVO vo = this.getAdapterInfo();
	        String pgoName = vo.getPropGroupName();
	        //PropManager manager = PropManager.getInstance();
	        AdapterPropManager manager = AdapterPropManager.getInstance();
	        Properties prop = manager.getProperties(pgoName);
	        
	        
	        String url = prop.getProperty("URL");
        	//String url = "101.88.50.120:8285";
	        logger.debug("HttpExtOrgClientAdapterTestCall URL]["+url+"s]");
        
	        URL u = null;
			try {
				logger.debug("HttpExtOrgClientAdapterTestCall] url - ["+url+"]");
				u = new URL(url);
			} catch (MalformedURLException e) {
				logger.warn("HttpExtOrgClientAdapterTestCall] Error at new URL - "+e.getClass().getName()+" : "+e.getMessage());
				return false;
			}

			HttpURLConnection conn = null;

			try {
				conn = (HttpURLConnection) u.openConnection();
			} catch (Exception e) {
				logger.warn("HttpExtOrgClientAdapterTestCall] Error at openConnection - "+e.getClass().getName()+" : "+e.getMessage());
				return false;
	        }

			if(conn == null){
				logger.warn("HttpExtOrgClientAdapterTestCall] Error at HttpURLConnection is null ");
	            return false;
	        }
			logger.debug("HttpExtOrgClientAdapterTestCall ] try HttpURLConnection connect");
			conn.connect();
			logger.debug("HttpExtOrgClientAdapterTestCall end");
	        return true;

        } catch(Exception e) {
            //e.printStackTrace();
            logger.warn("HttpExtOrgClientAdapterTestCall] Error - "+e.getClass().getName()+" : "+e.getMessage());
            return false;      
        }
    }
    
//	   public static void main(String args[]) throws Exception {
//	   test();
//}

//    private static void test() throws Exception {
//    	HttpExtOrgClientAdapterTestCall test = new HttpExtOrgClientAdapterTestCall();
//    	System.out.println("HttpExtOrgClientAdapterTestCall ] isAlive - ["+test.isAlive()+"]");
//    }
} 

