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 9beb98aeb05b1248b46648dd46d0abde03ad1635
Author: Benoit Tellier <[email protected]>
AuthorDate: Wed Nov 18 16:54:56 2020 +0700

    JAMES-3441 Enable disabling group event consumption
---
 .../destination/conf/listeners.xml                 |   1 +
 .../destination/conf/listeners.xml                 |   1 +
 .../modules/mailbox/ListenersConfiguration.java    |  26 ++++-
 .../mailbox/MailboxListenersLoaderImpl.java        |  10 +-
 .../apache/james/DisabledGroupExecutionTest.java   | 114 +++++++++++++++++++++
 5 files changed, 143 insertions(+), 9 deletions(-)

diff --git 
a/dockerfiles/run/guice/cassandra-rabbitmq-ldap/destination/conf/listeners.xml 
b/dockerfiles/run/guice/cassandra-rabbitmq-ldap/destination/conf/listeners.xml
index 948cb92..758c823 100644
--- 
a/dockerfiles/run/guice/cassandra-rabbitmq-ldap/destination/conf/listeners.xml
+++ 
b/dockerfiles/run/guice/cassandra-rabbitmq-ldap/destination/conf/listeners.xml
@@ -21,6 +21,7 @@
 <!-- Read https://james.apache.org/server/config-listeners.html for further 
details -->
 
 <listeners>
+  <executeGroupListeners>true</executeGroupListeners>
   <listener>
     <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class>
     <async>true</async>
diff --git 
a/dockerfiles/run/guice/cassandra-rabbitmq/destination/conf/listeners.xml 
b/dockerfiles/run/guice/cassandra-rabbitmq/destination/conf/listeners.xml
index 948cb92..758c823 100644
--- a/dockerfiles/run/guice/cassandra-rabbitmq/destination/conf/listeners.xml
+++ b/dockerfiles/run/guice/cassandra-rabbitmq/destination/conf/listeners.xml
@@ -21,6 +21,7 @@
 <!-- Read https://james.apache.org/server/config-listeners.html for further 
details -->
 
 <listeners>
+  <executeGroupListeners>true</executeGroupListeners>
   <listener>
     <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class>
     <async>true</async>
diff --git 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java
 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java
index 14abbd6..5ed36e6 100644
--- 
a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java
+++ 
b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java
@@ -19,36 +19,52 @@
 package org.apache.james.modules.mailbox;
 
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.commons.configuration2.HierarchicalConfiguration;
 import org.apache.commons.configuration2.tree.ImmutableNode;
 
 import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 
 public class ListenersConfiguration {
 
     public static ListenersConfiguration of(ListenerConfiguration... 
listenersConfiguration) {
-        return new 
ListenersConfiguration(ImmutableList.copyOf(listenersConfiguration));
+        return new 
ListenersConfiguration(ImmutableList.copyOf(listenersConfiguration), true);
+    }
+
+    public static ListenersConfiguration disabled() {
+        return new ListenersConfiguration(ImmutableList.of(), false);
     }
 
     public static ListenersConfiguration 
from(HierarchicalConfiguration<ImmutableNode> configuration) {
         List<HierarchicalConfiguration<ImmutableNode>> listeners = 
configuration.configurationsAt("listener");
+        Optional<Boolean> consumeGroups = 
Optional.ofNullable(configuration.getBoolean("executeGroupListeners", null));
 
         return new ListenersConfiguration(listeners
-            .stream()
-            .map(ListenerConfiguration::from)
-            .collect(Guavate.toImmutableList()));
+                .stream()
+                .map(ListenerConfiguration::from)
+                .collect(Guavate.toImmutableList()),
+            consumeGroups.orElse(true));
     }
     
     private final List<ListenerConfiguration> listenersConfiguration;
+    private final boolean enableGroupListenerConsumption;
 
