Example usage for org.springframework.batch.item ItemStreamException ItemStreamException

List of usage examples for org.springframework.batch.item ItemStreamException ItemStreamException

Introduction

In this page you can find the example usage for org.springframework.batch.item ItemStreamException ItemStreamException.

Prototype

public ItemStreamException(String msg, Throwable nested) 

Source Link

Document

Constructs a new instance with a message and nested exception.

Usage

From source file:org.geoserver.backuprestore.writer.CatalogFileWriter.java

private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
    OutputState outputState = getOutputState();

    if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }//from w  w  w  .j a  va  2 s. c om

    try {
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
}

From source file:org.geoserver.backuprestore.writer.CatalogFileWriter.java

private OutputState getOutputState() {
    if (state == null) {
        File file;/*from   ww  w .  j  ava  2s .  co  m*/
        try {
            file = resource.getFile();
        } catch (IOException e) {
            throw new ItemStreamException("Could not convert resource to file: [" + resource + "]", e);
        }
        Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
        state = new OutputState();
        state.setDeleteIfExists(shouldDeleteIfExists);
        state.setAppendAllowed(append);
        state.setEncoding(encoding);
    }
    return state;
}

From source file:org.springframework.batch.core.step.item.ChunkMonitor.java

@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
    super.open(executionContext);
    if (streamsRegistered) {
        stream.open(executionContext);/*from w  w  w  .  j  a v a  2 s . c  o  m*/
        ChunkMonitorData data = new ChunkMonitorData(executionContext.getInt(getExecutionContextKey(OFFSET), 0),
                0);
        holder.set(data);
        if (reader == null) {
            logger.warn("No ItemReader set (must be concurrent step), so ignoring offset data.");
            return;
        }
        for (int i = 0; i < data.offset; i++) {
            try {
                reader.read();
            } catch (Exception e) {
                throw new ItemStreamException("Could not position reader with offset: " + data.offset, e);
            }
        }

        resetOffset();
    }
}

From source file:org.springframework.batch.item.file.FlatFileItemWriter.java

/**
 * @see ItemStream#close()//from   www .  ja v  a2 s  .  c  om
 */
@Override
public void close() {
    super.close();
    if (state != null) {
        try {
            if (footerCallback != null && state.outputBufferedWriter != null) {
                footerCallback.writeFooter(state.outputBufferedWriter);
                state.outputBufferedWriter.flush();
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write footer before closing", e);
        } finally {
            state.close();
            if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
                try {
                    resource.getFile().delete();
                } catch (IOException e) {
                    throw new ItemStreamException("Failed to delete empty file on close", e);
                }
            }
            state = null;
        }
    }
}

From source file:org.springframework.batch.item.file.FlatFileItemWriter.java

private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
    OutputState outputState = getOutputState();
    if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }/*from  w  ww .j ava 2s  .c  om*/
    try {
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

From source file:org.springframework.batch.item.support.AbstractFileItemWriter.java

protected OutputState getOutputState() {
    if (state == null) {
        File file;/*from  w w w. ja v a 2s.c  o m*/
        try {
            file = resource.getFile();
        } catch (IOException e) {
            throw new ItemStreamException("Could not convert resource to file: [" + resource + "]", e);
        }
        Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
        state = new OutputState();
        state.setDeleteIfExists(shouldDeleteIfExists);
        state.setAppendAllowed(append);
        state.setEncoding(encoding);
    }
    return state;
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Open the output source//  w w w .  jav  a2 s  . c o m
 * 
 * @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
 */
@SuppressWarnings("unchecked")
@Override
public void open(ExecutionContext executionContext) {
    super.open(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))) {
        startAtPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME));
        currentRecordCount = executionContext.getLong(getExecutionContextKey(WRITE_STATISTICS_NAME));
        if (executionContext.containsKey(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME))) {
            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
            restarted = false;
            startAtPosition = 0;
        } else {
            restarted = true;
        }
    } else {
        currentRecordCount = 0;
        restarted = false;
    }

    open(startAtPosition);

    if (startAtPosition == 0) {
        try {
            if (headerCallback != null) {
                UnclosedElementCollectingEventWriter headerCallbackWriter = new UnclosedElementCollectingEventWriter(
                        delegateEventWriter);
                headerCallback.write(headerCallbackWriter);
                unclosedHeaderCallbackElements = headerCallbackWriter.getUnclosedElements();
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write headerItems", e);
        }
    }

    this.initialized = true;

}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Flush and close the output source.//from   w  ww. j ava 2s .c o m
 * 
 * @see org.springframework.batch.item.ItemStream#close()
 */
@Override
public void close() {
    super.close();

    XMLEventFactory factory = createXmlEventFactory();
    try {
        delegateEventWriter.add(factory.createCharacters(""));
    } catch (XMLStreamException e) {
        log.error(e);
    }

    try {
        if (footerCallback != null) {
            XMLEventWriter footerCallbackWriter = delegateEventWriter;
            if (restarted && !unclosedHeaderCallbackElements.isEmpty()) {
                footerCallbackWriter = new UnopenedElementClosingEventWriter(delegateEventWriter,
                        bufferedWriter, unclosedHeaderCallbackElements);
            }
            footerCallback.write(footerCallbackWriter);
        }
        delegateEventWriter.flush();
        endDocument(delegateEventWriter);
    } catch (IOException e) {
        throw new ItemStreamException("Failed to write footer items", e);
    } catch (XMLStreamException e) {
        throw new ItemStreamException("Failed to write end document tag", e);
    } finally {

        try {
            delegateEventWriter.close();
        } catch (XMLStreamException e) {
            log.error("Unable to close file resource: [" + resource + "] " + e);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                log.error("Unable to close file resource: [" + resource + "] " + e);
            } finally {
                if (!transactional) {
                    closeStream();
                }
            }
        }
        if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
            try {
                resource.getFile().delete();
            } catch (IOException e) {
                throw new ItemStreamException("Failed to delete empty file on close", e);
            }
        }
    }

    this.initialized = false;
}