Resource Management


API

YASL provides a resource manager for handling I18N strings and images. The interface YASLResourceManager (from org.yasl.arch.resources) defines the API for the resource manager. YASLResourceManagerImpl (from org.yasl.arch.impl.resources) is a default implementation of the YASLResourceManager interface. Developers may want to extend YASLResourceManagerImpl to provide additional convenience methods, but it is unlikely that applications will require a new implementation of YASLResourceManager.

The API methods from YASLResourceManager are listed below:


Locale and Bundle

/**
 * Returns the current Locale being used by this 
 * resource manager.
 *
 * @return Locale
 */
public Locale getLocale();

/**
 * Sets the Locale to be used by this resource manager.
 *
 * @param locale Locale
 */
public void setLocale(Locale locale);

/**
 * Returns the default bundle for the application's resource manager.
 *
 * @return String
 */
public String getDefaultBundle();


Accessing Images

/**
 * Loads an image as an ImageIcon. Pass in image name with fully
 * qualified package name. For example,
 * /org/myapp/images/someicon.gif
 * 
 * Note that this method assumes the ClassLoader to be used for
 * loading the image is the same as the ClassLoader for the
 * implementation class. If you are loading an image from a jar
 * file that was loaded at runtime, you will need to use
 * the signature that takes a ClassLoader argument.
 * 
 * @param iconName String
 * @return ImageIcon
 */
public ImageIcon loadImageIcon(String iconName);

/**
 * Loads an image as an ImageIcon. Pass in image name with fully
 * qualified package name. For example,
 * /org/myapp/images/someicon.gif
 * 
 * This method also takes a ClassLoader. If you are trying to 
 * load images from a jar that was not on the initial classpath,
 * you must pass in the classloader for the jar file that was
 * loaded at runtime.
 * 
 * @param iconName String
 * @param classLoader ClassLoader
 * @return ImageIcon
 */
public ImageIcon loadImageIcon(String iconName, ClassLoader classLoader);

/**
 * Loads an image as an ImageIcon after getting the fully
 * qualified package name from a resource bundle.
 *
 * @param bundle String
 * @param key String
 * @return ImageIcon
 */
public ImageIcon loadImageIcon(String bundle, String key);

/**
 * Loads an image as an ImageIcon after getting the fully
 * qualified package name from a resource bundle based on the
 * passed Locale.
 *
 * @param bundle String
 * @param key String
 * @param locale Locale
 * @return ImageIcon
 */
public ImageIcon loadImageIcon(String bundle, String key, Locale locale);


Accessing Strings

/**
 * Loads a String associated with the passed key from the named
 * resource bundle.
 *
 * @param bundle String
 * @param key String
 * @return String
 */
public String getString(String bundle, String key);

/**
 * Loads a String associated with the passed key from the named
 * resource bundle based on the passed Locale.
 *
 * @param bundle String
 * @param key String
 * @param locale Locale
 * @return String
 */
public String getString(String bundle, String key, Locale locale);

/**
 * Loads a String associated with the passed key from the named
 * resource bundle based on the passed Locale.
 * 
 * This method also takes a ClassLoader. If you are trying to 
 * load strings from a properties file in a jar that was not
 * on the initial classpath, you must pass in the classloader 
 * for the jar file.
 * 
 * @param bundle String
 * @param key String
 * @param locale Locale
 * @param classLoader ClassLoader
 * @return String
 */
public String getString(String bundle, String key, Locale locale, 
                        ClassLoader classLoader);    


Accessing the Resource Manager

The preferred method for accessing the resource manaager is the accessor method from the YASLApplication interface.

/**
 * Returns the resource manager using the standard resource
 * manager key. If the resource manager has not been set when
 * this method is called, a default resource manager will
 * be set.
 *
 * @return YASLResourceManager
 * @throws YASLApplicationException
 */
public YASLResourceManager getResourceManager()
        throws YASLApplicationException;

The resource manager is stored in the singletons map using YASLApplication.KEY_RESOURCE_MANAGER as the key. If an application extends YASLResourceManagerImpl or implments its own version of YASLResourceManager, it must store the resource manager in the singletons map using the key above. This can be done during XML configuration--the preferred method--or via the setResourceManager method from the YASLApplication interface.

/**
 * Sets the resource manager using the standard resource
 * manager key.
 *
 * @param rmanager YASLResourceManager
 * @throws YASLApplicationException
 */
public void setResourceManager(YASLResourceManager rmanager)
        throws YASLApplicationException;


Setting the Default Bundle

The resource manager API has a method for accessing a default resource bundle. The value of the default resource bundle is set in the constructor for the YASLResourceManagerImpl class. If the value is not explicitly set in YASLResourceManagerImpl's constructor, getDefaultBundle() will return null.

The sample below is drawn from yaslHelloWorldConfig.xml.

<yaslConfig>
  <property key="bundleName" 
               value="org.yasl.helloworld.resources.helloWorldGuiStrings"/>
  ...
 
  <singletonsConfig>
    <!-- resource manager -->
    <singleton class="org.yasl.arch.impl.resources.YASLResourceManagerImpl" 
                  key="yasl_resource_manager">
      <argPrimitive type="String" value="$bundleName"/>
    </singleton>
  ...