
package com.eactive.eai.adapter.webt;

import com.eactive.eai.adapter.ElinkDefaultAdapter;
import com.eactive.eai.adapter.socket2.common.SMSLogger;
import com.eactive.eai.common.util.CommonLib;
import com.eactive.eai.common.util.Logger;
import tmax.webt.*;

import java.util.Properties;

public class WebTClientAdapter extends ElinkDefaultAdapter {
	public static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);

	public static final String ADAPTER_GORUP_NAME = "ADAPTER_GROUP_NAME";

	public static final String POOL_NAME = "pool.name";
	public static final String SERVICE_NAME = "service.name";
	public static final String RESPONSE_TYPE = "response.type";
	public static final String HOST_NAME = "host.name";
	public static final String PORT_NUMBER = "port.number";
	public static final String MAX_CONNECTION = "max.connection";

	public WebTClientAdapter() {
		// empty
	}

	// -Dwebt.properties=
	public Object execute(Properties prop, Object message) throws Exception {
		String adapterGroupName = prop.getProperty(ADAPTER_GORUP_NAME);
		String adapterName = prop.getProperty("adapterName");

		byte[] data = null;
		if (message instanceof byte[]) {
			data = (byte[]) message;
		} else if (message instanceof String) {
			data = ((String) message).getBytes();
		} else {
			throw new Exception("RECEAIAHA011");
		}

		String serviceName = prop.getProperty(SERVICE_NAME, "SEAIRECV");
		String responseType = prop.getProperty(RESPONSE_TYPE, "ASYNC");

		WebtConnection connection = null;
		try {
			connection = WebtConnectionPool.getConnection(adapterGroupName + "." + adapterName, 100);
			if (logger.isDebug()) {
				logger.debug("WebTClientAdapter] ServiceName " + "[" + serviceName + "]");
			}

			WebtRemoteService service = new WebtRemoteService(serviceName, connection);
			WebtBuffer request = service.createCarrayBuffer();
			request.setBytes(data);
			if (logger.isDebug()) {
				logger.debug("WebTClientAdapter] Send Data " + "[" + new String(data) + "]");
				logger.debug("WebTClientAdapter] Send Data dump " + CommonLib.getDumpMessage(data));
			}

			if ("ASYN".equals(responseType)) {
				/*************************************
				 * 테스트 소스, 강제로 WebtIOException 발생
				 ***********************************
				 * if("_OPB_OU_WBT_AsC{CBS_i111}".equals(prop.getProperty("adapterName")) ||
				 * "_OPB_OU_WBT_AsC{CBS_i121}".equals(prop.getProperty("adapterName")) ||
				 * "_OPB_OU_WBT_AsC{CBS_i211}".equals(prop.getProperty("adapterName")) ||
				 * "_OPB_OU_WBT_AsC{CBS_i221}".equals(prop.getProperty("adapterName"))){ throw
				 * new WebtIOException("Node 1 down, retry test exception throw"); }
				 * ///////////////////////////////////////////////////////////////////////////////////////////
				 */

				WebtAttribute attr = new WebtAttribute();
				attr.setTPNOTIME(true); // 위에서
				attr.setTPNOTRAN(true);
				attr.setTPNOREPLY(true);
				attr.setTPBLOCK(true);
				int response = service.tpacall(request, attr);
				data = null;
				if (logger.isDebug()) {
					logger.debug("WebTClientAdapter] Recv Data Async " + "[" + response + "]");
				}
			} else {
				WebtAttribute attr = new WebtAttribute();
				attr.setTPNOTIME(true);
				attr.setTPNOTRAN(true);
				WebtBuffer response = service.tpcall(request, attr);
				data = response.getBytes();
				if (logger.isDebug()) {
					logger.debug("WebTClientAdapter] Recv Data Sync " + "[" + new String(data) + "]");
					logger.debug("WebTClientAdapter] Recv Data dump " + CommonLib.getDumpMessage(data));
				}
			}
		} catch (Exception e) {
			try {
				String errorCode = "RECEAIOWT001";
				String addMsg = ", " + e.getMessage() + ",[" + prop.getProperty(HOST_NAME) + ":"
						+ prop.getProperty(PORT_NUMBER) + "]";
				SMSLogger.loggingSMSWithCode(errorCode, addMsg);
			} catch (Throwable t) {
				logger.error("[EAIDEBUG] SMS logging Error occured. but ignore this. - " + t.toString());
			}
			throw e;
		} finally {
			// WebtConnection 객체 반납
			// WebtConnectionPool.putConnection(connection);
			if (connection != null) {
				connection.close();
			}
		}

		return data;
	}

}
