RESOLVED - BATCH-983: Remove StepExecutionResourceProxy in favor of late-binding
applied patches
This commit is contained in:
@@ -1,295 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.batch.core.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
|
||||
import org.springframework.batch.core.converter.JobParametersConverter;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.FileSystemResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Strategy for locating different resources on the file system. For each unique
|
||||
* step execution, the same file handle will be returned. A unique step is
|
||||
* defined as having the same job instance and step name. An external file mover
|
||||
* (such as an EAI solution) should rename and move any input files to conform
|
||||
* to the pattern defined here.<br/>
|
||||
*
|
||||
* If no pattern is passed in, then following default is used:
|
||||
*
|
||||
* <pre>
|
||||
* data/%JOB_NAME%/%STEP_NAME%.txt
|
||||
* </pre>
|
||||
*
|
||||
* The %% variables are replaced with the corresponding bean property at run
|
||||
* time, when the factory method is executed. To insert {@link JobParameters}
|
||||
* use a pattern with the parameter key surrounded by %%, e.g.
|
||||
*
|
||||
* <pre>
|
||||
* //home/jobs/data/%JOB_NAME%/%STEP_NAME%-%schedule.date%.txt
|
||||
* </pre>
|
||||
*
|
||||
* Note that the default pattern does not start with a separator. Because of the
|
||||
* implementation of the Spring Core Resource abstractions, it would need to
|
||||
* start with a double forward slash "//" to resolve to an absolute directory
|
||||
* (or else use a full URL with the file: prefix).<br/>
|
||||
*
|
||||
* To use this resource it must be initialised with a {@link StepExecution}.
|
||||
* The best way to do that is to register it as a listener in the step that is
|
||||
* going to need it. For this reason the resource implements
|
||||
* {@link StepExecutionListener}.
|
||||
*
|
||||
* @author Tomas Slanina
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @see Resource
|
||||
*/
|
||||
public class StepExecutionResourceProxy extends StepExecutionListenerSupport implements Resource, ResourceLoaderAware,
|
||||
StepExecutionListener {
|
||||
|
||||
private static final String JOB_NAME_PATTERN = "%JOB_NAME%";
|
||||
|
||||
private static final String STEP_NAME_PATTERN = "%STEP_NAME%";
|
||||
|
||||
private static final String DEFAULT_PATTERN = "data/%JOB_NAME%/" + "%STEP_NAME%.txt";
|
||||
|
||||
private String filePattern = DEFAULT_PATTERN;
|
||||
|
||||
private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
|
||||
|
||||
private ResourceLoader resourceLoader = new FileSystemResourceLoader();
|
||||
|
||||
private Resource delegate;
|
||||
|
||||
/**
|
||||
* @param relativePath
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#createRelative(java.lang.String)
|
||||
*/
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.createRelative(relativePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#exists()
|
||||
*/
|
||||
public boolean exists() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#getDescription()
|
||||
*/
|
||||
public String getDescription() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getFile()
|
||||
*/
|
||||
public File getFile() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#getFilename()
|
||||
*/
|
||||
public String getFilename() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getFilename();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.InputStreamSource#getInputStream()
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getInputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getURI()
|
||||
*/
|
||||
public URI getURI() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getURI();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getURL()
|
||||
*/
|
||||
public URL getURL() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getURL();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#isOpen()
|
||||
*/
|
||||
public boolean isOpen() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.isOpen();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#isReadable()
|
||||
*/
|
||||
public boolean isReadable() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.isReadable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.core.io.Resource#lastModified()
|
||||
*/
|
||||
public long lastModified() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.lastModified();
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobParametersConverter} used to translate
|
||||
* {@link JobParameters} into {@link Properties}. Defaults to a
|
||||
* {@link DefaultJobParametersConverter}.
|
||||
* @param jobParametersConverter the {@link JobParametersConverter} to set
|
||||
*/
|
||||
public void setJobParametersFactory(JobParametersConverter jobParametersConverter) {
|
||||
this.jobParametersConverter = jobParametersConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always false because we are expecting to be step scoped.
|
||||
*
|
||||
* @see org.springframework.beans.factory.config.AbstractFactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
|
||||
*/
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* helper method for <code>createFileName()</code>
|
||||
*/
|
||||
private String replacePattern(String string, String pattern, String replacement) {
|
||||
|
||||
if (string == null)
|
||||
return null;
|
||||
|
||||
// check to ensure pattern exists in string.
|
||||
if (string.indexOf(pattern) != -1) {
|
||||
return StringUtils.replace(string, pattern, replacement);
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a filename given a pattern and step context information.
|
||||
*
|
||||
* Deliberate package access, so that the method can be accessed by unit
|
||||
* tests
|
||||
* @param jobName
|
||||
* @param stepName
|
||||
* @param properties
|
||||
*/
|
||||
private String createFileName(String jobName, String stepName, Properties properties) {
|
||||
Assert.notNull(filePattern, "filename pattern is null");
|
||||
|
||||
String fileName = filePattern;
|
||||
|
||||
fileName = replacePattern(fileName, JOB_NAME_PATTERN, jobName == null ? "job" : jobName);
|
||||
fileName = replacePattern(fileName, STEP_NAME_PATTERN, stepName);
|
||||
|
||||
if (properties != null) {
|
||||
for (Entry<Object, Object> entry : properties.entrySet()) {
|
||||
String key = (String) entry.getKey();
|
||||
fileName = replacePattern(fileName, "%" + key + "%", (String) entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFilePattern(String filePattern) {
|
||||
this.filePattern = replacePattern(filePattern, "\\", File.separator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the properties of the enclosing {@link StepExecution} that will
|
||||
* be needed to create a file name.
|
||||
*
|
||||
* @see org.springframework.batch.core.StepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution)
|
||||
*/
|
||||
public void beforeStep(StepExecution execution) {
|
||||
String stepName = execution.getStepName();
|
||||
String jobName = execution.getJobExecution().getJobInstance().getJobName();
|
||||
Properties properties = jobParametersConverter.getProperties(execution.getJobExecution().getJobInstance()
|
||||
.getJobParameters());
|
||||
delegate = resourceLoader.getResource(createFileName(jobName, stepName, properties));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to the proxied Resource if set, otherwise returns the value of {@link #setFilePattern(String)}.
|
||||
*/
|
||||
public String toString() {
|
||||
return (delegate == null) ? filePattern : delegate.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.batch.core.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.step.StepSupport;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.DescriptiveResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link StepExecutionResourceProxy}
|
||||
*
|
||||
* @author robert.kasanicky
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class StepExecutionResourceProxyTests extends TestCase {
|
||||
|
||||
/**
|
||||
* Object under test
|
||||
*/
|
||||
private StepExecutionResourceProxy resource = new StepExecutionResourceProxy();
|
||||
|
||||
private char pathsep = File.separatorChar;
|
||||
|
||||
private String path = "data" + pathsep;
|
||||
|
||||
private JobInstance jobInstance;
|
||||
|
||||
private StepExecution stepExecution;
|
||||
|
||||
/**
|
||||
* mock step context
|
||||
*/
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
|
||||
jobInstance = new JobInstance(0L, new JobParameters(), "testJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
Step step = new StepSupport("bar");
|
||||
stepExecution = jobExecution.createStepExecution(step.getName());
|
||||
resource.beforeStep(stepExecution);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* regular use with valid context and pattern provided
|
||||
*/
|
||||
public void testCreateFileName() throws Exception {
|
||||
doTestPathName("bar.txt", path);
|
||||
}
|
||||
|
||||
public void testNullFilePattern() throws Exception {
|
||||
resource.setFilePattern(null);
|
||||
try {
|
||||
resource.beforeStep(stepExecution);
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testNonStandardFilePattern() throws Exception {
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/" + "%STEP_NAME%-job");
|
||||
resource.beforeStep(stepExecution);
|
||||
doTestPathName("bar-job", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
public void testNonStandardFilePatternWithJobParameters() throws Exception {
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/%job.key%-foo");
|
||||
jobInstance = new JobInstance(0L, new JobParametersBuilder().addString("job.key", "spam")
|
||||
.toJobParameters(), "testJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
Step step = new StepSupport("bar");
|
||||
resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
doTestPathName("spam-foo", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
// public void testNonExistentJobParameter() throws Exception{
|
||||
//
|
||||
// resource.setFilePattern("foo/data/%JOB_NAME%/%non.key%-foo");
|
||||
// jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam")
|
||||
// .toJobParameters(), "testJob");
|
||||
// JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
// Step step = new StepSupport("bar");
|
||||
// try{
|
||||
// resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
// fail();
|
||||
// }
|
||||
// catch(Exception ex){
|
||||
// //expected, if there isn't a JobParameter for that key, it should throw an exception
|
||||
// }
|
||||
// }
|
||||
|
||||
public void testLongJobParameter() throws Exception {
|
||||
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/%job.key(long)%-foo");
|
||||
jobInstance = new JobInstance(0L, new JobParametersBuilder().addLong("job.key", 123L)
|
||||
.toJobParameters(), "testJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
Step step = new StepSupport("bar");
|
||||
resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
doTestPathName("123-foo", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
public void testResoureLoaderAware() throws Exception {
|
||||
resource = new StepExecutionResourceProxy();
|
||||
resource.setResourceLoader(new DefaultResourceLoader() {
|
||||
public Resource getResource(String location) {
|
||||
return new ByteArrayResource("foo".getBytes());
|
||||
}
|
||||
});
|
||||
resource.beforeStep(stepExecution);
|
||||
assertTrue(resource.exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* toString delegates to the proxied resource.
|
||||
*/
|
||||
public void testToString() {
|
||||
resource = new StepExecutionResourceProxy();
|
||||
resource.setResourceLoader(new DefaultResourceLoader() {
|
||||
public Resource getResource(String location) {
|
||||
return new DescriptiveResource("toStringTestResource") {
|
||||
public String toString() {
|
||||
return "to-string-test-resource";
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
resource.beforeStep(stepExecution);
|
||||
assertEquals("to-string-test-resource", resource.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* If delegate is not set toString returns the filePattern.
|
||||
*/
|
||||
public void testToStringWithNullDelegate() {
|
||||
resource = new StepExecutionResourceProxy();
|
||||
String filePattern = "arbitrary pattern";
|
||||
resource.setFilePattern("arbitrary pattern");
|
||||
assertEquals(filePattern, resource.toString());
|
||||
}
|
||||
|
||||
private void doTestPathName(String filename, String path) throws Exception, IOException {
|
||||
String returnedPath = resource.getFile().getAbsolutePath();
|
||||
String absolutePath = new File(path + jobInstance.getJobName() + pathsep + filename).getAbsolutePath();
|
||||
assertEquals(absolutePath, returnedPath);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user