提交 901663ec 编写于 作者: sinat_25235033's avatar sinat_25235033

use cache when loading resource from document

上级 7eadc1bf
......@@ -2,14 +2,11 @@ package com.usthe.sureness.provider.ducument;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
* 资源文件里内容的操作类
......@@ -35,6 +32,16 @@ public class DocumentResourceAccess {
public static DocumentResourceEntity loadConfig() throws IOException {
Yaml yaml = new Yaml();
InputStream inputStream = DocumentResourceAccess.class.getClassLoader().getResourceAsStream(yamlFileName);
if (inputStream == null) {
File yamlFile = new File(yamlFileName);
if (yamlFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(yamlFile)) {
return yaml.loadAs(fileInputStream, DocumentResourceEntity.class);
} catch (IOException e) {
throw new IOException(e);
}
}
}
if (inputStream == null) {
throw new FileNotFoundException("sureness file: " + DEFAULT_FILE_NAME + " not found, " +
"please create the file if you need config resource");
......@@ -43,25 +50,10 @@ public class DocumentResourceAccess {
}
/**
* 将更新的信息写入到配置文件中
* @param entity 写入的内容实体
* @throws IOException 文件不存在或者读写异常时
* config file path name
* @param fileName 文件路径名称
*/
public static void dumpConfig(DocumentResourceEntity entity) throws IOException {
Yaml yaml = new Yaml();
URL url = DocumentResourceAccess.class.getClassLoader().getResource(yamlFileName);
if (url == null) {
throw new FileNotFoundException("sureness file: " + DEFAULT_FILE_NAME + " not found, " +
"please create the file if you need config resource");
}
try (Writer writer = new OutputStreamWriter(new FileOutputStream(url.getPath()), StandardCharsets.UTF_8)) {
yaml.dump(entity, writer);
} catch (IOException e) {
throw new IOException(e);
}
}
public static void setYamlName(String name) {
yamlFileName = name;
public static void setYamlName(String fileName) {
yamlFileName = fileName;
}
}
......@@ -16,6 +16,7 @@ import java.util.Optional;
import java.util.Set;
/**
* provide resource and account data loading from yaml document
* @author tomsun28
* @date 22:40 2019-05-24
*/
......@@ -36,7 +37,9 @@ public class DocumentResourceDefaultProvider implements PathTreeProvider, Surene
@Override
public Set<String> providePathData() {
try {
DocumentResourceEntity entity = DocumentResourceAccess.loadConfig();
if (entity == null) {
entity = DocumentResourceAccess.loadConfig();
}
List<String> resource = entity.getResourceRole();
if (resource != null) {
return new HashSet<>(entity.getResourceRole());
......@@ -51,7 +54,9 @@ public class DocumentResourceDefaultProvider implements PathTreeProvider, Surene
@Override
public Set<String> provideExcludedResource() {
try {
DocumentResourceEntity entity = DocumentResourceAccess.loadConfig();
if (entity == null) {
entity = DocumentResourceAccess.loadConfig();
}
List<String> resource = entity.getExcludedResource();
if (resource != null) {
return new HashSet<>(resource);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册