INT-356: Changed namespace parsing to resolve incompatible MessageCreator problem. type attribute on ftpsource no longer supported.

This commit is contained in:
Iwein Fuld
2008-09-01 17:41:19 +00:00
parent 12a0142d7b
commit a27ae5726e
12 changed files with 80 additions and 210 deletions

View File

@@ -16,15 +16,10 @@
package org.springframework.integration.adapter.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.adapter.file.ByteArrayFileMessageCreator;
import org.springframework.integration.adapter.file.FileMessageCreator;
import org.springframework.integration.adapter.file.TextFileMessageCreator;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Base class for directory-based sources.
@@ -35,46 +30,16 @@ public abstract class AbstractDirectorySourceParser extends AbstractSimpleBeanDe
public static final String MESSAGE_CREATOR_REFERENCE_ATTRIBUTE = "message-creator";
public static final String TYPE_ATTRIBUTE = "type";
private final boolean deleteFileAfterMessageCreation;
public AbstractDirectorySourceParser(boolean deleteFileAfterMessageCreation){
this.deleteFileAfterMessageCreation = deleteFileAfterMessageCreation;
}
@Override
protected boolean isEligibleAttribute(String attributeName) {
return !MESSAGE_CREATOR_REFERENCE_ATTRIBUTE.equals(attributeName)
&& !TYPE_ATTRIBUTE.equals(attributeName)
&& super.isEligibleAttribute(attributeName);
return !MESSAGE_CREATOR_REFERENCE_ATTRIBUTE.equals(attributeName) && super.isEligibleAttribute(attributeName);
}
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
String messageCreatorReference = element.getAttribute(MESSAGE_CREATOR_REFERENCE_ATTRIBUTE);
String type = element.getAttribute(TYPE_ATTRIBUTE);
if (StringUtils.hasText(type) && StringUtils.hasText(messageCreatorReference)) {
throw new ConfigurationException(
"Either the 'type' or the 'message-creator' attributes are allowed, but not both.");
}
if (StringUtils.hasText(messageCreatorReference)) {
beanDefinition.addConstructorArgReference(messageCreatorReference);
}
else {
if ("text".equals(type)) {
beanDefinition.addConstructorArgValue(new TextFileMessageCreator(deleteFileAfterMessageCreation));
}
else if ("binary".equals(type)) {
beanDefinition.addConstructorArgValue(new ByteArrayFileMessageCreator(deleteFileAfterMessageCreation));
}
else {
beanDefinition.addConstructorArgValue(new FileMessageCreator());
}
}
}
}

View File

