package com.eactive.eai.common.util;

import java.util.Iterator;
import java.util.Properties;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.adapter.Keys;

public final class ServiceUtil {
	private ServiceUtil() {

	}

//   Adapter의 타임아웃값을 추출
	public static int getTimeoutValue(String adapterGroupName) {
		int timeout = -1; // seconds
		try {
			AdapterManager adapterManager = AdapterManager.getInstance();
			AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(adapterGroupName);
			AdapterVO adptVO = null;
            Iterator<AdapterVO> it = adptGrpVO.getAdapters();
	        if(it.hasNext()) adptVO = it.next();
			if (adptVO == null) {
				timeout = -1;
				return timeout;
			}

			AdapterPropManager manager = AdapterPropManager.getInstance();
			Properties adapterProp = manager.getProperties(adptVO.getPropGroupName());

			// Socket Adapter Timeout is Seconds
			if (Keys.TYPE_NET2.equals(adptGrpVO.getType())) {
				timeout = Integer.parseInt(adapterProp.getProperty("timeout"));
			}
			// HTTP Adapter Timeout is Mili-Seconds
			else if (Keys.TYPE_HTTP.equals(adptGrpVO.getType())) {
				timeout = Integer.parseInt((String) adapterProp.getProperty("HTTP_TIME_OUT"));
				timeout = timeout / 1000; // ms -> src
			}
			// Other Adapter set Default Timeout
		} catch (Exception e) {
			timeout = -1;
		}
		return timeout;
	}
}