feat: 新增文件附件接口、调整代码格式.
This commit is contained in:
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() {
|
public void refresh() {
|
||||||
try {
|
try {
|
||||||
instanceLock.lock();
|
instanceLock.lock();
|
||||||
if (listableInstanceFactories.isEmpty())
|
if (listableInstanceFactories.isEmpty()) {
|
||||||
|
log.error("启动失败, 未找到任何有效的实例工厂!");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化所有实例工厂
|
// 初始化所有实例工厂
|
||||||
listableInstanceFactories.stream()
|
listableInstanceFactories.stream()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.serliunx.ddns.core.factory;
|
package com.serliunx.ddns.core.factory;
|
||||||
|
|
||||||
|
import com.serliunx.ddns.core.FileAttachment;
|
||||||
import com.serliunx.ddns.core.InstanceFileFilter;
|
import com.serliunx.ddns.core.InstanceFileFilter;
|
||||||
import com.serliunx.ddns.core.instance.Instance;
|
import com.serliunx.ddns.core.instance.Instance;
|
||||||
|
|
||||||
@@ -9,23 +10,47 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件相关实例工厂, 定义所有来源为文件的实例工厂通用逻辑
|
* 文件相关实例工厂, 定义所有来源为文件的实例工厂通用逻辑
|
||||||
|
*
|
||||||
* @see JacksonFileInstanceFactory 使用Jackson序列化、反序列化的实例
|
* @see JacksonFileInstanceFactory 使用Jackson序列化、反序列化的实例
|
||||||
* @see YamlFileInstanceFactory 使用SankeYaml序列化、反序列化的实例
|
* @see YamlFileInstanceFactory 使用SankeYaml序列化、反序列化的实例
|
||||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
* @since 2024/5/15
|
* @since 2024/5/15
|
||||||
*/
|
*/
|
||||||
public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
public abstract class FileInstanceFactory extends AbstractInstanceFactory implements FileAttachment {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储实例信息的文件夹路径
|
* 存储实例信息的文件夹路径
|
||||||
*/
|
*/
|
||||||
protected String instanceDir;
|
protected String instanceDir;
|
||||||
|
protected Set<File> filesAttachments;
|
||||||
|
|
||||||
public FileInstanceFactory(String instanceDir) {
|
public FileInstanceFactory(String instanceDir) {
|
||||||
this.instanceDir = 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
|
@Override
|
||||||
public int getPriority() {
|
public int getPriority() {
|
||||||
return 256;
|
return 256;
|
||||||
@@ -39,6 +64,9 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
|||||||
@Override
|
@Override
|
||||||
protected Set<Instance> load() {
|
protected Set<Instance> load() {
|
||||||
Set<File> files = loadFiles();
|
Set<File> files = loadFiles();
|
||||||
|
|
||||||
|
filesAttachments = files;
|
||||||
|
|
||||||
if (files != null && !files.isEmpty()) {
|
if (files != null && !files.isEmpty()) {
|
||||||
return files.stream()
|
return files.stream()
|
||||||
.map(this::loadInstance)
|
.map(this::loadInstance)
|
||||||
|
|||||||
29
src/test/java/com/serliunx/ddns/test/AttachmentTest.java
Normal file
29
src/test/java/com/serliunx/ddns/test/AttachmentTest.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.serliunx.ddns.test;
|
||||||
|
|
||||||
|
import com.serliunx.ddns.constant.SystemConstants;
|
||||||
|
import com.serliunx.ddns.core.Attachment;
|
||||||
|
import com.serliunx.ddns.core.FileAttachment;
|
||||||
|
import com.serliunx.ddns.core.factory.FileInstanceFactory;
|
||||||
|
import com.serliunx.ddns.core.factory.JsonFileInstanceFactory;
|
||||||
|
import com.serliunx.ddns.core.factory.YamlFileInstanceFactory;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
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 class AttachmentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAttachment() {
|
||||||
|
FileInstanceFactory fileInstanceFactory = new YamlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR);
|
||||||
|
fileInstanceFactory.refresh();
|
||||||
|
|
||||||
|
Collection<File> files = fileInstanceFactory.getAttachments();
|
||||||
|
files.forEach(f -> System.out.println(f.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,9 +22,9 @@ public class ContextTest {
|
|||||||
public void testGenericContext() {
|
public void testGenericContext() {
|
||||||
GenericInstanceContext genericInstanceContext = new GenericInstanceContext(true);
|
GenericInstanceContext genericInstanceContext = new GenericInstanceContext(true);
|
||||||
|
|
||||||
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
// genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
genericInstanceContext.addListableInstanceFactory(new YamlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
// genericInstanceContext.addListableInstanceFactory(new YamlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
genericInstanceContext.addListableInstanceFactory(new JsonFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
// genericInstanceContext.addListableInstanceFactory(new JsonFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
|
|
||||||
genericInstanceContext.refresh();
|
genericInstanceContext.refresh();
|
||||||
genericInstanceContext.getInstances().forEach(System.out::println);
|
genericInstanceContext.getInstances().forEach(System.out::println);
|
||||||
|
|||||||
Reference in New Issue
Block a user