mschachter 01/06/18 15:50:38 Added: resources/src/java/org/apache/commons/resources AbstractResource.java Resource.java Log: - Added Resource interface and AbstractResource class Revision Changes Path 1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/AbstractResource.java Index: AbstractResource.java =================================================================== package org.apache.commons.resources; import java.util.Locale; import java.util.TimeZone; import java.util.MissingResourceException; import java.io.Reader; import java.io.InputStream; import java.io.StringReader; import java.io.Serializable; import java.io.ByteArrayInputStream; import java.net.URL; /** * This class implements some of the methods of the Resource interface * and should be extended to shield developers from changes in the interface. * The only method not implemented is * {@link Resource#getData(String,Locale,TimeZone) getData(String,Locale,TimeZone)}. * Implementators are encouraged to implement more efficient getReader() and * getStream() methods for large-sized content. */ public abstract class AbstractResource implements Resource { /** * The name of this resource */ protected String name = "Abstract Resource"; /** * The configuration URL for this resource */ protected URL config; /** * This is called on to initialize the Resource, before any of the * getData methods are called, and after the setConfigurationURL() method * is called. The default implementation wipes the hard drive of the * unsuspecting developer... */ public void init() { ; } /** * This method is called when the manager of this resource decides that * it's no longer needed. The default implementation does nothing. */ public void destroy() { ; } /** * Get the name of this resource * @return A logical String representation of the name of this resource */ public String getName() { return name; } /** * Set the name of this resource * @param name The name of this resource */ public void setName(String name) { this.name = name; } /** * Retrieves content based on the locale and time zone specified. Note * that this method has the potential to cause memory problems for content * that is relatively large. The default implementation of this returns * the String value of {@link #getData(String,Locale,TimeZone) getData} * with the arguments given in the default system encoding. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument. * @return A String representing the data specified by key. The encoding * of the String is implementation dependent. */ public String getString(String key, Locale locale, TimeZone timeZone) throws MissingResourceException { return new String(getData(key, locale, timeZone)); } /** * Retrieves an InputStream representing the content based on the key, locale, * and time zone specified. The default implementation constructs a * ByteArrayInputStream based on the bytes retrieved from * {@link #getData(String,Locale,TimeZone) getData}. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument all together. */ public InputStream getStream(String key, Locale locale, TimeZone timeZone) throws MissingResourceException { return new ByteArrayInputStream(getData(key, locale, timeZone)); } /** * Retrieves a Reader representing the content based on the key, locale, * and time zone specified. The default implementation returns a StringReader * with the data returned from {@link #getString(String,Locale,TimeZone) getString}. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument all together. */ public Reader getReader(String key, Locale locale, TimeZone timeZone) throws MissingResourceException { return new StringReader(getString(key, locale, timeZone)); } /** * This method is guaranteed to be called before the init() method for this * class. * @param config The URL of the configuration file for this resource */ public void setConfig(URL config) { this.config = config; } } 1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resource.java Index: Resource.java =================================================================== package org.apache.commons.resources; import java.util.Locale; import java.util.TimeZone; import java.util.MissingResourceException; import java.io.Reader; import java.io.InputStream; import java.io.Serializable; import java.net.URL; /** * This class represents an internationalized resource, that is, anything * that can pull specific content out in the context of a certain locale and * optionally time zone. The basic idea is that each peice of content is * associated with a String key, and that key is used to retrieve the content. * Developers using implementations of this class should keep in mind that * they should use the getStream method that returns an InputStream and write * directly to the Stream they're using for resources that contain large size * content.<br /> * Developers implementing resources should extend the * {@link org.apache.commons.resources.AbstractResource AbstractResource} * class and override the necessary methods to shield themselves from any * changes that may occur in this interface. */ public interface Resource extends Serializable { /** * This is called on to initialize the Resource, before any of the * getData* methods are called, and after the setConfigurationURL() method * is called. */ public void init(); /** * This method is called when the manager of this resource decides that * it's no longer needed. */ public void destroy(); /** * Get the name of this resource * @return A logical String representation of the name of this resource */ public String getName(); /** * Retrieves content based on the locale and time zone specified. Note * that this method has the potential to cause memory problems for content * that is relatively large. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument all together. */ public byte[] getData(String key, Locale locale, TimeZone timeZone) throws MissingResourceException; /** * Retrieves content based on the locale and time zone specified. Note * that this method has the potential to cause memory problems for content * that is relatively large. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument. * @return A String representing the data specified by key. The encoding * of the String is implementation dependent. */ public String getString(String key, Locale locale, TimeZone timeZone) throws MissingResourceException; /** * Retrieves an InputStream representing the content based on the key, locale, * and time zone specified * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument all together. */ public InputStream getStream(String key, Locale locale, TimeZone timeZone) throws MissingResourceException; /** * Retrieves a Reader representing the content based on the key, locale, * and time zone specified * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone. Resource implementations may ignore * the time zone argument all together. */ public Reader getReader(String key, Locale locale, TimeZone timeZone) throws MissingResourceException; /** * This method is guaranteed to be called before the init() method for this * class. * @param config The URL of the configuration file for this resource */ public void setConfig(URL config); }