org.apache.commons.resources.impl
Class ResourcesBase

java.lang.Object
  extended by org.apache.commons.resources.impl.ResourcesBase
All Implemented Interfaces:
Serializable, Resources
Direct Known Subclasses:
CollectionResourcesBase, ResourceBundleResources

public abstract class ResourcesBase
extends Object
implements Resources

Convenience base class for Resources implementations.

Default implementations of the content retrieval methods are provided for all methods except getObject(). The default methods for the other content retrieval methods are coded in terms of getString(), on the assumption that most uses of Resources are of this type. However, they can be easily overridden as needed.

See Also:
CollectionResourcesBase, JDBCResources, PropertyResources, ResourceBundleResources, WebappPropertyResources, WebappXMLResources, XMLResources, Serialized Form

Constructor Summary
ResourcesBase()
          Create a new Resources instance with no name.
ResourcesBase(String name)
          Create a new Resources instance with the specified logical name.
 
Method Summary
 void destroy()
          This method must be called when the manager of this resource decides that it's no longer needed.
 int getBufferSize()
          Return the size of the buffer to use when converting InputStream or Reader objects.
 byte[] getBytes(String key, Locale locale)
          Return the content for the specified key as a byte array, localized based on the specified locale.
 InputStream getInputStream(String key, Locale locale)
          Return the content for the specified key as an InputStream, localized based on the specified locale.
abstract  Iterator getKeys()
          Return an Iterator over the defined keys in this Resources instance.
 String getName()
          Return the logical name of this Resources instance.
abstract  Object getObject(String key, Locale locale)
          Return the content for the specified key as an Object, localized based on the specified locale.
 Reader getReader(String key, Locale locale)
          Return the content for the specified key as a Reader, localized based on the specified locale.
 String getString(String key, Locale locale)
          Return the content for the specified key as a String, localized based on the specified locale.
 void init()
          This must be called to initialize the data content of this Resources instance, before any of the getXxx() methods are called.
 boolean isReturnNull()
          Return true if resource getter methods will return null instead of throwing an exception on invalid key values.
 void setBufferSize(int bufferSize)
          Set the size of the buffer to use when converting InputStream or Reader objects.
 void setReturnNull(boolean returnNull)
          Set a flag determining whether resource getter methods should return null instead of throwing an exception on invalid key values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourcesBase

public ResourcesBase()

Create a new Resources instance with no name.


ResourcesBase

public ResourcesBase(String name)

Create a new Resources instance with the specified logical name.

Parameters:
name - Logical name of the new instance
Method Detail

init

public void init()

This must be called to initialize the data content of this Resources instance, before any of the getXxx() methods are called.

The default implementation does nothing.

Specified by:
init in interface Resources
Throws:
ResourcesException - if an error occurs during initialization

destroy

public void destroy()

This method must be called when the manager of this resource decides that it's no longer needed. After this method is called, no further calls to any of the getXxx() methods are allowed.

The default implementation does nothing.

Specified by:
destroy in interface Resources
Throws:
ResourcesException - if an error occurs during finalization

getKeys

public abstract Iterator getKeys()

Return an Iterator over the defined keys in this Resources instance.

Specified by:
getKeys in interface Resources
Returns:
The keys contained in this resources instance.

getName

public String getName()

Return the logical name of this Resources instance.

Specified by:
getName in interface Resources
Returns:
The name of this resources instance.

isReturnNull

public boolean isReturnNull()

Return true if resource getter methods will return null instead of throwing an exception on invalid key values.

Specified by:
isReturnNull in interface Resources
Returns:
'true' if null is returned for invalid key values.

setReturnNull

public void setReturnNull(boolean returnNull)

Set a flag determining whether resource getter methods should return null instead of throwing an exception on invalid key values.

Specified by:
setReturnNull in interface Resources
Parameters:
returnNull - The new flag value

getBufferSize

public int getBufferSize()

Return the size of the buffer to use when converting InputStream or Reader objects.

Returns:
The buffer size.

setBufferSize

public void setBufferSize(int bufferSize)

Set the size of the buffer to use when converting InputStream or Reader objects.

Parameters:
bufferSize - The buffer size.

getBytes

public byte[] getBytes(String key,
                       Locale locale)

Return the content for the specified key as a byte array, localized based on the specified locale.

The default implementation calls getString() and converts the value to a byte array.

Specified by:
getBytes in interface Resources
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
content for a specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false

getInputStream

public InputStream getInputStream(String key,
                                  Locale locale)

Return the content for the specified key as an InputStream, localized based on the specified locale.

The default implementation calls getsBytes() and returns an input stream over the resulting byte array.

Specified by:
getInputStream in interface Resources
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
content for a specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false

getObject

public abstract Object getObject(String key,
                                 Locale locale)

Return the content for the specified key as an Object, localized based on the specified locale.

There is no default implementation of this method. Concrete subclasses must provide such an implementation.

Specified by:
getObject in interface Resources
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
content for a specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false

getReader

public Reader getReader(String key,
                        Locale locale)

Return the content for the specified key as a Reader, localized based on the specified locale.

The default implementation calls getString() and returns a reader over the resulting characters.

Specified by:
getReader in interface Resources
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
content for a specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false

getString

public String getString(String key,
                        Locale locale)

Return the content for the specified key as a String, localized based on the specified locale.

The default implementation calls getObject() and converts the result to a String if necessary.

Specified by:
getString in interface Resources
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
content for a specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.