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;
|
||||
}
|
||||
Reference in New Issue
Block a user