package com.eactive.eai.common.routing;

import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.util.Logger;

import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;


/** 
 * 1. 기능 : ElinkESBProcess를 호출하는 공통 Class
 * 2. 처리 개요 : 
 * *     - 
 * 3. 주의사항 
 *
 * @author  : DongHoon Lee
 * @version : v 1.0.0
 * @see : 관련 기능을 참조
 * @since   : 
 *  : 
 */
public class ElinkESBProcessProxy
{
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	public static String PROXY_ERROR_CODE = "RECEAICPR001";	
	
	private static ConcurrentHashMap<String, Class> classLocalCache = new ConcurrentHashMap<String, Class>();
	
    /** 
     * 1. 기능 :  ElinkESBProcess의 호출 메소드(Local|Remote 방식)
     * 2. 처리 개요 : 
     *     - 
     * 3. 주의사항 
     *
	 * @param	serviceURI Route하기 위한 Process의 URI
	 * @param	eaiMessage EAIMessage Object
	 * @param	isLocal Local Process 호출인지 Remote Process 호출인지 여부
     * @param	prop Remote Process호출인 경우 (RouteKeys.WLI_URL, RouteKeys.WLI_USER,RouteKeys.WLI_PWD)를 설정 
	 * @return 결과 EAIMessage
     * @exception
     **/
	public static EAIMessage callElinkESBProcess(String serviceURI, EAIMessage eaiMessage, boolean isLocal, Properties prop) throws Exception
	{
		EAIMessage retMsg  = null;
		try {
			if (isLocal){				
				Class cl = classLocalCache.get(serviceURI);
				
				if(cl == null) {
					cl = Class.forName(serviceURI);
					classLocalCache.put(serviceURI, cl);
				}
				Process proc = (Process)cl.newInstance();
	            proc.callService(eaiMessage, prop);
	            
	            retMsg  = proc.clientReturn();
			}else{
				if(eaiMessage == null) {
					throw new Exception("EAIMessage is null");
				}
				else {
					retMsg = RemoteProxyClient.callProxyBean(eaiMessage, prop);
				}
			}
		} catch( ClassNotFoundException e) {            
			if (logger.isError()) logger.error("ElinkProcessProxy] callElinkESBProcess["+serviceURI+"] Class Not Found Exception : ");
			throw new Exception(PROXY_ERROR_CODE);			
		} catch( Exception e) {            
			if (logger.isError()) logger.error("ElinkProcessProxy] callElinkESBProcess["+serviceURI+"] Exception : "+e.getMessage(),e);
			throw new Exception(PROXY_ERROR_CODE);			
		}
		return retMsg;
	}
}
