Package org.beanio
Class StreamFactory
- java.lang.Object
-
- org.beanio.StreamFactory
-
- Direct Known Subclasses:
DefaultStreamFactory
public abstract class StreamFactory extends java.lang.Object
AStreamFactory
is used to load BeanIO mapping files and createBeanReader
,BeanWriter
,Unmarshaller
andMarshaller
instances.The default
StreamFactory
implementation can be safely shared across multiple threads.- Since:
- 1.0
- See Also:
BeanReader
,BeanWriter
,Unmarshaller
,Marshaller
-
-
Constructor Summary
Constructors Constructor Description StreamFactory()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Marshaller
createMarshaller(java.lang.String name)
Creates a newMarshaller
for marshalling bean objects.BeanReader
createReader(java.lang.String name, java.io.File file)
Creates a newBeanReader
for reading from a file.BeanReader
createReader(java.lang.String name, java.io.Reader in)
Creates a newBeanReader
for reading from the given input stream.abstract BeanReader
createReader(java.lang.String name, java.io.Reader in, java.util.Locale locale)
Creates a newBeanReader
for reading from a stream.BeanReader
createReader(java.lang.String name, java.lang.String filename)
Creates a newBeanReader
for reading from a file.Unmarshaller
createUnmarshaller(java.lang.String name)
Creates a newUnmarshaller
for unmarshalling records.abstract Unmarshaller
createUnmarshaller(java.lang.String name, java.util.Locale locale)
Creates a newUnmarshaller
for unmarshalling records.BeanWriter
createWriter(java.lang.String name, java.io.File file)
Creates a newBeanWriter
for writing to the given file.abstract BeanWriter
createWriter(java.lang.String name, java.io.Writer out)
Creates a newBeanWriter
for writing to a stream.abstract void
define(StreamBuilder builder)
Defines a new stream mapping.protected java.lang.ClassLoader
getClassLoader()
Returns the class loader to use for resolving classpath resources and bean objects declared in a mapping file.protected void
init()
This method is invoked after a StreamFactory is loaded and all attributes have been set.abstract boolean
isMapped(java.lang.String streamName)
Test whether a mapping configuration exists for a named stream.void
load(java.io.File file)
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.void
load(java.io.File file, java.util.Properties properties)
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.void
load(java.io.InputStream in)
Loads a BeanIO mapping file, and adds the configured streams to this factory.abstract void
load(java.io.InputStream in, java.util.Properties properties)
Loads a BeanIO mapping file, and adds the configured streams to this factory.void
load(java.lang.String filename)
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.void
load(java.lang.String filename, java.util.Properties properties)
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.void
loadResource(java.lang.String resource)
Loads a BeanIO mapping file from the application's classpath.void
loadResource(java.lang.String resource, java.util.Properties properties)
Loads a BeanIO mapping file from the application's classpath.static StreamFactory
newInstance()
Returns a newStreamFactory
instance.static StreamFactory
newInstance(java.lang.ClassLoader classLoader)
Returns a newStreamFactory
instance.protected void
setClassLoader(java.lang.ClassLoader cl)
Sets the class loader to use for resolving classpath resources and bean objects declared in a mapping files.
-
-
-
Method Detail
-
createReader
public BeanReader createReader(java.lang.String name, java.lang.String filename) throws java.lang.IllegalArgumentException, BeanReaderIOException
Creates a newBeanReader
for reading from a file.- Parameters:
name
- the name of the stream in the mapping filefilename
- the name of the file to read- Returns:
- the created
BeanReader
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support reading an input streamBeanReaderIOException
- if the file could not be opened for reading
-
createReader
public BeanReader createReader(java.lang.String name, java.io.File file) throws java.lang.IllegalArgumentException, BeanReaderIOException
Creates a newBeanReader
for reading from a file.- Parameters:
name
- the name of the stream in the mapping filefile
- theFile
to read- Returns:
- the created
BeanReader
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support reading an input streamBeanReaderIOException
- if the file could not be opened for reading
-
createReader
public BeanReader createReader(java.lang.String name, java.io.Reader in) throws java.lang.IllegalArgumentException
Creates a newBeanReader
for reading from the given input stream.- Parameters:
name
- the name of the stream in the mapping filein
- the input stream to read from- Returns:
- the created
BeanReader
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support reading an input stream
-
createReader
public abstract BeanReader createReader(java.lang.String name, java.io.Reader in, java.util.Locale locale) throws java.lang.IllegalArgumentException
Creates a newBeanReader
for reading from a stream.- Parameters:
name
- the name of the stream in the mapping filein
- the input stream to read fromlocale
- theLocale
used to format error messages, or null to useLocale.getDefault()
- Returns:
- the created
BeanReader
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support reading an input stream
-
createUnmarshaller
public Unmarshaller createUnmarshaller(java.lang.String name) throws java.lang.IllegalArgumentException
Creates a newUnmarshaller
for unmarshalling records.- Parameters:
name
- the name of the stream in the mapping file- Returns:
- the created
Unmarshaller
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support unmarshalling
-
createUnmarshaller
public abstract Unmarshaller createUnmarshaller(java.lang.String name, java.util.Locale locale)
Creates a newUnmarshaller
for unmarshalling records.- Parameters:
name
- the name of the stream in the mapping filelocale
- theLocale
used to format error messages, or null to useLocale.getDefault()
- Returns:
- the created
Unmarshaller
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support unmarshalling
-
createWriter
public BeanWriter createWriter(java.lang.String name, java.io.File file) throws java.lang.IllegalArgumentException, BeanWriterIOException
Creates a newBeanWriter
for writing to the given file.- Parameters:
name
- the name of the stream in the mapping filefile
- the file to write to- Returns:
- the created
BeanWriter
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support writing to an output streamBeanWriterIOException
- if the file could not be opened for writing
-
createWriter
public abstract BeanWriter createWriter(java.lang.String name, java.io.Writer out) throws java.lang.IllegalArgumentException
Creates a newBeanWriter
for writing to a stream.- Parameters:
name
- the name of the stream in the mapping fileout
- the output stream to write to- Returns:
- the created
BeanWriter
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support writing to an output stream
-
createMarshaller
public abstract Marshaller createMarshaller(java.lang.String name) throws java.lang.IllegalArgumentException
Creates a newMarshaller
for marshalling bean objects.- Parameters:
name
- the name of the stream in the mapping file- Returns:
- the created
Marshaller
- Throws:
java.lang.IllegalArgumentException
- if there is no stream configured for the given name, or if the stream mapping mode does not support marshalling
-
define
public abstract void define(StreamBuilder builder) throws BeanIOConfigurationException
Defines a new stream mapping.- Parameters:
builder
- theStreamBuilder
- Throws:
BeanIOConfigurationException
- if the stream builder is not valid- Since:
- 2.1.0
-
loadResource
public void loadResource(java.lang.String resource) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the application's classpath.- Parameters:
resource
- the configuration resource name- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is not found or invalid
-
loadResource
public void loadResource(java.lang.String resource, java.util.Properties properties) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the application's classpath.- Parameters:
resource
- the configuration resource nameproperties
- userProperties
for property substitution- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is not found or invalid
-
load
public void load(java.lang.String filename) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.- Parameters:
filename
- the name of the BeanIO configuration file to load- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is invalid
-
load
public void load(java.lang.String filename, java.util.Properties properties) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.- Parameters:
filename
- the name of the BeanIO configuration file to loadproperties
- userProperties
for property substitution- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is invalid
-
load
public void load(java.io.File file) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.- Parameters:
file
- the BeanIO configuration file to load- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is invalid
-
load
public void load(java.io.File file, java.util.Properties properties) throws BeanIOException, BeanIOConfigurationException
Loads a BeanIO mapping file from the file system, and adds the configured streams to this factory.- Parameters:
file
- the BeanIO configuration file to loadproperties
- userProperties
for property substitution- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while loading the fileBeanIOConfigurationException
- if the mapping file is invalid
-
load
public void load(java.io.InputStream in) throws java.io.IOException, BeanIOConfigurationException
Loads a BeanIO mapping file, and adds the configured streams to this factory.- Parameters:
in
- the input stream to read the mapping file from- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while reading the input streamBeanIOConfigurationException
- if the mapping file is invalidjava.io.IOException
-
load
public abstract void load(java.io.InputStream in, java.util.Properties properties) throws java.io.IOException, BeanIOConfigurationException
Loads a BeanIO mapping file, and adds the configured streams to this factory.- Parameters:
in
- the input stream to read the mapping file fromproperties
- userProperties
for property substitution- Throws:
BeanIOException
- if anIOException
or other fatal error is caught while reading the input streamBeanIOConfigurationException
- if the mapping file is invalidjava.io.IOException
-
newInstance
public static StreamFactory newInstance() throws BeanIOException
Returns a newStreamFactory
instance. The implementation class is resolved using the the BeanIO configuration settingorg.beanio.streamFactory
.- Returns:
- a new
StreamFactory
- Throws:
BeanIOException
- if aStreamFactory
could not be created- See Also:
Settings
-
newInstance
public static StreamFactory newInstance(java.lang.ClassLoader classLoader) throws BeanIOException
Returns a newStreamFactory
instance. An implementation class is loaded using the the BeanIO configuration settingorg.beanio.streamFactory
.- Parameters:
classLoader
- theClassLoader
to use to load the stream factory and all subcomponents. If null, the current thread's context class loader is used. If there is no context class loader for the thread, the class loader that loaded this class is used.- Returns:
- a new
StreamFactory
- Throws:
BeanIOException
- if aStreamFactory
could not be created- Since:
- 2.0
- See Also:
Settings
-
isMapped
public abstract boolean isMapped(java.lang.String streamName)
Test whether a mapping configuration exists for a named stream.- Parameters:
streamName
- the stream name to test for existence- Returns:
true
if a mapping configuration is found for the named stream- Since:
- 1.2
-
init
protected void init()
This method is invoked after a StreamFactory is loaded and all attributes have been set.- Since:
- 2.0
-
getClassLoader
protected java.lang.ClassLoader getClassLoader()
Returns the class loader to use for resolving classpath resources and bean objects declared in a mapping file.- Returns:
- the
ClassLoader
to use - Since:
- 2.0
-
setClassLoader
protected void setClassLoader(java.lang.ClassLoader cl)
Sets the class loader to use for resolving classpath resources and bean objects declared in a mapping files.- Parameters:
cl
- theClassLoader
to use- Since:
- 2.0
-
-