feat: 新增文件附件接口、调整代码格式.
This commit is contained in:
@@ -38,8 +38,8 @@ public class PropertiesConfiguration extends FileConfiguration {
|
||||
load();
|
||||
} catch (IOException e) {
|
||||
log.error("配置文件读取出现异常 => {}", e.toString());
|
||||
}finally {
|
||||
if(inputStream != null){
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
|
||||
43
src/main/java/com/serliunx/ddns/core/Attachment.java
Normal file
43
src/main/java/com/serliunx/ddns/core/Attachment.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.serliunx.ddns.core;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 附件定义
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/8/6
|
||||
*/
|
||||
public interface Attachment<E> {
|
||||
|
||||
/**
|
||||
* 是否存在指定附件
|
||||
*
|
||||
* @param e 附件信息
|
||||
* @return 存在返回真, 否则返回假
|
||||
*/
|
||||
boolean exists(E e);
|
||||
|
||||
/**
|
||||
* 分离指定附件
|
||||
*
|
||||
* @param e 附件
|
||||
* @return 成功分离返回真, 否则返回假(不存在时返回假)
|
||||
*/
|
||||
boolean detach(E e);
|
||||
|
||||
/**
|
||||
* 添加一个附件
|
||||
*
|
||||
* @param e 附件
|
||||
*/
|
||||
void attach(E e);
|
||||
|
||||
/**
|
||||
* 获取所有附件对象
|
||||
*
|
||||
* @return 所有附件对象
|
||||
*/
|
||||
Collection<E> getAttachments();
|
||||
}
|
||||
30
src/main/java/com/serliunx/ddns/core/FileAttachment.java
Normal file
30
src/main/java/com/serliunx/ddns/core/FileAttachment.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.serliunx.ddns.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 文件类型的附件
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/8/6
|
||||
*/
|
||||
public interface FileAttachment extends Attachment<File> {
|
||||
|
||||
/**
|
||||
* 检查所有文件附件是否都是文件夹
|
||||
*/
|
||||
default boolean isAllDirectories() {
|
||||
Collection<File> files = getAttachments();
|
||||
if (files == null || files.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (File file : files) {
|
||||
if (!file.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -52,8 +52,10 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
|
||||
public void refresh() {
|
||||
try {
|
||||
instanceLock.lock();
|
||||
if (listableInstanceFactories.isEmpty())
|
||||
if (listableInstanceFactories.isEmpty()) {
|
||||
log.error("启动失败, 未找到任何有效的实例工厂!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化所有实例工厂
|
||||
listableInstanceFactories.stream()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.serliunx.ddns.core.factory;
|
||||
|
||||
import com.serliunx.ddns.core.FileAttachment;
|
||||
import com.serliunx.ddns.core.InstanceFileFilter;
|
||||
import com.serliunx.ddns.core.instance.Instance;
|
||||
|
||||
@@ -9,23 +10,47 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 文件相关实例工厂, 定义所有来源为文件的实例工厂通用逻辑
|
||||
*
|
||||
* @see JacksonFileInstanceFactory 使用Jackson序列化、反序列化的实例
|
||||
* @see YamlFileInstanceFactory 使用SankeYaml序列化、反序列化的实例
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/5/15
|
||||
*/
|
||||
public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
||||
public abstract class FileInstanceFactory extends AbstractInstanceFactory implements FileAttachment {
|
||||
|
||||
/**
|
||||
* 存储实例信息的文件夹路径
|
||||
*/
|
||||
protected String instanceDir;
|
||||
protected Set<File> filesAttachments;
|
||||
|
||||
public FileInstanceFactory(String instanceDir) {
|
||||
this.instanceDir = instanceDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(File file) {
|
||||
return filesAttachments.contains(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean detach(File file) {
|
||||
if (!exists(file))
|
||||
return false;
|
||||
return filesAttachments.remove(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attach(File file) {
|
||||
filesAttachments.add(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<File> getAttachments() {
|
||||
return filesAttachments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 256;
|
||||
@@ -39,6 +64,9 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
||||
@Override
|
||||
protected Set<Instance> load() {
|
||||
Set<File> files = loadFiles();
|
||||
|
||||
filesAttachments = files;
|
||||
|
||||
if (files != null && !files.isEmpty()) {
|
||||
return files.stream()
|
||||
.map(this::loadInstance)
|
||||
|
||||
@@ -11,12 +11,12 @@ import com.serliunx.ddns.support.Assert;
|
||||
*/
|
||||
public final class InstanceUtils {
|
||||
|
||||
private InstanceUtils(){throw new UnsupportedOperationException();}
|
||||
private InstanceUtils() {throw new UnsupportedOperationException();}
|
||||
|
||||
public static void validateInstance(Instance instance) {
|
||||
Assert.notNull(instance);
|
||||
String instanceName = instance.getName();
|
||||
if(instanceName == null || instanceName.isEmpty()){
|
||||
if (instanceName == null || instanceName.isEmpty()) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.List;
|
||||
*/
|
||||
public final class ReflectionUtils {
|
||||
|
||||
private ReflectionUtils(){throw new UnsupportedOperationException();}
|
||||
private ReflectionUtils() {throw new UnsupportedOperationException();}
|
||||
|
||||
/**
|
||||
* 获取当前类声明的所有字段
|
||||
@@ -22,18 +22,18 @@ public final class ReflectionUtils {
|
||||
* @return 字段列表
|
||||
*/
|
||||
public static Field[] getDeclaredFields(Class<?> clazz, boolean setAccessible) {
|
||||
if(clazz == null){
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
Field[] declaredFields = clazz.getDeclaredFields();
|
||||
Field[] declaredFieldsInSuper = getDeclaredFields(clazz.getSuperclass(), setAccessible);
|
||||
if(declaredFieldsInSuper != null){
|
||||
if (declaredFieldsInSuper != null) {
|
||||
Field[] newFields = new Field[declaredFields.length + declaredFieldsInSuper.length];
|
||||
System.arraycopy(declaredFields, 0, newFields, 0, declaredFields.length);
|
||||
System.arraycopy(declaredFieldsInSuper, 0, newFields, declaredFields.length, declaredFieldsInSuper.length);
|
||||
declaredFields = newFields;
|
||||
}
|
||||
if(setAccessible){
|
||||
if (setAccessible) {
|
||||
for (Field declaredField : declaredFields) {
|
||||
declaredField.setAccessible(true);
|
||||
}
|
||||
@@ -64,9 +64,9 @@ public final class ReflectionUtils {
|
||||
List<Field> srcField = getDeclaredFieldList(srcClass, true);
|
||||
List<Field> destField = getDeclaredFieldList(destClass, true);
|
||||
for (Field field : destField) {
|
||||
if(onlyNull){
|
||||
if (onlyNull) {
|
||||
try {
|
||||
if(field.get(dest) != null){
|
||||
if (field.get(dest) != null) {
|
||||
continue;
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
@@ -74,10 +74,10 @@ public final class ReflectionUtils {
|
||||
}
|
||||
}
|
||||
for (Field sf : srcField) {
|
||||
if(sf.getName().equals(field.getName())){
|
||||
if (sf.getName().equals(field.getName())) {
|
||||
try {
|
||||
field.set(dest, sf.get(src));
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user