BeanIO 1.2.3

org.beanio.spring
Class BeanIOFlatFileItemWriter<T>

java.lang.Object
  extended by org.beanio.spring.BeanIOFlatFileItemWriter<T>
Type Parameters:
T - Class type written to the file
All Implemented Interfaces:
ResourceAwareItemWriterItemStream<T>, ItemStream, ItemWriter<T>, InitializingBean

public class BeanIOFlatFileItemWriter<T>
extends Object
implements ItemStream, ItemWriter<T>, ResourceAwareItemWriterItemStream<T>, InitializingBean

A Spring Batch item writer that uses a BeanIO stream mapping file to write items to a flat file. Restart capabilities are fully supported.

This implementation requires Spring 2.5 or greater, and Spring Batch 2.1.x.

Since:
1.2
Author:
Kevin Seim

Constructor Summary
BeanIOFlatFileItemWriter()
          Constructs a new BeanIOFlatFileItemWriter.
 
Method Summary
 void afterPropertiesSet()
           
 void close()
           
protected  void initializeStreamFactory()
          Creates a StreamFactory if one was not set, loads the stream mapping resource if set, and validates the StreamFactory has a mapping for the configured stream name.
 void open(ExecutionContext executionContext)
           
 void setAppendAllowed(boolean append)
          Flag to indicate that the target file should be appended if it already exists.
 void setEncoding(String encoding)
          Sets the output stream encoding.
 void setHeaderCallback(FlatFileHeaderCallback headerCallback)
          The FlatFileHeaderCallback if called before writing the first item to the file.
 void setLineSeparator(String lineSeparator)
          Sets the line separator used for the header and footer callback only.
 void setName(String name)
          Sets the name to use for prefixing keys added to the execution context.
 void setResource(Resource resource)
           
 void setSaveState(boolean saveState)
          Set the flag indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update.
 void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty)
          Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close.
 void setShouldDeleteIfExists(boolean shouldDeleteIfExists)
          Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created.
 void setStreamFactory(StreamFactory streamFactory)
          Sets the StreamFactory for loading stream mapping configurations.
 void setStreamMapping(Resource streamMapping)
          Sets the mapping file resource.
 void setStreamName(String streamName)
          Sets the mapping configuration's stream name for writing this output stream.
 void setTransactional(boolean transactional)
          Flag to indicate that writing to the buffer should be delayed if a transaction is active.
 void update(ExecutionContext executionContext)
           
 void write(List<? extends T> items)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanIOFlatFileItemWriter

public BeanIOFlatFileItemWriter()
Constructs a new BeanIOFlatFileItemWriter.

Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

open

public void open(ExecutionContext executionContext)
          throws ItemStreamException
Specified by:
open in interface ItemStream
Throws:
ItemStreamException

update

public void update(ExecutionContext executionContext)
            throws ItemStreamException
Specified by:
update in interface ItemStream
Throws:
ItemStreamException

close

public void close()
           throws ItemStreamException
Specified by:
close in interface ItemStream
Throws:
ItemStreamException

write

public void write(List<? extends T> items)
           throws Exception
Specified by:
write in interface ItemWriter<T>
Throws:
Exception

setStreamFactory

public void setStreamFactory(StreamFactory streamFactory)
Sets the StreamFactory for loading stream mapping configurations. If not set, a new default StreamFactory is created.

Parameters:
streamFactory - the StreamFactory to use for loading stream mapping configurations

setStreamMapping

public void setStreamMapping(Resource streamMapping)
Sets the mapping file resource. A mapping file is required if a stream factory is not set.

Parameters:
streamMapping - the stream mapping resource

setStreamName

public void setStreamName(String streamName)
Sets the mapping configuration's stream name for writing this output stream.

Parameters:
streamName - the stream name

setResource

public void setResource(Resource resource)
Specified by:
setResource in interface ResourceAwareItemWriterItemStream<T>

setEncoding

public void setEncoding(String encoding)
Sets the output stream encoding. If not set, the default system charset is used to write the output stream.

Parameters:
encoding - output stream encoding

initializeStreamFactory

protected void initializeStreamFactory()
                                throws Exception
Creates a StreamFactory if one was not set, loads the stream mapping resource if set, and validates the StreamFactory has a mapping for the configured stream name.

Throws:
IllegalStateException - if the configured stream name is not found in the StreamFactory after loading the stream mapping
Exception

setAppendAllowed

public void setAppendAllowed(boolean append)
Flag to indicate that the target file should be appended if it already exists. If this flag is set then the flag shouldDeleteIfExists is automatically set to false, so that flag should not be set explicitly. Defaults to false.

Parameters:
append - the flag value to set

setShouldDeleteIfExists

public void setShouldDeleteIfExists(boolean shouldDeleteIfExists)
Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created. Defaults to true, so no appending except on restart. If set to false and appendAllowed is also false then there will be an exception when the stream is opened to prevent existing data being potentially corrupted.

Parameters:
shouldDeleteIfExists - the flag value to set

setShouldDeleteIfEmpty

public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty)
Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close. Defaults to false.

Parameters:
shouldDeleteIfEmpty - the flag value to set

setSaveState

public void setSaveState(boolean saveState)
Set the flag indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update. Setting this to false means that it will always start at the beginning on a restart.

Parameters:
saveState -

setTransactional

public void setTransactional(boolean transactional)
Flag to indicate that writing to the buffer should be delayed if a transaction is active. Defaults to true.

Parameters:
transactional - set to false to disable buffering writes for transactions

setName

public void setName(String name)
Sets the name to use for prefixing keys added to the execution context. Defaults to the class name.

Parameters:
name - context key prefix

setLineSeparator

public void setLineSeparator(String lineSeparator)
Sets the line separator used for the header and footer callback only. Other lines are terminated based on the BeanIO stream mapping configuration. Defaults to the System property 'line.separator'.

Parameters:
lineSeparator - the line separator to set

setHeaderCallback

public void setHeaderCallback(FlatFileHeaderCallback headerCallback)
The FlatFileHeaderCallback if called before writing the first item to the file. The configured line separator is written immediately following the header.

Parameters:
headerCallback - the FlatFileHeaderCallback to invoke when writing to a new file

BeanIO 1.2.3

Copyright © 2010-2012 Kevin Seim