package com.eactive.eai.adapter.socket.config;

import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.adapter.socket.common.DiagLogger;

import java.io.Serializable;


/**
* 1. 기능 : SocketAdapter의 구성정보 및 속성을 나타내는 Object Class
* 2. 처리 개요 :
*     - 구성정보의 DEFAULT값을 갖고 있으며, 각 속성에 대한 getter/setter 기능을 제공한다.
* 3. 주의사항
*     - 해당 Property의 기본속성값을 확인하여, 해당 Property의 속성지정을 생략시 어떤 값을 갖는지
*       확인하여야한다.
* @param
* @return
* @exception
**/

public class ConfigurationContext implements Serializable {

    /**
     * 
     */
    private static final long   serialVersionUID = 1L;
    public  static final String INBOUND_SOCKET   = "INBOUND";
    public  static final String OUTBOUND_SOCKET  = "OUTBOUND";
    public  static final String IOBOUND_SOCKET   = "IOBOUND";

    public  static final String SERVER_SOCKET    = "SERVER";
    public  static final String CLIENT_SOCKET    = "CLIENT";
    public  static final String ADMIN_SOCKET     = "ADMIN";

    public  static final String SYNC_MODE        = "SYNC";
    public  static final String ASYNC_MODE       = "ASYN";

    public  static final String TRUE_FLAG        = "Y";
    public  static final String FALSE_FLAG       = "N";

    /**
     * Configuration properties
     */
    private String    adapterGroupName    = "";                    // SOCKET ADADAPTER GROUP NAME
    private String    adapterName         = "";                    // SOCKET ADAPTER 구성 이름

    private String    boundUsage          = INBOUND_SOCKET;        // INBOUND | OUTBOUND
    private String    socketType          = SERVER_SOCKET;         // SERVER | CLIENT

    private String    hostName            = "localhost";           // BIND할 서버의 호스트명 또는 IP ADDRESS
    private int       portNumber          = 0;                     // BIND할 서버의 PORT NUMBER

    private String    localHostName       = "";                    // Client Socket Type에 한하여 설정
    private int       localPortNumber     = 0;                     // Client Socket Type으로 원격서버와 연결시 BIND할localPortNumber

    private int       connLimitPerIp      = 0;                     // SERVER TYPE SOCKET의 경우 REMOTE의 특정 IP별  CONNECTION 수를 제한시키는 것
    private int       maxConnection       = 1;                     // 원격시스템과 Socket Connection 최대허용 수
                                                                   // Server Socket의 경우 - Server Listening port로 접속허용할 최대 수
                                                                   // Client Socket의 경우 - Remote Host의 지정 포트로 연결할 최대 Connection 수

    private int       defaultSession      = 2;                     // Inbound의 Default Session 유지 수
    private int       maxSession          = 5;                     // INBOUND의 경우한하여 의미가 있음.
                                                                   // Socket Adapter의 N개의 Connection에 대해 WLI session을 연결할 최대 수 (실행 Thread Pool의 Session수)

    private int       timeout             = 0;                     // SOCKET READ TIMEOUT값 설정
                                                                   // 송수신 메시지의 Socket Read Timeout값을 설정 (단위 : 초) [timeout으로 변경]

    private int       sessionTimeout      = 10;                    // 10초의 Session Timeout Time

    private String    responseType        = SYNC_MODE;             // SYNC_MODE, ASYNC_MODE
    private String    socketReuse         = TRUE_FLAG;             // "Y" - Socket Connection reuse
                                                                   // "N" - try to connect each transaction

    private int       llFieldIndex        = 0;                     // LL필드 시작 Index
    private int       llFieldLength       = 2;                     // LL필드길이 2-unsigned short, 4 ~ 8 Numeric Char
    private String    ackProtocol         = FALSE_FLAG;            // ACK송수신 여부 -- Client Type의 Socket에 적용되며, 서버는 Protocol에 의해 작동판단...

    private String    bwkClsCd            = "";                    // 업무구분코드
    private boolean   responseOnly        = false;                 // 어댑터가 응답전용모드인지 여부
    private boolean   usePollMsg          = false;                 // polling 여부

    private int       traceLevel          = DiagLogger.DEFAULT;    // 추적관리 수준 - 0, 1, 2
    private String    description         = "";                    // Socket Adapter Configuration에 대한 용도설명
    //
    private boolean   started             = true;


    private AdapterVO adapterInfo         = null;
    
    public void setAdapterVO(AdapterVO info) {
    	this.adapterInfo = info; 
    }
    
    public AdapterVO getAdapterVO() {
    	return this.adapterInfo; 
    }


    public ConfigurationContext() {
    }

