package com.xxl.job.admin;

import com.xxl.job.admin.util.ZookeeperClientUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;

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

/**
 * @author xuxueli 2018-10-28 00:38:13
 */
@SpringBootApplication
public class XxlJobAdminApplication {

	public static void main(String[] args){
		init(args);
        SpringApplication.run(XxlJobAdminApplication.class, args);
	}

	private static void init(String[] args){
		// 初始化zookeeper客户端工具类
		if(args == null || args.length == 0){
			throw new RuntimeException("请传入zookeeper的地址");
		}
		if (args.length < 2){
			throw new RuntimeException("请传入项目启动的端口号---第二个参数");
		}
		System.setProperty("server.port", args[1]);
		ZookeeperClientUtil.init(args[0], "/weixt/conf");
		// 创建zookeeper客户端
		ZookeeperClientUtil configurationClient = new ZookeeperClientUtil();
		setSystemPropertie(configurationClient,"spring.datasource.url");
		setSystemPropertie(configurationClient,"spring.datasource.username");
		setSystemPropertie(configurationClient,"spring.datasource.password");
	}

	private static void setSystemPropertie(ZookeeperClientUtil configurationClient, String key){
		String value = configurationClient.getData(ZookeeperClientUtil.mainPath + "/" + key);
		if(StringUtils.isEmpty(value)){
			throw new RuntimeException("zookeeper中没有该属性的值：" + key);
		}
		System.setProperty(key,value);
	}
}