mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 03:26:59 +00:00
fix[storage]: fixed the issue that when packaged as a jar, the resource could not get the absolute path
This commit is contained in:
@@ -172,6 +172,8 @@ See the License for the specific language governing permissions and limitations
|
||||
- maven代理设置(非全局),-DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=10809
|
||||

|
||||
|
||||
- 设置自动下载源代码,将上图的sources,documentation,annotations勾上
|
||||
|
||||
## 6 使用Idea默认的快捷键设置
|
||||
|
||||
- eclipse快捷键设置,个人习惯
|
||||
|
||||
@@ -15,10 +15,7 @@ package com.zfoo.protocol.util;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -99,32 +96,10 @@ public abstract class StringUtils {
|
||||
*/
|
||||
public static final String ENGLISH_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
public static final Set<Character> ENGLISH_SET = Set.of(
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
||||
);
|
||||
public static final Set<Character> ENGLISH_SET = Set.of('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
||||
|
||||
|
||||
public static final Set<Character> STOP_WORD = Set.of(
|
||||
' ', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '/', '[', '{', ']', '}'
|
||||
, '\\', '|', ';', ':', '\'', '"', ',', '<', '.', '>', '?',
|
||||
'·', '!', '¥', ',', '。', '、', 'ˇ', '#', '%', '&', '(', ')', '.', '/', ':', ';', '<', '=', '>', '?', '〔', '〕', '\', '_', '{', '|', '}',
|
||||
'—', '~', '‖', '…', '‘', '’', '“', '”', '〈', '〉', '《', '》', '「', '」', '『',
|
||||
'』', '〖', '〗', '【', '】', '±', '+', '-', '×', '÷', '∧', '∨', '∏', '∪', '∩', '∈', '√', '⊥', '⊙', '∫',
|
||||
'∮', '≡', '≌', '≈', '∽', '∝', '≠', '≮', '≯', '≤', '≥', '∞', '∶', '∵', '∴', '∷', '♂', '♀', '°', '′', '〃',
|
||||
'℃', '$', '¤', '¢', '£', '‰', '§', '☆', '★', '〇', '○', '●', '◎', '◇', '◆', '□', '■', '△', '▽', '⊿', '▲',
|
||||
'▼', '◣', '◤', '◢', '◥', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▉', '▊', '▋', '▌', '▍', '▎', '▏', '▓',
|
||||
'※', '→', '←', '↑', '↓', '↖', '↗', '↘', '↙', '〓',
|
||||
'①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩',
|
||||
'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖', '⒗', '⒘', '⒙', '⒚', '⒛', '⑴', '⑵', '⑶', '⑷', '⑸', '⑹', '⑺', '⑻', '⑼', '⑽', '⑾', '⑿', '⒀', '⒁', '⒂', '⒃', '⒄', '⒅', '⒆', '⒇',
|
||||
'Ⅰ', 'Ⅱ', 'Ⅲ', 'Ⅳ', 'Ⅴ', 'Ⅵ', 'Ⅶ', 'Ⅷ', 'Ⅸ', 'Ⅹ', 'Ⅺ', 'Ⅻ',
|
||||
'ⅰ', 'ⅱ', 'ⅲ', 'ⅳ', 'ⅴ', 'ⅵ', 'ⅶ', 'ⅷ', 'ⅸ', 'ⅹ',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'Ρ', '∑', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π',
|
||||
'ρ', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', '^', '﹊', '﹍', '╭', '╮', '╰', '╯',
|
||||
'々', '', '@', '*', '卐', '㎎', '㎏', '㎜', '㎝', '㎞', '㎡', '㏄', '㏎', '㏑', '㏒', '㏕',
|
||||
'´', '﹏');
|
||||
public static final Set<Character> STOP_WORD = Set.of(' ', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '/', '[', '{', ']', '}', '\\', '|', ';', ':', '\'', '"', ',', '<', '.', '>', '?', '·', '!', '¥', ',', '。', '、', 'ˇ', '#', '%', '&', '(', ')', '.', '/', ':', ';', '<', '=', '>', '?', '〔', '〕', '\', '_', '{', '|', '}', '—', '~', '‖', '…', '‘', '’', '“', '”', '〈', '〉', '《', '》', '「', '」', '『', '』', '〖', '〗', '【', '】', '±', '+', '-', '×', '÷', '∧', '∨', '∏', '∪', '∩', '∈', '√', '⊥', '⊙', '∫', '∮', '≡', '≌', '≈', '∽', '∝', '≠', '≮', '≯', '≤', '≥', '∞', '∶', '∵', '∴', '∷', '♂', '♀', '°', '′', '〃', '℃', '$', '¤', '¢', '£', '‰', '§', '☆', '★', '〇', '○', '●', '◎', '◇', '◆', '□', '■', '△', '▽', '⊿', '▲', '▼', '◣', '◤', '◢', '◥', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▉', '▊', '▋', '▌', '▍', '▎', '▏', '▓', '※', '→', '←', '↑', '↓', '↖', '↗', '↘', '↙', '〓', '①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩', '⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖', '⒗', '⒘', '⒙', '⒚', '⒛', '⑴', '⑵', '⑶', '⑷', '⑸', '⑹', '⑺', '⑻', '⑼', '⑽', '⑾', '⑿', '⒀', '⒁', '⒂', '⒃', '⒄', '⒅', '⒆', '⒇', 'Ⅰ', 'Ⅱ', 'Ⅲ', 'Ⅳ', 'Ⅴ', 'Ⅵ', 'Ⅶ', 'Ⅷ', 'Ⅸ', 'Ⅹ', 'Ⅺ', 'Ⅻ', 'ⅰ', 'ⅱ', 'ⅲ', 'ⅳ', 'ⅴ', 'ⅵ', 'ⅶ', 'ⅷ', 'ⅸ', 'ⅹ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Ρ', '∑', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ρ', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', '^', '﹊', '﹍', '╭', '╮', '╰', '╯', '々', '', '@', '*', '卐', '㎎', '㎏', '㎜', '㎝', '㎞', '㎡', '㏄', '㏎', '㏑', '㏒', '㏕', '´', '﹏');
|
||||
|
||||
|
||||
// Empty checks
|
||||
@@ -428,6 +403,28 @@ public abstract class StringUtils {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize the given String into a String array via a StringTokenizer.
|
||||
*/
|
||||
public static String[] tokenize(String str, String delimiters) {
|
||||
if (isEmpty(str)) {
|
||||
return EMPTY_ARRAY;
|
||||
}
|
||||
if (isEmpty(delimiters)) {
|
||||
return new String[]{str};
|
||||
}
|
||||
var st = new StringTokenizer(str, delimiters);
|
||||
var tokens = new ArrayList<String>();
|
||||
while (st.hasMoreTokens()) {
|
||||
var token = trim(st.nextToken());
|
||||
if (isBlank(token)) {
|
||||
continue;
|
||||
}
|
||||
tokens.add(token);
|
||||
}
|
||||
return ArrayUtils.listToArray(tokens, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化字符串
|
||||
* 此方法只是简单将占位符 {} 按照顺序替换为参数
|
||||
|
||||
@@ -27,12 +27,14 @@ import com.zfoo.storage.model.config.StorageConfig;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
import com.zfoo.storage.model.vo.ResourceDef;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.type.ClassMetadata;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -76,7 +78,7 @@ public class StorageManager implements IStorageManager {
|
||||
var resourceDefinitionMap = new HashMap<Class<?>, ResourceDef>();
|
||||
|
||||
// 扫描Excel的class类文件
|
||||
var clazzNameSet = scanResourceAnno(storageConfig.getScanPackages());
|
||||
var clazzNameSet = scanResourceAnno(StringUtils.tokenize(storageConfig.getScanPackage(), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
|
||||
|
||||
// 通过class类文件扫描excel文件地址
|
||||
for (var clazzName : clazzNameSet) {
|
||||
@@ -84,13 +86,15 @@ public class StorageManager implements IStorageManager {
|
||||
try {
|
||||
resourceClazz = Class.forName(clazzName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(StringUtils.format("无法获取资源类[{}]", clazzName));
|
||||
// 无法获取资源类
|
||||
throw new RuntimeException(StringUtils.format("Unable to get resource [class:{}]", clazzName));
|
||||
}
|
||||
|
||||
var resourceFile = scanResourceFile(resourceClazz);
|
||||
ResourceDef resourceDef = new ResourceDef(resourceClazz, resourceFile);
|
||||
if (resourceDefinitionMap.containsKey(resourceClazz)) {
|
||||
throw new RuntimeException(StringUtils.format("类的资源定义[{}]已经存在[{}]", resourceClazz, resourceDef));
|
||||
// 类的资源定义已经存在
|
||||
throw new RuntimeException(StringUtils.format("The resource definition of the class [{}] already exists [{}]", resourceClazz, resourceDef));
|
||||
}
|
||||
resourceDefinitionMap.put(resourceClazz, resourceDef);
|
||||
}
|
||||
@@ -103,7 +107,8 @@ public class StorageManager implements IStorageManager {
|
||||
var fieldList = ReflectionUtils.notStaticAndTransientFields(clazz);
|
||||
for (var field : fieldList) {
|
||||
if (Modifier.isPublic(field.getModifiers())) {
|
||||
throw new RunException("因为静态资源类是不能被修改的,资源类[class:{}]的属性[filed:{}]不能被public修饰,用private修饰或者开启配置writeable属性", clazz, field.getName());
|
||||
// 因为静态资源类是不能被修改的,资源类的属性不能被public修饰,用private修饰或者开启配置writeable属性
|
||||
throw new RunException("Static resource classes cannot be modified, [class:{}][filed:{}] cannot be modified by public, use private modified or enable writeable configuration", clazz, field.getName());
|
||||
}
|
||||
|
||||
var setMethodName = StringUtils.EMPTY;
|
||||
@@ -113,7 +118,8 @@ public class StorageManager implements IStorageManager {
|
||||
// 没有setMethod是正确的
|
||||
}
|
||||
if (StringUtils.isNotBlank(setMethodName)) {
|
||||
throw new RunException("因为静态资源类是不能被修改的,资源类[class:{}]的属性[filed:{}]不能含有set方法[{}],删除set方法或者开启配置writeable属性", clazz, field.getName(), setMethodName);
|
||||
// 因为静态资源类是不能被修改的,资源类的属性不能含有set方法[{}],删除set方法或者开启配置writeable属性
|
||||
throw new RunException("Static resource classes cannot be modified, [class:{}][filed:{}] cannot contain set method [{}], delete set method or enable writeable configuration", clazz, field.getName(), setMethodName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,57 +147,50 @@ public class StorageManager implements IStorageManager {
|
||||
for (var beanName : beanNames) {
|
||||
var bean = applicationContext.getBean(beanName);
|
||||
|
||||
ReflectionUtils.filterFieldsInClass(bean.getClass()
|
||||
, field -> field.isAnnotationPresent(ResInjection.class)
|
||||
, field -> {
|
||||
Type type = field.getGenericType();
|
||||
ReflectionUtils.filterFieldsInClass(bean.getClass(), field -> field.isAnnotationPresent(ResInjection.class), field -> {
|
||||
Type type = field.getGenericType();
|
||||
|
||||
if (!(type instanceof ParameterizedType)) {
|
||||
throw new RuntimeException(StringUtils.format("[bean:{}]类型声明不正确,不是泛型类", bean.getClass().getSimpleName()));
|
||||
}
|
||||
if (!(type instanceof ParameterizedType)) {
|
||||
throw new RuntimeException(StringUtils.format("[bean:{}] type declaration is incorrect, not a generic class", bean.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
|
||||
|
||||
// @ResInjection
|
||||
// Storage<Integer, ActivityResource> resources;
|
||||
Class<?> keyClazz = (Class<?>) types[0];
|
||||
// @ResInjection
|
||||
// Storage<Integer, ActivityResource> resources;
|
||||
Class<?> keyClazz = (Class<?>) types[0];
|
||||
|
||||
Class<?> resourceClazz = (Class<?>) types[1];
|
||||
Class<?> resourceClazz = (Class<?>) types[1];
|
||||
|
||||
Storage<?, ?> storage = storageMap.get(resourceClazz);
|
||||
Storage<?, ?> storage = storageMap.get(resourceClazz);
|
||||
|
||||
if (storage == null) {
|
||||
throw new RuntimeException(StringUtils.format("静态类资源[resource:{}]不存在", resourceClazz.getSimpleName()));
|
||||
}
|
||||
if (storage == null) {
|
||||
throw new RuntimeException(StringUtils.format("Static class [resource:{}] does not exist", resourceClazz.getSimpleName()));
|
||||
}
|
||||
|
||||
Field[] idFields = ReflectionUtils.getFieldsByAnnoInPOJOClass(resourceClazz, Id.class);
|
||||
if (idFields.length != 1) {
|
||||
throw new RuntimeException(StringUtils.format("静态类资源[resource:{}]配置没有注解id", resourceClazz.getSimpleName()));
|
||||
}
|
||||
Field[] idFields = ReflectionUtils.getFieldsByAnnoInPOJOClass(resourceClazz, Id.class);
|
||||
if (idFields.length != 1) {
|
||||
throw new RuntimeException(StringUtils.format("Static class [resource:{}] has no @Id annotation", resourceClazz.getSimpleName()));
|
||||
}
|
||||
|
||||
if (!keyClazz.getSimpleName().toLowerCase().contains(idFields[0].getType().getSimpleName().toLowerCase())) {
|
||||
throw new RuntimeException(StringUtils.format("注入静态类配置资源[storage:{}]的[key:{}]类型和泛型类型[type:{}]不匹配"
|
||||
, resourceClazz.getSimpleName(), idFields[0].getType().getSimpleName(), keyClazz.getSimpleName()));
|
||||
}
|
||||
if (!keyClazz.getSimpleName().toLowerCase().contains(idFields[0].getType().getSimpleName().toLowerCase())) {
|
||||
// 注入静态类配置资源的类型和泛型类型不匹配
|
||||
throw new RuntimeException(StringUtils.format("Inject static class configuration [storage:{}][key:{}] type and generic type [type:{}] do not match", resourceClazz.getSimpleName(), idFields[0].getType().getSimpleName(), keyClazz.getSimpleName()));
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
ReflectionUtils.setField(field, bean, storage);
|
||||
storage.setRecycle(false);
|
||||
});
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
ReflectionUtils.setField(field, bean, storage);
|
||||
storage.setRecycle(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initAfter() {
|
||||
if (storageConfig.isRecycle()) {
|
||||
storageMap.entrySet().stream()
|
||||
.filter(it -> it.getValue().isRecycle())
|
||||
.map(it -> it.getValue())
|
||||
.forEach(it -> it.recycleStorage());
|
||||
storageMap.entrySet().stream().filter(it -> it.getValue().isRecycle()).map(it -> it.getValue()).forEach(it -> it.recycleStorage());
|
||||
} else {
|
||||
storageMap.entrySet().stream()
|
||||
.map(it -> it.getValue())
|
||||
.forEach(it -> it.setRecycle(false));
|
||||
storageMap.entrySet().stream().map(it -> it.getValue()).forEach(it -> it.setRecycle(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,10 +198,11 @@ public class StorageManager implements IStorageManager {
|
||||
public Storage<?, ?> getStorage(Class<?> clazz) {
|
||||
var storage = storageMap.get(clazz);
|
||||
if (storage == null) {
|
||||
throw new RunException("没有定义[{}]的Storage,无法获取", clazz.getCanonicalName());
|
||||
throw new RunException("There is no [{}] defined Storage and unable to get it", clazz.getCanonicalName());
|
||||
}
|
||||
if (storage.isRecycle()) {
|
||||
throw new RunException("Storage没有使用[{}],为了节省内存提前释放了它;只有使用ResInjection注解的Storage才能被动态获取或者关闭配置recycle属性", clazz.getCanonicalName());
|
||||
// Storage没有使用,为了节省内存提前释放了它;只有使用ResInjection注解的Storage才能被动态获取或者关闭配置recycle属性
|
||||
throw new RunException("Storage [{}] is not used, it was freed to save memory; use @ResInjection or turn off recycle configuration", clazz.getCanonicalName());
|
||||
}
|
||||
return storage;
|
||||
}
|
||||
@@ -228,7 +228,7 @@ public class StorageManager implements IStorageManager {
|
||||
|
||||
try {
|
||||
var result = new HashSet<String>();
|
||||
for(var scanPackage:scanPackages) {
|
||||
for (var scanPackage : scanPackages) {
|
||||
var packageSearchPath = ResourceUtils.CLASSPATH_URL_PREFIX + scanPackage.replace(StringUtils.PERIOD, StringUtils.SLASH) + StringUtils.SLASH + SUFFIX_PATTERN;
|
||||
var resources = resourcePatternResolver.getResources(packageSearchPath);
|
||||
var resourceName = com.zfoo.storage.model.anno.Resource.class.getName();
|
||||
@@ -245,7 +245,7 @@ public class StorageManager implements IStorageManager {
|
||||
}
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("无法读取资源信息:" + e);
|
||||
throw new RuntimeException("Unable to read resource information", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,55 +254,35 @@ public class StorageManager implements IStorageManager {
|
||||
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
|
||||
|
||||
try {
|
||||
var resourceDefSet = new HashSet<ResourceDef>();
|
||||
for(var resourceLocation: storageConfig.getResourceLocations()) {
|
||||
// 一个class类只能匹配一个资源文件,如果匹配多个则会有歧义
|
||||
var resourceSet = new HashSet<Resource>();
|
||||
var resourceLocations = StringUtils.tokenize(storageConfig.getResourceLocation(), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
|
||||
for (var resourceLocation : resourceLocations) {
|
||||
var packageSearchPath = StringUtils.format("{}/**/{}.*", resourceLocation, clazz.getSimpleName());
|
||||
packageSearchPath = packageSearchPath.replaceAll("//", "/");
|
||||
try {
|
||||
for(var resource :resourcePatternResolver.getResources(packageSearchPath)){
|
||||
var resourceFilename=resource.getFilename();
|
||||
if(resourceFilename.endsWith(ResourceEnum.CSV.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLS.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLSX.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.JSON.getType())){
|
||||
resourceDefSet.add(new ResourceDef(clazz,resource));
|
||||
}
|
||||
|
||||
}
|
||||
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resourceSet.add(it));
|
||||
} catch (Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// 通配符无法匹配根目录,所以如果找不到,再从根目录查找一遍
|
||||
if (resourceDefSet.isEmpty()) {
|
||||
if (resourceSet.isEmpty()) {
|
||||
packageSearchPath = StringUtils.format("{}/{}.*", resourceLocation, clazz.getSimpleName());
|
||||
packageSearchPath = packageSearchPath.replaceAll("//", "/");
|
||||
for(var resource :resourcePatternResolver.getResources(packageSearchPath)){
|
||||
var resourceFilename=resource.getFilename();
|
||||
if(resourceFilename.endsWith(ResourceEnum.CSV.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLS.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLSX.getType())
|
||||
|| resourceFilename.endsWith(ResourceEnum.JSON.getType())){
|
||||
resourceDefSet.add(new ResourceDef(clazz,resource));
|
||||
}
|
||||
}
|
||||
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resourceSet.add(it));
|
||||
}
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(resourceDefSet)) {
|
||||
throw new RuntimeException(StringUtils.format("资源类[class:{}]无法找到配置文件", clazz.getSimpleName()));
|
||||
} else if (resourceDefSet.size() > 1) {
|
||||
var resourceNames=new String[resourceDefSet.size()];
|
||||
var index=0;
|
||||
for(var resourceDef:resourceDefSet){
|
||||
resourceNames[index++]=StringUtils.format("[{}]{}",index,resourceDef.getResource().getFile().getAbsolutePath());
|
||||
}
|
||||
StringJoiner stringJoiner=new StringJoiner(",","[","]");
|
||||
Arrays.stream(resourceNames).forEach(stringJoiner::add);
|
||||
throw new RuntimeException(StringUtils.format("资源类[class:{}]找到重复的配置文件{}", clazz.getSimpleName(),stringJoiner.toString()));
|
||||
} else {
|
||||
return ((ResourceDef)(resourceDefSet.toArray()[0])).getResource();
|
||||
if (CollectionUtils.isEmpty(resourceSet)) {
|
||||
throw new FileNotFoundException(clazz.getSimpleName());
|
||||
}
|
||||
if (resourceSet.size() > 1) {
|
||||
var resourceNames = resourceSet.stream().map(it -> it.getFilename()).collect(Collectors.joining(StringUtils.COMMA));
|
||||
throw new RuntimeException(StringUtils.format("Resource [class:{}] has duplicate configuration [{}]", clazz.getSimpleName(), resourceNames));
|
||||
}
|
||||
|
||||
return resourceSet.stream().findFirst().get();
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(ExceptionUtils.getMessage(e));
|
||||
|
||||
@@ -21,9 +21,9 @@ public class StorageConfig {
|
||||
|
||||
private String id;
|
||||
|
||||
private String[] scanPackages;
|
||||
private String scanPackage;
|
||||
|
||||
private String[] resourceLocations;
|
||||
private String resourceLocation;
|
||||
|
||||
// 类的属性是否可写,如果为false则类的属性必须为private并且不能有set方法
|
||||
private boolean writeable;
|
||||
@@ -39,20 +39,20 @@ public class StorageConfig {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String[] getScanPackages() {
|
||||
return scanPackages;
|
||||
public String getScanPackage() {
|
||||
return scanPackage;
|
||||
}
|
||||
|
||||
public void setScanPackages(String[] scanPackages) {
|
||||
this.scanPackages = scanPackages;
|
||||
public void setScanPackage(String scanPackage) {
|
||||
this.scanPackage = scanPackage;
|
||||
}
|
||||
|
||||
public String[] getResourceLocations() {
|
||||
return resourceLocations;
|
||||
public String getResourceLocation() {
|
||||
return resourceLocation;
|
||||
}
|
||||
|
||||
public void setResourceLocations(String[] resourceLocations) {
|
||||
this.resourceLocations = resourceLocations;
|
||||
public void setResourceLocation(String resourceLocation) {
|
||||
this.resourceLocation = resourceLocation;
|
||||
}
|
||||
|
||||
public boolean isWriteable() {
|
||||
|
||||
@@ -15,9 +15,6 @@ package com.zfoo.storage.model.vo;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 4.0
|
||||
@@ -41,28 +38,4 @@ public class ResourceDef {
|
||||
return resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ResourceDef that = (ResourceDef) o;
|
||||
boolean equal= false;
|
||||
try {
|
||||
equal = this.resource.getFile().getAbsolutePath().equals(that.resource.getFile().getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return equal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hashCode=0;
|
||||
try {
|
||||
hashCode=this.resource.getFile().getAbsolutePath().hashCode();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,10 +66,10 @@ public class StorageDefinitionParser implements BeanDefinitionParser {
|
||||
}
|
||||
|
||||
resolvePlaceholder("id", "id", builder, element, parserContext);
|
||||
resolvePlaceholder("package", "scanPackages", builder, scanElement, parserContext);
|
||||
resolvePlaceholder("package", "scanPackage", builder, scanElement, parserContext);
|
||||
resolvePlaceholder("writeable", "writeable", builder, scanElement, parserContext);
|
||||
resolvePlaceholder("recycle", "recycle", builder, scanElement, parserContext);
|
||||
resolvePlaceholder("location", "resourceLocations", builder, resourceElement, parserContext);
|
||||
resolvePlaceholder("location", "resourceLocation", builder, resourceElement, parserContext);
|
||||
|
||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||
}
|
||||
@@ -90,8 +90,6 @@ public class StorageDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
private void resolvePlaceholder(String attributeName, String fieldName, BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
|
||||
var attributeValue = element.getAttribute(attributeName);
|
||||
attributeValue=attributeValue.replaceAll(";",",");
|
||||
attributeValue=attributeValue.replaceAll(" ",",");
|
||||
var environment = parserContext.getReaderContext().getEnvironment();
|
||||
var placeholder = environment.resolvePlaceholders(attributeValue);
|
||||
builder.addPropertyValue(fieldName, placeholder);
|
||||
|
||||
@@ -78,8 +78,8 @@ public class ExportBinaryTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
var config = new StorageConfig();
|
||||
config.setScanPackages(new String[]{"com.zfoo.storage.export"});
|
||||
config.setResourceLocations(new String[]{"classpath:/excel"});
|
||||
config.setScanPackage("com.zfoo.storage.export");
|
||||
config.setResourceLocation("classpath:/excel");
|
||||
config.setWriteable(true);
|
||||
config.setRecycle(false);
|
||||
var storageManager = new StorageManager();
|
||||
|
||||
Reference in New Issue
Block a user