diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/DelegatingItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/DelegatingItemReader.java deleted file mode 100644 index bbdd3a7fa..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/DelegatingItemReader.java +++ /dev/null @@ -1,85 +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.item.support; - -import org.springframework.batch.item.ItemReader; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.util.Assert; - -/** - * Simple wrapper around {@link ItemReader}. The item reader is expected to - * take care of open and close operations. If necessary it should be registered - * as a step scoped bean to ensure that the lifecycle methods are called. - * - * The implementation is thread-safe if the delegate is thread-safe. - * - * @author Dave Syer - */ -public class DelegatingItemReader implements ItemReader, InitializingBean { - - private ItemReader itemReader; - - /** - * Default constructor. - */ - public DelegatingItemReader() { - super(); - } - - /** - * Convenience constructor for setting mandatory property. - */ - public DelegatingItemReader(ItemReader itemReader) { - this(); - this.itemReader = itemReader; - } - - public void afterPropertiesSet() throws Exception { - Assert.notNull(itemReader, "ItemReader must not be null."); - } - - /** - * Get the next object from the input source. - * @throws Exception - * @see org.springframework.batch.item.ItemReader#read() - */ - public T read() throws Exception { - return itemReader.read(); - } - - /** - * Setter for input source. - * @param source - */ - public void setItemReader(ItemReader source) { - this.itemReader = source; - } - - /* - * (non-Javadoc) - * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionContext) - */ - public void mark() { - } - - /* - * (non-Javadoc) - * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionContext) - */ - public void reset() { - } -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java deleted file mode 100644 index 1ddf798dd..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java +++ /dev/null @@ -1,94 +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.item.support; - -import junit.framework.TestCase; - -import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.ItemReader; - -/** - * Unit test for {@link DelegatingItemReader} - * - * @author Robert Kasanicky - */ -public class DelegatingItemReaderTests extends TestCase { - - // object under test - private DelegatingItemReader itemReader = new DelegatingItemReader(); - - private ItemReader delegateReader; - - private ExecutionContext executionContext; - - // create input template and inject it to data provider - protected void setUp() throws Exception { - executionContext = new ExecutionContext(); - delegateReader = new MockItemReader(this, executionContext); - itemReader.setItemReader(delegateReader); - } - - public void testAfterPropertiesSet() throws Exception { - // shouldn't throw an exception since the input source is set - itemReader.afterPropertiesSet(); - } - - public void testNullItemReader() { - try { - itemReader.setItemReader(null); - itemReader.afterPropertiesSet(); - fail(); - } - catch (Exception ex) { - assertTrue(ex instanceof IllegalArgumentException); - } - } - - /** - * Uses input template to provide the domain object. - * @throws Exception - */ - public void testNext() throws Exception { - Object result = itemReader.read(); - assertSame("domain object is provided by the input template", this, result); - } - - private static class MockItemReader extends AbstractItemStreamItemReader { - - private Object value; - - public void update(ExecutionContext executionContext) { - executionContext.putString("value", "foo"); - } - - public MockItemReader(Object value, ExecutionContext executionContext) { - this.value = value; - } - - public Object read() { - return value; - } - - public void close(ExecutionContext executionContext) { - } - - public void open(ExecutionContext executionContext) { - } - - } - -} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/ExceptionThrowingItemReaderProxy.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/ExceptionThrowingItemReaderProxy.java index 4f8cfd443..1384091c3 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/ExceptionThrowingItemReaderProxy.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/ExceptionThrowingItemReaderProxy.java @@ -16,10 +16,8 @@ package org.springframework.batch.sample.support; - import org.springframework.batch.core.UnexpectedJobExecutionException; import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.support.DelegatingItemReader; /** * Hacked {@link ItemReader} that throws exception on a given record number @@ -27,28 +25,36 @@ import org.springframework.batch.item.support.DelegatingItemReader; * * @author Robert Kasanicky * @author Lucas Ward - * + * */ -public class ExceptionThrowingItemReaderProxy extends DelegatingItemReader { +public class ExceptionThrowingItemReaderProxy implements ItemReader { private int counter = 0; + private int throwExceptionOnRecordNumber = 4; - + + private ItemReader delegate; + /** - * @param throwExceptionOnRecordNumber The number of record on which exception should be thrown + * @param throwExceptionOnRecordNumber The number of record on which + * exception should be thrown */ public void setThrowExceptionOnRecordNumber(int throwExceptionOnRecordNumber) { this.throwExceptionOnRecordNumber = throwExceptionOnRecordNumber; } - + public T read() throws Exception { - + counter++; if (counter == throwExceptionOnRecordNumber) { - throw new UnexpectedJobExecutionException("Planned failure on count="+counter); + throw new UnexpectedJobExecutionException("Planned failure on count=" + counter); } - - return super.read(); + + return delegate.read(); + } + + public void setDelegate(ItemReader delegate) { + this.delegate = delegate; } } diff --git a/spring-batch-samples/src/main/resources/jobs/restartSample.xml b/spring-batch-samples/src/main/resources/jobs/restartSample.xml index 0dd704e3d..b38137758 100644 --- a/spring-batch-samples/src/main/resources/jobs/restartSample.xml +++ b/spring-batch-samples/src/main/resources/jobs/restartSample.xml @@ -34,7 +34,7 @@ - + itemReader = new ExceptionThrowingItemReaderProxy(); - itemReader.setItemReader(new ListItemReader(new ArrayList() {{ + itemReader.setDelegate(new ListItemReader(new ArrayList() {{ add("a"); add("b"); add("c");