package com.eactive.eai.adapter.http.dynamic;

import com.eactive.eai.adapter.listener.AdapterException;
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
import com.eactive.eai.common.util.Logger;

public class HttpAdapterListener extends AdapterListenerSupport {

	final static Logger logger2 = HttpLoggerManager.getInstance().getLogger();
	final static Logger errLogger = HttpLoggerManager.getInstance().getErrLogger();

	public void start() throws AdapterException {
		HttpDynamicPortInAdapterManager manager = HttpDynamicPortInAdapterManager.getInstance();
		try {
			if (info == null) {
				throw new Exception("HttpAdapterListener | AdapterVO가 NULL입니다.");
			}
			manager.addAdapter(info);

			try {
				manager.start(info);
				if(logger2.isInfo()) {
					logger2.info("[HttpAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
							+ "] config [" + info + "] start");
				}
			} catch (Exception e) {
				manager.removeAdapter(info);
				throw e;
			}
		} catch (Exception e) {
			logger.error(info.getName() + " | HttpAdapterListener | cannot start : " + e.getMessage(), e);
			throw new AdapterException(info.getName() + " | HttpAdapterListener | cannot start - " + e.getMessage());
		}
	}

	public void stop() throws AdapterException {
		HttpDynamicPortInAdapterManager manager = HttpDynamicPortInAdapterManager.getInstance();
		try {
			if (info == null) {
				throw new Exception("HttpAdapterListener | AdapterVO가 NULL입니다.");
			}

			manager.removeAdapter(info);
			manager.stop(info);
			if(logger2.isInfo()) {
				logger2.info("[HttpAdapterListener] AdapterName [" + info.getAdapterGroupName() + "." + info.getName()
						+ "] adapter info [" + info + "] stop");
			}
		} catch (Exception e) {
			logger.error(info.getName() + " | HttpAdapterListener | cannot stop : " + e.getMessage(), e);
			throw new AdapterException(info.getName() + " | HttpAdapterListener | cannot stop - " + e.getMessage());
		}
	}
}
