Use dependency injection to create Sone instance.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Jan 2013 06:13:08 +0000 (07:13 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Jan 2013 18:03:59 +0000 (19:03 +0100)
pom.xml
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java
src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java
src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java

diff --git a/pom.xml b/pom.xml
index ccb8c1b..b3edb8d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <version>0.1</version>
                </dependency>
                <dependency>
+                       <groupId>com.google.inject</groupId>
+                       <artifactId>guice</artifactId>
+                       <version>3.0</version>
+               </dependency>
+               <dependency>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>14.0-rc1</version>
index fcde685..6f32230 100644 (file)
@@ -70,6 +70,8 @@ import net.pterodactylus.util.version.Version;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
+import com.google.inject.Inject;
+
 import freenet.keys.FreenetURI;
 
 /**
@@ -192,6 +194,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @param webOfTrustUpdater
         *            The WebOfTrust updater
         */
+       @Inject
        public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater) {
                super("Sone Core");
                this.configuration = configuration;
@@ -1893,6 +1896,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                loadConfiguration();
                updateChecker.addUpdateListener(this);
                updateChecker.start();
+               identityManager.addIdentityListener(this);
+               identityManager.start();
+               webOfTrustUpdater.init();
                webOfTrustUpdater.start();
        }
 
@@ -1932,6 +1938,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
                soneDownloader.stop();
+               identityManager.removeIdentityListener(this);
+               identityManager.stop();
        }
 
        //
index a8f3efa..4e8b49a 100644 (file)
@@ -33,6 +33,7 @@ import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.logging.Logging;
 
 import com.db4o.ObjectContainer;
+import com.google.inject.Inject;
 
 import freenet.client.ClientMetadata;
 import freenet.client.FetchException;
