SRJRCFrames
v0.1.296

de.schwarzrot.app.service
Class XtensibleResourceBundleMessageSource

java.lang.Object
  extended by org.springframework.context.support.AbstractMessageSource
      extended by de.schwarzrot.app.service.XtensibleResourceBundleMessageSource
All Implemented Interfaces:
BeanClassLoaderAware, HierarchicalMessageSource, MessageSource

public class XtensibleResourceBundleMessageSource
extends AbstractMessageSource
implements BeanClassLoaderAware

MessageSource implementation that accesses resource bundles using specified basenames. This class relies on the underlying JDK's ResourceBundle implementation, in combination with the JDK's standard message parsing provided by MessageFormat.

This MessageSource is copied from org.springframework.context.support.ResourceBundleMessageSource. Unfortunatelly that class offers no oportunity to extend the resource-bundle at runtime.

The main difference is the possibility to add basenames, so that the same MessageSource could be extended by plugins or other dynamic application parts.

Extending ResourceBundleMessageSource was not possible due to the lack of access to basenames from derived classes.

Author:
Rod Johnson, Juergen Hoeller

adapted by:, Reinhard Mantey

See Also:
addBasename(java.lang.String), ResourceBundleMessageSource, ResourceBundle, MessageFormat

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractMessageSource
logger
 
Constructor Summary
XtensibleResourceBundleMessageSource()
           
 
Method Summary
 void addBasename(String basename)
          main reason for duplicating org.springframework.context.support.ResourceBundleMessageSource This way plugins could add their resource-bundles without changing code in base-classes.
 void addBasenames(String[] basenames)
          add multiple basenames - useful to add the basenames from another XtensibleResourceBundleMessageSource
protected  ResourceBundle doGetBundle(String basename, Locale locale)
          Obtain the resource bundle for the given basename and Locale.
 String[] getBasenames()
          to merge different MessageSources we need to get the basenames ...
protected  ClassLoader getBundleClassLoader()
          Return the ClassLoader to load resource bundles with.
protected  MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale)
          Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.
protected  ResourceBundle getResourceBundle(String basename, Locale locale)
          Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.
protected  MessageFormat resolveCode(String code, Locale locale)
          Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.
protected  String resolveCodeWithoutArguments(String code, Locale locale)
          Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing).
 void setBasename(String basename)
          Set a single basename, following ResourceBundle conventions: essentially, a fully-qualified classpath location.
 void setBasenames(String[] basenames)
          Set an array of basenames, each following ResourceBundle conventions: essentially, a fully-qualified classpath location.
 void setBeanClassLoader(ClassLoader classLoader)
           
 void setBundleClassLoader(ClassLoader classLoader)
          Set the ClassLoader to load resource bundles with.
 String toString()
          Show the configuration of this MessageSource.
 
Methods inherited from class org.springframework.context.support.AbstractMessageSource
createMessageFormat, formatMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isAlwaysUseMessageFormat, isUseCodeAsDefaultMessage, renderDefaultMessage, resolveArguments, setAlwaysUseMessageFormat, setParentMessageSource, setUseCodeAsDefaultMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XtensibleResourceBundleMessageSource

public XtensibleResourceBundleMessageSource()
Method Detail

addBasename

public void addBasename(String basename)
main reason for duplicating org.springframework.context.support.ResourceBundleMessageSource This way plugins could add their resource-bundles without changing code in base-classes.

Opposed to setBasenames() any existing basenames are preserved.

Parameters:
basename - - the new base to add

addBasenames

public void addBasenames(String[] basenames)
add multiple basenames - useful to add the basenames from another XtensibleResourceBundleMessageSource

Parameters:
basenames - - the bunch of new bases to add

getBasenames

public String[] getBasenames()
to merge different MessageSources we need to get the basenames ...

Returns:
the basenames as String array.

setBasename

public void setBasename(String basename)
Set a single basename, following ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.

Messages will normally be held in the "/lib" or "/classes" directory of a web application's WAR structure. They can also be held in jar files on the class path.

Note that ResourceBundle names are effectively classpath locations: As a consequence, the JDK's standard ResourceBundle treats dots as package separators. This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.

Parameters:
basename - - the new basename to add
See Also:
setBasenames(java.lang.String[]), ResourceBundle.getBundle(String)

setBasenames

public void setBasenames(String[] basenames)
Set an array of basenames, each following ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.

The associated resource bundles will be checked sequentially when resolving a message code. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.

Note that ResourceBundle names are effectively classpath locations: As a consequence, the JDK's standard ResourceBundle treats dots as package separators. This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.

See Also:
setBasename(java.lang.String), ResourceBundle.getBundle(String)

setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
Specified by:
setBeanClassLoader in interface BeanClassLoaderAware

setBundleClassLoader

public void setBundleClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with.

Default is the containing BeanFactory's bean ClassLoader, or the default ClassLoader determined by ClassUtils.getDefaultClassLoader() if not running within a BeanFactory.


toString

public String toString()
Show the configuration of this MessageSource.

Overrides:
toString in class Object

doGetBundle

protected ResourceBundle doGetBundle(String basename,
                                     Locale locale)
                              throws MissingResourceException
Obtain the resource bundle for the given basename and Locale.

Parameters:
basename - the basename to look for
locale - the Locale to look for
Returns:
the corresponding ResourceBundle
Throws:
MissingResourceException - if no matching bundle could be found
See Also:
ResourceBundle.getBundle(String, java.util.Locale, ClassLoader), getBundleClassLoader()

getBundleClassLoader

protected ClassLoader getBundleClassLoader()
Return the ClassLoader to load resource bundles with.

Default is the containing BeanFactory's bean ClassLoader.

See Also:
setBundleClassLoader(java.lang.ClassLoader)

getMessageFormat

protected MessageFormat getMessageFormat(ResourceBundle bundle,
                                         String code,
                                         Locale locale)
                                  throws MissingResourceException
Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.

Parameters:
bundle - the ResourceBundle to work on
code - the message code to retrieve
locale - the Locale to use to build the MessageFormat
Returns:
the resulting MessageFormat, or null if no message defined for the given code
Throws:
MissingResourceException - if thrown by the ResourceBundle

getResourceBundle

protected ResourceBundle getResourceBundle(String basename,
                                           Locale locale)
Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.

Parameters:
basename - the basename of the ResourceBundle
locale - the Locale to find the ResourceBundle for
Returns:
the resulting ResourceBundle, or null if none found for the given basename and Locale

resolveCode

protected MessageFormat resolveCode(String code,
                                    Locale locale)
Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.

Specified by:
resolveCode in class AbstractMessageSource

resolveCodeWithoutArguments

protected String resolveCodeWithoutArguments(String code,
                                             Locale locale)
Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing).

Overrides:
resolveCodeWithoutArguments in class AbstractMessageSource

SRJRCFrames
v0.1.296

hosted at
Find SRJRCFrames at SourceForge.net. Fast, secure and free:
           Open Source Software download
Submit a bug or request a feature

SRJRCFrames is published according to the GNU General Public License
Copyright 2005-2012 Reinhard Mantey - some rights reserved.