james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [07/17] james-project git commit: JAMES-2414 Allow to potentially configure execution mode on custom mailbox listener
Date Tue, 12 Jun 2018 00:03:26 GMT
JAMES-2414 Allow to potentially configure execution mode on custom mailbox listener


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

Branch: refs/heads/master
Commit: aa4de6a94ca9c28e462fa6ad2e694154c29a000f
Parents: bfb5d14
Author: benwa <btellier@linagora.com>
Authored: Thu Jun 7 16:21:18 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Jun 12 07:01:28 2018 +0700

----------------------------------------------------------------------
 .../apache/james/mailbox/MailboxListener.java   |  4 ++++
 .../spamassassin/SpamAssassinListener.java      | 13 +++++++++--
 .../modules/mailbox/ListenerConfiguration.java  | 13 ++++++++---
 .../mailbox/MailboxListenersLoaderImpl.java     | 24 ++++++++++++++++++--
 .../mailbox/ListenerConfigurationTest.java      | 12 ++++++++++
 5 files changed, 59 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/aa4de6a9/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 664a107..fc248d6 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,6 +40,10 @@ import org.apache.james.mailbox.quota.QuotaSize;
  */
 public interface MailboxListener {
 
+    interface ConfigurableExecutionMode {
+        void set(ExecutionMode executionMode);
+    }
+
     enum ListenerType {
         ONCE,
         EACH_NODE,

http://git-wip-us.apache.org/repos/asf/james-project/blob/aa4de6a9/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 7160625..b25fd08 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,10 +20,12 @@ 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;
@@ -42,17 +44,24 @@ import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 
-public class SpamAssassinListener implements SpamEventListener {
+public class SpamAssassinListener implements SpamEventListener, MailboxListener.ConfigurableExecutionMode
{
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SpamAssassinListener.class);
 
     private final SpamAssassin spamAssassin;
     private final MailboxSessionMapperFactory mapperFactory;
+    private Optional<ExecutionMode> executionMode;
 
     @Inject
     public SpamAssassinListener(SpamAssassin spamAssassin, MailboxSessionMapperFactory mapperFactory)
{
         this.spamAssassin = spamAssassin;
         this.mapperFactory = mapperFactory;
+        this.executionMode = Optional.empty();
+    }
+
+    @Override
+    public void set(ExecutionMode executionMode) {
+        this.executionMode = Optional.of(executionMode);
     }
 
     @Override
@@ -62,7 +71,7 @@ public class SpamAssassinListener implements SpamEventListener {
 
     @Override
     public ExecutionMode getExecutionMode() {
-        return ExecutionMode.ASYNCHRONOUS;
+        return executionMode.orElse(ExecutionMode.ASYNCHRONOUS);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/aa4de6a9/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java
----------------------------------------------------------------------
diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java
index f2d5ea3..ec99546 100644
--- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java
+++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java
@@ -30,11 +30,12 @@ public class ListenerConfiguration {
     public static ListenerConfiguration from(HierarchicalConfiguration configuration) {
         String listenerClass = configuration.getString("class");
         Preconditions.checkState(!Strings.isNullOrEmpty(listenerClass), "class name is mandatory");
-        return new ListenerConfiguration(listenerClass, extractSubconfiguration(configuration));
+        Optional<Boolean> isAsync = Optional.ofNullable(configuration.getBoolean("async",
null));
+        return new ListenerConfiguration(listenerClass, extractSubconfiguration(configuration),
isAsync);
     }
 
     public static ListenerConfiguration forClass(String clazz) {
-        return new ListenerConfiguration(clazz, Optional.empty());
+        return new ListenerConfiguration(clazz, Optional.empty(), Optional.empty());
     }
 
     private static Optional<HierarchicalConfiguration> extractSubconfiguration(HierarchicalConfiguration
configuration) {
@@ -45,10 +46,12 @@ public class ListenerConfiguration {
 
     private final String clazz;
     private final Optional<HierarchicalConfiguration> configuration;
+    private final Optional<Boolean> isAsync;
 
-    private ListenerConfiguration(String clazz, Optional<HierarchicalConfiguration>
configuration) {
+    private ListenerConfiguration(String clazz, Optional<HierarchicalConfiguration>
configuration, Optional<Boolean> isAsync) {
         this.clazz = clazz;
         this.configuration = configuration;
+        this.isAsync = isAsync;
     }
 
     public String getClazz() {
@@ -58,4 +61,8 @@ public class ListenerConfiguration {
     public Optional<HierarchicalConfiguration> getConfiguration() {
         return configuration;
     }
+
+    public Optional<Boolean> isAsync() {
+        return isAsync;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/aa4de6a9/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 68a8643..3e59a78 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
@@ -30,6 +30,7 @@ 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 {
@@ -80,8 +81,14 @@ public class MailboxListenersLoaderImpl implements Configurable, MailboxListener
             LOGGER.info("Loading user registered mailbox listener {}", listenerClass);
             Class<MailboxListener> clazz = classLoader.locateClass(listenerClass);
             MailboxListener listener = mailboxListenerFactory.createInstance(clazz);
-            if (listener instanceof Configurable && configuration.getConfiguration().isPresent())
{
-                ((Configurable)listener).configure(configuration.getConfiguration().get());
+            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::set);
             }
             return listener;
         } catch (ClassNotFoundException | ConfigurationException e) {
@@ -89,4 +96,17 @@ public class MailboxListenersLoaderImpl implements Configurable, MailboxListener
             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) {
+        if (isAsync) {
+            return MailboxListener.ExecutionMode.ASYNCHRONOUS;
+        }
+        return MailboxListener.ExecutionMode.SYNCHRONOUS;
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/aa4de6a9/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java
b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java
index 011f3ec..b2db84c 100644
--- a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java
+++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java
@@ -53,4 +53,16 @@ public class ListenerConfigurationTest {
 
         assertThat(listenerConfiguration.getClazz()).isEqualTo(expectedClazz);
     }
+
+    @Test
+    public void isAsyncShouldReturnConfiguredValue() {
+        DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder();
+        String expectedClazz = "MyClassName";
+        configuration.addProperty("class", expectedClazz);
+        configuration.addProperty("async", "false");
+
+        ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration);
+
+        assertThat(listenerConfiguration.isAsync()).contains(false);
+    }
 }


---------------------------------------------------------------------
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