Skip to content

Commit a8fbb6a

Browse files
authored
Merge pull request #5 from Pixel-Services/FlashConfiguration
FlashConfiguration Class and Configuration Testing
2 parents 334af56 + e5427c0 commit a8fbb6a

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
</repository>
2929
</distributionManagement>
3030

31+
<repositories>
32+
<repository>
33+
<id>jitpack.io</id>
34+
<url>https://jitpack.io</url>
35+
</repository>
36+
</repositories>
37+
3138
<dependencies>
3239
<!-- JSON Parsing -->
3340
<dependency>
@@ -36,6 +43,13 @@
3643
<version>20240303</version>
3744
</dependency>
3845

46+
<!-- YAML Parsing -->
47+
<dependency>
48+
<groupId>com.github.Carleslc.Simple-YAML</groupId>
49+
<artifactId>Simple-Yaml</artifactId>
50+
<version>1.8.4</version>
51+
</dependency>
52+
3953
<!-- Logging -->
4054
<dependency>
4155
<groupId>org.slf4j</groupId>

src/main/java/flash/TestServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package flash;
22

3+
import flash.config.FlashConfiguration;
34
import flash.route.RouteController;
45

56
import static flash.Flash.*;
@@ -18,5 +19,10 @@ public static void main(String[] args) {
1819

1920
// Start the server
2021
init();
22+
23+
// Load & Test Config
24+
FlashConfiguration config = new FlashConfiguration();
25+
config.set("test", "Hello from the config!");
26+
System.out.println(config.get("test"));
2127
}
2228
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package flash.config;
2+
3+
import org.simpleyaml.configuration.file.YamlConfiguration;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Files;
11+
12+
/**
13+
* The FlashConfiguration class provides utility methods for loading, saving, and managing YAML configuration files.
14+
*/
15+
public class FlashConfiguration extends YamlConfiguration {
16+
private static final Logger logger = LoggerFactory.getLogger(FlashConfiguration.class);
17+
private final File file;
18+
19+
/**
20+
* Constructs a FlashConfiguration instance of the default configuration file.
21+
*/
22+
public FlashConfiguration() {
23+
this("config.yml");
24+
}
25+
26+
/**
27+
* Constructs a FlashConfiguration instance with a specified file path.
28+
*
29+
* @param path the path to the configuration file.
30+
*/
31+
public FlashConfiguration(String path) {
32+
this(getFile(path));
33+
}
34+
35+
/**
36+
* Constructs a FlashConfiguration instance with a specified file.
37+
*
38+
* @param file the configuration file.
39+
*/
40+
private FlashConfiguration(File file) {
41+
super(loadConfig(file));
42+
this.file = file;
43+
}
44+
45+
/**
46+
* Loads a YamlConfiguration from a file.
47+
*
48+
* @param file the configuration file to load
49+
* @return the loaded YamlConfiguration
50+
*/
51+
private static YamlConfiguration loadConfig(File file) {
52+
try {
53+
return YamlConfiguration.loadConfiguration(file);
54+
} catch (IOException e) {
55+
logger.error("Failed to load configuration file: {}", file.getPath(), e);
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
60+
/**
61+
* Retrieves the configuration file.
62+
*
63+
* @param path the path to the configuration file.
64+
* @return the configuration file
65+
*/
66+
private static File getFile(String path) {
67+
File file = new File(path);
68+
try {
69+
if (!file.exists()) {
70+
try (InputStream resourceStream = FlashConfiguration.class.getClassLoader().getResourceAsStream(file.getName())) {
71+
if (resourceStream != null) {
72+
Files.copy(resourceStream, file.toPath());
73+
} else {
74+
file.createNewFile();
75+
}
76+
}
77+
}
78+
return file;
79+
} catch (IOException e) {
80+
logger.error("Failed to create configuration file: {}", path, e);
81+
throw new RuntimeException(e);
82+
}
83+
}
84+
85+
/**
86+
* Saves the configuration file to disk.
87+
*/
88+
public void save() {
89+
try {
90+
save(file);
91+
} catch (IOException e) {
92+
logger.error("Failed to save configuration file: {}", file.getPath(), e);
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)