Repository: james-project Updated Branches: refs/heads/master c0ea1007f -> 23d22a0e9
JAMES-2429 Simplify mailet configuration with guice One should be able to omit the default package from a path. This would allow us to better structure mailets without paying the cost of FQDN in our configuration. For instance we can write `<mailet match="dlp.DLP" class="...">` instead of being forced to write `<mailet match="org.apache.james.transport.matchers.dlp.DLP" class="...">` Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/19caa770 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/19caa770 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/19caa770 Branch: refs/heads/master Commit: 19caa770da809b386700c2739857f62f6eb66be5 Parents: 9cc86fe Author: benwa <[email protected]> Authored: Thu Jun 14 12:48:08 2018 +0700 Committer: benwa <[email protected]> Committed: Tue Jun 19 16:52:40 2018 +0700 ---------------------------------------------------------------------- .../apache/james/utils/GuiceGenericLoader.java | 23 +++++++++++++---- .../james/transport/mailets/sub/TestMailet.java | 25 +++++++++++++++++++ .../transport/matchers/sub/TestMatcher.java | 26 ++++++++++++++++++++ .../james/utils/GuiceMailetLoaderTest.java | 14 +++++++++++ .../james/utils/GuiceMatcherLoaderTest.java | 13 ++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/19caa770/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java index a3f8130..cdd6d41 100644 --- a/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java +++ b/server/container/guice/mailet/src/main/java/org/apache/james/utils/GuiceGenericLoader.java @@ -19,6 +19,10 @@ package org.apache.james.utils; +import java.util.Optional; + +import org.apache.james.util.OptionalUtils; + import com.google.inject.Injector; public class GuiceGenericLoader<T> { @@ -34,15 +38,24 @@ public class GuiceGenericLoader<T> { public T instanciate(String className) throws Exception { - Class<T> clazz = extendedClassLoader.locateClass(constructFullName(className)); + Class<T> clazz = locateClass(className); return injector.getInstance(clazz); } - private String constructFullName(String name) { - if (! name.contains(".")) { - return defaultPackageName + name; + private Class<T> locateClass(String className) throws ClassNotFoundException { + return OptionalUtils.orSuppliers( + () -> tryLocateClass(className), + () -> tryLocateClass(defaultPackageName + className), + () -> tryLocateClass(defaultPackageName + "." + className)) + .orElseThrow(() -> new ClassNotFoundException(className)); + } + + private Optional<Class<T>> tryLocateClass(String className) { + try { + return Optional.of(extendedClassLoader.locateClass(className)); + } catch (ClassNotFoundException e) { + return Optional.empty(); } - return name; } } http://git-wip-us.apache.org/repos/asf/james-project/blob/19caa770/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/TestMailet.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/TestMailet.java b/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/TestMailet.java new file mode 100644 index 0000000..1c9a98d --- /dev/null +++ b/server/container/guice/mailet/src/test/java/org/apache/james/transport/mailets/sub/TestMailet.java @@ -0,0 +1,25 @@ +/**************************************************************** + * 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 org.apache.james.transport.mailets.Null; + +public class TestMailet extends Null { +} http://git-wip-us.apache.org/repos/asf/james-project/blob/19caa770/server/container/guice/mailet/src/test/java/org/apache/james/transport/matchers/sub/TestMatcher.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/test/java/org/apache/james/transport/matchers/sub/TestMatcher.java b/server/container/guice/mailet/src/test/java/org/apache/james/transport/matchers/sub/TestMatcher.java new file mode 100644 index 0000000..c6b3b41 --- /dev/null +++ b/server/container/guice/mailet/src/test/java/org/apache/james/transport/matchers/sub/TestMatcher.java @@ -0,0 +1,26 @@ +/**************************************************************** + * 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.matchers.sub; + +import org.apache.james.transport.matchers.All; + +public class TestMatcher extends All { + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/19caa770/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 446af81..0bafffd 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 @@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; import org.apache.james.transport.mailets.AddFooter; +import org.apache.james.transport.mailets.sub.TestMailet; import org.apache.mailet.Mailet; import org.apache.mailet.base.test.FakeMailContext; import org.apache.mailet.base.test.FakeMailetConfig; @@ -57,6 +58,19 @@ public class GuiceMailetLoaderTest { } @Test + public void getMailetShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { + GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, + new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + + Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() + .mailetName("sub.TestMailet") + .mailetContext(FakeMailContext.defaultContext()) + .build()); + + assertThat(mailet).isInstanceOf(TestMailet.class); + } + + @Test public void getMailetShouldThrowOnBadType() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM)); http://git-wip-us.apache.org/repos/asf/james-project/blob/19caa770/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMatcherLoaderTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMatcherLoaderTest.java b/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMatcherLoaderTest.java index b1c69cb..01e709b 100644 --- a/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMatcherLoaderTest.java +++ b/server/container/guice/mailet/src/test/java/org/apache/james/utils/GuiceMatcherLoaderTest.java @@ -57,6 +57,19 @@ public class GuiceMatcherLoaderTest { } @Test + public void getMatcherShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { + GuiceMatcherLoader guiceMailetLoader = new GuiceMatcherLoader(injector, + new ExtendedClassLoader(THROWING_FILE_SYSTEM)); + + Matcher matcher = guiceMailetLoader.getMatcher(FakeMatcherConfig.builder() + .matcherName("sub.TestMatcher") + .mailetContext(FakeMailContext.defaultContext()) + .build()); + + assertThat(matcher).isInstanceOf(All.class); + } + + @Test public void getMatcherShouldThrowOnBadType() throws Exception { GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM)); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