@@ -83,6 +84,7 @@ public class FreenetInterface {
         * @param node
         *            The node to interact with
         */
+       @Inject
        public FreenetInterface(Node node) {
                this.node = node;
                this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
index bdfc1ae..3d4ad7d 100644 (file)
@@ -33,6 +33,8 @@ import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.validation.Validation;
 
+import com.google.inject.Inject;
+
 /**
  * Updates WebOfTrust identity data in a background thread because communicating
  * with the WebOfTrust plugin can potentially last quite long.
@@ -60,6 +62,7 @@ public class WebOfTrustUpdater extends AbstractService {
         * @param webOfTrustConnector
         *            The web of trust connector
         */
+       @Inject
        public WebOfTrustUpdater(WebOfTrustConnector webOfTrustConnector) {
                super("Trust Updater");
                this.webOfTrustConnector = webOfTrustConnector;
index c1ed953..a0e3c40 100644 (file)
@@ -29,6 +29,9 @@ import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.validation.Validation;
+
+import com.google.inject.Inject;
+
 import freenet.pluginmanager.FredPluginFCP;
 import freenet.pluginmanager.PluginNotFoundException;
 import freenet.pluginmanager.PluginReplySender;
@@ -79,6 +82,7 @@ public class FcpInterface {
         * @param core
         *            The core
         */
+       @Inject
        public FcpInterface(Core core) {
                commands.put("Version", new VersionCommand(core));
                commands.put("GetLocalSones", new GetLocalSonesCommand(core));
index 9145cf5..1dae44b 100644 (file)
@@ -22,6 +22,9 @@ import java.util.HashMap;
 import java.util.Map;
 
 import net.pterodactylus.util.collection.Pair;
+
+import com.google.inject.Inject;
+
 import freenet.pluginmanager.FredPluginTalker;
 import freenet.pluginmanager.PluginNotFoundException;
 import freenet.pluginmanager.PluginRespirator;
@@ -49,6 +52,7 @@ public class PluginConnector implements FredPluginTalker {
         * @param pluginRespirator
         *            The plugin respirator
         */
+       @Inject
        public PluginConnector(PluginRespirator pluginRespirator) {
                this.pluginRespirator = pluginRespirator;
        }
index 568a637..04d9561 100644 (file)
@@ -30,6 +30,9 @@ import net.pterodactylus.sone.freenet.plugin.PluginException;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+
 /**
  * The identity manager takes care of loading and storing identities, their
  * contexts, and properties. It does so in a way that does not expose errors via
@@ -77,7 +80,8 @@ public class IdentityManager extends AbstractService {
         *            The context to focus on (may be {@code null} to ignore
         *            contexts)
         */
-       public IdentityManager(WebOfTrustConnector webOfTrustConnector, String context) {
+       @Inject
+       public IdentityManager(WebOfTrustConnector webOfTrustConnector, @Named("WebOfTrustContext") String context) {
                super("Sone Identity Manager", false);
                this.webOfTrustConnector = webOfTrustConnector;
                this.context = context;
index c4e8d89..18dbaff 100644 (file)
@@ -30,6 +30,9 @@ import net.pterodactylus.sone.freenet.plugin.PluginConnector;
 import net.pterodactylus.sone.freenet.plugin.PluginException;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
+
+import com.google.inject.Inject;
+
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
@@ -59,6 +62,7 @@ public class WebOfTrustConnector {
         * @param pluginConnector
         *            The plugin connector
         */
+       @Inject
        public WebOfTrustConnector(PluginConnector pluginConnector) {
                this.pluginConnector = pluginConnector;
        }
index 5b28b85..6d32b50 100644 (file)
@@ -37,9 +37,17 @@ import net.pterodactylus.util.config.MapConfigurationBackend;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.logging.LoggingListener;
 import net.pterodactylus.util.version.Version;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Singleton;
+import com.google.inject.name.Names;
+
 import freenet.client.async.DatabaseDisabledException;
 import freenet.l10n.BaseL10n.LANGUAGE;
 import freenet.l10n.PluginL10n;
+import freenet.node.Node;
 import freenet.pluginmanager.FredPlugin;
 import freenet.pluginmanager.FredPluginBaseL10n;
 import freenet.pluginmanager.FredPluginFCP;
@@ -107,9 +115,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
        /** The web of trust connector. */
        private WebOfTrustConnector webOfTrustConnector;
 
-       /** The identity manager. */
-       private IdentityManager identityManager;
-
        //
        // ACCESSORS
        //
@@ -178,33 +183,50 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                        }
                }
 
-               boolean startupFailed = true;
-               try {
-                       /* create freenet interface. */
-                       FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
+               final Configuration startConfiguration = oldConfiguration;
 
-                       /* create web of trust connector. */
-                       PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
-                       webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
-                       identityManager = new IdentityManager(webOfTrustConnector, "Sone");
+               /* Freenet injector configuration. */
+               AbstractModule freenetModule = new AbstractModule() {
 
-                       /* create trust updater. */
-                       WebOfTrustUpdater trustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
-                       trustUpdater.init();
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       protected void configure() {
+                               bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
+                               bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
+                       }
+               };
+               /* Sone injector configuration. */
+               AbstractModule soneModule = new AbstractModule() {
 
-                       /* create core. */
-                       core = new Core(oldConfiguration, freenetInterface, identityManager, trustUpdater);
+                       @Override
+                       protected void configure() {
+                               bind(Configuration.class).toInstance(startConfiguration);
+                               bind(FreenetInterface.class).in(Singleton.class);
+                               bind(PluginConnector.class).in(Singleton.class);
+                               bind(WebOfTrustConnector.class).in(Singleton.class);
+                               bind(WebOfTrustUpdater.class).in(Singleton.class);
+                               bind(IdentityManager.class).in(Singleton.class);
+                               bind(String.class).annotatedWith(Names.named("WebOfTrustContext")).toInstance("Sone");
+                               bind(SonePlugin.class).toInstance(SonePlugin.this);
+                               bind(FcpInterface.class).in(Singleton.class);
+                       }
+               };
+               Injector injector = Guice.createInjector(freenetModule, soneModule);
+               core = injector.getInstance(Core.class);
+
+               /* create web of trust connector. */
+               webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
 
-                       /* create the web interface. */
-                       webInterface = new WebInterface(this);
-                       core.addCoreListener(webInterface);
+               /* create FCP interface. */
+               fcpInterface = injector.getInstance(FcpInterface.class);
+               core.setFcpInterface(fcpInterface);
 
-                       /* create FCP interface. */
-                       fcpInterface = new FcpInterface(core);
-                       core.setFcpInterface(fcpInterface);
+               /* create the web interface. */
+               webInterface = injector.getInstance(WebInterface.class);
+               core.addCoreListener(webInterface);
 
-                       /* create the identity manager. */
-                       identityManager.addIdentityListener(core);
+               boolean startupFailed = true;
+               try {
 
                        /* start core! */
                        core.start();
@@ -215,7 +237,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                        webInterface.start();
                        webInterface.setFirstStart(firstStart);
                        webInterface.setNewConfig(newConfig);
-                       identityManager.start();
                        startupFailed = false;
                } finally {
                        if (startupFailed) {
@@ -241,9 +262,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                        /* stop the core. */
                        core.stop();
 
-                       /* stop the identity manager. */
-                       identityManager.stop();
-
                        /* stop the web of trust connector. */
                        webOfTrustConnector.stop();
                } catch (Throwable t1) {
index 045cc11..42a383f 100644 (file)
@@ -131,6 +131,9 @@ import net.pterodactylus.util.version.Version;
 import net.pterodactylus.util.web.RedirectPage;
 import net.pterodactylus.util.web.StaticPage;
 import net.pterodactylus.util.web.TemplatePage;
+
+import com.google.inject.Inject;
+
 import freenet.clients.http.SessionManager;
 import freenet.clients.http.SessionManager.Session;
 import freenet.clients.http.ToadletContainer;
@@ -215,6 +218,7 @@ public class WebInterface implements CoreListener {
         * @param sonePlugin
         *            The Sone plugin
         */
+       @Inject
        public WebInterface(SonePlugin sonePlugin) {
                this.sonePlugin = sonePlugin;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();