package com.eactive.eai.common.logger;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;

import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.jms.JmsServiceLocator;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;

/**
 * 1. 기능 : LogListener를 관리하는 Manager 클래스 
 * 2. 처리 개요 : ServiceLocator로부터 queue를 get하여 Receiver를 생성 관리한한다. 
 * 3. 주의사항
 *
 * @author :
 * @version : v 1.0.0
 * @see : 관련 기능을 참조
 * @since : :
 */
public class LogListener implements MessageListener, Lifecycle {
	private String name;
	private LifecycleSupport lifecycle = new LifecycleSupport(this);
	private boolean started;

	private QueueConnection qcon;
	private QueueSession qsession;
	private QueueReceiver qreceiver;
	private Queue queue;

	public LogListener(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void onMessage(Message msg) {
		// write code here!
	}

	public void start() throws LifecycleException {
		if (started)
			throw new LifecycleException("RECEAICLG101");
		lifecycle.fireLifecycleEvent(STARTING_EVENT, null);

		try {
			JmsServiceLocator locator = JmsServiceLocator.getInstance();
			QueueConnectionFactory  qconFactory = locator.getQueueConnectionFactory(Keys.LOG_CONNECTION_FACTORY);
			if (qconFactory == null) {
				throw new Exception("QueueConnectionFactory not found  - " + Keys.LOG_CONNECTION_FACTORY);
			}
			qcon = qconFactory.createQueueConnection();
			qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			queue = locator.getQueue(Keys.LOG_QUEUE);
			qreceiver = qsession.createReceiver(queue);
			qreceiver.setMessageListener(this);
			qcon.start();
		} catch (Exception e) {
			throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICLG102"));
		}

		started = true;
		lifecycle.fireLifecycleEvent(STARTED_EVENT, null);
	}

	public void stop() throws LifecycleException {
		if (!started)
			throw new LifecycleException("RECEAICLG103");

		lifecycle.fireLifecycleEvent(STOPING_EVENT, null);

		try {
			if (qreceiver != null)
				qreceiver.close();
		} catch (Exception e) {
			// nothing to do
		}
		try {
			if (qreceiver != null)
				qsession.close();
		} catch (Exception e) {
			// nothing to do
		}
		try {
			if (qreceiver != null)
				qcon.close();
		} catch (Exception e) {
			// nothing to do
		}

		started = false;
		lifecycle.fireLifecycleEvent(STOPPED_EVENT, null);
	}

	public void addLifecycleListener(LifecycleListener listener) {
		lifecycle.addLifecycleListener(listener);
	}

	public LifecycleListener[] findLifecycleListeners() {
		return lifecycle.findLifecycleListeners();
	}

	public void removeLifecycleListener(LifecycleListener listener) {
		lifecycle.removeLifecycleListener(listener);
	}

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

}
