package com.alibaba.otter.canal.deployer;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * canal独立版本启动的入口类
 * 
 * @author jianghang 2012-11-6 下午05:20:49
 * @version 1.0.0
 */
public class CanalLauncher {

    private static final String CLASSPATH_URL_PREFIX = "classpath:";
    private static final Logger logger               = LoggerFactory.getLogger(CanalLauncher.class);

    public static void main(String[] args) throws Throwable {
        try {
            String conf = System.getProperty("canal.conf", "classpath:canal.properties");
            Properties properties = new Properties();
            if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
                conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
                properties.load(CanalLauncher.class.getClassLoader().getResourceAsStream(conf));
            } else {
                properties.load(new FileInputStream(conf));
            }

            logger.info("## start the canal server.");
            final CanalController controller = new CanalController(properties);
            controller.start();
            logger.info("## the canal server is running now ......");
            Runtime.getRuntime().addShutdownHook(new Thread() {

                public void run() {
                    try {
                        logger.info("## stop the canal server");
                        controller.stop();
                    } catch (Throwable e) {
                        logger.warn("##something goes wrong when stopping canal Server:", e);
                    } finally {
                        logger.info("## canal server is down.");
                    }
                }

            });
        } catch (Throwable e) {
            logger.error("## Something goes wrong when starting up the canal Server:", e);
            System.exit(0);
        }
    }
}

@@ -560,32 +560,209 @@
 %7B%0A        try %7B%0A
+            logger.info(%22## set default uncaught exception handler%22);%0A            setGlobalUncaughtExceptionHandler();%0A%0A            logger.info(%22## load canal configurations%22);%0A
             Stri
@@ -2148,11 +2148,339 @@
  %7D%0A    %7D
+%0A%0A    private static void setGlobalUncaughtExceptionHandler() %7B%0A        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() %7B%0A            @Override%0A            public void uncaughtException(Thread t, Throwable e) %7B%0A                logger.error(%22UnCaughtException%22, e);%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A

add uncaughtexception handler