/*
 * Created on 2005. 2. 26.
 */
package com.eactive.eai.adapter.socket2.config;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;

import java.util.HashMap;
import java.util.Properties;


/**
 * @author janet
 */
public class SocketLoggerManager {	
	private static SocketLoggerManager  instance 	= new SocketLoggerManager();
	
	private HashMap                   	pool 		= new HashMap() ;  // KEY - adapterGroupName, VALUE - logger	   
	private String                    	eaiServer;
	public static String              	DEFAULT_LOGGER_NAME = "NET";
    public static String              	ERROR_LOGGER_NAME   = "ERRORNET";
    
    public SocketLoggerManager() {
    	eaiServer = EAIServerManager.getInstance().getLocalServerName();
    	if (eaiServer == null)
    		eaiServer = "";
	}
	
	public static SocketLoggerManager getInstance() {
		return instance;
	}
    
	public void stop() {
        this.removeAll();
    }
    
    public void removeAll() {
        pool.clear();
    }
    
    // Socket Adapter Group Name별 Logger생성 및 관리...
    public void start(String adapterGroupName) {
        this.addLogger(adapterGroupName);
    }
    
    public synchronized void addLogger(String adapterGroupName) {
        String  loggerName  = this.getLoggerName( adapterGroupName );
        Object o = pool.get( loggerName );
    	if ( o == null ) {
    		synchronized ( pool ) {
    			o =  pool.get( loggerName );
    			if ( o == null ) {
                    Logger  logger      = Logger.getLogger( loggerName );
//                    boolean isNull = (logger == null );
                    pool.put( loggerName, logger );
    			}
    		}
    	}
    }
    
    private String getLoggerName(String adapterGroupName) {
        StringBuffer sb = new StringBuffer();
        sb.append("FileLogger{").append( adapterGroupName ).append("}_").append( this.eaiServer );
        
        return sb.toString();
    }
    
    private String getLoggerBase(String adapterGroupName) {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("FileLogger{").append( adapterGroupName ).append("}");
        
        return strBuff.toString();
    }
    
    public void removeLogger(String adapterGroupName) {
        pool.remove( this.getLoggerName( adapterGroupName ));
    }
                
    public void stop(String adapterGroupName) {
        // adapterGroupName의 session stop & clear
        this.removeLogger( adapterGroupName );
    }
    
    public Logger getLogger( String adapterGroupName ) {
        return getLoggerGroup( adapterGroupName );
    }
    
    public Logger getErrLogger() {
        return getLoggerGroup( ERROR_LOGGER_NAME );
    }
    
    public Logger getLoggerGroup( String adapterGroupName ) {
        
        Logger logger = (Logger) pool.get( this.getLoggerName( adapterGroupName ) );
        if ( logger == null ) {
            if ( DEFAULT_LOGGER_NAME.equals( adapterGroupName ) ) {
                this.addLogger( adapterGroupName );
                logger = (Logger) pool.get( this.getLoggerName( adapterGroupName ) );
            } else {
                //PropManager pmanager = PropManager.getInstance();
            	AdapterPropManager pmanager = AdapterPropManager.getInstance();
                Properties loggerInfo= null;
                
                try {
                    loggerInfo = pmanager.getProperties( getLoggerBase(adapterGroupName) );
                } catch(Exception e) {
                	loggerInfo = null;
                }
                
                if ( loggerInfo == null ) {
                    logger = getLogger();
                } else {
                    this.addLogger( adapterGroupName );
                    logger = (Logger) pool.get( this.getLoggerName( adapterGroupName ) );
                }
            }
        }
        return logger;
    }
    
    public Logger getLogger() {
        //return Logger.getLogger( Logger.LOGGER_ADAPTER );
        Logger logger = (Logger) pool.get( this.getLoggerName( this.DEFAULT_LOGGER_NAME ) );
        if ( logger == null ) {
            this.addLogger( DEFAULT_LOGGER_NAME );
            logger = (Logger) pool.get( this.getLoggerName( DEFAULT_LOGGER_NAME ) );
        }
        
        return logger;
    }
    
}
