From: David ‘Bombe’ Roden Date: Sat, 9 May 2015 08:15:42 +0000 (+0200) Subject: Start outfitting core classes for Guice X-Git-Tag: v2~179 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=252bc27aca417b31fa27716dfab7e6446d642a7c;p=rhynodge.git Start outfitting core classes for Guice --- diff --git a/build.gradle b/build.gradle index af795fc..d772187 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,7 @@ dependencies { compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.1.2" compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.1.2" compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.1.2" + compile group: "com.google.inject", name: "guice", version: "4.0" testCompile group: "junit", name: "junit", version:"4.12" testCompile group: "org.hamcrest", name: "hamcrest-library", version:"1.3" diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java b/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java index 7103dc5..079350b 100644 --- a/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java +++ b/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java @@ -28,6 +28,9 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; +import javax.inject.Inject; +import javax.inject.Singleton; + import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.actions.EmailAction; import net.pterodactylus.rhynodge.states.StateManager; @@ -37,6 +40,7 @@ import net.pterodactylus.rhynodge.states.StateManager; * * @author David ‘Bombe’ Roden */ +@Singleton public class Engine { private final StateManager stateManager; @@ -44,6 +48,7 @@ public class Engine { private final Map> scheduledFutures = new ConcurrentHashMap<>(); private final EmailAction errorEmailAction; + @Inject public Engine(StateManager stateManager, EmailAction errorEmailAction) { this.stateManager = stateManager; this.errorEmailAction = errorEmailAction; diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/Starter.java b/src/main/java/net/pterodactylus/rhynodge/engine/Starter.java index 7ca75ca..d824eb8 100644 --- a/src/main/java/net/pterodactylus/rhynodge/engine/Starter.java +++ b/src/main/java/net/pterodactylus/rhynodge/engine/Starter.java @@ -22,6 +22,7 @@ import java.io.IOException; import net.pterodactylus.rhynodge.actions.EmailAction; import net.pterodactylus.rhynodge.loader.ChainWatcher; import net.pterodactylus.rhynodge.states.StateManager; +import net.pterodactylus.rhynodge.states.StateManager.Directory; import net.pterodactylus.util.envopt.Parser; /** @@ -42,7 +43,7 @@ public class Starter { Options options = Parser.fromSystemEnvironment().parseEnvironment(Options::new); /* create the state manager. */ - StateManager stateManager = new StateManager(options.stateDirectory); + StateManager stateManager = new StateManager(Directory.of(options.stateDirectory)); /* create the engine. */ Engine engine = new Engine(stateManager, createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient)); diff --git a/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java b/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java index 3e08c29..bb8aff5 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/StateManager.java @@ -24,6 +24,9 @@ import java.io.File; import java.io.IOException; import java.util.Optional; +import javax.inject.Inject; +import javax.inject.Singleton; + import net.pterodactylus.rhynodge.State; import org.apache.log4j.Logger; @@ -38,6 +41,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; * * @author David ‘Bombe’ Roden */ +@Singleton public class StateManager { /** The logger. */ @@ -55,8 +59,9 @@ public class StateManager { * @param directory * The directory to store states in */ - public StateManager(String directory) { - this.directory = directory; + @Inject + public StateManager(Directory directory) { + this.directory = directory.getDirectory(); } // @@ -159,4 +164,22 @@ public class StateManager { return empty(); } + public static class Directory { + + private final String directory; + + private Directory(String directory) { + this.directory = directory; + } + + public String getDirectory() { + return directory; + } + + public static Directory of(String directory) { + return new Directory(directory); + } + + } + } diff --git a/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java b/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java index 6541c11..0821b68 100644 --- a/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java +++ b/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.util.Optional; import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.states.StateManager.Directory; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.io.Files; @@ -41,7 +42,7 @@ public class StateManagerTest { public StateManagerTest() throws IOException { tempFolder.create(); statePath = tempFolder.newFolder(); - stateManager = new StateManager(statePath.getPath()); + stateManager = new StateManager(Directory.of(statePath.getPath())); } @Test