Skip to content

Commit

Permalink
完成模块加载操作
Browse files Browse the repository at this point in the history
  • Loading branch information
Suomm committed Mar 17, 2021
1 parent 1d55641 commit 2e166da
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
*
* @return 名称
*/
String name();
String title();

/**
* 拓展模块的有关描述。
*
* @return 描述
*/
String details() default "";
String description() default "";

/**
* 拓展模块的版本信息。
Expand Down
2 changes: 0 additions & 2 deletions tran4f-graphics/conf/settings.properties

This file was deleted.

7 changes: 5 additions & 2 deletions tran4f-graphics/conf/settings.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# basePath 模块所在路径(默认 ./)
# modules 模块名称(默认加载模块 tran4f.config)
basePath:
- D:/Document
modules:
- tran4f.config
- test.abc
- tran4f.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.LinkedHashSet;
import java.util.Set;

/**
* <p>
* 2021/2/28
* </p>
* <p>封装配置信息。
*
* @author 王帅
* @since 1.0
Expand All @@ -35,6 +34,23 @@
@AllArgsConstructor
public class Settings {

/** 模块名称(默认模块 {@code tran4f.config}) */
private Set<String> modules;
/** 读取模块的路径位置(默认位置 {@code ./}) */
private Set<String> basePath;

/**
* 初始化一些基本的配置。
*
* @return {@code this}
*/
public Settings init() {
if (modules == null) modules = new LinkedHashSet<>();
if (basePath == null) basePath = new LinkedHashSet<>();
// 基本的配置
modules.add("tran4f.config");
basePath.add("./");
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import com.github.tran4f.annotation.Property;

import java.lang.module.Configuration;
import java.lang.module.FindException;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>
* 2021/2/28
* </p>
* <p>模块加载。
*
* @author 王帅
* @since 1.0
Expand All @@ -37,23 +37,23 @@ public final class ModuleLoader {
private ModuleLoader() {
}

private static final String BASE_PATH = "./";

/**
* 根据模块名称,加载模块。
*
* @param basePath 模块所在路径
* @param names 模块名称
* @return 加载之后的信息
* @exception FindException 模块加载失败
*/
public static void load(Collection<String> names) {
ModuleFinder finder = ModuleFinder.of(Path.of(BASE_PATH));
public static Map<String, Property> load(Collection<String> basePath, Collection<String> names) {
ModuleFinder finder = ModuleFinder.of(basePath.stream().map(Path::of).toArray(Path[]::new));
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), names);
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
layer.modules()
return layer.modules()
.stream()
.collect(Collectors.toMap(Module::getName, e -> e.getAnnotation(Property.class)))
.forEach((a, b) -> System.out.println(a + " === " + b));
.collect(Collectors.toMap(Module::getName, e -> e.getAnnotation(Property.class)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,38 @@
package com.github.tran4f.loader;

import com.github.tran4f.domain.Settings;
import com.github.tran4f.support.Tran4fException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.yaml.snakeyaml.Yaml;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
* XML 资源文件的加载器。
* YAML 资源文件的加载器。
*
* @author 王帅
*/
@Configuration
public class YamlLoader {

private static final Settings SETTINGS;

static {
try (
FileInputStream fis = new FileInputStream("conf/settings.yml");
BufferedInputStream bis = new BufferedInputStream(fis)
) {
SETTINGS = new Yaml().loadAs(bis, Settings.class);
/**
* 注册配置文件类。
*
* @return 设置
*/
@Bean
public Settings settings() {
try (BufferedReader br = new BufferedReader(
new FileReader("conf/settings.yml"))) {
Settings settings = new Yaml().loadAs(br, Settings.class);
if (settings == null)
settings = new Settings();
return settings.init();
} catch (IOException e) {
throw new Tran4fException(e);
return new Settings().init();
}
}

public static Settings getSettings() {
return SETTINGS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@

package com.github.tran4f.loader;

import com.github.tran4f.domain.Settings;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Set;
import java.security.Provider;

import static org.junit.jupiter.api.Assertions.*;

/**
* <p>
* 2021/3/17
* </p>
*
* @author 王帅
* @since 1.0
*/
public class ModuleLoaderTest {
@SpringBootTest(classes = YamlLoader.class)
class ModuleLoaderTest {

@Autowired
Settings settings;

@Test
void load() {
// ModuleLoader.load(Set.of("tran4f.config"));
ModuleLoader.load(settings.getBasePath(), settings.getModules()).forEach((k, v) -> {
System.out.println("module name:" + k);
System.out.println("module prop:" + v);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@

package com.github.tran4f.loader;

import com.github.tran4f.domain.Settings;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
* <p>
* 2021/2/28
* </p>
*
* @author 王帅
* @since 1.0
*/
public class YamlLoaderTest {
@SpringBootTest(classes = YamlLoader.class)
class YamlLoaderTest {

@Autowired
Settings settings;

@Test
public void get() {
System.out.println(YamlLoader.getSettings().getModules());
void load() {
System.out.println(settings);
}

}

0 comments on commit 2e166da

Please sign in to comment.