Class PrefixedMessageSource

java.lang.Object
net.solarnetwork.support.PrefixedMessageSource
All Implemented Interfaces:
org.springframework.context.HierarchicalMessageSource, org.springframework.context.MessageSource

public class PrefixedMessageSource extends Object implements org.springframework.context.MessageSource, org.springframework.context.HierarchicalMessageSource
Delegating MessageSource that dynamically removes a pre-configured prefix from all message codes.

The inspiration for this class was to support messages for objects that might be nested in other objects used in SettingSpecifierProvider implementations. When one provider proxies another, or uses nested bean paths, this class can be used to dynamically re-map message codes. For example a code delegate.url could be re-mapped to url.

Since:
1.43
Version:
1.0
Author:
matt
  • Constructor Details

    • PrefixedMessageSource

      public PrefixedMessageSource()
  • Method Details

    • setParentMessageSource

      public void setParentMessageSource(org.springframework.context.MessageSource parent)
      Specified by:
      setParentMessageSource in interface org.springframework.context.HierarchicalMessageSource
    • getParentMessageSource

      public org.springframework.context.MessageSource getParentMessageSource()
      Specified by:
      getParentMessageSource in interface org.springframework.context.HierarchicalMessageSource
    • getMessage

      public String getMessage(String code, Object[] args, String defaultMessage, Locale locale)
      Specified by:
      getMessage in interface org.springframework.context.MessageSource
    • getMessage

      public String getMessage(String code, Object[] args, Locale locale) throws org.springframework.context.NoSuchMessageException
      Specified by:
      getMessage in interface org.springframework.context.MessageSource
      Throws:
      org.springframework.context.NoSuchMessageException
    • getMessage

      public String getMessage(org.springframework.context.MessageSourceResolvable resolvable, Locale locale) throws org.springframework.context.NoSuchMessageException
      Specified by:
      getMessage in interface org.springframework.context.MessageSource
      Throws:
      org.springframework.context.NoSuchMessageException
    • getPrefix

      public String getPrefix()
      Get the singular message code prefix to dynamically remove from all message codes.
      Returns:
      the singular message code prefix
      See Also:
    • setPrefix

      public void setPrefix(String prefix)
      Set the singular message code prefix to dynamically remove from all message codes.

      This prefix will only be used with the singular delegate configured via setDelegate(MessageSource).

      Parameters:
      prefix - the singular message code prefix
      See Also:
    • getDelegate

      public org.springframework.context.MessageSource getDelegate()
      Get the singular MessageSource to use with the singular prefix.
      Returns:
      the singular message source delegate
    • setDelegate

      public void setDelegate(org.springframework.context.MessageSource delegate)
      Set the singular MessageSource to use with the singular prefix.

      Note when this method is used, then messages found in this delegate under un-prefixed keys will be returned without consulting any parent source first.

      Parameters:
      delegate - the singular delegate to use
    • getDelegates

      public Map<String,org.springframework.context.MessageSource> getDelegates()
      Get the multi-prefix delegate mapping.
      Returns:
      a mapping of message code prefixes to associated MessageSource delegates
    • setDelegates

      public void setDelegates(Map<String,org.springframework.context.MessageSource> delegates)
      Set the multi-prefix delegate mapping.

      This configures any number of MessageSource delegates to handle specific message codes with associated prefix values.

      Parameters:
      delegates - the message code prefix mapping