commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base ResourceBundleResources.java ResourceBundleResourcesFactory.java ResourcesBase.java ResourcesFactoryBase.java
Date Sat, 28 Dec 2002 00:51:17 GMT
craigmcc    2002/12/27 16:51:17

  Added:       resources/src/java/org/apache/commons/resources/base
                        ResourceBundleResources.java
                        ResourceBundleResourcesFactory.java
                        ResourcesBase.java ResourcesFactoryBase.java
  Log:
  Initial implementation of a Resources/ResourcesFactory pair that wraps
  java.util.ResourceBundle families that share the same base name.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourceBundleResources.java
  
  Index: ResourceBundleResources.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourceBundleResources.java,v
1.1 2002/12/28 00:51:17 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/28 00:51:17 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.impl;
  
  
  import java.io.ByteArrayInputStream;
  import java.io.InputStream;
  import java.io.Reader;
  import java.io.StringReader;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Locale;
  import java.util.Map;
  import java.util.MissingResourceException;
  import java.util.ResourceBundle;
  import java.util.TimeZone;
  import org.apache.commons.resources.Resources;
  import org.apache.commons.resources.ResourcesException;
  
  
  /**
   * <p>Concrete implementation of {@link Resources} that wraps a set
   * (one per Locale) of <code>java.util.ResourceBundle</code> instances
   * that share a common base name.  The <code>timeZone</code> argument
   * is ignored in all resource getter method implementations.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/28 00:51:17 $
   */
  
  public class ResourceBundleResources extends ResourcesBase {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * <p>Create a new {@link Resources} instance with the specified
       * logical name and bundle base name.</p>
       *
       * @param name Logical name of the new instance
       * @param base Fully qualified base name of the <code>ResourceBundle</code>
       *  instances to be wrapped
       */
      public ResourceBundleResources(String name, String base) {
  
          this.name = name;
          this.base = base;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * <p>The fully qualified base name of the <code>ResourceBundle</code>
       * instances to be wrapped.</p>
       */
      protected String base = null;
  
  
      /**
       * <p>Two-level cache of <code>ResourceBundle</code> instances
       * that have previously been acquired by this {@link Resources}
       * instance.  The first-level key is the <code>ClassLoader</code>
       * instance used to load the bundles.  The first-level value is a
       * <code>java.util.Map</code> that is then, in turn, keyed by the
       * <code>java.util.Locale</code> to which a particular bundle belongs.</p>
       */
      protected Map bundles = new HashMap();
  
  
      /**
       * <p>The logical name of this {@link Resources} instance.</p>
       */
      protected String name = null;
  
  
      // ------------------------------------------------------ Lifecycle Methods
  
  
      /**
       * <p>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 <code>getXxx()</code> methods are
       * allowed.</p>
       *
       * @exception ResourcesException if an error occurs during finalization
       */
      public void destroy() throws ResourcesException {
  
          synchronized (bundles) {
              Iterator loaders = bundles.keySet().iterator();
              while (loaders.hasNext()) {
                  ClassLoader loader = (ClassLoader) loaders.next();
                  ((Map) bundles.get(loader)).clear();
              }
              bundles.clear();
          }
  
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * <p>Return the fully qualified base name of the
       * <code>ResourceBundle</code> instances we are wrapping.</p>
       */
      public String getBase() {
  
          return (this.base);
  
      }
  
  
      /**
       * <p>Return the logical name of this {@link Resources} instance.</p>
       */
      public String getName() {
  
          return (this.name);
  
      }
  
  
      // ---------------------------------------------- Content Retrieval Methods
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * byte array, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public byte[] getBytes(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
  
          throw new UnsupportedOperationException(); // FIXME
  
      }
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as an
       * InputStream, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public InputStream getInputStream(String key, Locale locale,
                                        TimeZone timeZone)
          throws ResourcesException {
  
          throw new UnsupportedOperationException();  // FIXME
  
      }
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as an
       * Object, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public Object getObject(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
  
          try {
              ResourceBundle bundle = getBundle(locale, timeZone);
              return (bundle.getObject(key));
          } catch (MissingResourceException e) {
              throw new ResourcesException(e);
          }
  
      }
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * Reader, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public Reader getReader(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
  
          throw new UnsupportedOperationException(); // FIXME
  
      }
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * String, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public String getString(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
  
          try {
              ResourceBundle bundle = getBundle(locale, timeZone);
              return (bundle.getString(key));
          } catch (ClassCastException e) {
              throw new ResourcesException(e);
          } catch (MissingResourceException e) {
              throw new ResourcesException(e);
          }
  
      }
  
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * <p>Return the appropriate <code>ResourceBundle</code> instance
       * that corresponds to the specified <code>locale</code> and
       * <code>timeZone</code> parameters.  The first time a particular
       * bundle is requested, cache it so that subsequent requests will
       * operate more quickly.</p>
       *
       * <p>The default implementation of this method unconditionally ignores the
       * <code>timeZone</code> argument.</p>
       *
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception MissingResourceException if the requested Resourcebundle
       *  cannot be acquired
       */
      protected ResourceBundle getBundle(Locale locale, TimeZone timeZone)
          throws MissingResourceException {
  
          if (locale == null) {
              locale = Locale.getDefault();
          }
  
          synchronized (bundles) {
  
              // Locate or create the Map for the appropriate ClassLoader
              ClassLoader loader =
                  Thread.currentThread().getContextClassLoader();
              if (loader == null) {
                  loader = this.getClass().getClassLoader();
              }
              Map loaderMap = (Map) bundles.get(loader);
              if (loaderMap == null) {
                  loaderMap = new HashMap();
                  bundles.put(loader, loaderMap);
              }
  
              // Locate or create the requested ResourceBundle instance
              ResourceBundle bundle =
                  (ResourceBundle) loaderMap.get(locale);
              if (bundle == null) {
                  bundle = ResourceBundle.getBundle(base, locale, loader);
                  loaderMap.put(locale, bundle);
              }
              return (bundle);
  
          }
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourceBundleResourcesFactory.java
  
  Index: ResourceBundleResourcesFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourceBundleResourcesFactory.java,v
1.1 2002/12/28 00:51:17 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/28 00:51:17 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.impl;
  
  
  import java.util.HashMap;
  import java.util.Map;
  import org.apache.commons.resources.Resources;
  import org.apache.commons.resources.ResourcesException;
  import org.apache.commons.resources.ResourcesFactory;
  
  
  /**
   * <p>Concrete implementation of {@link ResourcesFactory} that creates
   * {@link Resources} instances that wrap a set (one per Locale) of
   * <code>java.util.ResourceBundle</code> instances that share a common
   * base name.  The configuration String that is passed to the
   * <code>getResources()</code> method must contain the fully qualified
   * Java name of the underlying <code>ResourceBundle</code> family
   * that is to be wrapped.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/28 00:51:17 $
   */
  
  public class ResourceBundleResourcesFactory extends ResourcesFactoryBase {
      
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * <p>The set of {@link Resources} instances previously created by
       * this {@link ResourcesFactory}, keyed by logical name.</p>
       */
      protected Map resources = new HashMap();
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * <p>Create (if necessary) and return a {@link Resources} instance
       * for the specified logical name, with a default configuration.</p>
       * For convenience, the logical name is also assumed to by the
       * configuration String.</p>
       *
       * @param name Logical name of the {@link Resources} instance to
       *  be returned
       *
       * @exception ResourcesException if a {@link Resources} instance
       *  of the specified logical name cannot be returned.
       */
      public Resources getResources(String name)
          throws ResourcesException {
  
          return (getResources(name, name));
  
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * <p>Create and return a new {@link Resources} instance with the
       * specified logical name, after calling its <code>init()</code>
       * method.</p>
       *
       * @param name Logical name of the {@link Resources} instance to create
       * @param config Configuration string for this resource (if any)
       *
       * @exception ResourcesException if a {@link Resources} instance
       *  of the specified logical name cannot be created.
       */
      protected Resources createResources(String name, String config)
          throws ResourcesException {
  
          Resources resources = new ResourceBundleResources(name, config);
          resources.init();
          return (resources);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourcesBase.java
  
  Index: ResourcesBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourcesBase.java,v
1.1 2002/12/28 00:51:17 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/28 00:51:17 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.impl;
  
  
  import java.io.ByteArrayInputStream;
  import java.io.InputStream;
  import java.io.Reader;
  import java.io.StringReader;
  import java.util.Locale;
  import java.util.TimeZone;
  import org.apache.commons.resources.Resources;
  import org.apache.commons.resources.ResourcesException;
  
  
  /**
   * <p>Convenience base class for {@link Resources} implementations.</p>
   *
   * @author Mike Schachter (mschachter@hp.com)
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/28 00:51:17 $
   */
  
  public abstract class ResourcesBase implements Resources {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * <p>Create a new {@link Resources} instance with no name.</p>
       */
      public ResourcesBase() {
  
          this(null);
  
      }
  
  
      /**
       * <p>Create a new {@link Resources} instance with the specified
       * logical name.</p>
       *
       * @param name Logical name of the new instance
       */
      public ResourcesBase(String name) {
  
          this.name = name;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * <p>The logical name of this {@link Resources} instance.</p>
       */
      protected String name = null;
  
  
      // ------------------------------------------------------ Lifecycle Methods
  
  
      /**
       * <p>This must be called to initialize the data content of this
       * {@link Resources} instance, before any of the <code>getXxx()</code>
       * methods are called.</p>
       *
       * @exception ResourcesException if an error occurs during initialization
       */
      public void init() throws ResourcesException {
  
          ; // The default implementation does nothing
  
      }
  
  
      /**
       * <p>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 <code>getXxx()</code> methods are
       * allowed.</p>
       *
       * @exception ResourcesException if an error occurs during finalization
       */
      public void destroy() throws ResourcesException {
  
          ; // The default implementation does nothing
  
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * <p>Return the logical name of this {@link Resources} instance.</p>
       */
      public String getName() {
  
          return (this.name);
  
      }
  
  
      // ---------------------------------------------- Content Retrieval Methods
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * byte array, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public abstract byte[] getBytes(String key, Locale locale,
                                      TimeZone timeZone)
          throws ResourcesException;
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as an
       * InputStream, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public abstract InputStream getInputStream(String key, Locale locale,
                                                 TimeZone timeZone)
          throws ResourcesException;
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as an
       * Object, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public abstract Object getObject(String key, Locale locale,
                                       TimeZone timeZone)
          throws ResourcesException;
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * Reader, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public abstract Reader getReader(String key, Locale locale,
                                       TimeZone timeZone)
          throws ResourcesException;
  
  
      /**
       * <p>Return the content for the specified <code>key</code> as a
       * String, localized based on the specified <code>locale</code>
       * and/or <code>timeZone</code>.</p>
       *
       * @param key Identifier for the requested content
       * @param locale Locale with which to localize retrieval,
       *  or <code>null</code> for the default Locale
       * @param timeZone TimeZone with which to localize retrieval,
       *  or <code>null</code> for the default TimeZone
       *
       * @exception ResourcesException if an error occurs retrieving or
       *  returning the requested content
       */
      public abstract String getString(String key, Locale locale,
                                       TimeZone timeZone)
          throws ResourcesException;
  
  
  
  
  
  
      /**
       * 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 #getBytes(String,Locale,TimeZone) getBytes}
       * 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.  Resources 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 ResourcesException    {
              return new String(getBytes(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 #getBytes(String,Locale,TimeZone) getBytes}.
       * @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.  Resources implementations may ignore
       *                 the time zone argument all together.
       */
      /*
      public InputStream getInputStream(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
              return new ByteArrayInputStream(getBytes(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.  Resources implementations may ignore
       *                 the time zone argument all together.
       */
      /*
      public Reader getReader(String key, Locale locale, TimeZone timeZone)
          throws ResourcesException {
              return new StringReader(getString(key, locale, timeZone));
      }
      */
  
  }
  
  
  
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourcesFactoryBase.java
  
  Index: ResourcesFactoryBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/base/ResourcesFactoryBase.java,v
1.1 2002/12/28 00:51:17 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/28 00:51:17 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.impl;
  
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import org.apache.commons.resources.Resources;
  import org.apache.commons.resources.ResourcesException;
  import org.apache.commons.resources.ResourcesFactory;
  
  
  /**
   * <p>Convenience base class for {@link ResourcesFactory} implementations.
   * This implementation caches the {@link Resources} instances returned by
   * a protected <code>createResources()</code> method, which must be implemented
   * by concrete subclasses.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/28 00:51:17 $
   */
  
  public abstract class ResourcesFactoryBase implements ResourcesFactory {
      
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * <p>The set of {@link Resources} instances previously created by
       * this {@link ResourcesFactory}, keyed by logical name.</p>
       */
      protected Map resources = new HashMap();
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * <p>Create (if necessary) and return a {@link Resources} instance
       * for the specified logical name, with a default configuration.</p>
       *
       * @param name Logical name of the {@link Resources} instance to
       *  be returned
       *
       * @exception ResourcesException if a {@link Resources} instance
       *  of the specified logical name cannot be returned.
       */
      public Resources getResources(String name)
          throws ResourcesException {
  
          return (getResources(name, null));
  
      }
  
  
      /**
       * <p>Create (if necessary) and return a {@link Resources} instance
       * for the specified logical name, with a configuration based on
       * the specified configuration String.</p>
       *
       * @param name Logical name of the {@link Resources} instance to
       *  be returned
       * @param config Configuration string for this resource (meaning
       *  is dependent upon the {@link ResourcesFactory} implementation
       *  being utilized), or <code>null</code> for the default
       *  configuration
       *
       * @exception ResourcesException if a {@link Resources} instance
       *  of the specified logical name cannot be returned.
       */
      public Resources getResources(String name, String config)
          throws ResourcesException {
  
          synchronized (resources) {
              Resources instance = (Resources) resources.get(name);
              if (instance == null) {
                  instance = createResources(name, config);
                  resources.put(name, instance);
              }
              return (instance);
          }
  
      }
  
  
      /**
       * <p>Release any internal references to {@link Resources} instances
       * that have been returned previously, after calling the
       * <code>destroy()</code> method on each such instance.</p>
       *
       * @exception ResourcesException if a problem occurred while releasing
       */
      public void release() throws ResourcesException {
  
          synchronized (resources) {
              Iterator names = resources.keySet().iterator();
              while (names.hasNext()) {
                  String name = (String) names.next();
                  ((Resources) resources.get(name)).destroy();
              }
              resources.clear();
          }
  
      }
  
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * <p>Create and return a new {@link Resources} instance with the
       * specified logical name, after calling its <code>init()</code>
       * method.  Concrete subclasses <strong>MUST</strong>
       * implement this method.</p>
       *
       * @param name Logical name of the {@link Resources} instance to create
       * @param config Configuration string for this resource (if any)
       *
       * @exception ResourcesException if a {@link Resources} instance
       *  of the specified logical name cannot be created.
       */
      protected abstract Resources createResources(String name, String config)
          throws ResourcesException;
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message