IN PROGRESS - issue BATCH-267: Re-organise I/O (io) packages
http://jira.springframework.org/browse/BATCH-267 Rationalize interface locations to minimize chance of cycles in future. Rename and refactor OXM interfaces a bit to be more consistent
This commit is contained in:
@@ -30,6 +30,7 @@ import java.util.Properties;
|
||||
|
||||
import org.springframework.batch.io.exception.BatchCriticalException;
|
||||
import org.springframework.batch.io.exception.BatchEnvironmentException;
|
||||
import org.springframework.batch.io.file.transform.Converter;
|
||||
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.ResourceLifecycle;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.io.file;
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
/**
|
||||
* Generic converter interface for transforming an object into another form for
|
||||
@@ -8,7 +8,7 @@ import javax.xml.stream.XMLEventReader;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public interface FragmentDeserializer {
|
||||
public interface EventReaderDeserializer {
|
||||
|
||||
Object deserializeFragment(XMLEventReader eventReader);
|
||||
}
|
||||
@@ -10,19 +10,12 @@ import javax.xml.stream.XMLEventWriter;
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public interface ObjectToXmlSerializer {
|
||||
|
||||
/**
|
||||
* Set event writer objects should be serialized to.
|
||||
*
|
||||
* @param writer
|
||||
*/
|
||||
void setEventWriter(XMLEventWriter writer);
|
||||
public interface EventWriterSerializer {
|
||||
|
||||
/**
|
||||
* Serialize an Object.
|
||||
*
|
||||
* @param output
|
||||
*/
|
||||
void serializeObject(Object output);
|
||||
void serializeObject(XMLEventWriter writer, Object output);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
|
||||
private TransactionalEventReader txReader;
|
||||
|
||||
private FragmentDeserializer fragmentDeserializer;
|
||||
private EventReaderDeserializer eventReaderDeserializer;
|
||||
|
||||
private Resource resource;
|
||||
|
||||
@@ -88,7 +88,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
currentRecordCount++;
|
||||
if (moveCursorToNextFragment(fragmentReader)) {
|
||||
fragmentReader.markStartFragment();
|
||||
item = fragmentDeserializer.deserializeFragment(fragmentReader);
|
||||
item = eventReaderDeserializer.deserializeFragment(fragmentReader);
|
||||
fragmentReader.markFragmentProcessed();
|
||||
}
|
||||
} while (skipRecords.contains(new Long(currentRecordCount)));
|
||||
@@ -139,11 +139,11 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fragmentDeserializer maps xml fragments corresponding to records
|
||||
* @param eventReaderDeserializer maps xml fragments corresponding to records
|
||||
* to objects
|
||||
*/
|
||||
public void setFragmentDeserializer(FragmentDeserializer fragmentDeserializer) {
|
||||
this.fragmentDeserializer = fragmentDeserializer;
|
||||
public void setFragmentDeserializer(EventReaderDeserializer eventReaderDeserializer) {
|
||||
this.eventReaderDeserializer = eventReaderDeserializer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +185,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(resource, "The Resource must not be null.");
|
||||
Assert.state(resource.exists(), "Input resource does not exist: [" + resource + "]");
|
||||
Assert.notNull(fragmentDeserializer, "The FragmentDeserializer must not be null.");
|
||||
Assert.notNull(eventReaderDeserializer, "The FragmentDeserializer must not be null.");
|
||||
Assert.hasLength(fragmentRootElementName, "The FragmentRootElementName must not be null");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ItemWriter} which uses
|
||||
* StAX and {@link ObjectToXmlSerializer} for serializing object to XML.
|
||||
* StAX and {@link EventWriterSerializer} for serializing object to XML.
|
||||
*
|
||||
* This output source also provides restart, statistics and transaction
|
||||
* features by implementing corresponding interfaces.
|
||||
@@ -64,7 +64,7 @@ public class StaxEventItemWriter implements ItemWriter, ResourceLifecycle, Resta
|
||||
private Resource resource;
|
||||
|
||||
// xml serializer
|
||||
private ObjectToXmlSerializer serializer;
|
||||
private EventWriterSerializer serializer;
|
||||
|
||||
// encoding to be used while reading from the resource
|
||||
private String encoding = DEFAULT_ENCODING;
|
||||
@@ -123,7 +123,7 @@ public class StaxEventItemWriter implements ItemWriter, ResourceLifecycle, Resta
|
||||
*
|
||||
* @param serializer the Object to XML serializer
|
||||
*/
|
||||
public void setSerializer(ObjectToXmlSerializer serializer) {
|
||||
public void setSerializer(EventWriterSerializer serializer) {
|
||||
this.serializer = serializer;
|
||||
}
|
||||
|
||||
@@ -271,7 +271,6 @@ public class StaxEventItemWriter implements ItemWriter, ResourceLifecycle, Resta
|
||||
try {
|
||||
delegateEventWriter = outputFactory.createXMLEventWriter(os, encoding);
|
||||
eventWriter = new NoStartEndDocumentStreamWriter(delegateEventWriter);
|
||||
serializer.setEventWriter(eventWriter);
|
||||
if (!restarted) {
|
||||
startDocument(delegateEventWriter);
|
||||
}
|
||||
@@ -375,7 +374,7 @@ public class StaxEventItemWriter implements ItemWriter, ResourceLifecycle, Resta
|
||||
}
|
||||
|
||||
currentRecordCount++;
|
||||
serializer.serializeObject(output);
|
||||
serializer.serializeObject(eventWriter, output);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.batch.io.xml.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.batch.io.xml.EventWriterSerializer;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
|
||||
/**
|
||||
* Object to xml serializer that wraps a Spring OXM {@link Marshaller} object.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class MarshallingEventWriterSerializer implements EventWriterSerializer {
|
||||
|
||||
private Marshaller marshaller;
|
||||
|
||||
public MarshallingEventWriterSerializer(Marshaller marshaller) {
|
||||
this.marshaller = marshaller;
|
||||
}
|
||||
|
||||
public void serializeObject(XMLEventWriter writer, Object output) {
|
||||
Result result = new StaxResult(writer);
|
||||
try {
|
||||
marshaller.marshal(output, result);
|
||||
}
|
||||
catch (IOException xse) {
|
||||
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + result.getSystemId()
|
||||
+ "]", xse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package org.springframework.batch.io.xml.oxm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.batch.io.xml.ObjectToXmlSerializer;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
|
||||
/**
|
||||
* Object to xml serializer that wraps a Spring-OXM
|
||||
* Marshaller object.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class MarshallingObjectToXmlSerializer implements ObjectToXmlSerializer{
|
||||
|
||||
private Marshaller marshaller;
|
||||
|
||||
private Result result;
|
||||
|
||||
public MarshallingObjectToXmlSerializer(Marshaller marshaller){
|
||||
this.marshaller = marshaller;
|
||||
}
|
||||
|
||||
public void setEventWriter(XMLEventWriter writer) {
|
||||
result = new StaxResult(writer);
|
||||
}
|
||||
|
||||
public void serializeObject(Object output) {
|
||||
|
||||
try {
|
||||
marshaller.marshal(output, result);
|
||||
} catch (IOException xse) {
|
||||
throw new DataAccessResourceFailureException(
|
||||
"Unable to write to file resource: [" + result.getSystemId() + "]", xse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,23 +4,23 @@ import java.io.IOException;
|
||||
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
|
||||
import org.springframework.batch.io.xml.FragmentDeserializer;
|
||||
import org.springframework.batch.io.xml.EventReaderDeserializer;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
|
||||
/**
|
||||
* Delegates deserializing to Spring-WS {@link Unmarshaller}.
|
||||
* Delegates deserializing to Spring OXM {@link Unmarshaller}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Lucas Ward
|
||||
*/
|
||||
public class UnmarshallingFragmentDeserializer implements FragmentDeserializer {
|
||||
public class UnmarshallingEventReaderDeserializer implements EventReaderDeserializer {
|
||||
|
||||
private Unmarshaller unmarshaller;
|
||||
|
||||
public UnmarshallingFragmentDeserializer(Unmarshaller unmarshaller){
|
||||
public UnmarshallingEventReaderDeserializer(Unmarshaller unmarshaller){
|
||||
Assert.notNull(unmarshaller);
|
||||
this.unmarshaller = unmarshaller;
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import java.util.Collections;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.FlatFileItemWriter;
|
||||
import org.springframework.batch.io.file.transform.Converter;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
|
||||
@@ -37,7 +37,7 @@ public class StaxEventReaderItemReaderTests extends TestCase {
|
||||
// test xml input
|
||||
private String xml = "<root> <fragment> <misc1/> </fragment> <misc2/> <fragment> testString </fragment> </root>";
|
||||
|
||||
private FragmentDeserializer deserializer = new MockFragmentDeserializer();
|
||||
private EventReaderDeserializer deserializer = new MockFragmentDeserializer();
|
||||
|
||||
private static final String FRAGMENT_ROOT_ELEMENT = "fragment";
|
||||
|
||||
@@ -277,7 +277,7 @@ public class StaxEventReaderItemReaderTests extends TestCase {
|
||||
* document events for the fragment root & end tags + skips the fragment
|
||||
* contents.
|
||||
*/
|
||||
private static class MockFragmentDeserializer implements FragmentDeserializer {
|
||||
private static class MockFragmentDeserializer implements EventReaderDeserializer {
|
||||
|
||||
/**
|
||||
* A simple mapFragment implementation checking the
|
||||
@@ -339,7 +339,7 @@ public class StaxEventReaderItemReaderTests extends TestCase {
|
||||
/**
|
||||
* Moves cursor inside the fragment body and causes rollback.
|
||||
*/
|
||||
private static class ExceptionFragmentDeserializer implements FragmentDeserializer {
|
||||
private static class ExceptionFragmentDeserializer implements EventReaderDeserializer {
|
||||
|
||||
public Object deserializeFragment(XMLEventReader eventReader) {
|
||||
eventReader.next();
|
||||
|
||||
@@ -12,7 +12,7 @@ import junit.framework.TestCase;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.batch.io.xml.StaxEventItemWriter;
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingObjectToXmlSerializer;
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingEventWriterSerializer;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -55,7 +55,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
*/
|
||||
public void testWrite() throws Exception {
|
||||
Marshaller marshaller = new InputCheckMarshaller();
|
||||
MarshallingObjectToXmlSerializer serializer = new MarshallingObjectToXmlSerializer(marshaller);
|
||||
MarshallingEventWriterSerializer serializer = new MarshallingEventWriterSerializer(marshaller);
|
||||
writer.setSerializer(serializer);
|
||||
|
||||
// see asserts in the marshaller
|
||||
@@ -193,7 +193,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
source.setResource(resource);
|
||||
|
||||
Marshaller marshaller = new SimpleMarshaller();
|
||||
MarshallingObjectToXmlSerializer serializer = new MarshallingObjectToXmlSerializer(marshaller);
|
||||
MarshallingEventWriterSerializer serializer = new MarshallingEventWriterSerializer(marshaller);
|
||||
source.setSerializer(serializer);
|
||||
|
||||
source.setEncoding("UTF-8");
|
||||
|
||||
@@ -26,7 +26,7 @@ import javax.xml.transform.Result;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingObjectToXmlSerializer;
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingEventWriterSerializer;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
@@ -39,21 +39,23 @@ import org.springframework.oxm.XmlMappingException;
|
||||
*/
|
||||
public class MarshallingObjectToXmlSerializerTests extends TestCase {
|
||||
|
||||
MarshallingObjectToXmlSerializer xmlSerializer;
|
||||
MarshallingEventWriterSerializer xmlSerializer;
|
||||
|
||||
MockMarshaller mockMarshaller = new MockMarshaller();
|
||||
|
||||
private StubXmlEventWriter writer;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
xmlSerializer = new MarshallingObjectToXmlSerializer(mockMarshaller);
|
||||
xmlSerializer.setEventWriter(new StubXmlEventWriter());
|
||||
xmlSerializer = new MarshallingEventWriterSerializer(mockMarshaller);
|
||||
writer = new StubXmlEventWriter();
|
||||
}
|
||||
|
||||
public void testSuccessfulWrite(){
|
||||
|
||||
Object objectToOutput = new Object();
|
||||
xmlSerializer.serializeObject(objectToOutput);
|
||||
xmlSerializer.serializeObject(writer, objectToOutput);
|
||||
assertEquals(objectToOutput, mockMarshaller.getMarshalledObject());
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ public class MarshallingObjectToXmlSerializerTests extends TestCase {
|
||||
|
||||
mockMarshaller.setThrowException(true);
|
||||
try{
|
||||
xmlSerializer.serializeObject(new Object());
|
||||
xmlSerializer.serializeObject(writer, new Object());
|
||||
fail("Exception expected");
|
||||
}catch(DataAccessResourceFailureException ex){
|
||||
//expected
|
||||
|
||||
@@ -8,21 +8,21 @@ import javax.xml.stream.XMLInputFactory;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.io.xml.oxm.UnmarshallingFragmentDeserializer;
|
||||
import org.springframework.batch.io.xml.oxm.UnmarshallingEventReaderDeserializer;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
/**
|
||||
* Tests for {@link UnmarshallingFragmentDeserializer}
|
||||
* Tests for {@link UnmarshallingEventReaderDeserializer}
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class UnmarshallingFragmentDeserializerTests extends TestCase {
|
||||
|
||||
// object under test
|
||||
private UnmarshallingFragmentDeserializer deserializer;
|
||||
private UnmarshallingEventReaderDeserializer deserializer;
|
||||
|
||||
private XMLEventReader eventReader;
|
||||
private String xml = "<root> </root>";
|
||||
@@ -37,7 +37,7 @@ public class UnmarshallingFragmentDeserializerTests extends TestCase {
|
||||
eventReader = XMLInputFactory.newInstance().createXMLEventReader(input.getInputStream());
|
||||
unmarshaller = (Unmarshaller) unmarshallerControl.getMock();
|
||||
unmarshallerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
|
||||
deserializer = new UnmarshallingFragmentDeserializer(unmarshaller);
|
||||
deserializer = new UnmarshallingEventReaderDeserializer(unmarshaller);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +81,7 @@ public class UnmarshallingFragmentDeserializerTests extends TestCase {
|
||||
*/
|
||||
public void testExceptionOnNullUnmarshaller() {
|
||||
try {
|
||||
deserializer = new UnmarshallingFragmentDeserializer(null);
|
||||
deserializer = new UnmarshallingEventReaderDeserializer(null);
|
||||
fail("Exception expected");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
|
||||
@@ -8,7 +8,7 @@ import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.oxm.domain.Trade;
|
||||
import org.springframework.batch.io.xml.StaxEventItemReader;
|
||||
import org.springframework.batch.io.xml.oxm.UnmarshallingFragmentDeserializer;
|
||||
import org.springframework.batch.io.xml.oxm.UnmarshallingEventReaderDeserializer;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
@@ -26,7 +26,7 @@ public abstract class AbstractStaxEventReaderItemReaderTests extends TestCase {
|
||||
source.setResource(resource);
|
||||
|
||||
source.setFragmentRootElementName("trade");
|
||||
UnmarshallingFragmentDeserializer deserializer = new UnmarshallingFragmentDeserializer(getUnmarshaller());
|
||||
UnmarshallingEventReaderDeserializer deserializer = new UnmarshallingEventReaderDeserializer(getUnmarshaller());
|
||||
source.setFragmentDeserializer(deserializer);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.springframework.batch.io.oxm.domain.Trade;
|
||||
import org.springframework.batch.io.xml.StaxEventItemWriter;
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingObjectToXmlSerializer;
|
||||
import org.springframework.batch.io.xml.oxm.MarshallingEventWriterSerializer;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -56,7 +56,7 @@ public abstract class AbstractStaxEventWriterItemWriterTests extends TestCase {
|
||||
resource = new FileSystemResource(outputFile);
|
||||
writer.setResource(resource);
|
||||
|
||||
MarshallingObjectToXmlSerializer mapper = new MarshallingObjectToXmlSerializer(getMarshaller());
|
||||
MarshallingEventWriterSerializer mapper = new MarshallingEventWriterSerializer(getMarshaller());
|
||||
writer.setSerializer(mapper);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import org.springframework.batch.io.file.Converter;
|
||||
import org.springframework.batch.io.file.transform.Converter;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.io.file.Converter;
|
||||
import org.springframework.batch.io.file.transform.Converter;
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
value="data/staxJob/input/20070918.testStream.xmlFileStep.xml" />
|
||||
<property name="fragmentDeserializer">
|
||||
<bean
|
||||
class="org.springframework.batch.io.xml.oxm.UnmarshallingFragmentDeserializer">
|
||||
class="org.springframework.batch.io.xml.oxm.UnmarshallingEventReaderDeserializer">
|
||||
<constructor-arg>
|
||||
<bean
|
||||
class="org.springframework.oxm.xstream.XStreamMarshaller">
|
||||
@@ -66,7 +66,7 @@
|
||||
</bean>
|
||||
|
||||
<bean
|
||||
class="org.springframework.batch.io.xml.oxm.MarshallingObjectToXmlSerializer"
|
||||
class="org.springframework.batch.io.xml.oxm.MarshallingEventWriterSerializer"
|
||||
id="tradeMarshallingSerializer">
|
||||
<constructor-arg>
|
||||
<bean
|
||||
|
||||
Reference in New Issue
Block a user