mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-22 02:25:44 +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:
@@ -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