tonyjoe
Jan 6th, 2008, 11:51 PM
In addition to allowing users to pass in a Resource to the SimpleFlatFileInputSource
also allow users to pass in a resource location. Currently the simpleTaskletJob.xml contains this:
...
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="testInputTemplate" class="org.springframework.batch.io.file.support.DefaultF latFileInputSource">
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="tradeTokenizer" />
<property name="fieldSetMapper" ref="tradeMapper" />
</bean>
<bean id="fileLocator"
class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="java.lang.String"
value="data/simpleTaskletJob/input/20070122.teststream.ImportTradeDataStep.txt" />
</bean>
...
If you add this code to the bottom of the SimpleFlatFileInputSource:
public class SimpleFlatFileInputSource {
....
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
protected boolean allowZeroLengthFiles = false;
public Resource getResource() {
return resource;
}
/**
* Setter for resourceLocation property. The location of an input stream that can be
* read.
* Uses the underlying {@link CitiDefaultFlatFileInputSource#resourcePatternReso lver},
* to convert a <code>location</code> into a {@link Resource}, which is then
* set on the {@link CitiDefaultFlatFileInputSource#resource}.
* @param resource The <code>FILE_SEPARATOR</code>s in the filename can be
* either "/" or "\", the {@link PathMatchingResourcePatternResolver} will
* convert them appropriately for the platform you're on.<br/>
* Without indicating a prefix, the "classpath*:" prefix will be implied.
* Put the "file:" prefix in front of any location you wish to pass in that
* resides outside of the classpath.
* @throws IOException
*/
public void setResourceLocation(String location) throws IOException {
setResource(location);
}
/**
* @param resourcePatternResolver If not set, defaults to {@link PathMatchingResourcePatternResolver}
*/
public void setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver) {
this.resourcePatternResolver = resourcePatternResolver;
}
}
the simpleTaskletJob.xml could contain something like this:
...
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="testInputTemplate" class="org.springframework.batch.io.file.support.DefaultF latFileInputSource">
<property name="resourceLocation" value="file:src-test/**/margins/incoming/somefile.txt" />
<property name="tokenizer" ref="tradeTokenizer" />
<property name="fieldSetMapper" ref="tradeMapper" />
</bean>
...
This is especially useful when you want to use wildcarding in the name of a resource location.
also allow users to pass in a resource location. Currently the simpleTaskletJob.xml contains this:
...
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="testInputTemplate" class="org.springframework.batch.io.file.support.DefaultF latFileInputSource">
<property name="resource" ref="fileLocator" />
<property name="tokenizer" ref="tradeTokenizer" />
<property name="fieldSetMapper" ref="tradeMapper" />
</bean>
<bean id="fileLocator"
class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="java.lang.String"
value="data/simpleTaskletJob/input/20070122.teststream.ImportTradeDataStep.txt" />
</bean>
...
If you add this code to the bottom of the SimpleFlatFileInputSource:
public class SimpleFlatFileInputSource {
....
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
protected boolean allowZeroLengthFiles = false;
public Resource getResource() {
return resource;
}
/**
* Setter for resourceLocation property. The location of an input stream that can be
* read.
* Uses the underlying {@link CitiDefaultFlatFileInputSource#resourcePatternReso lver},
* to convert a <code>location</code> into a {@link Resource}, which is then
* set on the {@link CitiDefaultFlatFileInputSource#resource}.
* @param resource The <code>FILE_SEPARATOR</code>s in the filename can be
* either "/" or "\", the {@link PathMatchingResourcePatternResolver} will
* convert them appropriately for the platform you're on.<br/>
* Without indicating a prefix, the "classpath*:" prefix will be implied.
* Put the "file:" prefix in front of any location you wish to pass in that
* resides outside of the classpath.
* @throws IOException
*/
public void setResourceLocation(String location) throws IOException {
setResource(location);
}
/**
* @param resourcePatternResolver If not set, defaults to {@link PathMatchingResourcePatternResolver}
*/
public void setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver) {
this.resourcePatternResolver = resourcePatternResolver;
}
}
the simpleTaskletJob.xml could contain something like this:
...
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="testInputTemplate" class="org.springframework.batch.io.file.support.DefaultF latFileInputSource">
<property name="resourceLocation" value="file:src-test/**/margins/incoming/somefile.txt" />
<property name="tokenizer" ref="tradeTokenizer" />
<property name="fieldSetMapper" ref="tradeMapper" />
</bean>
...
This is especially useful when you want to use wildcarding in the name of a resource location.