package tutorial;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;

import kr.fujitsu.com.ffw.daemon.core.CurrentContext;
import kr.fujitsu.com.ffw.daemon.core.DaemonConfigLocator;
import kr.fujitsu.com.ffw.daemon.core.config.node.EngineContainer;
import kr.fujitsu.com.ffw.daemon.net.ActionFactory;
import kr.fujitsu.com.ffw.daemon.net.DaemonContainer;
import kr.fujitsu.com.ffw.daemon.net.polling.PollingAction;

import org.apache.log4j.Logger;

public class TestContainer extends DaemonContainer {
	private static Logger logger = Logger
			.getLogger(TestContainer.class);
	private static DaemonConfigLocator locator = null;

	
	public TestContainer(EngineContainer context) throws Exception {
		super(context);
	}

	public void initialize() {
	}

	public void loop() {
		try {
			long period = 0L;
			if (getContext().getEngineAttribute()
					.getAttribute("POLLING_PERIOD") != null)
				period = Long.parseLong(getContext().getEngineAttribute()
						.getAttribute("POLLING_PERIOD").getValue());
			else {
				throw new Exception(
						"ActionPollingContainer를 이용하기 위해서는 반드시  <engine-attribute> 항목에 POLLING_PERIOD가 지정되어 있어야 합니다.");
			}

			Thread.sleep(period);

			String actionMode = "0";
			if (getContext().getEngineAttribute().getAttribute("ACTION_TIME") != null) {
				String actionTime = getContext().getEngineAttribute()
						.getAttribute("ACTION_TIME").getValue();
				if (!(actionTime.equals(""))) {
					Date date = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
					String currTime = sdf.format(date);

					if ((currTime.equals(actionTime))
							|| (actionTime.equals("9999")))
						actionMode = "1";
				}
			} else {
				throw new Exception("ACTION_TIME 시간 지정시 해당 시분에 지정작업을 수행합니다");
			}

			PollingAction action = null;
			action = (PollingAction) ActionFactory.createAction(locator
					.getCurrentContext().getContext().getAction());

			action.setMode(actionMode);

			Thread t = new Thread(action, "polling-thread#1");
			t.start();
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public void destroy() {
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//String containerName = "cms_FileAgent";
		//String containerName = "cms_NhsMasterDataRcv";
		String containerName = "cms_NhsSysInfoRcv";
		String mode = "xml";
		String path = "C:/spcnhs/workspace/sms-1.0.1/xml/daemon-config.xml".replaceFirst("-path:", "");
		
		System.out.println("DaemonL... PID :" + ManagementFactory.getRuntimeMXBean().getName().split("@")[0] );
		
		try{
			locator = DaemonConfigLocator.getInstance(mode, path);
			locator.setCurrentContext(new CurrentContext(locator.getDaemonConfig().getNode().getEngineContainer(containerName)));
			
			
			DaemonContainer container = new TestContainer(locator.getCurrentContext().getContext());

			Thread t = new Thread(container);
			t.start();
		
		}catch(Exception i){
			System.out.println("[ERROR] " + i.getStackTrace().toString());
		}
	}

}
