|
|
@@ -1,127 +1,242 @@
|
|
|
package com.uas.report;
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.report.annotation.DynamicValue;
|
|
|
+import com.uas.report.util.FileUtils;
|
|
|
+import com.uas.report.util.ObjectUtils;
|
|
|
+import com.uas.report.util.ResourceUtils;
|
|
|
+import com.uas.report.util.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
- * 不同环境下的配置信息(来自spring cloud)
|
|
|
- *
|
|
|
+ * 不同环境下的配置信息(来自自定义的配置文件)
|
|
|
+ *
|
|
|
* @author sunyj
|
|
|
* @since 2017年1月10日 下午3:32:30
|
|
|
*/
|
|
|
-@Configuration
|
|
|
+@Component
|
|
|
public class SpecialProperties {
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
+ * 配置文件名称
|
|
|
+ */
|
|
|
+ private static final String PROPERTY_FILE_NAME = "report.properties";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 记录配置文件最后修改时间
|
|
|
+ */
|
|
|
+ private long propertyFileLastModified;
|
|
|
+
|
|
|
+ private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
+ /**
|
|
|
* 本地资源根路径
|
|
|
*/
|
|
|
- @Value("${localBaseDir}")
|
|
|
+ @DynamicValue("localBaseDir")
|
|
|
private String localBaseDir;
|
|
|
|
|
|
/**
|
|
|
* 本地资源图片路径
|
|
|
*/
|
|
|
- @Value("${localImagesDir}")
|
|
|
+ @DynamicValue("localImagesDir")
|
|
|
private String localImagesDir;
|
|
|
|
|
|
/**
|
|
|
* 本地资源jrxml模板路径
|
|
|
*/
|
|
|
- @Value("${localJrxmlDir}")
|
|
|
+ @DynamicValue("localJrxmlDir")
|
|
|
private String localJrxmlDir;
|
|
|
|
|
|
/**
|
|
|
* 标准账套(存放标准模板)
|
|
|
*/
|
|
|
- @Value("${standardMaster}")
|
|
|
+ @DynamicValue("standardMaster")
|
|
|
private String standardMaster;
|
|
|
|
|
|
/**
|
|
|
* 本地是否拥有标准模板
|
|
|
*/
|
|
|
- @Value("${hasStandardJrxmls}")
|
|
|
+ @DynamicValue("hasStandardJrxmls")
|
|
|
private boolean hasStandardJrxmls;
|
|
|
|
|
|
/**
|
|
|
* 标准模板地址
|
|
|
*/
|
|
|
- @Value("${standardJrxmlsUrl}")
|
|
|
+ @DynamicValue("standardJrxmlsUrl")
|
|
|
private String standardJrxmlsUrl;
|
|
|
|
|
|
/**
|
|
|
* 主账套与其子帐套是否共用模板
|
|
|
*/
|
|
|
- @Value("${shareJrxmlsWithSubMaster}")
|
|
|
+ @DynamicValue("shareJrxmlsWithSubMaster")
|
|
|
private boolean shareJrxmlsWithSubMaster;
|
|
|
|
|
|
/**
|
|
|
* 数据源配置信息
|
|
|
*/
|
|
|
- @Value("${datasource}")
|
|
|
+ @DynamicValue("datasource")
|
|
|
private String dataSourceInformation;
|
|
|
|
|
|
- public String getLocalBaseDir() {
|
|
|
+ public SpecialProperties() {
|
|
|
+ mayLoad();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载配置
|
|
|
+ */
|
|
|
+ private void mayLoad() {
|
|
|
+ try {
|
|
|
+ File propertyFile = new File(PROPERTY_FILE_NAME);
|
|
|
+ if (!propertyFile.exists()) {
|
|
|
+ // 复制类路径下默认的配置文件
|
|
|
+ File defaultPropertyFile = ResourceUtils.getFile(PROPERTY_FILE_NAME);
|
|
|
+ if (defaultPropertyFile == null) {
|
|
|
+ throw new FileNotFoundException("配置文件不存在:" + PROPERTY_FILE_NAME);
|
|
|
+ }
|
|
|
+ FileUtils.copy(defaultPropertyFile, propertyFile);
|
|
|
+ }
|
|
|
+ long lastModified = propertyFile.lastModified();
|
|
|
+ // 如果配置文件有修改,就重新加载
|
|
|
+ if (propertyFileLastModified >= lastModified) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("加载配置 " + PROPERTY_FILE_NAME + "...");
|
|
|
+ propertyFileLastModified = propertyFile.lastModified();
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.load(new FileInputStream(propertyFile));
|
|
|
+
|
|
|
+ // 通过反射注入配置
|
|
|
+ Field[] declaredFields = getClass().getDeclaredFields();
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ DynamicValue dynamicValue = declaredField.getAnnotation(DynamicValue.class);
|
|
|
+ if (dynamicValue == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String expression = dynamicValue.value();
|
|
|
+ if (StringUtils.isEmpty(expression)) {
|
|
|
+ throw new IllegalStateException("配置项的表达式为空:" + declaredField.getName() + "=" + expression);
|
|
|
+ }
|
|
|
+ Object value = properties.get(expression);
|
|
|
+ if (ObjectUtils.isEmpty(value)) {
|
|
|
+ throw new IllegalArgumentException("配置项的值为空:" + expression + "=" + value);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ObjectUtils.setValue(this, declaredField, value);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new IllegalStateException("通过反射注入配置失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IllegalStateException("配置加载失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新配置
|
|
|
+ *
|
|
|
+ * @param json json 格式的配置
|
|
|
+ */
|
|
|
+ public void update(String json) {
|
|
|
+ if (StringUtils.isEmpty(json)) {
|
|
|
+ throw new IllegalArgumentException("json 为空");
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ // 更新 json 中指定的配置项
|
|
|
+ Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
|
|
|
+ for (Map.Entry<String, Object> entry : entrySet) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Object value = entry.getValue();
|
|
|
+ if (ObjectUtils.isEmpty(value)) {
|
|
|
+ throw new IllegalArgumentException("参数值为空:" + entry);
|
|
|
+ }
|
|
|
+ Field field;
|
|
|
+ try {
|
|
|
+ field = getClass().getDeclaredField(key);
|
|
|
+ ObjectUtils.setValue(this, field, value);
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ throw new IllegalArgumentException("不支持配置项:" + key, e);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new IllegalStateException("通过反射修改配置失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ save();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存配置
|
|
|
+ */
|
|
|
+ private void save() {
|
|
|
+ try {
|
|
|
+ Properties properties = new Properties();
|
|
|
+ Field[] declaredFields = getClass().getDeclaredFields();
|
|
|
+ // 通过反射读取所有配置,写入本地文件
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ DynamicValue dynamicValue = declaredField.getAnnotation(DynamicValue.class);
|
|
|
+ if (dynamicValue == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String expression = dynamicValue.value();
|
|
|
+ if (StringUtils.isEmpty(expression)) {
|
|
|
+ throw new IllegalStateException("配置项的表达式为空:" + declaredField.getName() + "=" + expression);
|
|
|
+ }
|
|
|
+ Object value = ObjectUtils.getValue(declaredField, this);
|
|
|
+ if (ObjectUtils.isEmpty(value)) {
|
|
|
+ throw new IllegalArgumentException("配置项的值为空:" + expression + "=" + value);
|
|
|
+ }
|
|
|
+ properties.setProperty(expression, value.toString());
|
|
|
+ }
|
|
|
+ properties.store(new FileOutputStream(new File(PROPERTY_FILE_NAME)), "report properties");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IllegalStateException("配置保存失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getLocalBaseDir() {
|
|
|
+ mayLoad();
|
|
|
return localBaseDir;
|
|
|
}
|
|
|
|
|
|
- public void setLocalBaseDir(String localBaseDir) {
|
|
|
- this.localBaseDir = localBaseDir;
|
|
|
- }
|
|
|
-
|
|
|
public String getLocalImagesDir() {
|
|
|
+ mayLoad();
|
|
|
return localImagesDir;
|
|
|
}
|
|
|
|
|
|
- public void setLocalImagesDir(String localImagesDir) {
|
|
|
- this.localImagesDir = localImagesDir;
|
|
|
- }
|
|
|
-
|
|
|
public String getLocalJrxmlDir() {
|
|
|
+ mayLoad();
|
|
|
return localJrxmlDir;
|
|
|
}
|
|
|
|
|
|
- public void setLocalJrxmlDir(String localJrxmlDir) {
|
|
|
- this.localJrxmlDir = localJrxmlDir;
|
|
|
- }
|
|
|
-
|
|
|
public String getStandardMaster() {
|
|
|
+ mayLoad();
|
|
|
return standardMaster;
|
|
|
}
|
|
|
|
|
|
- public void setStandardMaster(String standardMaster) {
|
|
|
- this.standardMaster = standardMaster;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean hasStandardJrxmls() {
|
|
|
+ public boolean getHasStandardJrxmls() {
|
|
|
+ mayLoad();
|
|
|
return hasStandardJrxmls;
|
|
|
}
|
|
|
|
|
|
- public void setHasStandardJrxmls(boolean hasStandardJrxmls) {
|
|
|
- this.hasStandardJrxmls = hasStandardJrxmls;
|
|
|
- }
|
|
|
-
|
|
|
public String getStandardJrxmlsUrl() {
|
|
|
+ mayLoad();
|
|
|
return standardJrxmlsUrl;
|
|
|
}
|
|
|
|
|
|
- public void setStandardJrxmlsUrl(String standardJrxmlsUrl) {
|
|
|
- this.standardJrxmlsUrl = standardJrxmlsUrl;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean shareJrxmlsWithSubMaster() {
|
|
|
+ public boolean getShareJrxmlsWithSubMaster() {
|
|
|
+ mayLoad();
|
|
|
return shareJrxmlsWithSubMaster;
|
|
|
}
|
|
|
|
|
|
- public void setShareJrxmlsWithSubMaster(boolean shareJrxmlsWithSubMaster) {
|
|
|
- this.shareJrxmlsWithSubMaster = shareJrxmlsWithSubMaster;
|
|
|
- }
|
|
|
-
|
|
|
public String getDataSourceInformation() {
|
|
|
+ mayLoad();
|
|
|
return dataSourceInformation;
|
|
|
}
|
|
|
-
|
|
|
- public void setDataSourceInformation(String dataSourceInformation) {
|
|
|
- this.dataSourceInformation = dataSourceInformation;
|
|
|
- }
|
|
|
-
|
|
|
}
|