@@ -23,8 +23,10 @@ import java.util.regex.Pattern;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.adapter.file.ByteArrayFileMessageCreator;
import org.springframework.integration.adapter.file.FileSource;
import org.springframework.integration.adapter.file.RegexPatternFilenameFilter;
import org.springframework.integration.adapter.file.TextFileMessageCreator;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
@@ -36,6 +38,8 @@ import org.w3c.dom.Element;
*/
public class FileSourceParser extends AbstractDirectorySourceParser {
public static final String TYPE_ATTRIBUTE = "type";
public static final String DIRECTORY_ATTRIBUTE = "directory";
public static final String FILE_FILTER_ATTRIBUTE = "file-filter";
@@ -44,12 +48,6 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
public static final String FILENAME_PATTERN_ATTRIBUTE = "filename-pattern";
public FileSourceParser() {
super(false);
}
@Override
protected Class<?> getBeanClass(Element element) {
return FileSource.class;
@@ -57,18 +55,16 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
@Override
protected boolean isEligibleAttribute(String attributeName) {
return !DIRECTORY_ATTRIBUTE.equals(attributeName)
&& !FILE_FILTER_ATTRIBUTE.equals(attributeName)
return !DIRECTORY_ATTRIBUTE.equals(attributeName) && !FILE_FILTER_ATTRIBUTE.equals(attributeName)
&& !FILENAME_FILTER_ATTRIBUTE.equals(attributeName)
&& !FILENAME_PATTERN_ATTRIBUTE.equals(attributeName)
&& !FILENAME_PATTERN_ATTRIBUTE.equals(attributeName) && !TYPE_ATTRIBUTE.equals(attributeName)
&& super.isEligibleAttribute(attributeName);
}
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
String directoryLocation = element.getAttribute(DIRECTORY_ATTRIBUTE);
if (!directoryLocation.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
&& !isUrl(directoryLocation)) {
if (!directoryLocation.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX) && !isUrl(directoryLocation)) {
directoryLocation = "file:" + directoryLocation;
}
beanDefinition.addConstructorArgValue(directoryLocation);
@@ -88,6 +84,22 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
beanDefinition.addPropertyValue("filenameFilter", regexFilter);
}
super.postProcess(beanDefinition, element);
processTypeAttribute(beanDefinition, element);
}
private void processTypeAttribute(BeanDefinitionBuilder beanDefinition, Element element) {
if (beanDefinition.getRawBeanDefinition().getConstructorArgumentValues().getArgumentCount() == 2) {
// message-creator already defined, ignore type property
}
else {
String type = element.getAttribute(TYPE_ATTRIBUTE);
if ("text".equals(type)) {
beanDefinition.addConstructorArgValue(new TextFileMessageCreator(false));
}
else if ("binary".equals(type)) {
beanDefinition.addConstructorArgValue(new ByteArrayFileMessageCreator(false));
}
}
}
private boolean isUrl(String directoryLocation) {
@@ -100,14 +112,13 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
}
}
private void verifyAtMostOneAttributeSpecified(String ... attributes) {
private void verifyAtMostOneAttributeSpecified(String... attributes) {
boolean attributeSpecified = false;
for (String attribute : attributes) {
if (StringUtils.hasText(attribute)) {
if (attributeSpecified) {
throw new ConfigurationException("FileSource supports at most one of '"
+ FILE_FILTER_ATTRIBUTE + "', '" + FILENAME_FILTER_ATTRIBUTE
+ "', and '" + FILENAME_PATTERN_ATTRIBUTE + "'.");
throw new ConfigurationException("FileSource supports at most one of '" + FILE_FILTER_ATTRIBUTE
+ "', '" + FILENAME_FILTER_ATTRIBUTE + "', and '" + FILENAME_PATTERN_ATTRIBUTE + "'.");
}
attributeSpecified = true;
}

View File

@@ -27,6 +27,7 @@ import org.apache.commons.net.ftp.FTPFile;
import org.springframework.integration.adapter.file.AbstractDirectorySource;
import org.springframework.integration.adapter.file.Backlog;
import org.springframework.integration.adapter.file.FileSnapshot;
import org.springframework.integration.message.DefaultMessageCreator;
import org.springframework.integration.message.MessageCreator;
import org.springframework.util.Assert;
@@ -45,6 +46,10 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
private final FTPClientPool clientPool;
public FtpSource(FTPClientPool clientPool) {
this(new DefaultMessageCreator<List<File>>(), clientPool);
}
public FtpSource(MessageCreator<List<File>, List<File>> messageCreator, FTPClientPool clientPool) {
super(messageCreator);
this.clientPool = clientPool;
@@ -63,11 +68,9 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
@Override
protected void refreshSnapshotAndMarkProcessing(Backlog<FileSnapshot> directoryContentManager) throws IOException {
List<FileSnapshot> snapshot = new ArrayList<FileSnapshot>();
synchronized (directoryContentManager) {
populateSnapshot(snapshot);
directoryContentManager.processSnapshot(snapshot);
directoryContentManager.prepareForProcessing(maxFilesPerMessage);
}
populateSnapshot(snapshot);
directoryContentManager.processSnapshot(snapshot);
directoryContentManager.prepareForProcessing(maxFilesPerMessage);
}
@Override
@@ -98,7 +101,8 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
List<File> files = new ArrayList<File>();
List<FileSnapshot> toDo = this.getBacklog().getProcessingBuffer();
for (FileSnapshot fileSnapshot : toDo) {
//some awkwardness here because the local path may be different from the remote path
// some awkwardness here because the local path may be different
// from the remote path
File file = new File(this.localWorkingDirectory, fileSnapshot.getFileName());
if (file.exists()) {
file.delete();

View File

@@ -40,10 +40,6 @@ public class FtpSourceParser extends AbstractDirectorySourceParser {
private static final String POOL_ATTRIBUTE_REMOTEDIR = "remote-working-directory";
public FtpSourceParser() {
super(true);
}
@Override
protected Class<?> getBeanClass(Element element) {
return FtpSource.class;