    public ConfigurationContext(String adapterGroupName, String adapterName, String boundUsage, String socketType,
                                String hostName, int portNumber ) {

        this.adapterGroupName   = adapterGroupName;
        this.adapterName        = adapterName;
        this.boundUsage         = boundUsage;
        this.socketType         = socketType;
        this.hostName           = hostName;
        this.portNumber         = portNumber;

    }

    /**
     * @return Returns the adapterGroupName.
     */
    public String getAdapterGroupName() {
        return adapterGroupName;
    }
    /**
     * @param adapterGroupName The adapterGroupName to set.
     */
    public void setAdapterGroupName(String adapterGroupName) {
        this.adapterGroupName = adapterGroupName;
    }
    /**
     * @return Returns the defaultSession.
     */
    public int getDefaultSession() {
        return defaultSession;
    }
    /**
     * @param defaultSession The defaultSession to set.
     */
    public void setDefaultSession(
            int defaultSession) {
        this.defaultSession = defaultSession;
    }

    /**
     * @return Returns the socketReuse.
     */
    public String getSocketReuse() {
        return socketReuse;
    }
    /**
     * @return Returns the connLimitPerIp.
     */
    public int getConnLimitPerIp() {
        return connLimitPerIp;
    }
    /**
     * @param connLimitPerIp The connLimitPerIp to set.
     */
    public void setConnLimitPerIp(int connLimitPerIp) {
        this.connLimitPerIp = connLimitPerIp;
    }
    /**
     * @return Returns the adapterName.
     */
    public String getAdapterName() {
        return adapterName;
    }
    /**
     * @param adapterName The adapterName to set.
     */
    public void setAdapterName(String adapterName) {
        this.adapterName = adapterName;
    }
    /**
     * @return Returns the bizCode.
     */
    public String getBwkClsCd() {
        return bwkClsCd;
    }
    /**
     * @param bizCode The bizCode to set.
     */
    public void setBwkClsCd(String bizCode) {
        this.bwkClsCd = bizCode;
    }

    public String getAckProtocol() {
        return ackProtocol;
    }
    /**
     * @param bizCode The bizCode to set.
     */
    public void setAckProtocol(String ackProtocol) {
        this.ackProtocol = ackProtocol;
    }

    /**
     * @return Returns the boundUsage.
     */
    public String getBoundUsage() {
        return boundUsage;
    }
    /**
     * @param boundUsage The boundUsage to set.
     */
    public void setBoundUsage(String boundUsage) {
        this.boundUsage = boundUsage;
    }
    /**
     * @return Returns the description.
     */
    public String getDescription() {
        return description;
    }
    /**
     * @param description The description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }
    /**
     * @return Returns the hostName.
     */
    public String getHostName() {
        return hostName;
    }
    /**
     * @param hostName The hostName to set.
     */
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    /**
     * @return Returns the llFieldLength.
     */
    public int getLlFieldIndex() {
        return llFieldIndex;
    }
    /**
     * @param llFieldLength The llFieldLength to set.
     */
    public void setLlFieldIndex(int llFieldIndex) {
        this.llFieldIndex = llFieldIndex;
    }
    /**
     * @return Returns the llFieldLength.
     */
    public int getLlFieldLength() {
        return llFieldLength;
    }
    /**
     * @param llFieldLength The llFieldLength to set.
     */
    public void setLlFieldLength(int llFieldLength) {
        this.llFieldLength = llFieldLength;
    }
    /**
     * @return Returns the localHostName.
     */
    public String getLocalHostName() {
        return localHostName;
    }
    /**
     * @param localHostName The localHostName to set.
     */
    public void setLocalHostName(String localHostName) {
        this.localHostName = localHostName;
    }
    /**
     * @return Returns the localPortNumber.
     */
    public int getLocalPortNumber() {
        return localPortNumber;
    }
    /**
     * @param localPortNumber The localPortNumber to set.
     */
    public void setLocalPortNumber(int localPortNumber) {
        this.localPortNumber = localPortNumber;
    }

    /**
     * @return Returns the maxConnection.
     */
    public int getMaxConnection() {
        return maxConnection;
    }

    /**
     * @param maxConnection The maxConnection to set.
     */
    public void setMaxConnection(
            int maxConnection) {
        this.maxConnection = maxConnection;
    }

    /**
     * @return Returns the maxSession.
     */
    public int getMaxSession() {
        return maxSession;
    }
    /**
     * @param maxSession The maxSession to set.
     */
    public void setMaxSession(int maxSession) {
        this.maxSession = maxSession;
    }
    /**
     * @return Returns the portNumber.
     */
    public int getPortNumber() {
        return portNumber;
    }
    /**
     * @param portNumber The portNumber to set.
     */
    public void setPortNumber(int portNumber) {
        this.portNumber = portNumber;
    }
    /**
     * @return Returns the readTimeout.
     */
    public int getTimeout() {
        return timeout;
    }
    /**
     * @param readTimeout The readTimeout to set.
     */
    public void setTimeout(int readTimeout) {
        this.timeout = readTimeout;
    }

