JAMES-2368 Allow Guice module to enforce mailbox listeners
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/991194ab Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/991194ab Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/991194ab Branch: refs/heads/master Commit: 991194ab0fddd1e0a25f264c31f256ac8a8bb49b Parents: bddb372 Author: benwa <btell...@linagora.com> Authored: Wed May 2 11:57:53 2018 +0700 Committer: benwa <btell...@linagora.com> Committed: Fri May 4 13:39:06 2018 +0700 ---------------------------------------------------------------------- .../destination/conf/listeners.xml | 3 - .../cassandra/destination/conf/listeners.xml | 3 - .../src/test/resources/listeners.xml | 3 - .../modules/mailbox/DefaultEventModule.java | 8 +- .../modules/mailbox/GlobalMailboxListeners.java | 76 ----------- .../modules/mailbox/ListenerConfiguration.java | 44 +++++++ .../modules/mailbox/ListenersConfiguration.java | 47 +++++++ .../mailbox/MailboxListenersLoaderImpl.java | 86 ++++++++++++ .../modules/mailbox/NoopMailboxListener.java | 2 - .../mailbox/GlobalMailboxListenersTest.java | 132 ------------------- .../mailbox/ListenerConfigurationTest.java | 56 ++++++++ .../mailbox/MailboxListenersLoaderImplTest.java | 112 ++++++++++++++++ .../src/test/resources/listeners.xml | 3 - .../java/org/apache/james/jmap/JMAPModule.java | 4 + .../src/test/resources/listeners.xml | 3 - .../src/test/resources/listeners.xml | 6 - 16 files changed, 354 insertions(+), 234 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/dockerfiles/run/guice/cassandra-ldap/destination/conf/listeners.xml ---------------------------------------------------------------------- diff --git a/dockerfiles/run/guice/cassandra-ldap/destination/conf/listeners.xml b/dockerfiles/run/guice/cassandra-ldap/destination/conf/listeners.xml index 35ca919..eabdb5e 100644 --- a/dockerfiles/run/guice/cassandra-ldap/destination/conf/listeners.xml +++ b/dockerfiles/run/guice/cassandra-ldap/destination/conf/listeners.xml @@ -20,9 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> <listener> http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/dockerfiles/run/guice/cassandra/destination/conf/listeners.xml ---------------------------------------------------------------------- diff --git a/dockerfiles/run/guice/cassandra/destination/conf/listeners.xml b/dockerfiles/run/guice/cassandra/destination/conf/listeners.xml index 35ca919..eabdb5e 100644 --- a/dockerfiles/run/guice/cassandra/destination/conf/listeners.xml +++ b/dockerfiles/run/guice/cassandra/destination/conf/listeners.xml @@ -20,9 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> <listener> http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/cassandra-guice/src/test/resources/listeners.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/resources/listeners.xml b/server/container/guice/cassandra-guice/src/test/resources/listeners.xml index 35ca919..eabdb5e 100644 --- a/server/container/guice/cassandra-guice/src/test/resources/listeners.xml +++ b/server/container/guice/cassandra-guice/src/test/resources/listeners.xml @@ -20,9 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> <listener> http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java index 96e695c..c5895a9 100644 --- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/DefaultEventModule.java @@ -25,6 +25,7 @@ import javax.inject.Inject; import org.apache.commons.configuration.ConfigurationException; import org.apache.james.lifecycle.api.Configurable; +import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.store.event.DefaultDelegatingMailboxListener; import org.apache.james.mailbox.store.event.DelegatingMailboxListener; import org.apache.james.mailbox.store.event.EventDelivery; @@ -56,16 +57,17 @@ public class DefaultEventModule extends AbstractModule { bind(ListeningCurrentQuotaUpdater.class).in(Scopes.SINGLETON); bind(MailboxAnnotationListener.class).in(Scopes.SINGLETON); - bind(GlobalMailboxListeners.class).in(Scopes.SINGLETON); + bind(MailboxListenersLoader.class).in(Scopes.SINGLETON); + Multibinder.newSetBinder(binder(), MailboxListener.class); } @Singleton public static class ListenerRegistrationPerformer implements ConfigurationPerformer { private final ConfigurationProvider configurationProvider; - private final GlobalMailboxListeners listeners; + private final MailboxListenersLoader listeners; @Inject - public ListenerRegistrationPerformer(ConfigurationProvider configurationProvider, GlobalMailboxListeners listeners) { + public ListenerRegistrationPerformer(ConfigurationProvider configurationProvider, MailboxListenersLoader listeners) { this.configurationProvider = configurationProvider; this.listeners = listeners; } http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/GlobalMailboxListeners.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/GlobalMailboxListeners.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/GlobalMailboxListeners.java deleted file mode 100644 index d010174..0000000 --- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/GlobalMailboxListeners.java +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************** - * 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.modules.mailbox; - -import java.util.List; - -import org.apache.commons.configuration.HierarchicalConfiguration; -import org.apache.james.lifecycle.api.Configurable; -import org.apache.james.mailbox.MailboxListener; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.store.event.MailboxListenerRegistry; -import org.apache.james.utils.ExtendedClassLoader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.base.Throwables; -import com.google.inject.Inject; -import com.google.inject.Injector; - -public class GlobalMailboxListeners implements Configurable { - - private static final Logger LOGGER = LoggerFactory.getLogger(GlobalMailboxListeners.class); - - private final Injector injector; - private final MailboxListenerRegistry registry; - private final ExtendedClassLoader classLoader; - - @Inject - public GlobalMailboxListeners(Injector injector, MailboxListenerRegistry registry, ExtendedClassLoader classLoader) { - this.injector = injector; - this.registry = registry; - this.classLoader = classLoader; - } - - @Override - public void configure(HierarchicalConfiguration configuration) { - LOGGER.info("Loading user registered mailbox listeners"); - - List<HierarchicalConfiguration> listenersConfiguration = configuration.configurationsAt("listener"); - - listenersConfiguration.forEach(this::configureListener); - } - - @VisibleForTesting void configureListener(HierarchicalConfiguration configuration) { - String listenerClass = configuration.getString("class"); - Preconditions.checkState(!Strings.isNullOrEmpty(listenerClass), "class name is mandatory"); - try { - LOGGER.info("Loading user registered mailbox listener {}", listenerClass); - Class<MailboxListener> clazz = classLoader.locateClass(listenerClass); - MailboxListener listener = injector.getInstance(clazz); - registry.addGlobalListener(listener); - } catch (ClassNotFoundException | MailboxException e) { - LOGGER.error("Error while loading global listener {}", listenerClass, e); - Throwables.propagate(e); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/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 new file mode 100644 index 0000000..7258190 --- /dev/null +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenerConfiguration.java @@ -0,0 +1,44 @@ +/**************************************************************** + * 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.modules.mailbox; + +import org.apache.commons.configuration.HierarchicalConfiguration; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +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); + } + + private final String clazz; + + @VisibleForTesting ListenerConfiguration(String clazz) { + this.clazz = clazz; + } + + public String getClazz() { + return clazz; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..b499a1a --- /dev/null +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/ListenersConfiguration.java @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.modules.mailbox; + +import java.util.List; + +import org.apache.commons.configuration.HierarchicalConfiguration; + +import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; + +public class ListenersConfiguration { + + public static ListenersConfiguration from(HierarchicalConfiguration configuration) { + return new ListenersConfiguration( + configuration.configurationsAt("listener") + .stream() + .map(ListenerConfiguration::from) + .collect(Guavate.toImmutableList())); + } + + private final List<ListenerConfiguration> listenersConfiguration; + + @VisibleForTesting ListenersConfiguration(List<ListenerConfiguration> listenersConfiguration) { + this.listenersConfiguration = listenersConfiguration; + } + + public List<ListenerConfiguration> getListenersConfiguration() { + return listenersConfiguration; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/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 new file mode 100644 index 0000000..60219b8 --- /dev/null +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImpl.java @@ -0,0 +1,86 @@ +/**************************************************************** + * 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.modules.mailbox; + +import java.util.Set; + +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.james.lifecycle.api.Configurable; +import org.apache.james.mailbox.MailboxListener; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.store.event.MailboxListenerRegistry; +import org.apache.james.utils.ExtendedClassLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.VisibleForTesting; +import com.google.inject.Inject; +import com.google.inject.Injector; + +public class MailboxListenersLoader implements Configurable { + + private static final Logger LOGGER = LoggerFactory.getLogger(MailboxListenersLoader.class); + + private final Injector injector; + private final MailboxListenerRegistry registry; + private final ExtendedClassLoader classLoader; + private final Set<MailboxListener> guiceDefinedListeners; + + @Inject + public MailboxListenersLoader(Injector injector, MailboxListenerRegistry registry, + ExtendedClassLoader classLoader, Set<MailboxListener> guiceDefinedListeners) { + this.injector = injector; + this.registry = registry; + this.classLoader = classLoader; + this.guiceDefinedListeners = guiceDefinedListeners; + } + + @Override + public void configure(HierarchicalConfiguration configuration) { + LOGGER.info("Loading user registered mailbox listeners"); + + ListenersConfiguration listenersConfiguration = ListenersConfiguration.from(configuration); + + guiceDefinedListeners.forEach(this::register); + listenersConfiguration.getListenersConfiguration() + .forEach(this::configureListener); + } + + @VisibleForTesting void configureListener(ListenerConfiguration configuration) { + String listenerClass = configuration.getClazz(); + try { + LOGGER.info("Loading user registered mailbox listener {}", listenerClass); + Class<MailboxListener> clazz = classLoader.locateClass(listenerClass); + MailboxListener listener = injector.getInstance(clazz); + register(listener); + } catch (ClassNotFoundException e) { + LOGGER.error("Error while loading user registered global listener {}", listenerClass, e); + throw new RuntimeException(e); + } + } + + private void register(MailboxListener listener) { + try { + registry.addGlobalListener(listener); + } catch (MailboxException e) { + LOGGER.error("Error while registering global listener {}", listener, e); + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/NoopMailboxListener.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/NoopMailboxListener.java b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/NoopMailboxListener.java index ee08297..06b5f08 100644 --- a/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/NoopMailboxListener.java +++ b/server/container/guice/mailbox/src/main/java/org/apache/james/modules/mailbox/NoopMailboxListener.java @@ -20,8 +20,6 @@ package org.apache.james.modules.mailbox; import org.apache.james.mailbox.Event; import org.apache.james.mailbox.MailboxListener; -import org.apache.james.mailbox.MailboxListener.ExecutionMode; -import org.apache.james.mailbox.MailboxListener.ListenerType; public class NoopMailboxListener implements MailboxListener { http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/GlobalMailboxListenersTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/GlobalMailboxListenersTest.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/GlobalMailboxListenersTest.java deleted file mode 100644 index 40cd58a..0000000 --- a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/GlobalMailboxListenersTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/**************************************************************** - * 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.modules.mailbox; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.DefaultConfigurationBuilder; -import org.apache.james.filesystem.api.FileSystem; -import org.apache.james.mailbox.store.event.MailboxListenerRegistry; -import org.apache.james.utils.ExtendedClassLoader; -import org.junit.Before; -import org.junit.Test; - -import com.google.inject.Guice; - -public class GlobalMailboxListenersTest { - - private MailboxListenerRegistry registry; - private GlobalMailboxListeners testee; - - @Before - public void setup() throws Exception { - FileSystem fileSystem = mock(FileSystem.class); - when(fileSystem.getFile(anyString())) - .thenThrow(new FileNotFoundException()); - - registry = new MailboxListenerRegistry(); - testee = new GlobalMailboxListeners(Guice.createInjector(), registry, - new ExtendedClassLoader(fileSystem)); - } - - @Test - public void configureListenerShouldThrowWhenClassIsNotInTheConfiguration() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - - assertThatThrownBy(() -> testee.configureListener(configuration)) - .isInstanceOf(IllegalStateException.class); - } - - @Test - public void configureListenerShouldThrowWhenClassIsEmpty() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.addProperty("class", ""); - - assertThatThrownBy(() -> testee.configureListener(configuration)) - .isInstanceOf(IllegalStateException.class); - } - - @Test - public void configureListenerShouldThrowWhenClassCantBeLoaded() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.addProperty("class", "MyUnknownClass"); - - assertThatThrownBy(() -> testee.configureListener(configuration)) - .isInstanceOf(RuntimeException.class); - } - - @Test - public void configureListenerShouldThrowWhenClassCantBeCastToMailboxListener() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.addProperty("class", "java.lang.String"); - - assertThatThrownBy(() -> testee.configureListener(configuration)) - .isInstanceOf(RuntimeException.class); - } - - @Test - public void configureListenerShouldThrowWhenNotFullClassName() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.addProperty("class", "NoopMailboxListener"); - - assertThatThrownBy(() -> testee.configureListener(configuration)) - .isInstanceOf(RuntimeException.class); - } - - @Test - public void configureListenerShouldAddMailboxListenerWhenConfigurationIsGood() { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.addProperty("class", "org.apache.james.modules.mailbox.NoopMailboxListener"); - - testee.configureListener(configuration); - - assertThat(registry.getGlobalListeners()).hasSize(1); - } - - @Test - public void configureShouldAddMailboxListenersWhenConfigurationIsGood() throws ConfigurationException { - DefaultConfigurationBuilder configuration = toConfigutation("<listeners>" + - "<listener>" + - "<class>org.apache.james.modules.mailbox.NoopMailboxListener</class>" + - "</listener>" + - "<listener>" + - "<class>org.apache.james.modules.mailbox.NoopMailboxListener</class>" + - "</listener>" + - "</listeners>"); - - testee.configure(configuration); - - assertThat(registry.getGlobalListeners()).hasSize(2); - } - - private DefaultConfigurationBuilder toConfigutation(String configurationString) throws ConfigurationException { - DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); - configuration.load(new ByteArrayInputStream(configurationString.getBytes(StandardCharsets.UTF_8))); - return configuration; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/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 new file mode 100644 index 0000000..011f3ec --- /dev/null +++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/ListenerConfigurationTest.java @@ -0,0 +1,56 @@ +/**************************************************************** + * 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.modules.mailbox; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration.DefaultConfigurationBuilder; +import org.junit.Test; + +public class ListenerConfigurationTest { + + @Test + public void fromShouldThrowWhenClassIsNotInTheConfiguration() { + DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); + + assertThatThrownBy(() -> ListenerConfiguration.from(configuration)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void fromShouldThrowWhenClassIsEmpty() { + DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); + configuration.addProperty("class", ""); + + assertThatThrownBy(() -> ListenerConfiguration.from(configuration)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void getClazzShouldReturnTheClassNameFromTheConfiguration() { + DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); + String expectedClazz = "MyClassName"; + configuration.addProperty("class", expectedClazz); + + ListenerConfiguration listenerConfiguration = ListenerConfiguration.from(configuration); + + assertThat(listenerConfiguration.getClazz()).isEqualTo(expectedClazz); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java new file mode 100644 index 0000000..d1673db --- /dev/null +++ b/server/container/guice/mailbox/src/test/java/org/apache/james/modules/mailbox/MailboxListenersLoaderImplTest.java @@ -0,0 +1,112 @@ +/**************************************************************** + * 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.modules.mailbox; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.DefaultConfigurationBuilder; +import org.apache.james.filesystem.api.FileSystem; +import org.apache.james.mailbox.store.event.MailboxListenerRegistry; +import org.apache.james.utils.ExtendedClassLoader; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; + +public class MailboxListenersLoaderTest { + + private MailboxListenerRegistry registry; + private MailboxListenersLoader testee; + + @Before + public void setup() throws Exception { + FileSystem fileSystem = mock(FileSystem.class); + when(fileSystem.getFile(anyString())) + .thenThrow(new FileNotFoundException()); + + registry = new MailboxListenerRegistry(); + testee = new MailboxListenersLoader(Guice.createInjector(), registry, + new ExtendedClassLoader(fileSystem), ImmutableSet.of()); + } + + @Test + public void configureListenerShouldThrowWhenClassCantBeLoaded() { + ListenerConfiguration configuration = new ListenerConfiguration("MyUnknownClass"); + + assertThatThrownBy(() -> testee.configureListener(configuration)) + .isInstanceOf(RuntimeException.class); + } + + @Test + public void configureListenerShouldThrowWhenClassCantBeCastToMailboxListener() { + ListenerConfiguration configuration = new ListenerConfiguration("java.lang.String"); + + assertThatThrownBy(() -> testee.configureListener(configuration)) + .isInstanceOf(RuntimeException.class); + } + + @Test + public void configureListenerShouldThrowWhenNotFullClassName() { + ListenerConfiguration configuration = new ListenerConfiguration("NoopMailboxListener"); + + assertThatThrownBy(() -> testee.configureListener(configuration)) + .isInstanceOf(RuntimeException.class); + } + + @Test + public void configureListenerShouldAddMailboxListenerWhenConfigurationIsGood() { + ListenerConfiguration configuration = new ListenerConfiguration("org.apache.james.modules.mailbox.NoopMailboxListener"); + + testee.configureListener(configuration); + + assertThat(registry.getGlobalListeners()).hasSize(1); + } + + @Test + public void configureShouldAddMailboxListenersWhenConfigurationIsGood() throws ConfigurationException { + DefaultConfigurationBuilder configuration = toConfigutation("<listeners>" + + "<listener>" + + "<class>org.apache.james.modules.mailbox.NoopMailboxListener</class>" + + "</listener>" + + "<listener>" + + "<class>org.apache.james.modules.mailbox.NoopMailboxListener</class>" + + "</listener>" + + "</listeners>"); + + testee.configure(configuration); + + assertThat(registry.getGlobalListeners()).hasSize(2); + } + + private DefaultConfigurationBuilder toConfigutation(String configurationString) throws ConfigurationException { + DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder(); + configuration.load(new ByteArrayInputStream(configurationString.getBytes(StandardCharsets.UTF_8))); + return configuration; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/memory-guice/src/test/resources/listeners.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/memory-guice/src/test/resources/listeners.xml b/server/container/guice/memory-guice/src/test/resources/listeners.xml index 70b32e1..64f0770 100644 --- a/server/container/guice/memory-guice/src/test/resources/listeners.xml +++ b/server/container/guice/memory-guice/src/test/resources/listeners.xml @@ -20,9 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> </listeners> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java index 97e5834..b5bd236 100644 --- a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java +++ b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java @@ -29,6 +29,7 @@ import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.io.FileUtils; import org.apache.james.filesystem.api.FileSystem; +import org.apache.james.jmap.event.PropagateLookupRightListener; import org.apache.james.jmap.mailet.VacationMailet; import org.apache.james.jmap.methods.RequestHandler; import org.apache.james.jmap.send.PostDequeueDecoratorFactory; @@ -38,6 +39,7 @@ import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.jmap.utils.SystemMailboxesProviderImpl; import org.apache.james.jwt.JwtConfiguration; import org.apache.james.lifecycle.api.Configurable; +import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxManager.SearchCapabilities; import org.apache.james.mailetcontainer.impl.MatcherMailetPair; @@ -95,6 +97,8 @@ public class JMAPModule extends AbstractModule { bind(SystemMailboxesProvider.class).to(SystemMailboxesProviderImpl.class); bind(MailQueueItemDecoratorFactory.class).to(PostDequeueDecoratorFactory.class).in(Scopes.SINGLETON); + + Multibinder.newSetBinder(binder(), MailboxListener.class).addBinding().to(PropagateLookupRightListener.class); } @Provides http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml index 35ca919..eabdb5e 100644 --- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml +++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/listeners.xml @@ -20,9 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> <listener> http://git-wip-us.apache.org/repos/asf/james-project/blob/991194ab/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml index 35ca919..64f0770 100644 --- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml +++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/listeners.xml @@ -20,12 +20,6 @@ <listeners> <listener> - <class>org.apache.james.jmap.event.PropagateLookupRightListener</class> - </listener> - <listener> <class>org.apache.james.mailbox.spamassassin.SpamAssassinListener</class> </listener> - <listener> - <class>org.apache.james.mailbox.cassandra.MailboxOperationLoggingListener</class> - </listener> </listeners> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org