You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3122de43-9c1c-4565-a01b-0b4dc5e06411"><ac:plain-text-body><![CDATA[

[wiki:ResourcesUserGuide Home]

[wiki:ResourcesUserGuideImplementations Previous]

]]></ac:plain-text-body></ac:structured-macro>


4. Creating a Resources Implementation

You can implement the [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/Resources.html Resources] and [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/ResourcesFactory.html ResourcesFactory] interfaces directly, however, by far the easiest way is to use one of the base implementations provided.

The two base Resources classes are:

  • [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/ResourcesBase.html ResourcesBase] - implements Resources

    • void:init() - (optional) for initialization processing.
    • Iterator:getKeys() - (required) return the set of keys in this Resources
    • Object:getObject(key, Locale, TimeZone) - (required) retrieve the content for a key/Locale/Timezone
  • [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/CollectionResourcesBase.html CollectionResourcesBase] - extends ResourcesBase and provides a mechanism for caching a set of *Map*s containing key/value pairs for a Locale.

    • void:init() - (optional) for initialization processing.
    • Map:getLocaleMap(baseUrl, Locale) (required) returns a Map of key/value pairs for a Locale.

4.1 ResourcesBase

    public class MyResources extends ResourcesBase {

        private String config;

        public MyResources(String name, config) {
            super(name);
            this.config = config;
        }

        public void init() {
            // do initialization here, if required
        }

        public Iterator getKeys() {
            // must implement this method
        }

        public Object getObject(String key, Locale locale, TimeZone timeZone) {
            // must implement this method
        }

    }

4.2 CollectionsResourcesBase

    public class MyResources extends CollectionsResourcesBase {

        public MyResources(String name, config) {
            super(name, config);
        }

        public void init() {
            // do initialization here, if required
        }

        public Object getLocaleMap(String baseUrl, Locale locale) {
            // must implement this method
        }
    }

4.2 ResourcesFactory

To create a factory for your Resources implementation, extends the [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/ResourcesFactoryBase.html ResourcesFactoryBase] ...

    public class MyResourcesFactory extends ResourcesFactoryBase {

        public MyResources() {
            super();
        }

        protected Resources createResources(String name, String config) {
            Resources resources = new MyResources(name);
            Resources res = new PropertyResources(name, config);
            resources.setReturnNull(isReturnNull());
            resources.init();
            return resources;
        }
    }

N.B for implementations to be used in a Web Application there is a [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/WebappResourcesFactoryBase.html WebappResourcesFactoryBase] factory which includes a ServletContext property with appropriate read/write methods.


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e3c40fdd-8a1f-4d7c-9b08-b5db9d86c117"><ac:plain-text-body><![CDATA[

[wiki:ResourcesUserGuide Home]

[wiki:ResourcesUserGuideImplementations Previous]

]]></ac:plain-text-body></ac:structured-macro>

  • No labels