From 5d8f3ac133544177412ec3c533e5f5f92a7b1c35 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 16 Jan 2013 07:13:08 +0100 Subject: [PATCH] Use dependency injection to create Sone instance. --- pom.xml | 5 ++ .../java/net/pterodactylus/sone/core/Core.java | 8 +++ .../pterodactylus/sone/core/FreenetInterface.java | 2 + .../pterodactylus/sone/core/WebOfTrustUpdater.java | 3 + .../net/pterodactylus/sone/fcp/FcpInterface.java | 4 ++ .../sone/freenet/plugin/PluginConnector.java | 4 ++ .../sone/freenet/wot/IdentityManager.java | 6 +- .../sone/freenet/wot/WebOfTrustConnector.java | 4 ++ .../net/pterodactylus/sone/main/SonePlugin.java | 74 ++++++++++++++-------- .../net/pterodactylus/sone/web/WebInterface.java | 4 ++ 10 files changed, 85 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index ccb8c1b..b3edb8d 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,11 @@ 0.1 + com.google.inject + guice + 3.0 + + com.google.guava guava 14.0-rc1 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index fcde685..6f32230 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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(); } // diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index a8f3efa..4e8b49a 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index bdfc1ae..3d4ad7d 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -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; diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index c1ed953..a0e3c40 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -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)); diff --git a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java index 9145cf5..1dae44b 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java @@ -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; } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java index 568a637..04d9561 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -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; diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java index c4e8d89..18dbaff 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -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; } diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 5b28b85..6d32b50 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -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) { diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 045cc11..42a383f 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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(); -- 2.7.4