-    @VisibleForTesting ListenersConfiguration(List<ListenerConfiguration> 
listenersConfiguration) {
+    @VisibleForTesting ListenersConfiguration(List<ListenerConfiguration> 
listenersConfiguration, boolean enableGroupListenerConsumption) {
+        Preconditions.checkArgument(enableGroupListenerConsumption || 
listenersConfiguration.isEmpty(),
+            "'executeGroupListeners' can not be false while extra listeners 
are configured");
         this.listenersConfiguration = listenersConfiguration;
+        this.enableGroupListenerConsumption = enableGroupListenerConsumption;
     }
 
     public List<ListenerConfiguration> getListenersConfiguration() {
         return listenersConfiguration;
     }
+
+    public boolean isGroupListenerConsumptionEnabled() {
+        return enableGroupListenerConsumption;
+    }
 }
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 da6b8e0..2b40980 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
@@ -70,11 +70,13 @@ public class MailboxListenersLoaderImpl implements 
Configurable, MailboxListener
     public void configure(ListenersConfiguration listenersConfiguration) {
         LOGGER.info("Loading user registered mailbox listeners");
 
-        guiceDefinedListeners.forEach(eventBus::register);
+        if (listenersConfiguration.isGroupListenerConsumptionEnabled()) {
+            guiceDefinedListeners.forEach(eventBus::register);
 
-        listenersConfiguration.getListenersConfiguration().stream()
-            .map(this::createListener)
-            .forEach(this::register);
+            listenersConfiguration.getListenersConfiguration().stream()
+                .map(this::createListener)
+                .forEach(this::register);
+        }
     }
 
     @Override
diff --git 
a/server/container/guice/memory-guice/src/test/java/org/apache/james/DisabledGroupExecutionTest.java
 
b/server/container/guice/memory-guice/src/test/java/org/apache/james/DisabledGroupExecutionTest.java
new file mode 100644
index 0000000..b2579cd
--- /dev/null
+++ 
b/server/container/guice/memory-guice/src/test/java/org/apache/james/DisabledGroupExecutionTest.java
@@ -0,0 +1,114 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE;
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE_PASSWORD;
+import static org.apache.james.jmap.JMAPTestingConstants.BOB;
+import static org.apache.james.jmap.JMAPTestingConstants.BOB_PASSWORD;
+import static org.apache.james.jmap.JMAPTestingConstants.DOMAIN;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.james.mailbox.events.Event;
+import org.apache.james.mailbox.events.Group;
+import org.apache.james.mailbox.events.MailboxListener;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.modules.MailboxProbeImpl;
+import org.apache.james.modules.TestJMAPServerModule;
+import org.apache.james.modules.mailbox.ListenersConfiguration;
+import org.apache.james.utils.DataProbeImpl;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.reactivestreams.Publisher;
+
+import com.google.inject.multibindings.Multibinder;
+
+import reactor.core.publisher.Mono;
+
+class DisabledGroupExecutionTest {
+
+    public static class ReactiveNoopMailboxListener implements 
MailboxListener.ReactiveGroupMailboxListener {
+        public static class NoopMailboxListenerGroup extends Group {
+
+        }
+
+        static final Group GROUP = new NoopMailboxListenerGroup();
+
+        private final AtomicBoolean executed = new AtomicBoolean(false);
+
+        @Override
+        public Group getDefaultGroup() {
+            return GROUP;
+        }
+
+        @Override
+        public Publisher<Void> reactiveEvent(Event event) {
+            return Mono.fromRunnable(() -> executed.set(true));
+        }
+
+        @Override
+        public boolean isHandling(Event event) {
+            return true;
+        }
+
+        @Override
+        public void event(Event event) {
+            Mono.from(reactiveEvent(event)).block();
+        }
+
+        public boolean isExecuted() {
+            return executed.get();
+        }
+    }
+
+    static ReactiveNoopMailboxListener listener = new 
ReactiveNoopMailboxListener();
+
+    @RegisterExtension
+    static JamesServerExtension jamesServerExtension = new 
JamesServerBuilder<>(JamesServerBuilder.defaultConfigurationProvider())
+        .server(configuration -> 
MemoryJamesServerMain.createServer(configuration)
+            .overrideWith(new TestJMAPServerModule())
+            .overrideWith(binder -> Multibinder.newSetBinder(binder, 
MailboxListener.ReactiveGroupMailboxListener.class)
+                .addBinding()
+                .toInstance(listener))
+            .overrideWith(binder -> binder.bind(ListenersConfiguration.class)
+                .toInstance(ListenersConfiguration.disabled())))
+        .lifeCycle(JamesServerExtension.Lifecycle.PER_CLASS)
+        .build();
+
+    @BeforeEach
+    void setUp(GuiceJamesServer guiceJamesServer) throws Exception {
+        DataProbeImpl dataProbe = 
guiceJamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.fluent()
+            .addDomain(DOMAIN)
+            .addUser(ALICE.asString(), ALICE_PASSWORD)
+            .addUser(BOB.asString(), BOB_PASSWORD);
+    }
+
+    @Test
+    void listenerShouldNotBeExecutedWhenDisabled(GuiceJamesServer server) {
+        server.getProbe(MailboxProbeImpl.class)
+            .createMailbox(MailboxPath.inbox(ALICE));
+
+        assertThat(listener.isExecuted()).isFalse();
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to