com.jeesuite.confcenter.spring.CCPropertyPlaceholderConfigurer Maven / Gradle / Ivy
/**
*
*/
package com.jeesuite.confcenter.spring;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.StringUtils;
import com.jeesuite.confcenter.utils.HttpUtils;
/**
* @description
* @author vakin
* @date 2016年11月2日
*/
public class CCPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
private final static Logger logger = LoggerFactory.getLogger(CCPropertyPlaceholderConfigurer.class);
private boolean remoteEnabled = true;
public void setRemoteEnabled(boolean remoteEnabled) {
this.remoteEnabled = remoteEnabled;
}
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
}
@Override
protected Properties mergeProperties() throws IOException {
Properties properties = super.mergeProperties();
if(remoteEnabled){
properties.putAll(loadRemoteProperties());
}
return properties;
//
}
private Map loadRemoteProperties() throws FileNotFoundException, IOException{
Map props = new HashMap<>();
List files = fecthRemoteConfigFile();
for (File file : files) {
Properties p = new Properties();
p.load(new FileReader(file));
for(String key : p.stringPropertyNames()) {
String value = p.getProperty(key);
if(value != null && !"".equals(value.toString().trim())){
props.put(key, value);
}
}
}
return props;
}
private List fecthRemoteConfigFile() throws FileNotFoundException, IOException{
List files = new ArrayList<>();
String classRootDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();
if(!classRootDir.endsWith("/"))classRootDir = classRootDir + "/";
List urls = parseAllRemoteUrls();
for (String url : urls) {
String fileName = url.split("file=")[1];
logger.info("begin download remote file by url:{}",url);
try {
File file = HttpUtils.downloadFile(url, classRootDir + fileName);
if(file != null){
files.add(file);
logger.info("download {} ok!!",file.getPath());
}else{
logger.warn("download file[{}] failture from:{}",fileName,url);
}
} catch (Exception e) {
logger.warn("download file[{}] failture from:{},error:{}",fileName,url,e.getMessage());
}
}
return files;
}
private List parseAllRemoteUrls() throws FileNotFoundException, IOException{
List result = new ArrayList<>();
URL resource = Thread.currentThread().getContextClassLoader().getResource("confcenter.properties");
if(resource == null)throw new FileNotFoundException("配置文件[confcenter.properties]缺失");
Properties p = new Properties();
p.load(new FileReader(new File(resource.getPath())));
String apiUrl = p.getProperty("confcenter.url");
String app = p.getProperty("app.name");
String env = p.getProperty("app.env");
String version = p.getProperty("app.version");
//应用私有配置文件
String fileNames = p.getProperty("app.config.files");
if(org.apache.commons.lang3.StringUtils.isNotBlank(fileNames)){
String[] appFiles = StringUtils.commaDelimitedListToStringArray(fileNames);
for (String file : appFiles) {
result.add(String.format("%s?app=%s&env=%s&ver=%s&file=%s", apiUrl,app,env,version,file));
}
}
//全局配置文件
fileNames = p.getProperty("global.config.files");
if(org.apache.commons.lang3.StringUtils.isNotBlank(fileNames)){
String[] appFiles = StringUtils.commaDelimitedListToStringArray(fileNames);
for (String file : appFiles) {
result.add(String.format("%s?app=global&env=%s&ver=%s&file=%s", apiUrl,env,version,file));
}
}
return result;
}
}