JAMES-2441 Replace custom loader for SpamAssassin mailet by a generic system
We now can inject overrides to the mailet configuration. That way, next time we need to customize a mailet configuration, we don't need to write another MailetLoader. Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/02301a64 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/02301a64 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/02301a64 Branch: refs/heads/master Commit: 02301a640b4f4db6ed9273c460c8df2fdccd7520 Parents: ca78f13 Author: benwa <[email protected]> Authored: Wed Jun 27 14:21:10 2018 +0700 Committer: benwa <[email protected]> Committed: Mon Jul 2 11:48:52 2018 +0700 ---------------------------------------------------------------------- .../server/CamelMailetContainerModule.java | 3 + .../apache/james/utils/GuiceMailetLoader.java | 19 ++++- .../utils/MailetConfigurationOverride.java | 41 +++++++++ .../james/utils/GuiceMailetLoaderTest.java | 23 ++++-- .../SpamAssassinGuiceMailetLoader.java | 87 -------------------- .../methods/integration/SpamAssassinModule.java | 25 +++++- 6 files changed, 98 insertions(+), 100 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java b/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java index a8b9d7c..d96d2ab 100644 --- a/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java +++ b/server/container/guice/mailet/src/main/java/org/apache/james/modules/server/CamelMailetContainerModule.java @@ -47,6 +47,7 @@ import org.apache.james.user.api.UsersRepository; import org.apache.james.utils.ConfigurationPerformer; import org.apache.james.utils.GuiceMailetLoader; import org.apache.james.utils.GuiceMatcherLoader; +import org.apache.james.utils.MailetConfigurationOverride; import org.apache.mailet.MailetContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,6 +78,8 @@ public class CamelMailetContainerModule extends AbstractModule { bind(MailetLoader.class).to(GuiceMailetLoader.class); bind(MatcherLoader.class).to(GuiceMatcherLoader.class); + Multibinder.newSetBinder(binder(), MailetConfigurationOverride.class); + Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(MailetModuleConfigurationPerformer.class); Multibinder<CamelMailetContainerModule.TransportProcessorCheck> transportProcessorChecks = Multibinder.newSetBinder(binder(), CamelMailetContainerModule.TransportProcessorCheck.class); http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java index eb0975c..096aaee 100644 --- a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceMailetLoader.java @@ -19,12 +19,17 @@ package org.apache.james.utils; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + import javax.mail.MessagingException; import org.apache.james.mailetcontainer.api.MailetLoader; import org.apache.mailet.Mailet; import org.apache.mailet.MailetConfig; +import com.github.steveash.guavate.Guavate; import com.google.inject.Inject; import com.google.inject.Injector; @@ -33,21 +38,31 @@ public class GuiceMailetLoader implements MailetLoader { private static final String STANDARD_PACKAGE = "org.apache.james.transport.mailets."; private final GuiceGenericLoader<Mailet> genericLoader; + private final Map<Class<? extends Mailet>, MailetConfig> configurationOverrides; @Inject - public GuiceMailetLoader(Injector injector, ExtendedClassLoader extendedClassLoader) { + public GuiceMailetLoader(Injector injector, ExtendedClassLoader extendedClassLoader, Set<MailetConfigurationOverride> mailetConfigurationOverrides) { this.genericLoader = new GuiceGenericLoader<>(injector, extendedClassLoader, STANDARD_PACKAGE); + this.configurationOverrides = mailetConfigurationOverrides.stream() + .collect(Guavate.toImmutableMap( + MailetConfigurationOverride::getClazz, + MailetConfigurationOverride::getNewConfiguration)); } @Override public Mailet getMailet(MailetConfig config) throws MessagingException { try { Mailet result = genericLoader.instanciate(config.getMailetName()); - result.init(config); + result.init(resolveConfiguration(result, config)); return result; } catch (Exception e) { throw new MessagingException("Can not load mailet " + config.getMailetName(), e); } } + private MailetConfig resolveConfiguration(Mailet result, MailetConfig providedConfiguration) { + return Optional.ofNullable(configurationOverrides.get(result.getClass())) + .orElse(providedConfiguration); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/container/guice/mailet/src/main/java/org/apache/james/utils/MailetConfigurationOverride.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/MailetConfigurationOverride.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/MailetConfigurationOverride.java new file mode 100644 index 0000000..57fb752 --- /dev/null +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/MailetConfigurationOverride.java @@ -0,0 +1,41 @@ +/**************************************************************** + * 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.utils; + +import org.apache.mailet.Mailet; +import org.apache.mailet.MailetConfig; + +public class MailetConfigurationOverride { + private final Class<? extends Mailet> clazz; + private final MailetConfig newConfiguration; + + public MailetConfigurationOverride(Class<? extends Mailet> clazz, MailetConfig newConfiguration) { + this.clazz = clazz; + this.newConfiguration = newConfiguration; + } + + public Class<? extends Mailet> getClazz() { + return clazz; + } + + public MailetConfig getNewConfiguration() { + return newConfiguration; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMailetLoaderTest.java ---------------------------------------------------------------------- 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 0bafffd..b0cfae0 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 @@ -35,11 +35,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import com.google.common.collect.ImmutableSet; import com.google.inject.Guice; import com.google.inject.Injector; public class GuiceMailetLoaderTest { + public static final ImmutableSet<MailetConfigurationOverride> NO_MAILET_CONFIG_OVERRIDES = ImmutableSet.of(); @Rule public ExpectedException expectedException = ExpectedException.none(); private Injector injector = Guice.createInjector(); @@ -47,7 +49,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldLoadClass() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + new ExtendedClassLoader(THROWING_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("AddFooter") @@ -60,7 +63,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + new ExtendedClassLoader(THROWING_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("sub.TestMailet") @@ -73,7 +77,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldThrowOnBadType() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + new ExtendedClassLoader(THROWING_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); @@ -86,7 +91,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldLoadClassWhenInExtensionsJars() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); + new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") @@ -100,7 +106,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldBrowseRecursivelyExtensionsJars() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); + new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") @@ -114,7 +121,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailedShouldAllowCustomPackages() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); + new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("com.custom.mailets.AnotherMailet") @@ -128,7 +136,8 @@ public class GuiceMailetLoaderTest { @Test public void getMailetShouldThrowOnUnknownMailet() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, - new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); + new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), + NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinGuiceMailetLoader.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinGuiceMailetLoader.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinGuiceMailetLoader.java deleted file mode 100644 index 1ffcd12..0000000 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinGuiceMailetLoader.java +++ /dev/null @@ -1,87 +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.jmap.methods.integration; - -import javax.mail.MessagingException; - -import org.apache.commons.configuration.BaseConfiguration; -import org.apache.james.mailbox.spamassassin.SpamAssassinConfiguration; -import org.apache.james.mailetcontainer.api.MailetLoader; -import org.apache.james.mailetcontainer.impl.MailetConfigImpl; -import org.apache.james.transport.mailets.SpamAssassin; -import org.apache.james.util.Host; -import org.apache.james.utils.ExtendedClassLoader; -import org.apache.james.utils.GuiceGenericLoader; -import org.apache.mailet.Mailet; -import org.apache.mailet.MailetConfig; - -import com.google.inject.Inject; -import com.google.inject.Injector; - -public class SpamAssassinGuiceMailetLoader implements MailetLoader { - - private static final String STANDARD_PACKAGE = "org.apache.james.transport.mailets."; - - private final GuiceGenericLoader<Mailet> genericLoader; - private final SpamAssassinConfiguration spamAssassinConfiguration; - - @Inject - public SpamAssassinGuiceMailetLoader(Injector injector, ExtendedClassLoader extendedClassLoader, SpamAssassinConfiguration spamAssassinConfiguration) { - this.genericLoader = new GuiceGenericLoader<>(injector, extendedClassLoader, STANDARD_PACKAGE); - this.spamAssassinConfiguration = spamAssassinConfiguration; - } - - @Override - public Mailet getMailet(MailetConfig config) throws MessagingException { - String mailetName = config.getMailetName(); - try { - if (mailetName.equals(SpamAssassin.class.getSimpleName())) { - return configureSpamAssassinMailet(mailetName); - } - Mailet result = genericLoader.instanciate(mailetName); - result.init(config); - return result; - } catch (Exception e) { - throw new MessagingException("Can not load mailet " + mailetName, e); - } - } - - private Mailet configureSpamAssassinMailet(String mailetName) throws Exception { - Mailet mailet = genericLoader.instanciate(mailetName); - mailet.init(spamAssassinMailetConfig()); - return mailet; - } - - private MailetConfigImpl spamAssassinMailetConfig() throws MessagingException { - BaseConfiguration baseConfiguration = new BaseConfiguration(); - Host host = getHostOrThrow(spamAssassinConfiguration); - baseConfiguration.addProperty(SpamAssassin.SPAMD_HOST, host.getHostName()); - baseConfiguration.addProperty(SpamAssassin.SPAMD_PORT, host.getPort()); - - MailetConfigImpl mailetConfig = new MailetConfigImpl(); - mailetConfig.setConfiguration(baseConfiguration); - return mailetConfig; - } - - private Host getHostOrThrow(SpamAssassinConfiguration spamAssassinConfiguration) throws MessagingException { - return spamAssassinConfiguration.getHost() - .orElseThrow(() -> new MessagingException("SpamAssassin configuration missing")); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/02301a64/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinModule.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinModule.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinModule.java index c70ad26..ef1ea07 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinModule.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SpamAssassinModule.java @@ -22,15 +22,17 @@ import java.util.Optional; import javax.inject.Singleton; +import org.apache.commons.configuration.BaseConfiguration; import org.apache.james.mailbox.spamassassin.SpamAssassinConfiguration; -import org.apache.james.mailetcontainer.api.MailetLoader; +import org.apache.james.mailetcontainer.impl.MailetConfigImpl; import org.apache.james.util.Host; import org.apache.james.util.scanner.SpamAssassinExtension; import org.apache.james.util.scanner.SpamAssassinExtension.SpamAssassin; +import org.apache.james.utils.MailetConfigurationOverride; import com.google.inject.AbstractModule; import com.google.inject.Provides; -import com.google.inject.Scopes; +import com.google.inject.multibindings.Multibinder; public class SpamAssassinModule extends AbstractModule { @@ -42,8 +44,12 @@ public class SpamAssassinModule extends AbstractModule { @Override protected void configure() { - bind(SpamAssassinGuiceMailetLoader.class).in(Scopes.SINGLETON); - bind(MailetLoader.class).to(SpamAssassinGuiceMailetLoader.class); + Multibinder.newSetBinder(binder(), MailetConfigurationOverride.class) + .addBinding() + .toInstance( + new MailetConfigurationOverride( + org.apache.james.transport.mailets.SpamAssassin.class, + spamAssassinMailetConfig())); } @Provides @@ -53,4 +59,15 @@ public class SpamAssassinModule extends AbstractModule { return new SpamAssassinConfiguration(Optional.of(Host.from(spamAssassin.getIp(), spamAssassin.getBindingPort()))); } + private MailetConfigImpl spamAssassinMailetConfig() { + BaseConfiguration baseConfiguration = new BaseConfiguration(); + Host host = Host.from(spamAssassinExtension.getSpamAssassin().getIp(), spamAssassinExtension.getSpamAssassin().getBindingPort()); + baseConfiguration.addProperty(org.apache.james.transport.mailets.SpamAssassin.SPAMD_HOST, host.getHostName()); + baseConfiguration.addProperty(org.apache.james.transport.mailets.SpamAssassin.SPAMD_PORT, host.getPort()); + + MailetConfigImpl mailetConfig = new MailetConfigImpl(); + mailetConfig.setConfiguration(baseConfiguration); + return mailetConfig; + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
