package net.pterodactylus.rhynodge.engine;
import java.io.IOException;
+import java.util.Arrays;
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.rhynodge.loader.ChainWatcher.ChainDirectory;
+import net.pterodactylus.rhynodge.states.StateManager.StateDirectory;
import net.pterodactylus.util.envopt.Parser;
+import net.pterodactylus.util.inject.ObjectBinding;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
/**
* Rhynodge main starter class.
* Command-line arguments
*/
public static void main(String... arguments) throws IOException {
-
Options options = Parser.fromSystemEnvironment().parseEnvironment(Options::new);
+ EmailAction errorEmailAction =
+ createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient);
- /* create the state manager. */
- StateManager stateManager = new StateManager(Directory.of(options.stateDirectory));
-
- /* create the engine. */
- Engine engine = new Engine(stateManager, createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient));
+ Injector injector = Guice.createInjector(Arrays.asList(
+ ObjectBinding.forClass(StateDirectory.class).is(StateDirectory.of(options.stateDirectory)),
+ ObjectBinding.forClass(ChainDirectory.class).is(ChainDirectory.of(options.chainDirectory)),
+ ObjectBinding.forClass(EmailAction.class).is(errorEmailAction)
+ ));
/* start a watcher. */
- ChainWatcher chainWatcher = new ChainWatcher(engine, options.chainDirectory);
+ ChainWatcher chainWatcher = injector.getInstance(ChainWatcher.class);
chainWatcher.start();
}
import java.util.Set;
import java.util.concurrent.TimeUnit;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
import net.pterodactylus.rhynodge.Reaction;
import net.pterodactylus.rhynodge.engine.Engine;
import net.pterodactylus.rhynodge.loader.Chain.Parameter;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
+@Singleton
public class ChainWatcher extends AbstractExecutionThreadService {
/** The logger. */
* @param directory
* The directory to watch
*/
- public ChainWatcher(Engine engine, String directory) {
+ @Inject
+ public ChainWatcher(Engine engine, ChainDirectory directory) {
this.engine = engine;
- this.directory = directory;
+ this.directory = directory.getDirectory();
}
//
return (filename.lastIndexOf(".") > -1) ? filename.substring(0, filename.lastIndexOf(".")) : filename;
}
+ public static class ChainDirectory {
+
+ private final String directory;
+
+ private ChainDirectory(String directory) {
+ this.directory = directory;
+ }
+
+ public String getDirectory() {
+ return directory;
+ }
+
+ public static ChainDirectory of(String directory) {
+ return new ChainDirectory(directory);
+ }
+
+ }
+
}
/**
* Creates a new state manager. The given directory is assumed to exist.
*
- * @param directory
+ * @param stateDirectory
* The directory to store states in
*/
@Inject
- public StateManager(Directory directory) {
- this.directory = directory.getDirectory();
+ public StateManager(StateDirectory stateDirectory) {
+ this.directory = stateDirectory.getDirectory();
}
//
return empty();
}
- public static class Directory {
+ public static class StateDirectory {
private final String directory;
- private Directory(String directory) {
+ private StateDirectory(String directory) {
this.directory = directory;
}
return directory;
}
- public static Directory of(String directory) {
- return new Directory(directory);
+ public static StateDirectory of(String directory) {
+ return new StateDirectory(directory);
}
}
--- /dev/null
+package net.pterodactylus.util.inject;
+
+import com.google.inject.Module;
+
+/**
+ * Helper class for Guice configuration.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ObjectBinding {
+
+ public static <T> $1<T> forClass(Class<T> requestedClass) {
+ return new $1<>(requestedClass);
+ }
+
+ public static class $1<T> {
+
+ private final Class<T> requestedClass;
+
+ private $1(Class<T> requestedClass) {
+ this.requestedClass = requestedClass;
+ }
+
+ public Module is(T instance) {
+ return (binder) -> binder.bind(requestedClass).toInstance(instance);
+ }
+
+ }
+
+}
import java.util.Optional;
import net.pterodactylus.rhynodge.State;
-import net.pterodactylus.rhynodge.states.StateManager.Directory;
+import net.pterodactylus.rhynodge.states.StateManager.StateDirectory;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.io.Files;
public StateManagerTest() throws IOException {
tempFolder.create();
statePath = tempFolder.newFolder();
- stateManager = new StateManager(Directory.of(statePath.getPath()));
+ stateManager = new StateManager(StateDirectory.of(statePath.getPath()));
}
@Test
--- /dev/null
+package net.pterodactylus.util.inject;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ObjectBinding}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ObjectBindingTest {
+
+ @Test
+ public void constructorCanBeCalled() {
+ new ObjectBinding();
+ }
+
+ @Test
+ public void objectIsBound() {
+ TestClass testObject = new TestClass();
+ Injector injector = Guice.createInjector(ObjectBinding.forClass(TestInterface.class).is(testObject));
+ MatcherAssert.assertThat(injector.getInstance(TestInterface.class), Matchers.sameInstance(testObject));
+ }
+
+ public interface TestInterface { }
+
+ public static class TestClass implements TestInterface { }
+
+}