-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.java
More file actions
70 lines (60 loc) · 2.25 KB
/
Copy pathConfigManager.java
File metadata and controls
70 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.example.securefile;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
public class ConfigManager {
private final Properties props = new Properties();
private final Path file;
private ConfigManager(Path file) {
this.file = file;
}
public static ConfigManager loadDefault() {
try {
Path cfgDir = Path.of("config");
if (!Files.exists(cfgDir)) Files.createDirectories(cfgDir);
Path cfg = cfgDir.resolve("config.properties");
ConfigManager cm = new ConfigManager(cfg);
if (Files.exists(cfg)) {
try (InputStream in = Files.newInputStream(cfg)) {
cm.props.load(in);
}
} else {
cm.props.setProperty("db.url", "jdbc:mysql://localhost:3306/securefile_db?useSSL=false&serverTimezone=UTC");
cm.props.setProperty("db.user", "root");
cm.props.setProperty("db.password", "password");
cm.props.setProperty("db.path", "data/securefile.db");
cm.props.setProperty("log.path", "data/operations.log");
cm.props.setProperty("aes.key", "0123456789abcdef0123456789abcdef");
cm.save();
}
Path data = Path.of("data");
if (!Files.exists(data)) Files.createDirectories(data);
return cm;
} catch (IOException e) {
throw new RuntimeException("Failed to load config: " + e.getMessage(), e);
}
}
public String getDbPath() {
return props.getProperty("db.path", "data/securefile.db");
}
public String getLogPath() {
return props.getProperty("log.path", "data/operations.log");
}
public String getAesKey() {
return props.getProperty("aes.key");
}
public String get(String key) {
return props.getProperty(key);
}
public void set(String key, String value) {
props.setProperty(key, value);
}
public void save() {
try (OutputStream out = Files.newOutputStream(file)) {
props.store(out, "SecureFile Commander Pro Config");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}