Skip to content

Commit

Permalink
重构配置文件系统
Browse files Browse the repository at this point in the history
引入日志
  • Loading branch information
RedDragon0293 committed Jun 4, 2024
1 parent d649d56 commit 3bba5f5
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 121 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ dependencies {
//implementation('io.netty:netty-all:4.1.86.Final')
implementation('com.google.code.gson:gson:2.8.9')
//implementation('')
implementation 'org.apache.logging.log4j:log4j-api:2.23.1'
implementation 'org.apache.logging.log4j:log4j-core:2.23.1'

testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/cn/reddragon/eportal/EPortal.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cn.reddragon.eportal;

import cn.reddragon.eportal.controller.MainController;
import cn.reddragon.eportal.account.AccountManager;
import cn.reddragon.eportal.config.ConfigManager;
import cn.reddragon.eportal.controllers.MainController;
import cn.reddragon.eportal.utils.Authenticator;
import cn.reddragon.eportal.utils.Config;
import cn.reddragon.eportal.utils.LoginType;
import javafx.application.Application;
import javafx.application.Platform;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class EPortal extends Application {
});

public static void main(String[] args) {
askThread.setDaemon(true);
Authenticator.checkOnline();
try {
if (Authenticator.online) {
Expand All @@ -51,11 +53,13 @@ public static void main(String[] args) {
System.exit(0);
}
launch();
System.exit(0);
ConfigManager.saveConfigs();
}

@SuppressWarnings("unchecked")
@Override
public void start(Stage stage) throws IOException {
Thread.currentThread().setName("EPortal Main");
FXMLLoader fxmlLoader = new FXMLLoader(EPortal.class.getResource("hello-view.fxml"));
Parent root = fxmlLoader.load();
controller = fxmlLoader.getController();
Expand All @@ -71,18 +75,23 @@ public void start(Stage stage) throws IOException {
stage.setScene(scene);
stage.setMinWidth(root.prefWidth(-1));
stage.setMinHeight(root.prefHeight(-1));
stage.show();
String[] config = Config.read();
//String[] config = ConfigManager.read();
ConfigManager.loadConfigs();
TextField name = (TextField) root.lookup("#userNameField");
TextField pass = (TextField) root.lookup("#passwordField");
name.setText(config[0]);
pass.setText(config[1]);
box.setValue(box.getItems().get(Byte.parseByte(config[2])));
//name.setText(config[0]);
//pass.setText(config[1]);
if (!AccountManager.accounts.isEmpty()) {
name.setText(AccountManager.accounts.get(0).getName());
pass.setText(AccountManager.accounts.get(0).getPassword());
}
//box.setValue(box.getItems().get(Byte.parseByte(config[2])));
//updateUI();
askThread.start();
Authenticator.checkOnline();
if (!Authenticator.online) {
controller.onLoginButtonClick();
}
stage.show();
}
}
20 changes: 20 additions & 0 deletions src/main/java/cn/reddragon/eportal/account/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.reddragon.eportal.account;

public final class Account {
private final String name;
private final String password;

public Account(String name, String password) {
this.name = name;
this.password = password;
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}

}
11 changes: 11 additions & 0 deletions src/main/java/cn/reddragon/eportal/account/AccountManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cn.reddragon.eportal.account;

import java.util.ArrayList;

public class AccountManager {
public static final ArrayList<Account> accounts = new ArrayList<>();

public static void addAccount(String username, String password) {
accounts.add(new Account(username, password));
}
}
15 changes: 15 additions & 0 deletions src/main/java/cn/reddragon/eportal/config/AbstractConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.reddragon.eportal.config;

import com.google.gson.JsonElement;

public abstract class AbstractConfig {
public String name;

public AbstractConfig(String name) {
this.name = name;
}

public abstract JsonElement toJson();

public abstract void fromJson(JsonElement element);
}
68 changes: 68 additions & 0 deletions src/main/java/cn/reddragon/eportal/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cn.reddragon.eportal.config;

import cn.reddragon.eportal.config.configs.AccountConfig;
import cn.reddragon.eportal.config.configs.NetTypeConfig;
import com.google.gson.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Map.Entry;

public class ConfigManager {
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File configFile = new File("EPortal.json");
private static final ArrayList<AbstractConfig> configs = new ArrayList<>();
private static final Logger logger = LogManager.getLogger("ConfigManager");

static {
if (!configFile.exists()) {
try {
logger.info("未找到配置文件. 新建配置文件.");
configFile.createNewFile();
saveConfigs();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
configs.add(new AccountConfig());
configs.add(new NetTypeConfig());
}

public static void loadConfigs() {
try {
JsonElement element = JsonParser.parseReader(new BufferedReader(new FileReader(configFile)));
if (!element.isJsonNull()) {
JsonObject object = element.getAsJsonObject();
for (Entry<String, JsonElement> next : object.entrySet()) {
for (AbstractConfig config : configs) {
if (config.name.equals(next.getKey())) {
config.fromJson(next.getValue());
logger.info("成功加载配置文件 {}", config.name);
break;
}
}
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}

public static void saveConfigs() {
JsonObject root = new JsonObject();
for (AbstractConfig config : configs) {
root.add(config.name, config.toJson());
logger.info("成功保存配置文件 {}", config.name);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(configFile));
writer.write(gson.toJson(root));
writer.flush();
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.reddragon.eportal.config.configs;

import cn.reddragon.eportal.account.Account;
import cn.reddragon.eportal.account.AccountManager;
import cn.reddragon.eportal.config.AbstractConfig;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class AccountConfig extends AbstractConfig {

public AccountConfig() {
super("Account");
}

@Override
public JsonElement toJson() {
JsonArray array = new JsonArray();
for (Account it : AccountManager.accounts) {
JsonObject object = new JsonObject();
object.addProperty("Username", it.getName());
object.addProperty("Password", it.getPassword());
array.add(object);
}
return array;
}

@Override
public void fromJson(JsonElement element) {
JsonArray array = element.getAsJsonArray();
for (JsonElement it : array) {
JsonObject object = it.getAsJsonObject();
AccountManager.addAccount(object.get("Username").getAsString(), object.get("Password").getAsString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.reddragon.eportal.config.configs;

import cn.reddragon.eportal.EPortal;
import cn.reddragon.eportal.config.AbstractConfig;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import javafx.scene.control.ChoiceBox;

public class NetTypeConfig extends AbstractConfig {
private final ChoiceBox<String> selector = EPortal.controller.selector;

public NetTypeConfig() {
super("NetType");
}

@Override
public JsonElement toJson() {
return new JsonPrimitive(EPortal.controller.selector.getItems().indexOf(EPortal.controller.selector.getValue()));
}

@Override
public void fromJson(JsonElement element) {
selector.setValue(selector.getItems().get(element.getAsInt()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.reddragon.eportal.controller;
package cn.reddragon.eportal.controllers;

import cn.reddragon.eportal.account.AccountManager;
import cn.reddragon.eportal.utils.*;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -168,7 +169,9 @@ public void onLoginButtonClick() {
Platform.runLater(() -> resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString()));
if (result.equals("success")) {
Authenticator.userIndex = resultMessage.get("userIndex").getAsString();
Config.save(username, password, (byte) selector.getItems().indexOf(mode));
//ConfigManager.save(username, password, (byte) selector.getItems().indexOf(mode));
AccountManager.accounts.clear();
AccountManager.addAccount(username, password);
}
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cn.reddragon.eportal.controllers;

public class SettingsController {
}
108 changes: 0 additions & 108 deletions src/main/java/cn/reddragon/eportal/utils/Config.java

This file was deleted.

Loading

0 comments on commit 3bba5f5

Please sign in to comment.