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/10/22 05:44:03 UTC

[james-project] 04/04: JAMES-2866 Create a single child injector for extensions

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 53b864cd85bd7581b7e4ace5439a924b5e2ec1d8
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Tue Oct 22 01:11:03 2019 +0200

    JAMES-2866 Create a single child injector for extensions
    
     - Extensions SINGLETON bindings are preserved
     - Performance enhancement
---
 .../org/apache/james/utils/GuiceGenericLoader.java | 26 +++++++++-------------
 .../apache/james/utils/GuiceMailetLoaderTest.java  |  2 --
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/server/container/guice/guice-utils/src/main/java/org/apache/james/utils/GuiceGenericLoader.java b/server/container/guice/guice-utils/src/main/java/org/apache/james/utils/GuiceGenericLoader.java
index 1163907..97bdb1d 100644
--- a/server/container/guice/guice-utils/src/main/java/org/apache/james/utils/GuiceGenericLoader.java
+++ b/server/container/guice/guice-utils/src/main/java/org/apache/james/utils/GuiceGenericLoader.java
@@ -36,7 +36,6 @@ import com.google.inject.util.Modules;
 
 public class GuiceGenericLoader {
     private static final Logger LOGGER = LoggerFactory.getLogger(GuiceGenericLoader.class);
-    private static final Module NO_CHILD_MODULE = binder -> { };
 
     @VisibleForTesting
     public static GuiceGenericLoader forTesting(ExtendedClassLoader extendedClassLoader) {
@@ -47,19 +46,16 @@ public class GuiceGenericLoader {
         private final Injector injector;
         private final ExtendedClassLoader extendedClassLoader;
         private final NamingScheme namingSheme;
-        private final Module childModule;
 
-        private InvocationPerformer(Injector injector, ExtendedClassLoader extendedClassLoader, NamingScheme namingSheme, Module childModule) {
+        private InvocationPerformer(Injector injector, ExtendedClassLoader extendedClassLoader, NamingScheme namingSheme) {
             this.injector = injector;
             this.extendedClassLoader = extendedClassLoader;
             this.namingSheme = namingSheme;
-            this.childModule = childModule;
         }
 
         public T instantiate(ClassName className) throws ClassNotFoundException {
             Class<T> clazz = locateClass(className, namingSheme);
-            return injector.createChildInjector(childModule)
-                .getInstance(clazz);
+            return injector.getInstance(clazz);
         }
 
         private Class<T> locateClass(ClassName className, NamingScheme namingScheme) throws ClassNotFoundException {
@@ -88,35 +84,35 @@ public class GuiceGenericLoader {
 
     private final Injector injector;
     private final ExtendedClassLoader extendedClassLoader;
-    private final Module additionalExtensionBindings;
 
     @Inject
     public GuiceGenericLoader(Injector injector, ExtendedClassLoader extendedClassLoader, ExtensionConfiguration extensionConfiguration) {
-        this.injector = injector;
+
         this.extendedClassLoader = extendedClassLoader;
 
-        this.additionalExtensionBindings = Modules.combine(extensionConfiguration.getAdditionalGuiceModulesForExtensions()
+        Module additionalExtensionBindings = Modules.combine(extensionConfiguration.getAdditionalGuiceModulesForExtensions()
             .stream()
-            .map(Throwing.function(this::<Module>instantiateNoChildModule))
+            .map(Throwing.<ClassName, Module>function(className -> instantiateNoChildModule(injector, className)))
             .peek(module -> LOGGER.info("Enabling injects contained in " + module.getClass().getCanonicalName()))
             .collect(Guavate.toImmutableList()));
+        this.injector = injector.createChildInjector(additionalExtensionBindings);
     }
 
-    private  <T> T instantiateNoChildModule(ClassName className) throws ClassNotFoundException {
-        return new InvocationPerformer<T>(injector, extendedClassLoader, NamingScheme.IDENTITY, NO_CHILD_MODULE)
+    private <T> T instantiateNoChildModule(Injector injector, ClassName className) throws ClassNotFoundException {
+        return new InvocationPerformer<T>(injector, extendedClassLoader, NamingScheme.IDENTITY)
             .instantiate(className);
     }
 
     public <T> T instantiate(ClassName className) throws ClassNotFoundException {
-        return new InvocationPerformer<T>(injector, extendedClassLoader, NamingScheme.IDENTITY, additionalExtensionBindings)
+        return new InvocationPerformer<T>(injector, extendedClassLoader, NamingScheme.IDENTITY)
             .instantiate(className);
     }
 
     public <T> InvocationPerformer<T> withNamingSheme(NamingScheme namingSheme) {
-        return new InvocationPerformer<>(injector, extendedClassLoader, namingSheme, additionalExtensionBindings);
+        return new InvocationPerformer<>(injector, extendedClassLoader, namingSheme);
     }
 
     public <T> InvocationPerformer<T> withChildModule(Module childModule) {
-        return new InvocationPerformer<>(injector, extendedClassLoader, NamingScheme.IDENTITY, Modules.combine(additionalExtensionBindings, childModule));
+        return new InvocationPerformer<>(injector.createChildInjector(childModule), extendedClassLoader, NamingScheme.IDENTITY);
     }
 }
diff --git a/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMailetLoaderTest.java b/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMailetLoaderTest.java
index e261c1a..43c2434 100644
--- a/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMailetLoaderTest.java
+++ b/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMailetLoaderTest.java
@@ -33,7 +33,6 @@ import org.apache.mailet.Mailet;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -185,7 +184,6 @@ public class GuiceMailetLoaderTest {
         assertThatCode(() -> mailet.service(FakeMail.defaultFakeMail())).doesNotThrowAnyException();
     }
 
-    @Ignore("JAMES-2866 singleton are not shared between extensions")
     @Test
     public void allMailetsShouldShareTheSameSingleton() throws Exception {
         GuiceGenericLoader genericLoader = new GuiceGenericLoader(


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org