james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [13/17] james-project git commit: JAMES-2414 MailboxListeners should be fully final
Date Tue, 12 Jun 2018 00:03:32 GMT
JAMES-2414 MailboxListeners should be fully final


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f3127d87
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f3127d87
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f3127d87

Branch: refs/heads/master
Commit: f3127d870b3555cbfdc955d379ede001875a2edb
Parents: e9de12e
Author: benwa <btellier@linagora.com>
Authored: Fri Jun 8 09:43:04 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Jun 12 07:01:45 2018 +0700

----------------------------------------------------------------------
 .../apache/james/mailbox/MailboxListener.java   |  4 --
 .../QuotaThresholdCrossingListener.java         | 35 ++++--------
 .../QuotaThresholdListenersTestSystem.java      |  4 +-
 .../spamassassin/SpamAssassinListener.java      | 17 ++----
 .../spamassassin/SpamAssassinListenerTest.java  |  3 +-
 .../SpamAssassinListenerModule.java             |  4 +-
 .../modules/mailbox/MailboxListenerFactory.java | 57 +++++++++++++++++++-
 .../mailbox/MailboxListenersLoaderImpl.java     | 29 +++-------
 8 files changed, 82 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxListener.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxListener.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxListener.java
index d1aeb7d..9021aa9 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxListener.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxListener.java
@@ -40,10 +40,6 @@ import org.apache.james.mailbox.quota.QuotaSize;
  */
 public interface MailboxListener {
 
-    interface ConfigurableExecutionMode {
-        void setExecutionMode(ExecutionMode executionMode);
-    }
-
     enum ListenerType {
         ONCE,
         EACH_NODE,

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdCrossingListener.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdCrossingListener.java
b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdCrossingListener.java
index 526679c..d669cba 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdCrossingListener.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdCrossingListener.java
@@ -28,7 +28,6 @@ import org.apache.james.eventsourcing.EventSourcingSystem;
 import org.apache.james.eventsourcing.Subscriber;
 import org.apache.james.eventsourcing.eventstore.EventStore;
 import org.apache.james.filesystem.api.FileSystem;
-import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.mailbox.Event;
 import org.apache.james.mailbox.MailboxListener;
 import org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration;
@@ -42,35 +41,23 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableSet;
 
-public class QuotaThresholdCrossingListener implements MailboxListener, Configurable {
+public class QuotaThresholdCrossingListener implements MailboxListener {
     private static final Logger LOGGER = LoggerFactory.getLogger(QuotaThresholdCrossingListener.class);
-    private final EventStore eventStore;
-    private final MailetContext mailetContext;
-    private final UsersRepository usersRepository;
-    private final FileSystem fileSystem;
 
-    private EventSourcingSystem eventSourcingSystem;
+    private final EventSourcingSystem eventSourcingSystem;
 
     @Inject
-    public QuotaThresholdCrossingListener(MailetContext mailetContext,
-                                          UsersRepository usersRepository,
-                                          FileSystem fileSystem,
-                                          EventStore eventStore) {
-        this.eventStore = eventStore;
-        this.mailetContext = mailetContext;
-        this.usersRepository = usersRepository;
-        this.fileSystem = fileSystem;
+    public QuotaThresholdCrossingListener(MailetContext mailetContext, UsersRepository usersRepository,
+                                          FileSystem fileSystem, EventStore eventStore,
+                                          HierarchicalConfiguration config) {
+        this(mailetContext, usersRepository, fileSystem, eventStore, QuotaMailingListenerConfiguration.from(config));
     }
 
-    @Override
-    public void configure(HierarchicalConfiguration config) {
-        QuotaMailingListenerConfiguration configuration = QuotaMailingListenerConfiguration.from(config);
-        configure(configuration);
-    }
-
-    public void configure(QuotaMailingListenerConfiguration configuration) {
-        ImmutableSet<CommandHandler<?>> handlers = ImmutableSet.of(new DetectThresholdCrossingHandler(eventStore,
configuration));
-        ImmutableSet<Subscriber> subscribers = ImmutableSet.of(new QuotaThresholdMailer(mailetContext,
usersRepository, fileSystem, configuration));
+    public QuotaThresholdCrossingListener(MailetContext mailetContext, UsersRepository usersRepository,
+                                          FileSystem fileSystem, EventStore eventStore,
+                                          QuotaMailingListenerConfiguration config) {
+        ImmutableSet<CommandHandler<?>> handlers = ImmutableSet.of(new DetectThresholdCrossingHandler(eventStore,
config));
+        ImmutableSet<Subscriber> subscribers = ImmutableSet.of(new QuotaThresholdMailer(mailetContext,
usersRepository, fileSystem, config));
         eventSourcingSystem = new EventSourcingSystem(handlers, subscribers, eventStore);
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java
b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java
index f6be2c0..32f19e3 100644
--- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java
+++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java
@@ -41,9 +41,7 @@ public class QuotaThresholdListenersTestSystem {
         FileSystem fileSystem = new FileSystemImpl(new JamesServerResourceLoader("."));
 
         QuotaThresholdCrossingListener thresholdCrossingListener =
-            new QuotaThresholdCrossingListener(mailetContext, MemoryUsersRepository.withVirtualHosting(),
fileSystem, eventStore);
-
-        thresholdCrossingListener.configure(configuration);
+            new QuotaThresholdCrossingListener(mailetContext, MemoryUsersRepository.withVirtualHosting(),
fileSystem, eventStore, configuration);
 
         MockMailboxSession mailboxSession = new MockMailboxSession("system");
         delegatingListener.addGlobalListener(thresholdCrossingListener, mailboxSession);

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
index 6bfb91b..3f3f73a 100644
--- a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
+++ b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java
@@ -20,12 +20,10 @@ package org.apache.james.mailbox.spamassassin;
 
 import java.io.InputStream;
 import java.util.List;
-import java.util.Optional;
 
 import javax.inject.Inject;
 
 import org.apache.james.mailbox.Event;
-import org.apache.james.mailbox.MailboxListener;
 import org.apache.james.mailbox.Role;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxId;
@@ -44,24 +42,19 @@ import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 
-public class SpamAssassinListener implements SpamEventListener, MailboxListener.ConfigurableExecutionMode
{
+public class SpamAssassinListener implements SpamEventListener {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SpamAssassinListener.class);
 
     private final SpamAssassin spamAssassin;
     private final MailboxSessionMapperFactory mapperFactory;
-    private Optional<ExecutionMode> executionMode;
+    private final ExecutionMode executionMode;
 
     @Inject
-    public SpamAssassinListener(SpamAssassin spamAssassin, MailboxSessionMapperFactory mapperFactory)
{
+    public SpamAssassinListener(SpamAssassin spamAssassin, MailboxSessionMapperFactory mapperFactory,
ExecutionMode executionMode) {
         this.spamAssassin = spamAssassin;
         this.mapperFactory = mapperFactory;
-        this.executionMode = Optional.empty();
-    }
-
-    @Override
-    public void setExecutionMode(ExecutionMode executionMode) {
-        this.executionMode = Optional.of(executionMode);
+        this.executionMode = executionMode;
     }
 
     @Override
@@ -71,7 +64,7 @@ public class SpamAssassinListener implements SpamEventListener, MailboxListener.
 
     @Override
     public ExecutionMode getExecutionMode() {
-        return executionMode.orElse(ExecutionMode.ASYNCHRONOUS);
+        return executionMode;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
index f7b3ba3..a384c4b 100644
--- a/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
+++ b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java
@@ -31,6 +31,7 @@ import javax.mail.Flags;
 import javax.mail.util.SharedByteArrayInputStream;
 
 import org.apache.james.mailbox.DefaultMailboxes;
+import org.apache.james.mailbox.MailboxListener;
 import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
@@ -86,7 +87,7 @@ public class SpamAssassinListenerTest {
         spamCapitalMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER,
"SPAM"), UID_VALIDITY));
         trashMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "Trash"),
UID_VALIDITY));
 
-        listener = new SpamAssassinListener(spamAssassin, mapperFactory);
+        listener = new SpamAssassinListener(spamAssassin, mapperFactory, MailboxListener.ExecutionMode.SYNCHRONOUS);
     }
 
     @After

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/server/container/guice/mailbox-plugin-spamassassin/src/main/java/org/apache/james/modules/spamassassin/SpamAssassinListenerModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/mailbox-plugin-spamassassin/src/main/java/org/apache/james/modules/spamassassin/SpamAssassinListenerModule.java
b/server/container/guice/mailbox-plugin-spamassassin/src/main/java/org/apache/james/modules/spamassassin/SpamAssassinListenerModule.java
index 9a96618..862696e 100644
--- a/server/container/guice/mailbox-plugin-spamassassin/src/main/java/org/apache/james/modules/spamassassin/SpamAssassinListenerModule.java
+++ b/server/container/guice/mailbox-plugin-spamassassin/src/main/java/org/apache/james/modules/spamassassin/SpamAssassinListenerModule.java
@@ -26,14 +26,12 @@ import javax.inject.Singleton;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.james.mailbox.spamassassin.SpamAssassinConfiguration;
-import org.apache.james.mailbox.spamassassin.SpamAssassinListener;
 import org.apache.james.utils.PropertiesProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
-import com.google.inject.Scopes;
 
 public class SpamAssassinListenerModule extends AbstractModule {
     private static final Logger LOGGER = LoggerFactory.getLogger(SpamAssassinListenerModule.class);
@@ -42,7 +40,7 @@ public class SpamAssassinListenerModule extends AbstractModule {
 
     @Override
     protected void configure() {
-        bind(SpamAssassinListener.class).in(Scopes.SINGLETON);
+
     }
 
     @Provides

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenerFactory.java
----------------------------------------------------------------------
diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenerFactory.java
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenerFactory.java
index 500517b..4c39661 100644
--- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenerFactory.java
+++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenerFactory.java
@@ -18,14 +18,67 @@
  ****************************************************************/
 package org.apache.james.modules.mailbox;
 
+import java.util.Optional;
+
 import javax.inject.Inject;
 
+import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.mailbox.MailboxListener;
 
+import com.google.common.base.Preconditions;
 import com.google.inject.Injector;
 
 public class MailboxListenerFactory {
 
+    public static class MailboxListenerBuilder {
+        private final Injector injector;
+        private Optional<Class<MailboxListener>> clazz;
+        private Optional<MailboxListener.ExecutionMode> executionMode;
+        private Optional<HierarchicalConfiguration> configuration;
+
+        public MailboxListenerBuilder(Injector injector) {
+            this.injector = injector;
+            this.clazz = Optional.empty();
+            this.executionMode = Optional.empty();
+            this.configuration = Optional.empty();
+        }
+
+        public MailboxListenerBuilder withExecutionMode(MailboxListener.ExecutionMode executionMode)
{
+            this.executionMode = Optional.of(executionMode);
+            return this;
+        }
+
+        public MailboxListenerBuilder withConfiguration(HierarchicalConfiguration configuration)
{
+            this.configuration = Optional.of(configuration);
+            return this;
+        }
+
+        public MailboxListenerBuilder withExecutionMode(Optional<MailboxListener.ExecutionMode>
executionMode) {
+            executionMode.ifPresent(this::withExecutionMode);
+            return this;
+        }
+
+        public MailboxListenerBuilder withConfiguration(Optional<HierarchicalConfiguration>
configuration) {
+            configuration.ifPresent(this::withConfiguration);
+            return this;
+        }
+
+        public MailboxListenerBuilder clazz(Class<MailboxListener> clazz) {
+            this.clazz = Optional.of(clazz);
+            return this;
+        }
+
+        public MailboxListener build() {
+            Preconditions.checkState(clazz.isPresent(), "'clazz' is mandatory");
+            return injector.createChildInjector(
+                binder -> binder.bind(MailboxListener.ExecutionMode.class)
+                    .toInstance(executionMode.orElse(MailboxListener.ExecutionMode.SYNCHRONOUS)),
+                binder -> binder.bind(HierarchicalConfiguration.class)
+                    .toInstance(configuration.orElse(new HierarchicalConfiguration())))
+                .getInstance(clazz.get());
+        }
+    }
+
     private final Injector injector;
 
     @Inject
@@ -33,7 +86,7 @@ public class MailboxListenerFactory {
         this.injector = injector;
     }
 
-    public MailboxListener createInstance(Class<MailboxListener> clazz) {
-        return injector.getInstance(clazz);
+    public MailboxListenerBuilder newInstance() {
+        return new MailboxListenerBuilder(injector);
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/f3127d87/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
----------------------------------------------------------------------
diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
index fced6be..866553c 100644
--- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
+++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java
@@ -20,7 +20,6 @@ package org.apache.james.modules.mailbox;
 
 import java.util.Set;
 
-import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.mailbox.MailboxListener;
@@ -30,7 +29,6 @@ import org.apache.james.utils.ExtendedClassLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.github.fge.lambdas.Throwing;
 import com.google.inject.Inject;
 
 public class MailboxListenersLoaderImpl implements Configurable, MailboxListenersLoader {
@@ -79,31 +77,18 @@ public class MailboxListenersLoaderImpl implements Configurable, MailboxListener
         String listenerClass = configuration.getClazz();
         try {
             LOGGER.info("Loading user registered mailbox listener {}", listenerClass);
-            Class<MailboxListener> clazz = classLoader.locateClass(listenerClass);
-            MailboxListener listener = mailboxListenerFactory.createInstance(clazz);
-            if (listener instanceof Configurable) {
-                configureListener(configuration, (Configurable) listener);
-            }
-            if (listener instanceof MailboxListener.ConfigurableExecutionMode) {
-                MailboxListener.ConfigurableExecutionMode configurableExecutionMode = (MailboxListener.ConfigurableExecutionMode)
listener;
-                configuration.isAsync()
-                    .map(this::getExecutionMode)
-                    .ifPresent(configurableExecutionMode::setExecutionMode);
-            }
-            return listener;
-        } catch (ClassNotFoundException | ConfigurationException e) {
+            return mailboxListenerFactory.newInstance()
+                .withConfiguration(configuration.getConfiguration())
+                .withExecutionMode(configuration.isAsync().map(this::getExecutionMode))
+                .clazz(classLoader.locateClass(listenerClass))
+                .build();
+        } catch (ClassNotFoundException e) {
             LOGGER.error("Error while loading user registered global listener {}", listenerClass,
e);
             throw new RuntimeException(e);
         }
     }
 
-    private void configureListener(ListenerConfiguration configuration, Configurable configurableListener)
throws ConfigurationException {
-        configuration.getConfiguration()
-            .ifPresent(Throwing.consumer(configurableListener::configure).sneakyThrow());
-    }
-
-
-    MailboxListener.ExecutionMode getExecutionMode(boolean isAsync) {
+    private MailboxListener.ExecutionMode getExecutionMode(boolean isAsync) {
         if (isAsync) {
             return MailboxListener.ExecutionMode.ASYNCHRONOUS;
         }


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


Mime
View raw message