♻️ 重构插件配置文件控制类
This commit is contained in:
parent
2d9f964163
commit
9303b023f2
@ -11,7 +11,7 @@ import org.jcnc.jnotepad.common.constants.AppConstants;
|
|||||||
import org.jcnc.jnotepad.common.constants.TextConstants;
|
import org.jcnc.jnotepad.common.constants.TextConstants;
|
||||||
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
||||||
import org.jcnc.jnotepad.controller.ResourceController;
|
import org.jcnc.jnotepad.controller.ResourceController;
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
||||||
import org.jcnc.jnotepad.controller.manager.Controller;
|
import org.jcnc.jnotepad.controller.manager.Controller;
|
||||||
import org.jcnc.jnotepad.plugin.PluginManager;
|
import org.jcnc.jnotepad.plugin.PluginManager;
|
||||||
@ -96,10 +96,10 @@ public class LunchApp extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
AppConfigController instance = AppConfigController.getInstance();
|
PluginConfigController instance = PluginConfigController.getInstance();
|
||||||
// 刷新插件配置文件
|
// 刷新插件配置文件
|
||||||
instance.getAppConfig().setPlugins(PluginManager.getInstance().getPluginInfos());
|
instance.getConfig().setPlugins(PluginManager.getInstance().getPluginInfos());
|
||||||
instance.writeAppConfig();
|
instance.writeConfig();
|
||||||
// 关闭线程池
|
// 关闭线程池
|
||||||
threadPool.shutdownNow();
|
threadPool.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package org.jcnc.jnotepad.app.config;
|
package org.jcnc.jnotepad.app.config;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
|
||||||
import org.jcnc.jnotepad.model.entity.ShortcutKey;
|
import org.jcnc.jnotepad.model.entity.ShortcutKey;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -25,8 +24,6 @@ public class AppConfig {
|
|||||||
private boolean textWrap;
|
private boolean textWrap;
|
||||||
private List<ShortcutKey> shortcutKey;
|
private List<ShortcutKey> shortcutKey;
|
||||||
|
|
||||||
private List<PluginInfo> plugins;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成默认应用配置对象。
|
* 生成默认应用配置对象。
|
||||||
*
|
*
|
||||||
@ -47,18 +44,9 @@ public class AppConfig {
|
|||||||
shortcutKeys.add(createShortcutKey("addItem", ""));
|
shortcutKeys.add(createShortcutKey("addItem", ""));
|
||||||
shortcutKeys.add(createShortcutKey("countItem", ""));
|
shortcutKeys.add(createShortcutKey("countItem", ""));
|
||||||
myData.setShortcutKey(shortcutKeys);
|
myData.setShortcutKey(shortcutKeys);
|
||||||
myData.setPlugins(new ArrayList<>());
|
|
||||||
return myData;
|
return myData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PluginInfo> getPlugins() {
|
|
||||||
return plugins;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPlugins(List<PluginInfo> plugins) {
|
|
||||||
this.plugins = plugins;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建 ShortcutKey 对象。
|
* 创建 ShortcutKey 对象。
|
||||||
*
|
*
|
||||||
|
|||||||
36
src/main/java/org/jcnc/jnotepad/app/config/PluginConfig.java
Normal file
36
src/main/java/org/jcnc/jnotepad/app/config/PluginConfig.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package org.jcnc.jnotepad.app.config;
|
||||||
|
|
||||||
|
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插件配置文件
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public class PluginConfig {
|
||||||
|
private List<PluginInfo> plugins;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成默认的插件配置文件
|
||||||
|
*
|
||||||
|
* @return org.jcnc.jnotepad.app.config.PluginConfig 插件配置文件
|
||||||
|
* @apiNote
|
||||||
|
* @since 2023/9/17 0:57
|
||||||
|
*/
|
||||||
|
public static PluginConfig generateDefaultPluginConfig() {
|
||||||
|
PluginConfig pluginConfig = new PluginConfig();
|
||||||
|
pluginConfig.setPlugins(new ArrayList<>());
|
||||||
|
return pluginConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PluginInfo> getPlugins() {
|
||||||
|
return plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlugins(List<PluginInfo> plugins) {
|
||||||
|
this.plugins = plugins;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,6 +28,12 @@ public class AppConstants {
|
|||||||
*/
|
*/
|
||||||
public static final Pattern TABNAME_PATTERN = Pattern.compile("^" + Pattern.quote(UiResourceBundle.getContent(TextConstants.NEW_FILE)) + "\\d+$");
|
public static final Pattern TABNAME_PATTERN = Pattern.compile("^" + Pattern.quote(UiResourceBundle.getContent(TextConstants.NEW_FILE)) + "\\d+$");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认属性
|
||||||
|
*/
|
||||||
|
public static final String DEFAULT_PROPERTY = "user.home";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 私有构造函数,防止该类被实例化。
|
* 私有构造函数,防止该类被实例化。
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package org.jcnc.jnotepad.controller;
|
package org.jcnc.jnotepad.controller;
|
||||||
|
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
import org.jcnc.jnotepad.exception.AppException;
|
import org.jcnc.jnotepad.exception.AppException;
|
||||||
import org.jcnc.jnotepad.plugin.PluginLoader;
|
import org.jcnc.jnotepad.plugin.PluginLoader;
|
||||||
import org.jcnc.jnotepad.util.LogUtil;
|
import org.jcnc.jnotepad.util.LogUtil;
|
||||||
@ -39,7 +39,7 @@ public class ResourceController {
|
|||||||
*/
|
*/
|
||||||
public void loadPlugins() {
|
public void loadPlugins() {
|
||||||
// 扫描并装载插件
|
// 扫描并装载插件
|
||||||
scanLoadPlugins(AppConfigController.getInstance().getPlungsPath());
|
scanLoadPlugins(PluginConfigController.getInstance().getPlungsPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +1,13 @@
|
|||||||
package org.jcnc.jnotepad.controller.config;
|
package org.jcnc.jnotepad.controller.config;
|
||||||
|
|
||||||
import org.jcnc.jnotepad.app.config.AppConfig;
|
import org.jcnc.jnotepad.app.config.AppConfig;
|
||||||
import org.jcnc.jnotepad.exception.AppException;
|
|
||||||
import org.jcnc.jnotepad.model.entity.ShortcutKey;
|
import org.jcnc.jnotepad.model.entity.ShortcutKey;
|
||||||
import org.jcnc.jnotepad.util.JsonUtil;
|
|
||||||
import org.jcnc.jnotepad.util.LogUtil;
|
|
||||||
import org.jcnc.jnotepad.util.PopUpUtil;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.jcnc.jnotepad.common.constants.AppConstants.DEFAULT_PROPERTY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应用程序配置控制器
|
* 应用程序配置控制器
|
||||||
*
|
*
|
||||||
@ -23,22 +15,17 @@ import java.util.List;
|
|||||||
*
|
*
|
||||||
* @author songdragon
|
* @author songdragon
|
||||||
*/
|
*/
|
||||||
public class AppConfigController {
|
public class AppConfigController extends BaseConfigController<AppConfig> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置文件名
|
* 配置文件名
|
||||||
*/
|
*/
|
||||||
public static final String CONFIG_NAME = "jnotepadConfig.json";
|
public static final String CONFIG_NAME = "jnotepadConfig.json";
|
||||||
private static final Logger logger = LogUtil.getLogger(AppConfigController.class);
|
|
||||||
private static final AppConfigController INSTANCE = new AppConfigController();
|
private static final AppConfigController INSTANCE = new AppConfigController();
|
||||||
private AppConfig appConfig;
|
private String configDir;
|
||||||
private static final String DEFAULT_PROPERTY = "user.home";
|
|
||||||
private String appConfigDir;
|
|
||||||
private String pluginsDir;
|
|
||||||
|
|
||||||
private AppConfigController() {
|
private AppConfigController() {
|
||||||
setAppConfigDir(Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad").toString());
|
configDir = Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad").toString();
|
||||||
setPluginsDir(Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad", "plugins").toString());
|
|
||||||
loadConfig();
|
loadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,79 +39,48 @@ public class AppConfigController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载配置文件内容。
|
* 获取配置文件Class类
|
||||||
*/
|
|
||||||
public void loadConfig() {
|
|
||||||
createConfigIfNotExists();
|
|
||||||
Path configPath = getConfigPath();
|
|
||||||
try {
|
|
||||||
logger.info("正在加载配置文件...");
|
|
||||||
// 存在则加载
|
|
||||||
String configContent = Files.readString(configPath);
|
|
||||||
appConfig = JsonUtil.OBJECT_MAPPER.readValue(configContent, AppConfig.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("加载配置文件错误", e);
|
|
||||||
throw new AppException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置文件持久化。
|
|
||||||
*/
|
|
||||||
public void writeAppConfig() {
|
|
||||||
createConfigIfNotExists();
|
|
||||||
writeAppConfig(this.appConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 appConfig 对象持久化到配置文件中。
|
|
||||||
*
|
*
|
||||||
* @param appConfig 应用配置对象
|
* @return 配置文件Class类
|
||||||
*/
|
*/
|
||||||
private void writeAppConfig(AppConfig appConfig) {
|
@Override
|
||||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(getConfigPath().toString()))) {
|
protected Class<AppConfig> getConfigClass() {
|
||||||
if (appConfig == null) {
|
return AppConfig.class;
|
||||||
appConfig = createConfigJson();
|
|
||||||
}
|
|
||||||
writer.write(JsonUtil.toJsonString(appConfig));
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("", e);
|
|
||||||
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!", null, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建配置文件如果不存在。
|
* 生成默认的配置文件
|
||||||
*/
|
|
||||||
public void createConfigIfNotExists() {
|
|
||||||
Path configPath = getConfigPath();
|
|
||||||
if (configPath.toFile().exists()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
File directory = new File(appConfigDir);
|
|
||||||
if (!directory.exists()) {
|
|
||||||
directory.mkdirs();
|
|
||||||
}
|
|
||||||
writeAppConfig(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取配置文件的路径。
|
|
||||||
*
|
*
|
||||||
* @return 配置文件的路径
|
* @return 默认的配置文件
|
||||||
*/
|
*/
|
||||||
public Path getConfigPath() {
|
@Override
|
||||||
return Paths.get(getAppConfigDir(), CONFIG_NAME);
|
protected AppConfig generateDefaultConfig() {
|
||||||
}
|
|
||||||
|
|
||||||
public Path getPlungsPath() {
|
|
||||||
return Paths.get(getPluginsDir());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AppConfig createConfigJson() {
|
|
||||||
return AppConfig.generateDefaultAppConfig();
|
return AppConfig.generateDefaultAppConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件名称
|
||||||
|
*
|
||||||
|
* @return 配置文件名称
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String getConfigName() {
|
||||||
|
return CONFIG_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件文件夹路径
|
||||||
|
*
|
||||||
|
* @return 配置文件夹路径
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String getConfigDir() {
|
||||||
|
return configDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfigDir(String configDir) {
|
||||||
|
this.configDir = configDir;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取自动换行设置,默认自动换行。
|
* 获取自动换行设置,默认自动换行。
|
||||||
@ -132,11 +88,11 @@ public class AppConfigController {
|
|||||||
* @return true,自动换行;false,不自动换行
|
* @return true,自动换行;false,不自动换行
|
||||||
*/
|
*/
|
||||||
public boolean getAutoLineConfig() {
|
public boolean getAutoLineConfig() {
|
||||||
return getAppConfig().isTextWrap();
|
return getConfig().isTextWrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAutoLineConfig(boolean isAutoLine) {
|
public void setAutoLineConfig(boolean isAutoLine) {
|
||||||
getAppConfig().setTextWrap(isAutoLine);
|
getConfig().setTextWrap(isAutoLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,8 +104,8 @@ public class AppConfigController {
|
|||||||
if (getLanguage().equals(language)) {
|
if (getLanguage().equals(language)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.appConfig.setLanguage(language);
|
getConfig().setLanguage(language);
|
||||||
writeAppConfig();
|
writeConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -158,7 +114,7 @@ public class AppConfigController {
|
|||||||
* @return 语言设置
|
* @return 语言设置
|
||||||
*/
|
*/
|
||||||
public String getLanguage() {
|
public String getLanguage() {
|
||||||
return this.appConfig.getLanguage();
|
return getConfig().getLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -167,31 +123,6 @@ public class AppConfigController {
|
|||||||
* @return 快捷键设置列表
|
* @return 快捷键设置列表
|
||||||
*/
|
*/
|
||||||
public List<ShortcutKey> getShortcutKey() {
|
public List<ShortcutKey> getShortcutKey() {
|
||||||
return this.appConfig.getShortcutKey();
|
return getConfig().getShortcutKey();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前配置文件所在目录。
|
|
||||||
*
|
|
||||||
* @return 所在目录
|
|
||||||
*/
|
|
||||||
public String getAppConfigDir() {
|
|
||||||
return appConfigDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAppConfigDir(String appConfigDir) {
|
|
||||||
this.appConfigDir = appConfigDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPluginsDir() {
|
|
||||||
return pluginsDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPluginsDir(String pluginsDir) {
|
|
||||||
this.pluginsDir = pluginsDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AppConfig getAppConfig() {
|
|
||||||
return appConfig;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,150 @@
|
|||||||
|
package org.jcnc.jnotepad.controller.config;
|
||||||
|
|
||||||
|
import org.jcnc.jnotepad.controller.interfaces.ConfigController;
|
||||||
|
import org.jcnc.jnotepad.exception.AppException;
|
||||||
|
import org.jcnc.jnotepad.util.JsonUtil;
|
||||||
|
import org.jcnc.jnotepad.util.LogUtil;
|
||||||
|
import org.jcnc.jnotepad.util.PopUpUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基本配置文件控制器抽象类
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public abstract class BaseConfigController<T> implements ConfigController<T> {
|
||||||
|
|
||||||
|
protected T config;
|
||||||
|
Logger logger = LogUtil.getLogger(this.getClass());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件Class类
|
||||||
|
*
|
||||||
|
* @return 配置文件Class类
|
||||||
|
*/
|
||||||
|
protected abstract Class<T> getConfigClass();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成默认的配置文件
|
||||||
|
*
|
||||||
|
* @return 默认的配置文件
|
||||||
|
*/
|
||||||
|
protected abstract T generateDefaultConfig();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件名称
|
||||||
|
*
|
||||||
|
* @return 配置文件名称
|
||||||
|
*/
|
||||||
|
protected abstract String getConfigName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件文件夹路径
|
||||||
|
*
|
||||||
|
* @return 配置文件夹路径
|
||||||
|
*/
|
||||||
|
protected abstract String getConfigDir();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件类
|
||||||
|
*
|
||||||
|
* @return 获取配置文件类
|
||||||
|
*/
|
||||||
|
public T getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfig(T config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载配置文件内容
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void loadConfig() {
|
||||||
|
createConfigIfNotExists();
|
||||||
|
// 存在则加载
|
||||||
|
try {
|
||||||
|
logger.info("正在加载配置文件:{}...", getConfigClass());
|
||||||
|
String configContent = Files.readString(getConfigPath());
|
||||||
|
config = JsonUtil.OBJECT_MAPPER.readValue(configContent, getConfigClass());
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("加载配置文件错误", e);
|
||||||
|
PopUpUtil.errorAlert("错误", "读写错误", "加载配置文件错误!", null, null);
|
||||||
|
throw new AppException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件持久化
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void writeConfig() {
|
||||||
|
createConfigIfNotExists();
|
||||||
|
writeConfig(getConfig());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件持久化
|
||||||
|
*
|
||||||
|
* @param config 配置文件对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void writeConfig(T config) {
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(getConfigPath().toString()))) {
|
||||||
|
if (config == null) {
|
||||||
|
config = createConfigJson();
|
||||||
|
}
|
||||||
|
writer.write(JsonUtil.toJsonString(config));
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("", e);
|
||||||
|
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!", null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果配置文件不存在则创建
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void createConfigIfNotExists() {
|
||||||
|
Path configPath = getConfigPath();
|
||||||
|
if (configPath.toFile().exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
File directory = new File(getConfigDir());
|
||||||
|
if (!directory.exists()) {
|
||||||
|
directory.mkdirs();
|
||||||
|
}
|
||||||
|
writeConfig(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建配置文件json实体
|
||||||
|
*
|
||||||
|
* @return 默认的配置文件实体
|
||||||
|
* @apiNote 返回默认的配置文件实体用于序列化jso
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public T createConfigJson() {
|
||||||
|
return generateDefaultConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件路径
|
||||||
|
*
|
||||||
|
* @return 配置文件路径
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Path getConfigPath() {
|
||||||
|
return Paths.get(getConfigDir(), getConfigName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,95 @@
|
|||||||
|
package org.jcnc.jnotepad.controller.config;
|
||||||
|
|
||||||
|
import org.jcnc.jnotepad.app.config.PluginConfig;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import static org.jcnc.jnotepad.common.constants.AppConstants.DEFAULT_PROPERTY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插件控制器
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public class PluginConfigController extends BaseConfigController<PluginConfig> {
|
||||||
|
/**
|
||||||
|
* 插件配置文件
|
||||||
|
*/
|
||||||
|
public static final String CONFIG_NAME = "pluginConfig.json";
|
||||||
|
private static final PluginConfigController INSTANCE = new PluginConfigController();
|
||||||
|
|
||||||
|
private String configDir;
|
||||||
|
private String pluginsDir;
|
||||||
|
|
||||||
|
private PluginConfigController() {
|
||||||
|
configDir = Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad").toString();
|
||||||
|
setPluginsDir(Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad", "plugins").toString());
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PluginConfigController getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件Class类
|
||||||
|
*
|
||||||
|
* @return 配置文件Class类
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Class<PluginConfig> getConfigClass() {
|
||||||
|
return PluginConfig.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成默认的配置文件
|
||||||
|
*
|
||||||
|
* @return 默认的配置文件
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected PluginConfig generateDefaultConfig() {
|
||||||
|
return PluginConfig.generateDefaultPluginConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件名称
|
||||||
|
*
|
||||||
|
* @return 配置文件名称
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String getConfigName() {
|
||||||
|
return CONFIG_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件文件夹路径
|
||||||
|
*
|
||||||
|
* @return 配置文件夹路径
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String getConfigDir() {
|
||||||
|
return configDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfigDir(String configDir) {
|
||||||
|
this.configDir = configDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取插件路径
|
||||||
|
*
|
||||||
|
* @return 插件路径
|
||||||
|
*/
|
||||||
|
public Path getPlungsPath() {
|
||||||
|
return Paths.get(getPluginsDir());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPluginsDir() {
|
||||||
|
return pluginsDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPluginsDir(String pluginsDir) {
|
||||||
|
this.pluginsDir = pluginsDir;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
package org.jcnc.jnotepad.controller.interfaces;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件控制器接口
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
*/
|
||||||
|
public interface ConfigController<T> {
|
||||||
|
/**
|
||||||
|
* 加载配置文件内容
|
||||||
|
*/
|
||||||
|
void loadConfig();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件持久化
|
||||||
|
*/
|
||||||
|
void writeConfig();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件持久化
|
||||||
|
*
|
||||||
|
* @param config 配置文件对象
|
||||||
|
*/
|
||||||
|
void writeConfig(T config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果配置文件不存在则创建
|
||||||
|
*/
|
||||||
|
void createConfigIfNotExists();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建配置文件json实体
|
||||||
|
*
|
||||||
|
* @return 默认的配置文件实体
|
||||||
|
* @apiNote 返回默认的配置文件实体用于序列化jso
|
||||||
|
*/
|
||||||
|
T createConfigJson();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置文件路径
|
||||||
|
*
|
||||||
|
* @return 配置文件路径
|
||||||
|
*/
|
||||||
|
Path getConfigPath();
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package org.jcnc.jnotepad.plugin;
|
package org.jcnc.jnotepad.plugin;
|
||||||
|
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
import org.jcnc.jnotepad.exception.AppException;
|
import org.jcnc.jnotepad.exception.AppException;
|
||||||
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
||||||
import org.jcnc.jnotepad.plugin.interfaces.Plugin;
|
import org.jcnc.jnotepad.plugin.interfaces.Plugin;
|
||||||
@ -113,7 +113,7 @@ public class PluginLoader {
|
|||||||
pluginInfo.setEnabled(false);
|
pluginInfo.setEnabled(false);
|
||||||
pluginInfos.add(pluginInfo);
|
pluginInfos.add(pluginInfo);
|
||||||
configPluginInfos.add(pluginInfo);
|
configPluginInfos.add(pluginInfo);
|
||||||
AppConfigController.getInstance().writeAppConfig();
|
PluginConfigController.getInstance().writeConfig();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ public class PluginLoader {
|
|||||||
*/
|
*/
|
||||||
public void loadPluginByPath(String pluginFilePath) {
|
public void loadPluginByPath(String pluginFilePath) {
|
||||||
File file = new File(pluginFilePath);
|
File file = new File(pluginFilePath);
|
||||||
loadPluginByFile(file, AppConfigController.getInstance().getAppConfig().getPlugins());
|
loadPluginByFile(file, PluginConfigController.getInstance().getConfig().getPlugins());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package org.jcnc.jnotepad.plugin;
|
package org.jcnc.jnotepad.plugin;
|
||||||
|
|
||||||
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.controller.config.PluginConfigController;
|
||||||
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
import org.jcnc.jnotepad.model.entity.PluginInfo;
|
||||||
import org.jcnc.jnotepad.util.LogUtil;
|
import org.jcnc.jnotepad.util.LogUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -56,10 +56,10 @@ public class PluginManager {
|
|||||||
public void unloadPlugin(PluginInfo pluginInfo) {
|
public void unloadPlugin(PluginInfo pluginInfo) {
|
||||||
// 删除集合中的插件信息
|
// 删除集合中的插件信息
|
||||||
pluginInfos.remove(pluginInfo);
|
pluginInfos.remove(pluginInfo);
|
||||||
AppConfigController instance = AppConfigController.getInstance();
|
PluginConfigController instance = PluginConfigController.getInstance();
|
||||||
instance.getAppConfig().getPlugins().remove(pluginInfo);
|
instance.getConfig().getPlugins().remove(pluginInfo);
|
||||||
// 刷新配置
|
// 刷新配置
|
||||||
instance.writeAppConfig();
|
instance.writeConfig();
|
||||||
// 删除本地插件jar包
|
// 删除本地插件jar包
|
||||||
Path plungsPath = instance.getPlungsPath();
|
Path plungsPath = instance.getPlungsPath();
|
||||||
try (Stream<Path> pathStream = Files.walk(plungsPath)) {
|
try (Stream<Path> pathStream = Files.walk(plungsPath)) {
|
||||||
@ -91,13 +91,13 @@ public class PluginManager {
|
|||||||
pluginInfo.setEnabled(false);
|
pluginInfo.setEnabled(false);
|
||||||
pluginInfo.setPlugin(null);
|
pluginInfo.setPlugin(null);
|
||||||
ThreadPoolManager.getThreadPool().submit(() -> {
|
ThreadPoolManager.getThreadPool().submit(() -> {
|
||||||
AppConfigController instance = AppConfigController.getInstance();
|
PluginConfigController instance = PluginConfigController.getInstance();
|
||||||
instance.getAppConfig().getPlugins().forEach(plugin -> {
|
instance.getConfig().getPlugins().forEach(plugin -> {
|
||||||
if ((pluginInfo.getName() + pluginInfo.getAuthor()).equals(plugin.getName() + plugin.getAuthor())) {
|
if ((pluginInfo.getName() + pluginInfo.getAuthor()).equals(plugin.getName() + plugin.getAuthor())) {
|
||||||
plugin.setEnabled(false);
|
plugin.setEnabled(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
instance.writeAppConfig();
|
instance.writeConfig();
|
||||||
ThreadPoolManager.threadContSelfSubtracting();
|
ThreadPoolManager.threadContSelfSubtracting();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user