Erwin Müller Blog

Resources Management Project

Natural Resources (Log Processor by David Stanley https://www.flickr.com/photos/davidstanleytravel/6051024483/)

Natural Resources (Log Processor by David Stanley https://www.flickr.com/photos/davidstanleytravel/6051024483/)

The project addresses the need for an application to manage text, image and binary resources. Text resources are usually the names and labels of a user interface, image resources hold logos and icons that are used in the user interface, and other images that are presented to the user. Binary resources are arbitrary resources like music and sounds. Those resources must be available in different contexts and to different parts of the application. Furthermore, different content must be delivered for the same resource identifiers based on the user locale.

Library Position Statement

For Application developers;
The resources management project Is an application programming interface (API) and application library.
That Enabled the management application resources, with the possibility of the same resource identifier returning different resources based on the user locale.

User Description

It is an application programming interface (API) and application library so it targets application developers.

Library Overview

Resources System Context Diagram

Resources System Context Diagram

The resource management library leverages the Java JDK ResourceBundle and in particular the PropertyResourceBundle API to retrieve the resources based on the user locale. It also extends the resource bundle API for caching and to image and binary resources.

The resources project contains those main components:

Features

  • Retrieving resources with a resource identifier;
  • Retrieving binary resources;
  • Retrieving text resources;
  • Retrieving images resources;
  • Retrieving templates resources;
  • Caching of resources;

License GNU Lesser General Public License v3 (LGPL-3)

Copyright 2012-2015 Erwin Müller .

Resources Management Library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with resources-templates. If not, see .

User Manual

All resources (binary, texts, templates and images) follow the same pattern because they all use the same interface and patterns and leverage ResourceBundle of the Java JDK. The resources storage is retrieved using the ResourceBundle#getBundle() method, and stored in the bundle cache. The resource with the specified identifier and locale is then retrieved from the bundle and stored in the resources cache. The resource is then available to the user. If the user requests the same resource, it can be retrieved from the cache.

The default cache is a simple HashMap, but different cache implementations can be added, for example Ehcache.

Binary Resources

Binary resources are generalized binary data resources and can be for example be sound, music or other files.

Resources Binary Class Diagram

Resources Binary Class Diagram

Class Use Case
BinariesFactory Create a new binaries resources bundle
Binaries Retrieve binary resource
BinaryResource Retrieve the binary data from the resource

Binary Resources Use Cases

Binary Resources Use Cases

Use Case: Create a new binaries resources bundle

  • Description

Creates a new binaries resources bundle from a specified resource bundle base name and optional class loader and optional resource bundle control.

  • Preconditions

The resource bundle is created pointing to binary resources;

  • Postconditions

The binaries resources bundle is available;

  • Basic Course of Actions

Under the resources directory, usually src/main/resources should be a resource bundle property file created that have the base name as the file name. That is, if the base name is MyResource, the following properties should be created:

  • MyResource_de_DE.properties
  • MyResource_en_US.properties
  • MyResource_en.properties

The content of those resources bundle properties file should be name := path entries, the name being the identifier of the resource, and the path the path of the resource.

The binaries resources bundle can then be created with the factory method.

Use Case: Retrieve binary resource

  • Description

Retrieves a binary resource from the binaries resources bundle.

  • Preconditions

The resource bundle is created pointing to binary resources;

  • Postconditions

The binary resource is available;

  • Basic Course of Actions

Use the resource getter method to retrieve the binary resource with the specified name and optionally the specified locale.

Use Case: Retrieve the binary data from the resource

  • Description

Retrieves the binary data from the resource.

  • Preconditions

The binary resources was retrieved;

  • Postconditions

The binary data is available;

  • Basic Course of Actions:

Use the getter method of the binary resource to retrieve the binary data.

Use Case: Discard binary data

  • Description

Discards the binary data from the binary resource, freeing the associated memory.

  • Preconditions

The binary resources was retrieved;

  • Postconditions

The binary data is discarded;

  • Basic Course of Actions

Use the discard method to discard the binary data.

Binary Resources Examples

Full example using the Guice injector method:

Text Resources

Text resources are any kind of text resources and can be for example single words, line or whole block of text.

Resources Texts Class Diagram

Resources Texts Class Diagram

Class Use Case
TextsFactory Create a new texts resources bundle
Texts Retrieve text resource
TextResource Retrieve the text data from the resource

Text Resources Use Cases

Text Resources Use Cases

Use Case: Create a new texts resources bundle

  • Description

Creates a new texts resources bundle from a specified resource bundle base name and optional class loader and optional resource bundle control.

  • Preconditions

The resource bundle is created pointing to text resources;

  • Postconditions

The texts resources bundle is available;

  • Basic Course of Actions

Under the resources directory, usually src/main/resources should be a resource bundle property file created that have the base name as the file name. That is, if the base name is MyResource, the following properties should be created:

  • MyResource_de_DE.properties
  • MyResource_en_US.properties
  • MyResource_en.properties

The content of those resources bundle properties file should be name := path entries, the name being the identifier of the resource, and the path the path of the resource.

The texts resources bundle can then be created with the factory method.

Use Case: Retrieve text resource

  • Description

Retrieves a text resource from the texts resources bundle.

  • Preconditions

The resource bundle is created pointing to text resources;

  • Postconditions

The text resource is available;

  • Basic Course of Actions

Use the resource getter method to retrieve the text resource with the specified name and optionally the specified locale.

Use Case: Retrieve the text from the resource

  • Description

Retrieves the text from the resource.

  • Preconditions

The text resources was retrieved;

  • Postconditions

The text is available;

  • Basic Course of Actions:

Use the getter method of the text resource to retrieve the text.

Text Resources Examples

Full example using the Guice injector method:

Central Text Resources

Since Java Swing action names, mnemonics and accelerator keys are also text resources, the library offers an interface to retrieve those properties from text resources.

Resources Texts Central Class Diagram

Resources Texts Central Class Diagram

Class Use Case
TextsResources Set resources bundles
TextsResources Retrieve the text from the resource
Retrieve action name
Retrieve label mnemonic
Retrieve accelerator key

Text Resources Central Use Cases

Text Resources Central Use Cases

Use Case: Set resources bundles

  • Description

Sets the resources bundles for texts resources, action names texts resources, mnemonic texts resources and accelerator key texts resources.

  • Preconditions

The resource bundles are created pointing to the text resources;

  • Postconditions

The texts are available;

  • Basic Course of Actions

Under the resources directory, usually src/main/resources, should be the resource bundle property files created for the texts resources, the action names texts resources, the mnemonic texts resources and the accelerator key texts resources. The default resource bundle names are Texts, Actions, ActionMnemonics and ActionAccelerators, but the resource bundle names could have the same name.

  • Texts_de_DE.properties
  • Texts_en_US.properties
  • Texts_en.properties
  • Actions_de_DE.properties
  • Actions_en_US.properties
  • Actions_en.properties
  • ActionMnemonics_de_DE.properties
  • ActionMnemonics_en_US.properties
  • ActionMnemonics_en.properties
  • ActionAccelerators_de_DE.properties
  • ActionAccelerators_en_US.properties
  • ActionAccelerators_en.properties

The content of those resources bundle properties file should be name := path entries, the name being the identifier of the resource, and the path the path of the resource.

The texts can then be created with the factory method with the specified properties, or just injected in the object.

Central Text Resources Examples

Full example using the Guice injector method:

Image Resources

Image resources are any kind of image resources and can be for example an icon or an image.

Resources Images Class Diagram

Resources Images Class Diagram

Class Use Case
ImagesFactory Create a new images resources bundle
Images Retrieve image resource
ImageScalingWorker Resize image
ImageResource Retrieve the image data from the resource

Image Resources Use Cases

Image Resources Use Cases

Use Case: Create a new images resources bundle

  • Description

Creates a new images resources bundle from a specified resource bundle base name and optional class loader and optional resource bundle control.

  • Preconditions

The resource bundle is created pointing to image resources;

  • Postconditions

The images resources bundle is available;

  • Basic Course of Actions

Under the resources directory, usually src/main/resources should be a resource bundle property file created that have the base name as the file name. That is, if the base name is MyImages, the following property file should be created:

  • MyImages.properties

The content of the resources bundle property file should be name := path entries, the name being the identifier of the resource, and the path the path of the resource. Images can be stored in multiple resolutions, compatible to mipmaps, that is, pre-calculated, optimized sequences of images. If the image is available in the pre-calculated resolution, the image is returned. Otherwise, the image is resized from the image that is available in the closest resolution. The resolutions are divided into the following sets:

  • xhdp, for extra high resolution,
  • hdp, for high resolution,
  • mdp, for medium resolution,
  • ldp, for low resolution.

The path inside the property file can contain the placeholder %s that is replaced with the resolution set name.

The images resources bundle can then be created with the factory method.

Use Case: Retrieve image resource

  • Description

Retrieves a image resource from the images resources bundle.

  • Preconditions

The resource bundle is created pointing to image resources;

  • Postconditions

The image resource is available;

  • Basic Course of Actions

Use the resource getter method to retrieve the image resource with the specified name and optionally the specified locale.

Use Case: Retrieve the image from the resource

  • Description

Retrieves the image from the resource.

  • Preconditions

The image resources was retrieved;

  • Postconditions

The image is available;

  • Basic Course of Actions:

Use the getter method of the image resource to retrieve the image.

Image Resources Examples

Full example using the Guice injector method:

Template Resources

Template resources are processed with a given data. The processed template is cached and the same text is returned for the same data. Different template engines can be used to parse the template resources.

Resources Templates Class Diagram

Resources Templates Class Diagram

Class Use Case
TemplatesFactory Create a new templates resources bundle
Templates Retrieve template resource
TemplateResource Retrieve the template data from the resource
TemplateResource Invalidate template text

Template Resources Use Cases

Template Resources Use Cases

Use Case: Create a new templates resources bundle

  • Description

Creates a new templates resources bundle from a specified resource bundle base name and optional class loader, optional templates attributes and optional resource bundle control.

  • Preconditions

The resource bundle is created pointing to template resources;

  • Postconditions

The templates resources bundle is available;

  • Basic Course of Actions

Under the resources directory, usually src/main/resources should be a resource bundle property file created that have the base name as the file name. That is, if the base name is MyResource, the following properties should be created:

  • MyResource_de_DE.properties
  • MyResource_en_US.properties
  • MyResource_en.properties

The content of those resources bundle properties file should be name := path entries, the name being the identifier of the resource, and the path the path of the resource.

The templates resources bundle can then be created with the factory method.

Use Case: Retrieve template resource

  • Description

Retrieves a template resource from the templates resources bundle.

  • Preconditions

The resource bundle is created pointing to template resources;

  • Postconditions

The template resource is available;

  • Basic Course of Actions

Use the resource getter method to retrieve the template resource with the specified name and optionally the specified locale.

Use Case: Retrieve the template text from the resource

  • Description

Retrieves the template text from the resource.

  • Preconditions

The template resource is available;

  • Postconditions

The template is available;

  • Basic Course of Actions:

Use the getter method of the template resource to retrieve the text.

Use Case: Invalidate template text

  • Description

Invalidates the cached text in the template resource.

  • Preconditions

The template resource is available;

  • Postconditions

The template text is invalidated and need to be reparsed by tha template engine;

  • Basic Course of Actions:

Use the invalidate method of the template resource to invalidate the cached text.

Template Resources Examples

Full example using the Guice injector method:

Leave a Reply