Add custom context type for dependency injection.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 16:00:33 +0000 (18:00 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 16:00:33 +0000 (18:00 +0200)
src/main/java/net/pterodactylus/sone/freenet/wot/Context.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java

diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Context.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Context.java
new file mode 100644 (file)
index 0000000..0f730fa
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Sone - Context.java - Copyright © 2014 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+/**
+ * Custom container for the Web of Trust context. This allows easier
+ * configuration of dependency injection.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Context {
+
+       private final String context;
+
+       public Context(String context) {
+               this.context = context;
+       }
+
+       public String getContext() {
+               return context;
+       }
+
+}
index e89c667..810e054 100644 (file)
@@ -33,7 +33,6 @@ import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import com.google.common.eventbus.EventBus;
 import com.google.inject.Inject;
-import com.google.inject.name.Named;
 
 /**
  * The identity manager takes care of loading and storing identities, their
@@ -74,11 +73,11 @@ public class IdentityManager extends AbstractService {
         *            contexts)
         */
        @Inject
-       public IdentityManager(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, @Named("WebOfTrustContext") String context) {
+       public IdentityManager(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, Context context) {
                super("Sone Identity Manager", false);
                this.eventBus = eventBus;
                this.webOfTrustConnector = webOfTrustConnector;
-               this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context));
+               this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context.getContext()));
        }
 
        //
index 9d31b35..5b27282 100644 (file)
@@ -34,6 +34,7 @@ import net.pterodactylus.sone.database.memory.MemoryDatabase;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
+import net.pterodactylus.sone.freenet.wot.Context;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
 import net.pterodactylus.sone.web.WebInterface;
@@ -51,7 +52,6 @@ import com.google.inject.Injector;
 import com.google.inject.Singleton;
 import com.google.inject.TypeLiteral;
 import com.google.inject.matcher.Matchers;
-import com.google.inject.name.Names;
 import com.google.inject.spi.InjectionListener;
 import com.google.inject.spi.TypeEncounter;
 import com.google.inject.spi.TypeListener;
@@ -225,10 +225,10 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                                bind(Configuration.class).toInstance(startConfiguration);
                                bind(FreenetInterface.class).in(Singleton.class);
                                bind(PluginConnector.class).in(Singleton.class);
+                               bind(Context.class).toInstance(new Context("Sone"));
                                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);
                                bind(Database.class).to(MemoryDatabase.class);
index 321f55c..2bb6d1f 100644 (file)
@@ -20,7 +20,7 @@ public class IdentityManagerTest {
 
        private final EventBus eventBus = mock(EventBus.class);
        private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
-       private final IdentityManager identityManager = new IdentityManager(eventBus, webOfTrustConnector, "Test");
+       private final IdentityManager identityManager = new IdentityManager(eventBus, webOfTrustConnector, new Context("Test"));
 
        @Test
        public void identityManagerPingsWotConnector() throws PluginException {