package tutorial;

import java.lang.management.ManagementFactory;
import java.net.BindException;
import java.util.Iterator;

import kr.fujitsu.com.ffw.daemon.DaemonLauncher;
import kr.fujitsu.com.ffw.daemon.LogPropertySetter;
import kr.fujitsu.com.ffw.daemon.ManagerSocket;
import kr.fujitsu.com.ffw.daemon.core.DaemonConfigLocator;
import kr.fujitsu.com.ffw.daemon.core.config.log.Log;
import kr.fujitsu.com.ffw.daemon.core.config.node.EngineContainer;
import kr.fujitsu.com.ffw.daemon.core.config.node.Node;


public class TestDaemon {

	private DaemonConfigLocator locator = null;

	public Node buildAndLaunchCommand(DaemonLauncher dl, String mode, String path, String classpath) throws Exception {
		
		Node node = this.locator.getDaemonConfig().getNode();
		

		Iterator it = node.getEngineContainers().keySet().iterator();
		String key = "";
		EngineContainer container = null;

		while (it.hasNext()) {
			key = (String) it.next();
			container = node.getEngineContainer(key);
			
			System.out.println(key + ", port =====> " + container.getApplicationPort());

			StringBuffer cmd = new StringBuffer();
			cmd.append("java ").append(container.getCommandOption() + " ")
					.append("-classpath" + " ")
					.append(classpath)
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-codec-1.9.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-compress-1.3.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-compress-1.12.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-io-2.5.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-net-1.4.1.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-net-3.3.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/commons-net-3.5.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/cyberpass.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/gson-2.2.2.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/gson-2.6.2.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/gson-2.6.2-javadoc.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/gson-2.6.2-sources.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/j2ssh-core-0.2.9.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/j2ssh-maverick-1.5.5.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/jsch-0.1.54.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/mysql-connector-java-5.1.15-bin.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/ojdbc5.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/ojdbc6.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/ojdbc7.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/ojdbc14.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/Seed.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/sms-framework-1.0.1.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/sqljdbc.jar")
					.append(";C:/spcwps/workspace/sms-1.0.1/lib/sqljdbc4.jar" + " ")
					.append(container.getType() + " ")
					.append(container.getName() + " ").append(mode + " ")
					.append(path);
			System.out.println(cmd.toString());
			//commandMap.put(container.getName(), cmd);
			try {
				new DaemonLauncher(container.getName(), cmd).start();
			} catch (Exception e) {
				//logger.error("", e);
			}
		}

		return node;
	}
	
	public void parseLocator(String mode, String path) throws Exception {
		locator = DaemonConfigLocator.getInstance(mode, path);
	}
	
	
    public static void main(String[] args) {
        String mode = null;
        String path = null;
        String classpath = null;
        
        DaemonLauncher launcher = null;
        Node n = null;
        String logFile = "C:/spcwps/workspace/sms-1.0.1/bin/log4j.xml";
        Log log = null;
        String option = "";
        
        System.out.println("DaemonL... PID :" + ManagementFactory.getRuntimeMXBean().getName().split("@")[0] );

        if ((args == null) || (args.length < 2)) {
            //System.out.println(">> use: java -server kr.fujitsu.com.ffw.daemon.DaemonLauncher -mode:xml -path:D:/java/workspace/sms-1.0.0/xml/daemon-config.sample.xml [-option:-shutdown]");
        }
        
        
        try {
            mode = "-mode:xml";
            path = "C:/spcwps/workspace/sms-1.0.1/xml/daemon-config.xml".replaceFirst("-path:", "");
            classpath = "C:/spcwps/workspace/sms-1.0.1/bin";

            launcher = new DaemonLauncher(mode, path);
            
            TestDaemon td = new TestDaemon();
            td.parseLocator(mode, path);
            
            //System.out.println(launcher);
            
            if (args.length >= 3)
                option =args[2];

            if (option.equals("-shutdown")) {
                launcher.forcedShutdown();
                return;
            }
            //n = launcher.buildAndLaunchCommand();
            n = td.buildAndLaunchCommand(launcher, mode, path, classpath);
            
            launcher.setNode(n);
            
            log = DaemonConfigLocator.getInstance().getDaemonConfig().getLog();
            LogPropertySetter.writeLog4JConfigXml(logFile, log);
            LogPropertySetter.loadLog4JConfigFile(logFile);
            new ManagerSocket(launcher).start();

     
            
        } catch (BindException e) {
            /*logger.error("[ERROR]Node manager port is already opened.");
            logger.error("[ERROR]DaemonLauncer can not be started.");
            logger.error("[ERROR]Please check the port number.");*/
        	System.out.println("[ERROR]Node manager port is already opened.");
            System.exit(-1);
        } catch (Exception e) {
            //logger.error("", e);

        	System.out.println(e);
            System.exit(-1);
        }
    }
}
