From 88469a40e677180866ae22f991dd547d0a367522 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 27 Jun 2014 18:00:33 +0200 Subject: [PATCH] Add custom context type for dependency injection. --- .../pterodactylus/sone/freenet/wot/Context.java | 38 ++++++++++++++++++++++ .../sone/freenet/wot/IdentityManager.java | 5 ++- .../net/pterodactylus/sone/main/SonePlugin.java | 4 +-- .../sone/freenet/wot/IdentityManagerTest.java | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/freenet/wot/Context.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 index 0000000..0f730fa --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Context.java @@ -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 . + */ + +package net.pterodactylus.sone.freenet.wot; + +/** + * Custom container for the Web of Trust context. This allows easier + * configuration of dependency injection. + * + * @author David ‘Bombe’ Roden + */ +public class Context { + + private final String context; + + public Context(String context) { + this.context = context; + } + + public String getContext() { + return context; + } + +} 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 e89c667..810e054 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -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())); } // diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 9d31b35..5b27282 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -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); diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java index 321f55c..2bb6d1f 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java @@ -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 { -- 2.7.4