The Resource framework classes and documentation.

Resources Content Index

Introduction
Configuring the Resource Framework
How to Obtain and Use Resources
Resource-specific Documentation
Creating an Implementation of a Resource

Introduction

The Jakarta Commons Resources package provides a framework and a number of implementations to retrieve Locale-specific data from a variety of sources. Current implementations provide mechanisms for retrieving data from a Java .properties file, the file system, or an XML file.

Configuration

The default way of configuration a resource is through an XML configuration file. However by implementing the ConfigurationReader interface and using the appropriate ResourcesManager constructor, you can load the resources using whatever way you wish to implement.

Configuration File

The configuration file is relatively simple. It has the following format:

        <?xml version="1.0" ?>
        <resources-config>
            <resources>
                <resource name="logical name of the resource"
                             factory="java factory class for the resource"
                             config="implementation dependent string representing the configuration file/data">

                    <set-property property="settable Java Bean property of resource"
                                     value="value of property" />
                </resource>
            </resources>
        </resources-config>
        

You can define any number of different types of resources in the configuration file. The following example creates two instances of resources, one that looks in a .properties file for it's information, and one that uses the file system:

        <?xml version="1.0" ?>
        <resources-config>
            <resources>
                <resource name="message"
                             factory="org.apache.commons.resources.message.PropertyMessageResourcesFactory"
                             config="some.properties.file.in.the.classpath">

                    <set-property property="returnNull"
                                     value="true" />
                </resource>

                <!-- In this resource, there is no "config" property necessary, so we leave it blank -->
                <resource name="file"
                             factory="org.apache.commons.resources.file.FileResourcesFactory"
                             config="">

                    <set-property property="baseDir"
                                     value="/usr/local/resources/files" />
                </resource>
            </resources>
        </resources-config>
        

Obtaining and Using Resources

The basic idea is to create an instance of ResourcesManager and use it's methods to retrieve the instance of Resources you need. There are two types of ResourcesManagers, the environment-neutral ResourcesManager and the webapp specific WebappResourcesManager. The purpose of WebappResourcesManager is to supply Resources that happen to be an implementation of WebappResources with a ServletContext reference, to increase their operability in a Servlet environment. Implementations of WebappResources that use the ServletContext object will not be able to run outside of a Servlet environment (unless the documentation for that resource says otherwise).

Here's an example Using ResourcesManager. Let's assume for the example that we can represent our config file in URL format, and that URL is "file:///usr/local/resources/resources-config.xml":

            //One of the constructors for ResourceManager takes a URL to the
            //configuration file as an argument, so we'll use that one in this
            //example.
            URL configURL = new URL("file:///usr/local/resources/resources-config.xml");
            //Now create the ResourcesManager.  From the javadocs, you'll see that
            //the second boolean argument says whether or not to initialize the
            //resources.  It's usually a good idea to pass true
            ResourcesManager manager = new ResourcesManager(configURL, true);

            //obtain the message resource we defined in the configuration file
            Resources messageResources = manager.getResources("message");
            //obtain the file resource defined in the configuration file
            Resources fileResources    = manager.getResources("file");
        

WebappResourcesManager is just as easy. The following is an example of initializing it from within a Servlet. For the example, the configuration file can be found in "example-app-context-path/WEB-INF/resources-config.xml":

            //this is the servlet's overidden init() method defined in javax.servlet.Servlet
            public void init(ServletConfig config) {
                //obtain a reference to the context for readability of this example
                ServletContext context = config.getServletContext();
                //construct the manager
                WebappResourcesManager webappManager = new WebappResourcesManager("/WEB-INF/resource-config.xml",
                                                                                  context);
                //since WebappResourcesManager extends ResourcesManager, we just use the same methods
                //we would use if it was just a ResourcesManager
                //obtain the message resource we defined in the configuration file
                Resources messageResources = manager.getResources("message");
                //obtain the file resource defined in the configuration file
                Resource fileResources    = manager.getResources("file");
            }
         

Resource Specific Documentation

FileResources - Resources implementation that uses the file system to obtain localized content.

PropertyMessageResources - Resources implementation that uses Java Resource Bundles (.properties files) to obtain localized messages.

XMLMessageResources - Resources implementation that uses XML files to obtain localized content.

WebappFileResources - Extension of FileResources that retrieves files from within the context of a web application.

Creating an Implementation of a Resource

The basic idea here is to extend the ResourcesBase class and provide implementations for the get* methods that take a Locale and TimeZone as an argument. If you want your resource to operate in a web application and have acess to a ServletContext, you can extend WebappResourcesBase instead. You must also produce a ResourcesFactory to create your resource. For more information see the API documentation on Resources, WebappResources, and ResourcesFactory.