    public int getSessionTimeout() {
        return sessionTimeout;
    }
    /**
     * @param readTimeout The readTimeout to set.
     */
    public void setSessionTimeout(int sessionTimeout) {
        this.sessionTimeout = sessionTimeout;
    }

    /**
     * @return Returns the responseType.
     */
    public String getResponseType() {
        return responseType;
    }

    public boolean isSyncMode() {
        return ( this.responseType.equals( SYNC_MODE ) ) ;
    }

    public boolean isAckProtocol() {
        return ( this.ackProtocol.equals( TRUE_FLAG ) ) ;
    }
    
    public boolean isResponseOnly() {
		return responseOnly;
	}

	public void setResponseOnly(boolean responseOnly) {
		this.responseOnly = responseOnly;
	}
	
	public boolean isUsePollMsg() {
		return usePollMsg;
	}

	public void setUsePollMsg(boolean usePollMsg) {
		this.usePollMsg = usePollMsg;
	}

	/**
     * @param responseType The responseType to set.
     */
    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }
    /**
     * @return Returns the socketReuse.
     */
    public boolean isSocketReuse() {
        return socketReuse.equals( TRUE_FLAG );
    }
    /**
     * @param socketReuse The socketReuse to set.
     */
    public void setSocketReuse(boolean socketReuse) {
        this.socketReuse = (socketReuse ? TRUE_FLAG : FALSE_FLAG);
    }

    public void setSocketReuse(String socketReuse) {
        this.socketReuse = socketReuse;
    }


    /**
     * @return Returns the socketType.
     */
    public String getSocketType() {
        return socketType;
    }
    /**
     * @param socketType The socketType to set.
     */
    public void setSocketType(String socketType) {
        this.socketType = socketType;
    }
    /**
     * @return Returns the traceLevel.
     */
    public int getTraceLevel() {
        return traceLevel;
    }
    /**
     * @param traceLevel The traceLevel to set.
     */
    public void setTraceLevel(int traceLevel) {
        this.traceLevel = traceLevel;
    }

    public void setStarted(boolean on) {
        this.started    = on;
    }

    public boolean isStarted() {
        return this.started;
    }

    public String toString() {
        StringBuffer    strBuff = new StringBuffer();
        strBuff.append( this.adapterName ).append( " [Adapter의 등록정보입니다. : " );
        strBuff.append(" adapter.group.name=").append( this.adapterGroupName ).append(",");
        strBuff.append(" bound.usage=").append( this.boundUsage ).append(",");
        strBuff.append(" socket.type=").append( this.socketType ).append(",");
        strBuff.append(" host.name=").append( this.hostName ).append(",");
        strBuff.append(" port.number=").append( this.portNumber).append(",");
        strBuff.append(" local.host.name=").append( this.localHostName ).append(",");
        strBuff.append(" local.port.number=").append( this.localPortNumber ).append(",");
        if ( this.connLimitPerIp > 0 ) {
            strBuff.append(" connection.limit.per.ip=").append( this.connLimitPerIp ).append(",");
        } else {
            strBuff.append(" connection.limit.per.ip=0(UNLIMITED)" ).append(",");
        }

        if ( this.maxConnection > 0 ) {
            strBuff.append(" max.connection=").append( this.maxConnection ).append(",");
        } else {
            strBuff.append(" max.connection=0(UNLIMITED)" ).append(",");
        }

        strBuff.append(" default.session=").append( this.defaultSession ).append(",");
        strBuff.append(" max.session=").append( this.maxSession ).append(",");
        strBuff.append(" timeout=").append( this.timeout ).append(",");
        strBuff.append(" session.timeout=").append( this.sessionTimeout ).append(",");
        strBuff.append(" response.type=").append( this.responseType ).append(",");
        strBuff.append(" socket.reuse=").append( this.socketReuse ).append(",");
        strBuff.append(" ll.field.index=").append( this.llFieldIndex ).append(",");
        strBuff.append(" ll.field.length=").append( this.llFieldLength ).append(",");
        strBuff.append(" ack.support=").append( this.ackProtocol ).append(",");
        strBuff.append(" business.work.class.code=").append( this.bwkClsCd ).append(",");

        if ( this.traceLevel == 2 ) {
            strBuff.append(" trace.level=").append( this.traceLevel ).append("(DEBUG)").append(",");
        } else if ( this.traceLevel == 1 ) {
            strBuff.append(" trace.level=").append( this.traceLevel ).append("(INFO)").append(",");
        } else {
            strBuff.append(" trace.level=").append( this.traceLevel ).append("(ERROR)").append(",");
        }

        strBuff.append(" description=").append(this.description).append("]");

        return strBuff.toString();
    }
}