Class Messages

java.lang.Object
java.util.ResourceBundle
org.apache.juneau.marshall.cp.Messages

public class Messages extends ResourceBundle
An enhanced ResourceBundle.

Wraps a ResourceBundle to provide some useful additional functionality.

  • Instead of throwing MissingResourceException, the ResourceBundle.getString(String) method will return "{!key}" if the message could not be found.
  • Supported hierarchical lookup of resources from parent parent classes.
  • Support for easy retrieval of localized bundles.
  • Support for generalized resource bundles (e.g. properties files containing keys for several classes).

The following example shows the basic usage of this class for retrieving localized messages:

# Contents of MyClass.properties foo = foo {0} MyClass.bar = bar {0}

public class MyClass { private static final Messages MESSAGES = Messages.of(MyClass.class); public void doFoo() { // A normal property. String foo = MESSAGES.getString("foo","x"); // == "foo x" // A property prefixed by class name. String bar = MESSAGES.getString("bar","x"); // == "bar x" // A non-existent property. String baz = MESSAGES.getString("baz","x"); // == "{!baz}" } }

The ability to resolve keys prefixed by class name allows you to place all your messages in a single file such as a common "Messages.properties" file along with those for other classes.

The following shows how to retrieve messages from a common bundle:

public class MyClass { private static final Messages MESSAGES = Messages.of(MyClass.class, "Messages"); }

Resource bundles are searched using the following base name patterns:

  • "{package}.{name}"
  • "{package}.i18n.{name}"
  • "{package}.nls.{name}"
  • "{package}.messages.{name}"

These patterns can be customized using the Messages.Builder.baseNames(String...) method.

Localized messages can be retrieved in the following way:

// Return value from Japan locale bundle. String foo = MESSAGES.forLocale(Locale.JAPAN).getString("foo");

  • Constructor Details

    • Messages

      protected Messages(Messages.Builder builder)
      Constructor.
      Parameters:
      builder - The builder for this object.
  • Method Details

    • create

      public static final Messages.Builder create(Class<?> forClass)
      Static creator.
      Parameters:
      forClass - The class we're creating this object for.
      Returns:
      A new builder.
    • of

      public static final Messages of(Class<?> forClass)
      Creates a new message bundle for the specified class.
      Parameters:
      forClass - The class we're creating this object for.
      Returns:
      A new message bundle belonging to the class.
    • of

      public static final Messages of(Class<?> forClass, String name)
      Creates a new message bundle for the specified class using the specified bundle name.
      Parameters:
      forClass - The class we're creating this object for.
      name - The bundle name (e.g. "Messages").
      If null, uses the class name.
      Returns:
      A new message bundle belonging to the class.
    • chain

      public static Messages chain(Messages child, Messages parent)
      Composes two pre-built bundles into a single parent-chained bundle without mutating either input.

      This is the public seam for parent-chain composition. Use it when one Messages bundle is built locally and needs to inherit unresolved keys from a separately-built bundle — for example, a REST mixin sub-context inheriting its host's bundle. The Messages.Builder chains inner locations via Messages.Builder.parent(Messages) during its own Messages.Builder.build() pass and does not expose this composition as a public API; chain(...) fills that gap without changing existing builder semantics.

      The returned bundle is a NEW chain of Messages instances that share the inner ResourceBundle, forClass, and locale of child's links. Neither child nor parent is mutated. Lookups follow child's chain first and fall through to parent for any key not present in the child chain.

      Parameters:
      child - The local bundle whose keys win first.
      If null, parent is returned unchanged.
      parent - The parent bundle whose keys are looked up when child's chain doesn't resolve.
      If null, child is returned unchanged.
      Returns:
      The composed bundle, or null if both arguments are null.
    • containsKey

      public boolean containsKey(String key)
      Overrides:
      containsKey in class ResourceBundle
    • findFirstString

      public String findFirstString(String... keys)
      Looks for all the specified keys in the resource bundle and returns the first value that exists.
      Parameters:
      keys - The list of possible keys.
      Returns:
      The resolved value, or null if no value is found or the resource bundle is missing.
    • forLocale

      public Messages forLocale(Locale locale)
      Returns this message bundle for the specified locale.
      Parameters:
      locale - The locale to get the messages for. Can be null (the default locale is used).
      Returns:
      A new Messages object. Never null.
    • getKeys

      Specified by:
      getKeys in class ResourceBundle
    • getString

      public String getString(String key, Object... args)
      Similar to ResourceBundle.getString(String) except allows you to pass in MessageFormat objects.
      Parameters:
      key - The resource bundle key.
      args - Optional MessageFormat-style arguments.
      Returns:
      The resolved value. Never null. "{!key}" if the key is missing.
    • keySet

      public Set<String> keySet()
      Overrides:
      keySet in class ResourceBundle
    • keySet

      public Set<String> keySet(String prefix)
      Returns all keys in this resource bundle with the specified prefix.

      Keys are returned in alphabetical order.

      Parameters:
      prefix - The prefix.
      Returns:
      The set of all keys in the resource bundle with the prefix.
    • properties

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • handleGetObject

      protected Object handleGetObject(String key)
      Specified by:
      handleGetObject in class ResourceBundle