⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcemanager.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class responsible for managing access to all resources within the
 * application.  This class allows acces to all of the application's resource
 * bundles, and provides a way to access values stored in those resources.
 * Objects using this class need only know the name of the resource and the
 * key (name) of the property that they wish to access - this class handles
 * the retrieval of the resources, and caches resources for quicker access.
 * Any resource accessible via
 * <code>java.util.ResourceBundle.getBundle</code> can be accessed using this
 * manager class.
 *
 * <p/>
 *
 * This class is a singleton, with one instance (accessible via the <code>
 * getInstance</code> method) responsible for handling all requests.
 *
 * <p/>
 *
 * For convenience, the names of the application's basic resource bundles are
 * provided within this class as constants.  These are not the only resources
 * that may be access using this class, however.  Any resource bundle available
 * at runtime can be acccessed via the <code>getString</code> methods in this
 * class.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:49:04 $
 * @since iRunningLog 1.0
 */
public final class ResourceManager {

    /** <code>Log</code> instance for this class. */
    private static final Log LOG = LogFactory.getLog(ResourceManager.class);

    /** Name of the default resource bundle. */
    public static final String BUNDLE_DEFAULT = "ApplicationResources";
    /** Name of the resource bundle containing service mappings. */
    public static final String BUNDLE_OBJECT_FACTORY = "ObjectFactory";

    /** Internal cache of <code>ResourceBundles</code>s. */
    private Map mResourceCache;

    /** Singleton instance. */
    private static final ResourceManager INSTANCE;

    static {
        INSTANCE = new ResourceManager();
    }

    /** Singleton class - does not expose a constructor. */
    private ResourceManager() {
        super();
        mResourceCache = Collections.synchronizedMap(new HashMap());
    }

    /**
     * Retrive the singleton <code>ResourceManager</code> instance.
     *
     * @return The singleton <code>ResourceManager</code> instance
     */
    public static ResourceManager getInstance() {
        return INSTANCE;
    }

    /**
     * Retrieve a value from the application's default resource bundle.  This
     * will return the property specified for the given key.
     *
     * @param key The name of the property to be returned
     * @return The value stored in the default resource bundle under the given
     *         key
     */
    public String getString(String key) {
        return getString(BUNDLE_DEFAULT, key);
    }

    /**
     * Retrieve a value from a resource bundle.  This method allows objects to
     * access a value stored in any resource that is accessible as a
     * <code>ResourceBundle</code>.
     *
     * @param resourceName The name of the resource containing the value to
     *                     be retrieved
     * @param key The name of the property to be returned
     * @return The value stored in the specified resource bundle for the
     *         given key
     */
    public String getString(String resourceName, String key) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getString: Retrieving String '" + key + "' from"
                      + " Resource '" + resourceName + "'");
        }

        if (resourceName == null || key  == null) {
            throw new NullPointerException("getString: The resource name and/or"
                                           + " key provided is null");
        }

        ResourceBundle bundle =
            (ResourceBundle) mResourceCache.get(resourceName);

        if (bundle == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("getString: Resource '" + resourceName + "' not"
                          + " found in cache, resource is being loaded");
            }

            // ResourceBundle will throw an exception for invalid resource name
            bundle = ResourceBundle.getBundle(resourceName);
            mResourceCache.put(resourceName, bundle);

            if (LOG.isDebugEnabled()) {
                LOG.debug("getString: Resource '" + resourceName
                          + "' now in cache");
            }
        }

        // ResourceBundle will throw an exception for an invalid key
        String value = bundle.getString(key);

        if (LOG.isDebugEnabled()) {
            LOG.debug("getString: Value for key '" + key + "' is '" + value
                      + "'");
        }

        return value;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -