This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new 9b790400c4 [JAMES-4126] improve GuiceMailetLoader (#2694) 9b790400c4 is described below commit 9b790400c48b17075f2b7bcf59fe88159eb59370 Author: Jean Helou <j...@xn--gml-cma.com> AuthorDate: Wed Apr 2 05:36:54 2025 +0200 [JAMES-4126] improve GuiceMailetLoader (#2694) --- .../java/org/apache/mailet/base/GenericMailet.java | 7 +++ .../org/apache/james/utils/GuiceMailetLoader.java | 22 +++++--- .../mailets/sub/ConstructorBoundTestMailet.java | 59 ++++++++++++++++++++++ .../apache/james/utils/GuiceMailetLoaderTest.java | 15 ++++++ 4 files changed, 97 insertions(+), 6 deletions(-) diff --git a/mailet/base/src/main/java/org/apache/mailet/base/GenericMailet.java b/mailet/base/src/main/java/org/apache/mailet/base/GenericMailet.java index d890b3898b..5aee0a39ab 100644 --- a/mailet/base/src/main/java/org/apache/mailet/base/GenericMailet.java +++ b/mailet/base/src/main/java/org/apache/mailet/base/GenericMailet.java @@ -66,6 +66,13 @@ public abstract class GenericMailet implements Mailet, MailetConfig { private MailetConfig config = null; + public GenericMailet(MailetConfig config) { + this.config = config; + } + + public GenericMailet() { + } + /** * Called by the mailer container to indicate to a mailet that the * mailet is being taken out of service. 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 42dca2746b..6ffaae6f02 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 @@ -43,17 +43,22 @@ public class GuiceMailetLoader implements MailetLoader { public GuiceMailetLoader(GuiceGenericLoader genericLoader, Set<MailetConfigurationOverride> mailetConfigurationOverrides) { this.genericLoader = genericLoader; this.configurationOverrides = mailetConfigurationOverrides.stream() - .collect(ImmutableMap.toImmutableMap( - MailetConfigurationOverride::getClazz, - MailetConfigurationOverride::getNewConfiguration)); + .collect(ImmutableMap.toImmutableMap( + MailetConfigurationOverride::getClazz, + MailetConfigurationOverride::getNewConfiguration + )); } @Override public Mailet getMailet(MailetConfig config) throws MessagingException { try { ClassName className = new ClassName(config.getMailetName()); - Mailet result = genericLoader.<Mailet>withNamingSheme(MAILET_NAMING_SCHEME) - .instantiate(className); + var mailetLoader = genericLoader.<Mailet>withNamingSheme(MAILET_NAMING_SCHEME); + Class<Mailet> mailetClass = mailetLoader.locateClass(className); + MailetConfig mailetConfig = resolveConfiguration(mailetClass, config); + Mailet result = mailetLoader.withChildModule( + binder -> binder.bind(MailetConfig.class).toInstance(mailetConfig) + ).instantiate(className); result.init(resolveConfiguration(result, config)); return result; } catch (Exception e) { @@ -63,6 +68,11 @@ public class GuiceMailetLoader implements MailetLoader { private MailetConfig resolveConfiguration(Mailet result, MailetConfig providedConfiguration) { return Optional.ofNullable(configurationOverrides.get(result.getClass())) - .orElse(providedConfiguration); + .orElse(providedConfiguration); + } + + private MailetConfig resolveConfiguration(Class<?> mailetClass, MailetConfig providedConfiguration) { + return Optional.ofNullable(configurationOverrides.get(mailetClass)) + .orElse(providedConfiguration); } } diff --git a/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/ConstructorBoundTestMailet.java b/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/ConstructorBoundTestMailet.java new file mode 100644 index 0000000000..fb06ab24ea --- /dev/null +++ b/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/ConstructorBoundTestMailet.java @@ -0,0 +1,59 @@ +/**************************************************************** + * 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.transport.mailets.sub; + +import jakarta.inject.Inject; +import jakarta.mail.MessagingException; + +import org.apache.mailet.Mail; +import org.apache.mailet.Mailet; +import org.apache.mailet.MailetConfig; + +public class ConstructorBoundTestMailet implements Mailet { + private final MailetConfig config; + + @Inject + public ConstructorBoundTestMailet(MailetConfig config) { + this.config = config; + } + + @Override + public void init(MailetConfig config) throws MessagingException { + } + + @Override + public void service(Mail mail) throws MessagingException { + + } + + @Override + public void destroy() { + } + + @Override + public MailetConfig getMailetConfig() { + return config; + } + + @Override + public String getMailetInfo() { + return ""; + } +} 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 257eafa75a..792955cd0a 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 @@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import jakarta.mail.MessagingException; import org.apache.james.transport.mailets.AddFooter; +import org.apache.james.transport.mailets.sub.ConstructorBoundTestMailet; import org.apache.james.transport.mailets.sub.TestMailet; import org.apache.mailet.Mailet; import org.apache.mailet.base.test.FakeMail; @@ -56,6 +57,20 @@ class GuiceMailetLoaderTest { assertThat(mailet).isInstanceOf(AddFooter.class); } + @Test + void getMailetShouldLoadClassBindingConfigurationByConstructor() throws Exception { + GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES); + + Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() + .mailetName("sub.ConstructorBoundTestMailet") + .mailetContext(FakeMailContext.defaultContext()) + .build()); + + assertThat(mailet).isInstanceOf(ConstructorBoundTestMailet.class); + assertThat(mailet.getMailetConfig()).isNotNull(); + } + @Test void getMailetShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClassLoader(THROWING_FILE_SYSTEM)); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org