-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f85ca03
commit 4b70726
Showing
23 changed files
with
358 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package fi.fabianadrian.webhooklogger.common.config; | ||
|
||
import org.spongepowered.configurate.CommentedConfigurationNode; | ||
import org.spongepowered.configurate.ConfigurateException; | ||
import org.spongepowered.configurate.ConfigurationOptions; | ||
import org.spongepowered.configurate.objectmapping.ObjectMapper; | ||
import org.spongepowered.configurate.serialize.SerializationException; | ||
import org.spongepowered.configurate.yaml.NodeStyle; | ||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader; | ||
|
||
import java.nio.file.Path; | ||
import java.util.function.UnaryOperator; | ||
|
||
public final class ConfigLoader<C> { | ||
private final YamlConfigurationLoader loader; | ||
private final ObjectMapper<C> mapper; | ||
|
||
public ConfigLoader(Class<C> configClass, Path configPath, UnaryOperator<ConfigurationOptions> options) { | ||
try { | ||
this.mapper = ObjectMapper.factory().get(configClass); | ||
} catch (SerializationException e) { | ||
throw new IllegalStateException( | ||
"Failed to initialize an object mapper for type: " + configClass.getSimpleName(), | ||
e | ||
); | ||
} | ||
|
||
this.loader = YamlConfigurationLoader.builder() | ||
.path(configPath) | ||
.nodeStyle(NodeStyle.BLOCK) | ||
.defaultOptions(options).build(); | ||
} | ||
|
||
public C load() throws ConfigurateException { | ||
CommentedConfigurationNode node = this.loader.load(); | ||
return this.mapper.load(node); | ||
} | ||
|
||
public void save(C config) throws ConfigurateException { | ||
CommentedConfigurationNode node = this.loader.createNode(); | ||
this.mapper.save(config, node); | ||
this.loader.save(node); | ||
} | ||
} |
79 changes: 37 additions & 42 deletions
79
common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,55 @@ | ||
package fi.fabianadrian.webhooklogger.common.config; | ||
|
||
import org.slf4j.Logger; | ||
import space.arim.dazzleconf.helper.ConfigurationHelper; | ||
import fi.fabianadrian.webhooklogger.common.config.serializer.PatternSerializer; | ||
import fi.fabianadrian.webhooklogger.common.config.serializer.ZoneIdSerializer; | ||
import org.spongepowered.configurate.ConfigurateException; | ||
|
||
import java.nio.file.Path; | ||
import java.time.ZoneId; | ||
import java.util.regex.Pattern; | ||
|
||
public final class ConfigManager { | ||
private final Logger logger; | ||
private final ConfigurationHelper<MainConfig> mainConfigHelper; | ||
private final ConfigurationHelper<EventsConfig> eventsConfigHelper; | ||
|
||
private volatile MainConfig mainConfigData; | ||
private volatile EventsConfig eventsConfigData; | ||
private final ConfigLoader<MainConfig> mainConfigLoader; | ||
private final ConfigLoader<EventsConfig> eventsConfigLoader; | ||
private MainConfig mainConfig; | ||
private EventsConfig eventsConfig; | ||
|
||
public ConfigManager(Path configPath) { | ||
this.mainConfigLoader = new ConfigLoader<>( | ||
MainConfig.class, | ||
configPath.resolve("config.yml"), | ||
options -> options.header("WebhookLogger Main Configuration") | ||
.serializers(builder -> builder | ||
.register(Pattern.class, PatternSerializer.INSTANCE) | ||
.register(ZoneId.class, ZoneIdSerializer.INSTANCE) | ||
) | ||
); | ||
this.eventsConfigLoader = new ConfigLoader<>( | ||
EventsConfig.class, | ||
configPath.resolve("events.yml"), | ||
options -> options.header("WebhookLogger Events Configuration") | ||
); | ||
} | ||
|
||
public ConfigManager(Path configPath, Logger logger) { | ||
this.logger = logger; | ||
public void reload() throws ConfigurateException { | ||
this.mainConfig = this.mainConfigLoader.load(); | ||
this.mainConfigLoader.save(this.mainConfig); | ||
|
||
ConfigurationHelperFactory helperFactory = new ConfigurationHelperFactory(configPath); | ||
mainConfigHelper = helperFactory.create("config.yml", MainConfig.class); | ||
eventsConfigHelper = helperFactory.create("events.yml", EventsConfig.class); | ||
this.eventsConfig = this.eventsConfigLoader.load(); | ||
this.eventsConfigLoader.save(this.eventsConfig); | ||
} | ||
|
||
public MainConfig mainConfig() { | ||
MainConfig configData = mainConfigData; | ||
if (configData == null) { | ||
throw new IllegalStateException("Configuration has not been loaded yet"); | ||
if (this.mainConfig == null) { | ||
throw new IllegalStateException("Main configuration isn't loaded"); | ||
} | ||
return configData; | ||
return this.mainConfig; | ||
} | ||
|
||
public EventsConfig eventsConfig() { | ||
EventsConfig configData = eventsConfigData; | ||
if (configData == null) { | ||
throw new IllegalStateException("Configuration has not been loaded yet"); | ||
} | ||
return configData; | ||
} | ||
|
||
public boolean reload() { | ||
boolean success = true; | ||
try { | ||
mainConfigData = mainConfigHelper.reloadConfigData(); | ||
} catch (Exception e) { | ||
logger.error("Could not load config.yml, falling back to default configuration", e); | ||
mainConfigData = mainConfigHelper.getFactory().loadDefaults(); | ||
success = false; | ||
} | ||
|
||
try { | ||
eventsConfigData = eventsConfigHelper.reloadConfigData(); | ||
} catch (Exception e) { | ||
logger.error("Could not load events.yml, falling back to default configuration", e); | ||
eventsConfigData = eventsConfigHelper.getFactory().loadDefaults(); | ||
success = false; | ||
if (this.eventsConfig == null) { | ||
throw new IllegalStateException("Events configuration isn't loaded"); | ||
} | ||
|
||
return success; | ||
return this.eventsConfig; | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
...src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigurationHelperFactory.java
This file was deleted.
Oops, something went wrong.
42 changes: 24 additions & 18 deletions
42
common/src/main/java/fi/fabianadrian/webhooklogger/common/config/EventsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
package fi.fabianadrian.webhooklogger.common.config; | ||
|
||
import fi.fabianadrian.webhooklogger.common.config.event.*; | ||
import space.arim.dazzleconf.annote.SubSection; | ||
import space.arim.dazzleconf.sorter.AnnotationBasedSorter; | ||
import org.spongepowered.configurate.objectmapping.ConfigSerializable; | ||
|
||
public interface EventsConfig { | ||
@AnnotationBasedSorter.Order(0) | ||
@SubSection | ||
ChatEventConfig chat(); | ||
@ConfigSerializable | ||
public class EventsConfig { | ||
private ChatEventConfig chat = new ChatEventConfig(); | ||
private CommandEventConfig command = new CommandEventConfig(); | ||
private DeathEventConfig death = new DeathEventConfig(); | ||
private JoinEventConfig join = new JoinEventConfig(); | ||
private QuitEventConfig quit = new QuitEventConfig(); | ||
|
||
@AnnotationBasedSorter.Order(1) | ||
@SubSection | ||
CommandEventConfig command(); | ||
public ChatEventConfig chat() { | ||
return chat; | ||
} | ||
|
||
@AnnotationBasedSorter.Order(2) | ||
@SubSection | ||
DeathEventConfig death(); | ||
public CommandEventConfig command() { | ||
return command; | ||
} | ||
|
||
@AnnotationBasedSorter.Order(3) | ||
@SubSection | ||
JoinEventConfig join(); | ||
public DeathEventConfig death() { | ||
return death; | ||
} | ||
|
||
@AnnotationBasedSorter.Order(4) | ||
@SubSection | ||
QuitEventConfig quit(); | ||
public JoinEventConfig join() { | ||
return join; | ||
} | ||
|
||
public QuitEventConfig quit() { | ||
return quit; | ||
} | ||
} |
Oops, something went wrong.