You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/20 01:57:06 UTC
[james-project] 38/49: [Refactoring] MockProtocolHandlerLoader
should rely on Guice
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d5f0072f5de20a986cdb70cbecb7c91ed6b1b16d
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Sun Nov 17 21:02:51 2019 +0700
[Refactoring] MockProtocolHandlerLoader should rely on Guice
---
server/protocols/protocols-library/pom.xml | 5 +
.../lib/mock/MockProtocolHandlerLoader.java | 112 +++++++--------------
server/protocols/protocols-pop3/pom.xml | 5 +
.../apache/james/pop3server/POP3ServerTest.java | 16 +--
server/protocols/protocols-smtp/pom.xml | 5 +
.../apache/james/smtpserver/SMTPServerTest.java | 30 +++---
6 files changed, 72 insertions(+), 101 deletions(-)
diff --git a/server/protocols/protocols-library/pom.xml b/server/protocols/protocols-library/pom.xml
index f5f8692..1ba9320 100644
--- a/server/protocols/protocols-library/pom.xml
+++ b/server/protocols/protocols-library/pom.xml
@@ -62,6 +62,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>com.google.inject</groupId>
+ <artifactId>guice</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</dependency>
diff --git a/server/protocols/protocols-library/src/test/java/org/apache/james/protocols/lib/mock/MockProtocolHandlerLoader.java b/server/protocols/protocols-library/src/test/java/org/apache/james/protocols/lib/mock/MockProtocolHandlerLoader.java
index d0fa6fe..cb56515 100644
--- a/server/protocols/protocols-library/src/test/java/org/apache/james/protocols/lib/mock/MockProtocolHandlerLoader.java
+++ b/server/protocols/protocols-library/src/test/java/org/apache/james/protocols/lib/mock/MockProtocolHandlerLoader.java
@@ -1,31 +1,57 @@
package org.apache.james.protocols.lib.mock;
-import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
-import javax.inject.Inject;
-import javax.inject.Named;
import org.apache.commons.configuration2.Configuration;
-import org.apache.james.metrics.api.MetricFactory;
-import org.apache.james.metrics.api.NoopMetricFactory;
import org.apache.james.protocols.api.handler.ProtocolHandler;
import org.apache.james.protocols.lib.handler.ProtocolHandlerLoader;
+import com.google.common.collect.ImmutableList;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+
public class MockProtocolHandlerLoader implements ProtocolHandlerLoader {
+ public static class Builder {
+ private final ImmutableList.Builder<Module> modules;
+
+ private Builder() {
+ modules = ImmutableList.builder();
+ }
+
+ public <T, U extends T> Builder put(Module module) {
+ modules.add(module);
+ return this;
+ }
+
+ public MockProtocolHandlerLoader build() {
+ return new MockProtocolHandlerLoader(Guice.createInjector(modules.build()));
+ }
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ private final Injector injector;
+ private final List<Object> loaderRegistry = new ArrayList<>();
+
+ private MockProtocolHandlerLoader(Injector injector) {
+ this.injector = injector;
+ }
+
@Override
public ProtocolHandler load(String name, Configuration config) throws LoadingException {
try {
- ProtocolHandler obj = create(name);
- injectResources(obj);
+ Class<?> aClass = Thread.currentThread().getContextClassLoader().loadClass(name);
+ ProtocolHandler obj = (ProtocolHandler) injector.getInstance(aClass);
postConstruct(obj);
init(obj, config);
synchronized (this) {
@@ -37,32 +63,6 @@ public class MockProtocolHandlerLoader implements ProtocolHandlerLoader {
}
}
- private final Map<String, Object> servicesByName = new HashMap<>();
- private final Map<Class<?>, Object> servicesByType = new HashMap<>();
-
- public Object get(String name) {
- return servicesByName.get(name);
- }
-
- public Object get(Class<?> type) {
- return servicesByType.get(type);
- }
-
- public <T, U extends T> void put(String role, Class<T> serviceType, U instance) {
- servicesByName.put(role, instance);
- servicesByType.put(serviceType, instance);
- }
-
- private final List<Object> loaderRegistry = new ArrayList<>();
-
- protected ProtocolHandler create(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
- try {
- return (ProtocolHandler) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
- } catch (InstantiationException e) {
- return (ProtocolHandler) Thread.currentThread().getContextClassLoader().loadClass(className).getConstructor(MetricFactory.class).newInstance(new NoopMetricFactory());
- }
- }
-
/**
* Dispose all loaded instances by calling the method of the instances which
* is annotated with @PreDestroy
@@ -102,43 +102,6 @@ public class MockProtocolHandlerLoader implements ProtocolHandlerLoader {
}
}
- private void injectResources(Object resource) {
- final Method[] methods = resource.getClass().getMethods();
- for (Method method : methods) {
- final Inject injectAnnotation = method.getAnnotation(Inject.class);
- if (injectAnnotation != null) {
- Object service = null;
- String name = null;
- Annotation[][] paramAnnotations = method.getParameterAnnotations();
- if (paramAnnotations.length == 1) {
- if (paramAnnotations[0].length == 1) {
- if (paramAnnotations[0][0].annotationType().equals(Named.class)) {
- name = ((Named) paramAnnotations[0][0]).value();
- }
- }
- }
- if (name != null) {
- // Name indicates a service
- service = get(name);
- }
- Class<?>[] parameterTypes = method.getParameterTypes();
- if (parameterTypes.length == 1) {
- service = get(parameterTypes[0]);
- }
- if (service != null) {
- try {
- Object[] args = { service };
- method.invoke(resource, args);
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
- throw new RuntimeException("Injection failed for object " + resource + " on method " + method + " with resource " + service, e);
- }
- } else {
- throw new RuntimeException("Injection failed for object " + resource + " on method " + method + " with resource name " + name + ", because no mapping was found");
- }
- }
- }
- }
-
private void init(Object resource, Configuration config) throws IllegalAccessException, InvocationTargetException {
Method[] methods = resource.getClass().getMethods();
for (Method method : methods) {
@@ -154,9 +117,4 @@ public class MockProtocolHandlerLoader implements ProtocolHandlerLoader {
&& method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].equals(Configuration.class);
}
-
- public Object getObjectForName(String name) {
- return get(name);
- }
-
}
diff --git a/server/protocols/protocols-pop3/pom.xml b/server/protocols/protocols-pop3/pom.xml
index c0a384b..60c797e 100644
--- a/server/protocols/protocols-pop3/pom.xml
+++ b/server/protocols/protocols-pop3/pom.xml
@@ -94,6 +94,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>com.google.inject</groupId>
+ <artifactId>guice</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<scope>test</scope>
diff --git a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
index 00d6d96..b07fee9 100644
--- a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
+++ b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java
@@ -55,6 +55,8 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
+import com.google.inject.name.Names;
+
public class POP3ServerTest {
private static final DomainList NO_DOMAIN_LIST = null;
@@ -716,9 +718,6 @@ public class POP3ServerTest {
}
protected void setUpServiceManager() throws Exception {
- protocolHandlerChain = new MockProtocolHandlerLoader();
- protocolHandlerChain.put("usersrepository", UsersRepository.class, usersRepository);
-
mailboxManager = InMemoryIntegrationResources.builder()
.authenticator((userid, passwd) -> {
try {
@@ -737,12 +736,13 @@ public class POP3ServerTest {
.storeQuotaManager()
.build()
.getMailboxManager();
-
- protocolHandlerChain.put("mailboxmanager", MailboxManager.class, mailboxManager);
-
fileSystem = new MockFileSystem();
- protocolHandlerChain.put("fileSystem", FileSystem.class, fileSystem);
-
+
+ protocolHandlerChain = MockProtocolHandlerLoader.builder()
+ .put(binder -> binder.bind(UsersRepository.class).toInstance(usersRepository))
+ .put(binder -> binder.bind(MailboxManager.class).annotatedWith(Names.named("mailboxmanager")).toInstance(mailboxManager))
+ .put(binder -> binder.bind(FileSystem.class).toInstance(fileSystem))
+ .build();
}
private void setupTestMails(MailboxSession session, MessageManager mailbox) throws MailboxException {
diff --git a/server/protocols/protocols-smtp/pom.xml b/server/protocols/protocols-smtp/pom.xml
index bf3968b..125125e 100644
--- a/server/protocols/protocols-smtp/pom.xml
+++ b/server/protocols/protocols-smtp/pom.xml
@@ -130,6 +130,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>com.google.inject</groupId>
+ <artifactId>guice</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
index 2bed383..ac5f4f9 100644
--- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
+++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
@@ -61,6 +61,8 @@ import org.apache.james.mailrepository.memory.MemoryMailRepositoryStore;
import org.apache.james.mailrepository.memory.MemoryMailRepositoryUrlStore;
import org.apache.james.mailrepository.memory.TestingMailRepositoryLoader;
import org.apache.james.metrics.api.Metric;
+import org.apache.james.metrics.api.MetricFactory;
+import org.apache.james.metrics.api.NoopMetricFactory;
import org.apache.james.protocols.api.utils.ProtocolServerUtils;
import org.apache.james.protocols.lib.mock.MockProtocolHandlerLoader;
import org.apache.james.protocols.netty.AbstractChannelPipelineFactory;
@@ -85,6 +87,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
+import com.google.inject.TypeLiteral;
public class SMTPServerTest {
@@ -244,28 +247,23 @@ public class SMTPServerTest {
}
protected void setUpFakeLoader() throws Exception {
-
- chain = new MockProtocolHandlerLoader();
-
- chain.put("usersrepository", UsersRepository.class, usersRepository);
-
dnsServer = new AlterableDNSServer();
- chain.put("dnsservice", DNSService.class, dnsServer);
-
- chain.put("mailStore", MailRepositoryStore.class, mailRepositoryStore);
-
- chain.put("fileSystem", FileSystem.class, fileSystem);
-
MemoryRecipientRewriteTable rewriteTable = new MemoryRecipientRewriteTable();
- chain.put("recipientrewritetable", RecipientRewriteTable.class, rewriteTable);
-
queueFactory = new MemoryMailQueueFactory(new RawMailQueueItemDecoratorFactory());
queue = queueFactory.createQueue(MailQueueFactory.SPOOL);
- chain.put("mailqueuefactory", MailQueueFactory.class, queueFactory);
MemoryDomainList domainList = new MemoryDomainList(mock(DNSService.class));
domainList.addDomain(Domain.LOCALHOST);
- chain.put("domainlist", DomainList.class, domainList);
-
+
+ chain = MockProtocolHandlerLoader.builder()
+ .put(binder -> binder.bind(DomainList.class).toInstance(domainList))
+ .put(binder -> binder.bind(new TypeLiteral<MailQueueFactory<?>>() {}).toInstance(queueFactory))
+ .put(binder -> binder.bind(RecipientRewriteTable.class).toInstance(rewriteTable))
+ .put(binder -> binder.bind(FileSystem.class).toInstance(fileSystem))
+ .put(binder -> binder.bind(MailRepositoryStore.class).toInstance(mailRepositoryStore))
+ .put(binder -> binder.bind(DNSService.class).toInstance(dnsServer))
+ .put(binder -> binder.bind(UsersRepository.class).toInstance(usersRepository))
+ .put(binder -> binder.bind(MetricFactory.class).to(NoopMetricFactory.class))
+ .build();
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org