Class PropertyFileUtil

java.lang.Object
jp.ecuacion.lib.core.util.PropertyFileUtil

public class PropertyFileUtil extends Object
Provides utility methods to read *.properties files.

It has following features added to ResourceBundle class packaged in JRE.

  1. To read all the ".properties" files in library modules and multiple modules in projects of an app.
  2. To read multiple kinds of ".properties" (application, messages, enum_names, item_names, ValidationMessages, ValidationMessagesWithField)
  3. To remove default locale from candidate locales
  4. To use "default" message by putting the postfix of the message ID ".default"
  5. To have the override function by java launch parameter (-D) or System.setProperty(...)
  6. To resolve property keys in the obtained value
  7. To resolve property keys in arguments

1. To read all the ".properties" files in library modules and multiple modules in app projects

If we talk about messages[_xxx].properties, this class reads ones in ecuacion libraries, and ones in your apps.
In ecuacion libraries an app is assumed to devided to some modules (=usually called "projects" in IDE), which are base, core, web (or none), batch.

If the name of your app is sample-app, module names would be :
sample-app-base : messages_base.properties
sample-app-core : messages_core.properties
sample-app-web : messages.properties
sample-app-batch: messages.properties

PropertyFileUtil.getMsg(...) will read all the messages properties above.
Duplicated definition detectable. (causes throwing exception)

And of course you can use localized files like messages_core_ja.properties because This class uses ResourceBundle inside to read properties files.


2. To read multiple kinds of ".properties" (application, messages, enum_names, item_names)

Firstly, In ecuacion-lib we have 4 kinds of property files.

PropertyFileUtil.getMsg(...) : messages[_xxx].properties
PropertyFileUtil.getApp(...) : application[_xxx].properties
PropertyFileUtil.getEnumName(...) : enum_names[_xxx].properties
PropertyFileUtil.getItemName(...) : item_names[_xxx].properties

messages.properties and application.properties are well-known.
enum_names.properties stores the localized name of the enum element, and item_names.properties stores the localized name of the item.
Usually these are also stored in messages.properties, but it's kind of messy so divided files are prepared.

PropertyFileUtil supports these 4 kinds of properties files.

kinds of property files
kind data the file has
application system settings
messages messages
item_names names of items
enum_names names of the elements of enums

3. To remove default locale from candidate locales

Java Standard ResourceBundle uses default locale (which is obtained by Locale.getDefault()) when the property file of specified locale is not found.
The default locale is usually equal to the locale of the OS, which means the result depends on the machine the program is executed on.

To avoid that situation deault locale is removed from candidate locales with this class.


4. To use "default" message by putting the postfix of the message ID ".default"

5. To Have the override function by java launch parameter (-D) or System.setProperty(...)

6. To resolve property keys in the obtained value

You can put a property key into a property value.
For example, you can define keys and values like this in messages.properties. By executing PropertyFileUtil.getMsg("message") you'll get "a-b-c".

     message=a-${messages:message_test1}-c
     message_test1=b

Recursive resolution is also supported so you can even define like the one below.
By executing PropertyFileUtil.getMsg("message") you'll get "a-b-c-d-e-f-g".

     message=a-${messages:message_test1}-c-${messages:message_test2}-g
     message_test1=b
     message_test2=d-${messages:message_test3}-f
     message_test3=e

Examples above uses ${messages:...} but you can also use other file kinds like ${application:...}, ${item_names:...} and ${enum_names:...}.

Recursive resolution is supported, but multiple layer of key is not supported. (which does not seem to be needed really)

     message=a-${messages:${messages:message_prefix}_test1}-c
     message_prefix=message
     message_test1=b

7. To resolve property keys in arguments

Miscellaneous

messages[_xxx].properties, enum_names[_xxx].properties, fiels_names[_xxx].properties need to have default locale file (like messages.properties. This is the rule of the library.
It leads the conclusion that hasXxx(...) (like hasMsg(...)) doesn't need to have locale argument. (default locale used)

  • Method Details

    • getApp

      @Nonnull public static String getApp(@RequireNonnull String key)
      Returns the value in application_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      the value of the property
    • hasApp

      public static boolean hasApp(@RequireNonnull String key)
      Returns the existence of the key in application_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      boolean value that shows whether properties has the key
    • getMsg

      @Nonnull public static String getMsg(@RequireNonnull String key, @RequireNonnull String... args)
      Returns the value of default locale in messages_xxx.properties.
      Parameters:
      key - the key of the property
      args - message arguments
      Returns:
      the value (message) of the property key (message ID)
    • getMsg

      @Nonnull public static String getMsg(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull String... args)
      Returns the localized value in messages_xxx.properties.
      Parameters:
      locale - locale, may be null which means no Locale specified.
      key - the key of the property
      args - message arguments
      Returns:
      the value (message) of the property key (message ID)
    • getMsg

      @Nonnull public static String getMsg(@RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args)
      Returns the value of default locale in messages_xxx.properties.
      Parameters:
      key - the key of the property
      args - message arguments, which can be message ID. The data type is Arg[], not Arg... because if Arg causes an error when you call getMsg(key) since the second parameter is unclear (String... or Arg....
      Returns:
      the value (message) of the property key (message ID)
    • getMsg

      @Nonnull public static String getMsg(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args)
      Returns the localized value in messages_xxx.properties.
      Parameters:
      locale - locale, may be null which means no Locale specified.
      key - the key of the property
      args - message arguments, which can be message ID. The data type is Arg[], not Arg... because if Arg causes an error when you call getMsg(key) since the second parameter is unclear (String... or Arg....
      Returns:
      the message corresponding to the message ID
    • hasMsg

      public static boolean hasMsg(@RequireNonnull String key)
      Returns the existence of the key in messages_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      boolean value that shows whether properties has the key (message ID)
    • getItemName

      @Nonnull public static String getItemName(@RequireNonnull String key)
      Returns the item name of default locale in item_names_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      the value of the property
    • getItemName

      @Nonnull public static String getItemName(@Nullable Locale locale, @RequireNonnull String key)
      Returns the localized item name in item_names_xxx.properties.
      Parameters:
      locale - locale, may be null which is treated as Locale.getDefault().
      key - the key of the property
      Returns:
      the value of the property
    • hasItemName

      public static boolean hasItemName(@RequireNonnull String key)
      Returns the existence of the key in item_names_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      boolean value that shows whether properties has the key
    • getEnumName

      @Nonnull public static String getEnumName(@RequireNonnull String key)
      Returns the enum name of default locale in enum_names_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      the value of the property
    • getEnumName

      @Nonnull public static String getEnumName(@Nullable Locale locale, @RequireNonnull String key)
      Returns the localized enum name in enum_names_xxx.properties.
      Parameters:
      locale - locale, may be null which is treated as Locale.getDefault().
      key - the key of the property
      Returns:
      the value of the property
    • hasEnumName

      public static boolean hasEnumName(@RequireNonnull String key)
      Returns the existence of the key in enam_names_xxx.properties.
      Parameters:
      key - the key of the property
      Returns:
      boolean value that shows whether properties has the key
    • getValidationMessage

      @Nonnull public static String getValidationMessage(@RequireNonnull String key, Map<String,String> argMap)
      Returns the property value of default locale in ValidationMessages[_locale].properties.

      Usually ValidationMessages[_locale].properties file satisfies validation message's requirement. But when you want to show error messages on the top message space and

      Parameters:
      key - the key of the property
      argMap - argMap
      Returns:
      the value of the property
    • getValidationMessage

      @Nonnull public static String getValidationMessage(@Nullable Locale locale, @RequireNonnull String key, Map<String,String> argMap)
      Returns the localized enum name in ValidationMessages[_locale].properties.
      Parameters:
      locale - locale, may be null which is treated as Locale.getDefault().
      key - the key of the property
      Returns:
      the value of the property
    • getValidationMessageWithField

      @Nonnull public static String getValidationMessageWithField(@RequireNonnull String key, Map<String,String> argMap)
      Returns the property value of default locale in ValidationMessagesWithField_xxx.properties.

      Usually ValidationMessages[_locale].properties file satisfies validation message's requirement. But when you want to show error messages on the top message space and

      Parameters:
      key - the key of the property
      argMap - argMap
      Returns:
      the value of the property
    • getValidationMessageWithField

      @Nonnull public static String getValidationMessageWithField(@Nullable Locale locale, @RequireNonnull String key, Map<String,String> argMap)
      Returns the localized enum name in enum_names_xxx.properties.
      Parameters:
      locale - locale, may be null which is treated as Locale.getDefault().
      key - the key of the property
      Returns:
      the value of the property
    • get

      @Nonnull public static String get(@RequireNonnull String propertyUtilFileKind, @RequireNonnull String key)
      Returns the property value of default locale.
      Parameters:
      propertyUtilFileKind - String value of PropertyUtilFileKind (application, messages, ...)
      key - the key of the property
      Returns:
      the value of the property
    • get

      @Nonnull public static String get(@RequireNonnull String propertyUtilFileKind, @Nullable Locale locale, @RequireNonnull String key)
      Returns the localized property value.
      Parameters:
      propertyUtilFileKind - String value of PropertyUtilFileKind (application, messages, ...)
      locale - locale, may be null which is treated as Locale.getDefault().
      key - the key of the property
      Returns:
      the value of the property
    • has

      public static boolean has(@RequireNonnull String propertyUtilFileKind, @RequireNonnull String key)
      Returns the existence of the key.
      Parameters:
      propertyUtilFileKind - String value of PropertyUtilFileKind (application, messages, ...)
      key - the key of the property
      Returns:
      the value of the property
    • addResourceBundlePostfix

      public static void addResourceBundlePostfix(String postfix)
      Adds postfix dinamically.

      If you add test for example, messages_test[_lang].properties, application_test[_lang].properties, ... are searched.

      In java 9 module system environment, you also need to Service Provider Interface(SPI) defined in `ecuacion-lib-core`.

      Parameters:
      postfix - postfix