From aedf03fad0487d413aa6df6fc7710255b3993097 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Mon, 4 Mar 2013 14:08:19 -0600 Subject: [PATCH] BATCH-1966: Some minor cleanup --- .../FaultTolerantStepFactoryBeanTests.java | 49 ++++++++++++------- .../batch/item/ItemStreamSupport.java | 26 +++++----- .../item/database/JdbcPagingItemReader.java | 16 +++--- .../batch/item/file/FlatFileItemWriter.java | 36 +++++++------- .../item/file/MultiResourceItemReader.java | 39 ++++++++------- .../support/AbstractItemStreamItemReader.java | 3 +- .../support/AbstractItemStreamItemWriter.java | 3 +- 7 files changed, 91 insertions(+), 81 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java index c8e9ee25a..93b5db364 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java @@ -831,16 +831,16 @@ public class FaultTolerantStepFactoryBeanTests { public void testItemStreamOpenedEvenWithTaskExecutor() throws Exception { writer.setFailures("4"); - ItemStreamReader reader = new AbstractItemStreamItemReader() { + ItemReader reader = new AbstractItemStreamItemReader() { @Override public void close() { - super.close(); + super.close(); closed = true; } @Override public void open(ExecutionContext executionContext) { - super.open(executionContext); + super.open(executionContext); opened = true; } @@ -869,29 +869,42 @@ public class FaultTolerantStepFactoryBeanTests { public void testNestedItemStreamOpened() throws Exception { writer.setFailures("4"); - ItemStreamReader reader = new AbstractItemStreamItemReader() { + ItemStreamReader reader = new ItemStreamReader() { + @Override + public void close() throws ItemStreamException { + } @Override - public String read() { + public void open(ExecutionContext executionContext) throws ItemStreamException { + } + + @Override + public void update(ExecutionContext executionContext) throws ItemStreamException { + } + + @Override + public String read() throws Exception, UnexpectedInputException, ParseException { return null; } }; - ItemStreamReader stream = new AbstractItemStreamItemReader() { + ItemStreamReader stream = new ItemStreamReader() { @Override - public void close() { - super.close(); + public void close() throws ItemStreamException { closed = true; } @Override - public void open(ExecutionContext executionContext) { - super.open(executionContext); + public void open(ExecutionContext executionContext) throws ItemStreamException { opened = true; } @Override - public String read() { + public void update(ExecutionContext executionContext) throws ItemStreamException { + } + + @Override + public String read() throws Exception, UnexpectedInputException, ParseException { return null; } }; @@ -916,21 +929,23 @@ public class FaultTolerantStepFactoryBeanTests { public void testProxiedItemStreamOpened() throws Exception { writer.setFailures("4"); - ItemStreamReader reader = new AbstractItemStreamItemReader() { + ItemStreamReader reader = new ItemStreamReader() { @Override - public void close() { - super.close(); + public void close() throws ItemStreamException { closed = true; } @Override - public void open(ExecutionContext executionContext) { - super.open(executionContext); + public void open(ExecutionContext executionContext) throws ItemStreamException { opened = true; } @Override - public String read() { + public void update(ExecutionContext executionContext) throws ItemStreamException { + } + + @Override + public String read() throws Exception, UnexpectedInputException, ParseException { return null; } }; diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemStreamSupport.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemStreamSupport.java index 3663f0df7..a52f9b336 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemStreamSupport.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemStreamSupport.java @@ -26,13 +26,13 @@ import org.springframework.batch.item.util.ExecutionContextUserSupport; */ public abstract class ItemStreamSupport implements ItemStream { - private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport(); - + private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport(); + /** * No-op. * @see org.springframework.batch.item.ItemStream#close() */ - @Override + @Override public void close() { } @@ -40,7 +40,7 @@ public abstract class ItemStreamSupport implements ItemStream { * No-op. * @see org.springframework.batch.item.ItemStream#open(ExecutionContext) */ - @Override + @Override public void open(ExecutionContext executionContext) { } @@ -48,20 +48,16 @@ public abstract class ItemStreamSupport implements ItemStream { * Return empty {@link ExecutionContext}. * @see org.springframework.batch.item.ItemStream#update(ExecutionContext) */ - @Override + @Override public void update(ExecutionContext executionContext) { } - - private ExecutionContextUserSupport getExecutionContextUserSupport() { - return executionContextUserSupport; - } - protected void setExecutionContextName(String name) { - this.getExecutionContextUserSupport().setName(name); - } + protected void setExecutionContextName(String name) { + executionContextUserSupport.setName(name); + } - public String getExecutionContextKey(String key) { - return this.getExecutionContextUserSupport().getKey(key); - } + public String getExecutionContextKey(String key) { + return executionContextUserSupport.getKey(key); + } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java index 0c4f3fbcf..9074fb1b5 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2012 the original author or authors. + * Copyright 2006-2013 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. @@ -222,7 +222,7 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme getParameterList(parameterValues, startAfterValues).toArray(), rowCallback); } } - + Collection result = (Collection) query; results.addAll(result); } @@ -240,12 +240,12 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme public void open(ExecutionContext executionContext) { if (isSaveState()) { startAfterValues = (Map) executionContext.get(getExecutionContextKey(START_AFTER_VALUE)); - + if(startAfterValues == null) { startAfterValues = new LinkedHashMap(); } } - + super.open(executionContext); } @@ -266,7 +266,7 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme } RowMapper startMapper = new RowMapper() { - @Override + @Override public Object mapRow(ResultSet rs, int i) throws SQLException { return rs.getObject(1); } @@ -316,7 +316,7 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme parameterList.add(keys.get(i).getValue()); } } - + if (logger.isDebugEnabled()) { logger.debug("Using parameterList:" + parameterList); } @@ -325,13 +325,13 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme @SuppressWarnings("rawtypes") private class PagingRowMapper implements RowMapper { - @Override + @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { startAfterValues = new LinkedHashMap(); for (Map.Entry sortKey : queryProvider.getSortKeys().entrySet()) { startAfterValues.put(sortKey.getKey(), rs.getObject(sortKey.getKey())); } - + return rowMapper.mapRow(rs, rowNum); } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java index 6d75baae1..6748a03b4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java @@ -58,7 +58,7 @@ import org.springframework.util.ClassUtils; * @author Michael Minella */ public class FlatFileItemWriter extends AbstractItemStreamItemWriter implements ResourceAwareItemWriterItemStream, - InitializingBean { +InitializingBean { private static final boolean DEFAULT_TRANSACTIONAL = true; @@ -97,7 +97,7 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple private boolean append = false; public FlatFileItemWriter() { - this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class)); + this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class)); } /** @@ -105,7 +105,7 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple * * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ - @Override + @Override public void afterPropertiesSet() throws Exception { Assert.notNull(lineAggregator, "A LineAggregator must be provided."); if (append) { @@ -150,7 +150,7 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple * * @param resource */ - @Override + @Override public void setResource(Resource resource) { this.resource = resource; } @@ -248,7 +248,7 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple * @throws Exception if the transformer or file output fail, * WriterNotOpenException if the writer has not been initialized. */ - @Override + @Override public void write(List items) throws Exception { if (!getOutputState().isInitialized()) { @@ -279,9 +279,9 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple /** * @see ItemStream#close() */ - @Override + @Override public void close() { - super.close(); + super.close(); if (state != null) { try { if (footerCallback != null && state.outputBufferedWriter != null) { @@ -313,10 +313,10 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple * * @see ItemStream#open(ExecutionContext) */ - @Override + @Override public void open(ExecutionContext executionContext) throws ItemStreamException { - super.open(executionContext); - + super.open(executionContext); + Assert.notNull(resource, "The resource must be set"); if (!getOutputState().isInitialized()) { @@ -351,9 +351,9 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple /** * @see ItemStream#update(ExecutionContext) */ - @Override + @Override public void update(ExecutionContext executionContext) { - super.update(executionContext); + super.update(executionContext); if (state == null) { throw new ItemStreamException("ItemStream not open or already closed."); } @@ -389,10 +389,10 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple state.setAppendAllowed(append); state.setEncoding(encoding); } - return (OutputState) state; + return state; } - - /** + + /** * The name of the component which will be used as a stem for keys in the * {@link ExecutionContext}. Subclasses should provide a default value, e.g. * the short form of the class name. @@ -400,7 +400,7 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple * @param name the name for the component */ public void setName(String name) { - this.setExecutionContextName(name); + this.setExecutionContextName(name); } /** @@ -601,12 +601,12 @@ public class FlatFileItemWriter extends AbstractItemStreamItemWriter imple final FileChannel channel = fileChannel; if (transactional) { TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() { - @Override + @Override public void run() { closeStream(); } }); - + writer.setEncoding(encoding); return writer; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java index df82c4d2e..a45bf0888 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java @@ -22,6 +22,7 @@ import java.util.Comparator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.ResourceAware; @@ -75,7 +76,7 @@ public class MultiResourceItemReader extends AbstractItemStreamItemReader /** * Compares resource filenames. */ - @Override + @Override public int compare(Resource r1, Resource r2) { return r1.getFilename().compareTo(r2.getFilename()); } @@ -83,13 +84,13 @@ public class MultiResourceItemReader extends AbstractItemStreamItemReader }; public MultiResourceItemReader() { - this.setExecutionContextName(ClassUtils.getShortName(MultiResourceItemReader.class)); + this.setExecutionContextName(ClassUtils.getShortName(MultiResourceItemReader.class)); } /** * Reads the next item, jumping to next resource if necessary. */ - @Override + @Override public T read() throws Exception, UnexpectedInputException, ParseException { if (noInput) { @@ -129,26 +130,26 @@ public class MultiResourceItemReader extends AbstractItemStreamItemReader delegate.setResource(resources[currentResource]); delegate.open(new ExecutionContext()); - item = readFromDelegate(); - } + item = readFromDelegate(); + } return item; } - private T readFromDelegate() throws Exception { - T item = delegate.read(); - if(item instanceof ResourceAware){ - ((ResourceAware) item).setResource(getCurrentResource()); - } - return item; - } + private T readFromDelegate() throws Exception { + T item = delegate.read(); + if(item instanceof ResourceAware){ + ((ResourceAware) item).setResource(getCurrentResource()); + } + return item; + } - /** + /** * Close the {@link #setDelegate(ResourceAwareItemReaderItemStream)} reader and reset instance variable values. */ - @Override + @Override public void close() throws ItemStreamException { - super.close(); + super.close(); delegate.close(); noInput = false; } @@ -157,9 +158,9 @@ public class MultiResourceItemReader extends AbstractItemStreamItemReader * Figure out which resource to start with in case of restart, open the delegate and restore delegate's position in * the resource. */ - @Override + @Override public void open(ExecutionContext executionContext) throws ItemStreamException { - super.open(executionContext); + super.open(executionContext); Assert.notNull(resources, "Resources must be set"); noInput = false; @@ -196,9 +197,9 @@ public class MultiResourceItemReader extends AbstractItemStreamItemReader /** * Store the current resource index and position in the resource. */ - @Override + @Override public void update(ExecutionContext executionContext) throws ItemStreamException { - super.update(executionContext); + super.update(executionContext); if (saveState) { executionContext.putInt(getExecutionContextKey(RESOURCE_KEY), currentResource); delegate.update(executionContext); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemReader.java index 96132ee34..606f9c73b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemReader.java @@ -17,7 +17,6 @@ package org.springframework.batch.item.support; import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.ItemStreamReader; import org.springframework.batch.item.ItemStreamSupport; @@ -26,6 +25,6 @@ import org.springframework.batch.item.ItemStreamSupport; * @author Dave Syer * */ -public abstract class AbstractItemStreamItemReader extends ItemStreamSupport implements ItemStreamReader { +public abstract class AbstractItemStreamItemReader extends ItemStreamSupport implements ItemReader { } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemWriter.java index 517c60eac..1d9e1e53d 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemStreamItemWriter.java @@ -17,7 +17,6 @@ package org.springframework.batch.item.support; import org.springframework.batch.item.ItemStreamSupport; -import org.springframework.batch.item.ItemStreamWriter; import org.springframework.batch.item.ItemWriter; @@ -26,6 +25,6 @@ import org.springframework.batch.item.ItemWriter; * @author Dave Syer * */ -public abstract class AbstractItemStreamItemWriter extends ItemStreamSupport implements ItemStreamWriter { +public abstract class AbstractItemStreamItemWriter extends ItemStreamSupport implements ItemWriter { }