Skip to content

Commit

Permalink
Remove DataAccessResourceFailureException in xml related classes
Browse files Browse the repository at this point in the history
Resolves #4132
  • Loading branch information
lcmarvin authored and fmbenhassine committed Jul 18, 2022
1 parent 0d7a160 commit 1e24e38
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 158 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2022 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.
Expand Down Expand Up @@ -52,7 +52,6 @@
import org.springframework.batch.support.transaction.TransactionAwareBufferedWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.util.Assert;
Expand All @@ -64,12 +63,12 @@
/**
* An implementation of {@link ItemWriter} which uses StAX and
* {@link Marshaller} for serializing object to XML.
*
*
* This item writer also provides restart, statistics and transaction features
* by implementing corresponding interfaces.
*
*
* The implementation is <b>not</b> thread-safe.
*
*
* @author Peter Zozom
* @author Robert Kasanicky
* @author Michael Minella
Expand Down Expand Up @@ -98,7 +97,7 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> impl

// unclosed header callback elements property name
private static final String UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME = "unclosedHeaderCallbackElements";

// restart data property name
private static final String WRITE_STATISTICS_NAME = "record.count";

Expand Down Expand Up @@ -159,11 +158,11 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> impl
private boolean forceSync;

private boolean shouldDeleteIfEmpty = false;

private boolean restarted = false;

private boolean initialized = false;

// List holding the QName of elements that were opened in the header callback, but not closed
private List<QName> unclosedHeaderCallbackElements = Collections.emptyList();

Expand All @@ -173,7 +172,7 @@ public StaxEventItemWriter() {

/**
* Set output file.
*
*
* @param resource the output file
*/
@Override
Expand All @@ -183,7 +182,7 @@ public void setResource(Resource resource) {

/**
* Set Object to XML marshaller.
*
*
* @param marshaller the Object to XML marshaller
*/
public void setMarshaller(Marshaller marshaller) {
Expand Down Expand Up @@ -212,7 +211,7 @@ public void setFooterCallback(StaxWriterCallback footerCallback) {
/**
* Flag to indicate that writes should be deferred to the end of a
* transaction if present. Defaults to true.
*
*
* @param transactional the flag to set
*/
public void setTransactional(boolean transactional) {
Expand All @@ -225,7 +224,7 @@ public void setTransactional(boolean transactional) {
* be lost if the OS crashes in between a write and a cache flush. Setting
* to true may result in slower performance for usage patterns involving
* many frequent writes.
*
*
* @param forceSync the flag value to set
*/
public void setForceSync(boolean forceSync) {
Expand All @@ -235,7 +234,7 @@ public void setForceSync(boolean forceSync) {
/**
* Flag to indicate that the target file should be deleted if no items have
* been written (other than header and footer) on close. Defaults to false.
*
*
* @param shouldDeleteIfEmpty the flag value to set
*/
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {
Expand All @@ -244,7 +243,7 @@ public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {

/**
* Get used encoding.
*
*
* @return the encoding used
*/
public String getEncoding() {
Expand All @@ -253,7 +252,7 @@ public String getEncoding() {

/**
* Set encoding to be used for output file.
*
*
* @param encoding the encoding to be used
*/
public void setEncoding(String encoding) {
Expand All @@ -271,7 +270,7 @@ public String getVersion() {

/**
* Set XML version to be used for output XML.
*
*
* @param version the XML version to be used
*/
public void setVersion(String version) {
Expand Down Expand Up @@ -303,7 +302,7 @@ public void setStandalone(Boolean standalone) {

/**
* Get the tag name of the root element.
*
*
* @return the root element tag name
*/
public String getRootTagName() {
Expand All @@ -314,16 +313,16 @@ public String getRootTagName() {
* Set the tag name of the root element. If not set, default name is used
* ("root"). Namespace URI and prefix can also be set optionally using the
* notation:
*
*
* <pre>
* {uri}prefix:root
* </pre>
*
*
* The prefix is optional (defaults to empty), but if it is specified then
* the uri must be provided. In addition you might want to declare other
* namespaces using the {@link #setRootElementAttributes(Map) root
* attributes}.
*
*
* @param rootTagName the tag name to be used for the root element
*/
public void setRootTagName(String rootTagName) {
Expand All @@ -332,7 +331,7 @@ public void setRootTagName(String rootTagName) {

/**
* Get the namespace prefix of the root element. Empty by default.
*
*
* @return the rootTagNamespacePrefix
*/
public String getRootTagNamespacePrefix() {
Expand All @@ -341,7 +340,7 @@ public String getRootTagNamespacePrefix() {

/**
* Get the namespace of the root element.
*
*
* @return the rootTagNamespace
*/
public String getRootTagNamespace() {
Expand All @@ -350,7 +349,7 @@ public String getRootTagNamespace() {

/**
* Get attributes of the root element.
*
*
* @return attributes of the root element
*/
public Map<String, String> getRootElementAttributes() {
Expand All @@ -360,7 +359,7 @@ public Map<String, String> getRootElementAttributes() {
/**
* Set the root element attributes to be written. If any of the key names
* begin with "xmlns:" then they are treated as namespace declarations.
*
*
* @param rootElementAttributes attributes of the root element
*/
public void setRootElementAttributes(Map<String, String> rootElementAttributes) {
Expand All @@ -370,7 +369,7 @@ public void setRootElementAttributes(Map<String, String> rootElementAttributes)
/**
* Set "overwrite" flag for the output file. Flag is ignored when output
* file processing is restarted.
*
*
* @param overwriteOutput If set to true, output file will be overwritten
* (this flag is ignored when processing is restart).
*/
Expand Down Expand Up @@ -403,7 +402,7 @@ public void afterPropertiesSet() throws Exception {
* Open the output source
*
* @param executionContext the batch context.
*
*
* @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
*/
@SuppressWarnings("unchecked")
Expand All @@ -414,7 +413,7 @@ public void open(ExecutionContext executionContext) {
Assert.notNull(resource, "The resource must be set");

long startAtPosition = 0;

// if restart data is provided, restart from provided offset
// otherwise start from beginning
if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
Expand All @@ -424,7 +423,7 @@ public void open(ExecutionContext executionContext) {
unclosedHeaderCallbackElements = (List<QName>) executionContext
.get(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME));
}

restarted = true;
if (shouldDeleteIfEmpty && currentRecordCount == 0) {
// previous execution deleted the output file because no items were written
Expand Down Expand Up @@ -476,7 +475,7 @@ private void open(long position) {
setPosition(position);
}
catch (IOException ioe) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", ioe);
throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", ioe);
}

XMLOutputFactory outputFactory = createXmlOutputFactory();
Expand Down Expand Up @@ -524,14 +523,14 @@ public void run() {
}
}
catch (XMLStreamException xse) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", xse);
throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", xse);
}
catch (UnsupportedEncodingException e) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource
throw new ItemStreamException("Unable to write to file resource: [" + resource
+ "] with encoding=[" + encoding + "]", e);
}
}
catch (IOException e) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e);
throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", e);
}
}

Expand Down Expand Up @@ -588,7 +587,7 @@ protected Result createStaxResult() {
* <li>rootTagNamespacePrefix for rootTagName</li>
* <li>any other xmlns namespace prefix declarations in the root element attributes</li>
* </ul>
*
*
* @param writer XML event writer
*
* @throws XMLStreamException thrown if error occurs while setting the
Expand Down Expand Up @@ -627,7 +626,7 @@ protected void initNamespaceContext(XMLEventWriter writer) throws XMLStreamExcep
* </ul>
* If this is not sufficient for you, simply override this method. Encoding,
* version and root tag name can be retrieved with corresponding getters.
*
*
* @param writer XML event writer
*
* @throws XMLStreamException thrown if error occurs.
Expand Down Expand Up @@ -685,7 +684,7 @@ protected void startDocument(XMLEventWriter writer) throws XMLStreamException {

/**
* Writes the EndDocument tag manually.
*
*
* @param writer XML event writer
*
* @throws XMLStreamException thrown if error occurs.
Expand All @@ -700,13 +699,13 @@ protected void endDocument(XMLEventWriter writer) throws XMLStreamException {
bufferedWriter.write("</" + nsPrefix + getRootTagName() + ">");
}
catch (IOException ioe) {
throw new DataAccessResourceFailureException("Unable to close file resource: [" + resource + "]", ioe);
throw new XMLStreamException("Unable to close file resource: [" + resource + "]", ioe);
}
}

/**
* Flush and close the output source.
*
*
* @see org.springframework.batch.item.ItemStream#close()
*/
@Override
Expand All @@ -727,7 +726,7 @@ public void close() {
if (restarted && !unclosedHeaderCallbackElements.isEmpty()) {
footerCallbackWriter = new UnopenedElementClosingEventWriter(
delegateEventWriter, bufferedWriter, unclosedHeaderCallbackElements);
}
}
footerCallback.write(footerCallbackWriter);
}
delegateEventWriter.flush();
Expand Down Expand Up @@ -784,7 +783,7 @@ private void closeStream() {

/**
* Write the value objects and flush them to the file.
*
*
* @param items the value object
*
* @throws IOException thrown if general error occurs.
Expand All @@ -809,18 +808,18 @@ public void write(List<? extends T> items) throws XmlMappingException, IOExcepti
eventWriter.flush();
if (forceSync) {
channel.force(false);
}
}
}
catch (XMLStreamException | IOException e) {
throw new WriteFailedException("Failed to flush the events", e);
}
}
}

/**
* Get the restart data.
*
* @param executionContext the batch context.
*
*
* @see org.springframework.batch.item.ItemStream#update(ExecutionContext)
*/
@Override
Expand All @@ -833,14 +832,14 @@ public void update(ExecutionContext executionContext) {
if (!unclosedHeaderCallbackElements.isEmpty()) {
executionContext.put(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME),
unclosedHeaderCallbackElements);
}
}
}
}

/*
* Get the actual position in file channel. This method flushes any buffered
* data before position is read.
*
*
* @return byte offset in file channel
*/
private long getPosition() {
Expand All @@ -855,15 +854,15 @@ private long getPosition() {
}
}
catch (Exception e) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e);
throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", e);
}

return position;
}

/**
* Set the file channel position.
*
*
* @param newPosition new file channel position
*/
private void setPosition(long newPosition) {
Expand All @@ -873,7 +872,7 @@ private void setPosition(long newPosition) {
channel.position(newPosition);
}
catch (IOException e) {
throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e);
throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", e);
}

}
Expand Down

0 comments on commit 1e24e38

Please sign in to comment.