INT-631 Added MailReceiverFactoryBean.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,10 +22,6 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.mail.ImapMailReceiver;
|
||||
import org.springframework.integration.mail.MailReceiver;
|
||||
import org.springframework.integration.mail.MailReceivingMessageSource;
|
||||
import org.springframework.integration.mail.Pop3MailReceiver;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
@@ -39,39 +35,35 @@ import org.springframework.util.xml.DomUtils;
|
||||
*/
|
||||
public class MailInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
|
||||
|
||||
private static final String BASE_PACKAGE = "org.springframework.integration.mail";
|
||||
|
||||
|
||||
@Override
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MailReceivingMessageSource.class);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
BASE_PACKAGE + ".MailReceivingMessageSource");
|
||||
builder.addConstructorArgReference(this.parseMailReceiver(element, parserContext));
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
private String parseMailReceiver(Element element, ParserContext parserContext) {
|
||||
String uri = element.getAttribute("store-uri");
|
||||
Assert.hasText(uri, "the 'store-uri' attribute is required");
|
||||
boolean isPop3 = uri.toLowerCase().startsWith("pop3");
|
||||
boolean isImap = uri.toLowerCase().startsWith("imap");
|
||||
Assert.isTrue(isPop3 || isImap, "the 'store-uri' must begin with 'pop3' or 'imap'");
|
||||
Class<? extends MailReceiver> receiverClass = isPop3 ? Pop3MailReceiver.class : ImapMailReceiver.class;
|
||||
BeanDefinitionBuilder receiverBuilder = BeanDefinitionBuilder.genericBeanDefinition(receiverClass);
|
||||
receiverBuilder.addConstructorArgValue(uri);
|
||||
String storeUri = element.getAttribute("store-uri");
|
||||
Assert.hasText(storeUri, "the 'store-uri' attribute is required");
|
||||
BeanDefinitionBuilder receiverBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
BASE_PACKAGE + ".config.MailReceiverFactoryBean");
|
||||
receiverBuilder.addPropertyValue("storeUri", storeUri);
|
||||
String propertiesRef = element.getAttribute("java-mail-properties");
|
||||
if (StringUtils.hasText(propertiesRef)) {
|
||||
receiverBuilder.addPropertyReference("javaMailProperties", propertiesRef);
|
||||
}
|
||||
boolean configuredFetchSize = false;
|
||||
Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
|
||||
if (pollerElement != null) {
|
||||
String mmpp = pollerElement.getAttribute("max-messages-per-poll");
|
||||
if (StringUtils.hasText(mmpp)) {
|
||||
receiverBuilder.addPropertyValue("maxFetchSize", mmpp);
|
||||
configuredFetchSize = true;
|
||||
}
|
||||
}
|
||||
if (!configuredFetchSize) {
|
||||
receiverBuilder.addPropertyValue("maxFetchSize", 1);
|
||||
}
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
receiverBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.integration.mail.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.integration.mail.AbstractMailReceiver;
|
||||
import org.springframework.integration.mail.ImapMailReceiver;
|
||||
import org.springframework.integration.mail.MailReceiver;
|
||||
import org.springframework.integration.mail.Pop3MailReceiver;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.3
|
||||
*/
|
||||
public class MailReceiverFactoryBean implements FactoryBean, DisposableBean {
|
||||
|
||||
private volatile String storeUri;
|
||||
|
||||
private volatile MailReceiver receiver;
|
||||
|
||||
private volatile Properties javaMailProperties;
|
||||
|
||||
private volatile int maxFetchSize = 1;
|
||||
|
||||
|
||||
public void setStoreUri(String storeUri) {
|
||||
this.storeUri = storeUri;
|
||||
}
|
||||
|
||||
public void setJavaMailProperties(Properties javaMailProperties) {
|
||||
this.javaMailProperties = javaMailProperties;
|
||||
}
|
||||
|
||||
public void setMaxFetchSize(int maxFetchSize) {
|
||||
this.maxFetchSize = maxFetchSize;
|
||||
}
|
||||
|
||||
public Object getObject() throws Exception {
|
||||
if (this.receiver == null) {
|
||||
this.receiver = this.createReceiver();
|
||||
}
|
||||
return this.receiver;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
return (this.receiver != null) ? this.receiver.getClass() : MailReceiver.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private MailReceiver createReceiver() {
|
||||
Assert.hasText(this.storeUri, "the store URI is required");
|
||||
boolean isPop3 = this.storeUri.toLowerCase().startsWith("pop3");
|
||||
boolean isImap = this.storeUri.toLowerCase().startsWith("imap");
|
||||
Assert.isTrue(isPop3 || isImap, "the store URI must begin with 'pop3' or 'imap'");
|
||||
AbstractMailReceiver receiver = isPop3 ? new Pop3MailReceiver(this.storeUri) : new ImapMailReceiver(this.storeUri);
|
||||
if (this.javaMailProperties != null) {
|
||||
receiver.setJavaMailProperties(this.javaMailProperties);
|
||||
}
|
||||
receiver.setMaxFetchSize(this.maxFetchSize);
|
||||
return receiver;
|
||||
}
|
||||
|
||||
public void destroy() throws Exception {
|
||||
if (this.receiver != null && this.receiver instanceof DisposableBean) {
|
||||
((DisposableBean) this.receiver).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user