package cc.smtweb.system.canal.example;

import cc.smtweb.framework.core.util.CommUtil;
import org.apache.commons.lang.StringUtils;

import java.util.Properties;

/**
 * 启动常用变量
 *
 * @author jianghang 2012-11-8 下午03:15:55
 * @version 1.0.0
 */
public class ClientConsts {

    public static final String ROOT                                 = "canal.server";
    public static final String SIZE                                 = ROOT + "." + "size";
    public static final String INSTANCE                             = ROOT + "." + "instance";
    public static final String IP                                   = ROOT + "." + "ip";
    public static final String PORT                                 = ROOT + "." + "port";
    public static final String USERNAME                             = ROOT + "." + "username";
    public static final String PASSWORD                             = ROOT + "." + "password";
    public static final String FILTER                               = ROOT + "." + "filter";
    public static final String FILEPATH                             = "canal.file.path";


    public static String getIp(Properties properties){
        return getProperty(properties,IP,"127.0.0.1");
    }

    public static int getPort(Properties properties){
        return Integer.parseInt(getProperty(properties,PORT,"11111"));
    }

    public static int getBatchSize(Properties properties){
        return Integer.parseInt(getProperty(properties,SIZE,"50"));
    }

    public static String getInstance(Properties properties){
        return getProperty(properties,INSTANCE,"example");
    }

    public static String getUsername(Properties properties){
        return getProperty(properties,USERNAME,"");
    }

    public static String getPassword(Properties properties){
        return getProperty(properties,PASSWORD,"");
    }

    public static String getFilter(Properties properties){
        return getProperty(properties,FILTER,".*\\..*");
    }

    public static String getFilepath(Properties properties){
        if(CommUtil.isLinux()){
            return getProperty(properties,FILEPATH,"/jujia/canal/canalFile");
        }
        return getProperty(properties,FILEPATH,"D:/jujia/canal/canalFile");
    }

    public static String getProperty(Properties properties, String key, String defaultValue) {
        String value = getProperty(properties, key);
        if (StringUtils.isEmpty(value)) {
            return defaultValue;
        } else {
            return value;
        }
    }

    public static String getProperty(Properties properties, String key) {
        key = StringUtils.trim(key);
        String value = System.getProperty(key);

        if (value == null) {
            value = System.getenv(key);
        }

        if (value == null) {
            value = properties.getProperty(key);
        }

        return StringUtils.trim(value);
    }
}
