private ProtocolHandler createProtocolHandler(String name) throws LoadingException { try { Class<ProtocolHandler> clazz = extendedClassLoader.locateClass(name); return injector.getInstance(clazz); } catch (ClassNotFoundException e) { throw new LoadingException("Can not load " + name); } }
private Stream<URL> expandFile(File file) throws MalformedURLException { if (file.isDirectory()) { return recursiveExpand(file); } LOGGER.info("Loading custom classpath resource {}", file.getAbsolutePath()); return Stream.of(file.toURI().toURL()); }
@Inject public ExtendedClassLoader(FileSystem fileSystem) { this.urlClassLoader = new URLClassLoader(retrieveExtensionsUrls(fileSystem), getClass().getClassLoader()); }
@Test public void getMailetShouldLoadClassWhenInExtensionsJars() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.mailets.CustomMailet"); }
@Test public void getMatcherShouldLoadClassWhenInExtensionsJars() throws Exception { GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("CustomMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.matchers.CustomMatcher"); }
@Test public void getMatcherShouldBrowseRecursivelyExtensionJars() throws Exception { GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("CustomMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.matchers.CustomMatcher"); }
@Override public MailboxListener createListener(ListenerConfiguration configuration) { String listenerClass = configuration.getClazz(); try { LOGGER.info("Loading user registered mailbox listener {}", listenerClass); return mailboxListenerFactory.newInstance() .withConfiguration(configuration.getConfiguration()) .withExecutionMode(configuration.isAsync().map(this::getExecutionMode)) .clazz(classLoader.locateClass(listenerClass)) .build(); } catch (ClassNotFoundException e) { LOGGER.error("Error while loading user registered global listener {}", listenerClass, e); throw new RuntimeException(e); } }
private URL[] retrieveExtensionsUrls(FileSystem fileSystem) { try { File file = fileSystem.getFile("file://" + EXTENSIONS_JARS_FOLDER_NAME); return recursiveExpand(file) .toArray(URL[]::new); } catch (IOException e) { LOGGER.info("No " + EXTENSIONS_JARS_FOLDER_NAME + " folder."); return new URL[]{}; } }
@Test public void getMailedShouldAllowCustomPackages() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("com.custom.mailets.AnotherMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("com.custom.mailets.AnotherMailet"); }
@Test public void getMatcherShouldAllowCustomPackages() throws Exception { GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM)); Matcher matcher = guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("com.custom.matchers.AnotherMatcher") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(matcher.getClass().getCanonicalName()) .isEqualTo("com.custom.matchers.AnotherMatcher"); }
@Test public void getMailetShouldBrowseRecursivelyExtensionsJars() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(RECURSIVE_CLASSPATH_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("CustomMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet.getClass().getCanonicalName()) .isEqualTo("org.apache.james.transport.mailets.CustomMailet"); }
@Test public void getMailetShouldLoadClass() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("AddFooter") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet).isInstanceOf(AddFooter.class); }
@Test public void getMailetShouldLoadClassWhenInSubPackageFromDefaultPackage() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("sub.TestMailet") .mailetContext(FakeMailContext.defaultContext()) .build()); assertThat(mailet).isInstanceOf(TestMailet.class); }
@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 getMatcherShouldLoadClass() throws Exception { GuiceMatcherLoader guiceMailetLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM)); Matcher matcher = guiceMailetLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("All") .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)); expectedException.expect(MessagingException.class); guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("org.apache.james.transport.mailets.Null") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMailetShouldThrowOnUnknownMailet() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("org.apache.james.transport.mailets.Unknown") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMatcherShouldThrowOnUnknownMailet() throws Exception { GuiceMatcherLoader guiceMatcherLoader = new GuiceMatcherLoader(injector, new ExtendedClassLoader(CLASSPATH_FILE_SYSTEM)); expectedException.expect(MessagingException.class); guiceMatcherLoader.getMatcher(FakeMatcherConfig.builder() .matcherName("org.apache.james.transport.matchers.Unknown") .mailetContext(FakeMailContext.defaultContext()) .build()); }
@Test public void getMailetShouldThrowOnBadType() throws Exception { GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(injector, new ExtendedClassLoader(THROWING_FILE_SYSTEM), NO_MAILET_CONFIG_OVERRIDES); expectedException.expect(MessagingException.class); guiceMailetLoader.getMailet(FakeMailetConfig.builder() .mailetName("org.apache.james.transport.matchers.SizeGreaterThan") .mailetContext(FakeMailContext.defaultContext()) .build()); }