1. change the location of config file 1. split config file 2. use multiple threads to load plugins Signed-off-by: pengzhile <pengzhile@gmail.com>pull/1/head
parent
881c5d2522
commit
7ba3c94e53
@ -1,93 +0,0 @@
|
||||
package com.janetfilter.core.commons;
|
||||
|
||||
import com.janetfilter.core.utils.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ConfigDetector {
|
||||
private static final String CONFIG_FILENAME = "janf_config.txt";
|
||||
|
||||
public static File detect(File currentDirectory, String args) {
|
||||
return detect(currentDirectory.getPath(), args);
|
||||
}
|
||||
|
||||
public static File detect(String currentDirectory, String args) {
|
||||
File configFile = tryFile(args); // by javaagent argument
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = tryFile(System.getenv("JANF_CONFIG")); // by env
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = tryFile(System.getProperty("janf.config")); // by -D argument
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory(currentDirectory); // in the same dir as the jar
|
||||
}
|
||||
|
||||
String userHome = System.getProperty("user.home");
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory(userHome, "." + CONFIG_FILENAME); // $HOME/.janf_config.txt
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory(userHome + File.pathSeparator + ".config"); // $HOME/.config/janf_config.txt
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory(userHome + File.pathSeparator + ".local" + File.pathSeparator + "/etc"); // $HOME/.local/etc/janf_config.txt
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory("/usr/local/etc"); // /usr/local/etc/janf_config.txt
|
||||
}
|
||||
|
||||
if (null == configFile) {
|
||||
configFile = searchDirectory("/etc"); // /etc/janf_config.txt
|
||||
}
|
||||
|
||||
return configFile;
|
||||
}
|
||||
|
||||
private static File searchDirectory(String dirPath) {
|
||||
return searchDirectory(dirPath, CONFIG_FILENAME);
|
||||
}
|
||||
|
||||
private static File searchDirectory(String dirPath, String filename) {
|
||||
if (StringUtils.isEmpty(dirPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File dirFile = new File(dirPath);
|
||||
if (!dirFile.isDirectory()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return tryFile(new File(dirFile, filename));
|
||||
}
|
||||
|
||||
private static File tryFile(String filePath) {
|
||||
if (StringUtils.isEmpty(filePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return tryFile(new File(filePath));
|
||||
}
|
||||
|
||||
private static File tryFile(File file) {
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!file.isFile()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!file.canRead()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.janetfilter.core.models;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class FilterConfig {
|
||||
private static FilterConfig current;
|
||||
|
||||
private final Map<String, List<FilterRule>> data;
|
||||
|
||||
public FilterConfig(Map<String, List<FilterRule>> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static FilterConfig getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public static void setCurrent(FilterConfig current) {
|
||||
FilterConfig.current = current;
|
||||
}
|
||||
|
||||
public static List<FilterRule> getBySection(String section) {
|
||||
do {
|
||||
if (null == current) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (null == current.data) {
|
||||
break;
|
||||
}
|
||||
|
||||
List<FilterRule> list = current.data.get(section);
|
||||
if (null == list) {
|
||||
break;
|
||||
}
|
||||
|
||||
return list;
|
||||
} while (false);
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.janetfilter.core.plugin;
|
||||
|
||||
import com.janetfilter.core.models.FilterRule;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PluginConfig {
|
||||
private final File file;
|
||||
private final Map<String, List<FilterRule>> data;
|
||||
|
||||
public PluginConfig(File file, Map<String, List<FilterRule>> data) {
|
||||
this.file = file;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<FilterRule> getBySection(String section) {
|
||||
return data.getOrDefault(section, new ArrayList<>());
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public Map<String, List<FilterRule>> getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue