All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.configcenter.client.ConfigLocalSave Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package com.github.javaclub.configcenter.client;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.configcenter.client.domain.CfgVO;
import com.github.javaclub.configcenter.client.util.DESCoder;
import com.github.javaclub.configcenter.client.util.LocalFileUtils;

/**
 * 本地保存一份配置,容灾
 */
public class ConfigLocalSave {
	
	private static final Logger logger = LoggerFactory.getLogger(ConfigLocalSave.class);
	private static ConfigLocalSave instance = new ConfigLocalSave();
	
	public static ConfigLocalSave getInstance() {
		return instance;
	}
	
	public synchronized Map get(int appid, String appkey, String configkey) {
		try {
			List> cfgList = readConfigFromFile(appid, appkey);
			if(cfgList != null){
				for (Map cfg : cfgList) {
					if (cfg.get("key").equals(configkey)) {
						return cfg;
					}
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		return null;
	}

	public synchronized boolean save(int appid, String appkey, String configkey, String configValue, long timestamp) {
		try {
			List> cfgList = readConfigFromFile(appid, appkey);
			List writeStr = new ArrayList();
			if (cfgList != null) {
				for (Map cfg : cfgList) {
					if (cfg.get("key").equals(configkey)) {  // 更新旧配置
						cfg.put("key", configkey);
						cfg.put("value", configValue);
						cfg.put("timestamp", String.valueOf(timestamp));
					}
					String str = JSONObject.toJSONString(cfg);
					if (!writeStr.contains(str)) {
						writeStr.add(str);
					}
				}
			} else {
				Map cfg = new HashMap();
				cfg.put("key", configkey);
				cfg.put("value", configValue);
				cfg.put("timestamp", String.valueOf(timestamp));
				writeStr.add(JSONObject.toJSONString(cfg));
			}
			FileUtils.writeLines(getFile(appid), writeStr, false);
			return true;
		} catch (Exception e) {
			logger.error("", e);
		}
		return false;
	}
	
	public synchronized boolean snapshot(int appid, String appkey, Collection cfgList) {
		try {
			if(cfgList == null || cfgList.isEmpty()){
				return false;
			}
			List writeStr = new ArrayList();
			for(CfgVO cfgVO: cfgList){
				Map cfg = new HashMap();
				cfg.put("key", cfgVO.getConfigkey());
				cfg.put("value", cfgVO.getConfigvalue());
				cfg.put("timestamp", cfgVO.getTimestamp());
				String str = JSONObject.toJSONString(cfg);
				if (!writeStr.contains(str)) {
					writeStr.add(DESCoder.encrypt(str, appkey));
				}
			}
			FileUtils.writeLines(getFile(appid),"utf-8", writeStr, false);
		} catch (Exception e) {
			logger.error("", e);
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public List> readConfigFromFile(int appid, String appkey) throws Exception {
		List cfgList = FileUtils.readLines(getFile(appid), "utf-8");
		if (cfgList != null && !cfgList.isEmpty()) {
			List> list = new ArrayList>();
			for (String cfg : cfgList) {
				list.add(JSONObject.parseObject(DESCoder.decrpty(cfg, appkey), Map.class));
			}
			return list;
		}
		return null;
	}

	private File getFile(int appid) throws IOException {
		File f = new File(LocalFileUtils.getLocalSaveDirPath() + "/" + appid);
		if (!f.exists()) {
			f.createNewFile();
		}
		return f;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy