Class PropertyFileUtil
*.properties files.
It has following features added to ResourceBundle class packaged in JRE.
- To read multiple kinds of
*.propertiesfiles - To read all the
*.propertiesfiles in ecuacion modules and multiple modules of an app - To remove default locale from candidate locales
- To avoid throwing an exception exen if a message key does not exist
- To use "default" value by putting ".default" postfix to the key
- To have the override function by java launch parameter (-D) or System.setProperty(...)
- To resolve property keys in the obtained value
- To resolve property keys in arguments
- To resolve EL expression
1. To read multiple kinds of *.properties files
It treats *.properties files below.
Localized ones take Locale as an argument,
and parameterized ones does String[] or Arg[].
(Details for Arg are in section 8.)
| file name | method to obtain values from files | localized | parameterized | description |
|---|---|---|---|---|
| application[_xxx].properties | getApplication(...) | Treats app settings | ||
| messages[_xxx].properties | getMessage(...) | ☑️ | ☑️ | Treats localized messages |
| strings[_xxx].properties | getString(...) | ☑️ | Treats non-localized messages | |
| item_names[_xxx].properties | getItemName(...) | ☑️ | Treats enum item names | |
| enum_names[_xxx].properties | getEnumName(...) | ☑️ | Treats enum value names | |
| ValidationMessages[_xxx].properties | getValidationMessage(...) | ☑️ | Treats jakarta validation messages, but it's never called from apps. It's used only from ecuacion-modules. No item names in them. | |
| ValidationMessagesWithItemNames[_xxx].properties | getValidationMessageWithItemName(...) | ☑️ | Treats jakarta validation messages, but it's never called from apps. It's used only from ecuacion-modules. Item names in them. | |
| ValidationMessagesPatternDescriptions[_xxx].properties | getValidationMessagePatternDescription(...) | ☑️ | Treats pattern expressing localized strings used for jakarta validation messages, but it's never called from apps. It's used only from ecuacion-modules. |
2. To read all the *.properties files in ecuacion modules
and multiple modules of an app
When we talk about messages[_xxx].properties,
this class reads ones in ecuacion modules
(like messages_lib_core.properties in ecuacion-lib-core),
and ones in your apps.
In ecuacion modules an app is assumed to divided into 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.getMessage(...) will read all the properties above.
Duplicated definition detectable. (causes exception)
And of course you can use localized files like messages_core_ja.properties
for localized files (see the table above)
because this class uses ResourceBundle inside to read them.
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
(of the server usually in web application environment),
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.
Note that it uses default locale
when you don't specify locale to get a string from localized *.properties files.
4. To avoid throwing an exception exen if a message key does not exist
Since application.properties has settings, exception should be thrown
when a required key does not exist.
On the other hand, since messages.properties has messages only
and even if it's shown on the screen, it's weird but not very fatal,
and furthermore it's better when developing because developers can see clearly
which messages are not defined
(System error screen has no concrete information),
so exception should not be thrown and just show the message key with [ ].
This feature offers shown key on screen with non-application properties.
5. To use "default" value by putting ".default" postfix to the key
It's kind of troublesome that you have to create *.properties
for pre-defined keys in ecuacion-modules.
But on the other hand, it's better for app developers to be able to change it
when it has to be.
So the keys in *.properties files contained in ecuacion modules
have ".default" postfix and those can be overrided in app modules
by defining the key in app *.properties files without ".default".
6. To Have the override function by java launch parameter (-D)
or System.setProperty(...)
You can override values with those settings.
(It's implemented for application.properties)
7. 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.getMessage("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.getMessage("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)
message=a-${+messages:${+messages:message_prefix}_test1}-c
message_prefix=message
message_test1=b
8. To resolve property keys in arguments
Sometimes you want to put not a static string, but a dynamic one,
and maybe localized one as a parameter
of messages.properties or strings.properties.
You can realize it by Arg instead of String as a parameter class.
9. To resolve EL expression
EL expression is supported. (Since jakarta validation does.)
You can define it like this.
math.addition=1 + 1 = ${1 + 1}.
Miscellaneous
messages[_xxx].properties,enum_names[_xxx].properties,fiels_names[_xxx].propertiesneed to have default locale file (likemessages.properties. This is the rule of the library.
It leads the conclusion thathasXxx(...) (like hasMsg(...))doesn't need to havelocaleargument. (default locale used)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classIs considered as an argument string, but you can set message ID replaced to message string withPropertyFileUtil.getMessage(String). -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddResourceBundlePostfix(String postfix) Adds postfix dinamically.static StringReturns the property value of default locale.static StringReturns the localized property value.static StringgetApplication(String key) Returns the value in application_xxx.properties.static StringgetApplicationOrElse(String key, String defaultValue) Returns the value in application_xxx.properties if it exists.static StringgetEnumName(String key) Returns the enum name of default locale in enum_names_xxx.properties.static StringgetEnumName(Locale locale, String key) Returns the localized enum name in enum_names_xxx.properties.static StringgetItemName(String key) Returns the item name of default locale in item_names_xxx.properties.static StringgetItemName(Locale locale, String key) Returns the localized item name in item_names_xxx.properties.static StringgetMessage(String key, String... args) Returns the value of default locale in messages_xxx.properties.static StringgetMessage(String key, PropertyFileUtil.Arg[] args) Returns the value of default locale in messages_xxx.properties.static StringgetMessage(Locale locale, String key, String... args) Returns the localized value in messages_xxx.properties.static StringgetMessage(Locale locale, String key, PropertyFileUtil.Arg[] args) Returns the localized value in messages_xxx.properties.static StringgetMessageWithItemName(String key, String... args) Returns the value of default locale in messagesWithItemNames_xxx.properties.static StringgetMessageWithItemName(String key, PropertyFileUtil.Arg[] args) Returns the value of default locale in messagesWithItemNames_xxx.properties.static StringgetMessageWithItemName(Locale locale, String key, String... args) Returns the localized value in messagesWithItemNames_xxx.properties.static StringgetMessageWithItemName(Locale locale, String key, PropertyFileUtil.Arg[] args) Returns the localized value in messagesWithItemNames_xxx.properties.static StringReturns the value in string_xxx.properties.static StringgetString(String key, PropertyFileUtil.Arg[] args) Returns the value in string_xxx.properties.static StringgetStringFromArg(Locale locale, PropertyFileUtil.Arg arg) Obtains string fromArg.static StringgetValidationMessage(String key, Map<String, Object> argMap) Returns the property value of default locale in ValidationMessages[_locale].properties.static StringReturns the localized enum name in ValidationMessages[_locale].properties.static StringReturns the property value of default locale in ValidationMessages[_locale].properties.static StringgetValidationMessagePatternDescription(Locale locale, String key) Returns the localized enum name in ValidationMessages[_locale].properties.static StringgetValidationMessageWithItemName(String key, Map<String, Object> argMap) Returns the property value of default locale in ValidationMessagesWithItemNames_xxx.properties.static StringReturns the localized enum name in enum_names_xxx.properties.static booleanReturns the existence of the key.static booleanhasApplication(String key) Returns the existence of the key in application_xxx.properties.static booleanhasEnumName(String key) Returns the existence of the key in enam_names_xxx.properties.static booleanhasItemName(String key) Returns the existence of the key in item_names_xxx.properties.static booleanhasMessage(String key) Returns the existence of the key in messages_xxx.properties.static booleanReturns the existence of the key in messagesWithItemNames_xxx.properties.static booleanReturns the existence of the key in strings_xxx.properties.
-
Method Details
-
getApplication
Returns the value in application_xxx.properties.- Parameters:
key- the key of the property- Returns:
- the value of the property
-
hasApplication
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
-
getApplicationOrElse
@Nullable public static String getApplicationOrElse(@RequireNonnull String key, String defaultValue) Returns the value in application_xxx.properties if it exists. Returns given default value if not.- Parameters:
key- the key of the propertydefaultValue- default value- Returns:
- the value of the property
-
getMessage
@Nonnull public static String getMessage(@RequireNonnull String key, @RequireNonnull String... args) Returns the value of default locale in messages_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMessage
@Nonnull public static String getMessage(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull String... args) Returns the localized value in messages_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMessage
@Nonnull public static String getMessage(@RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the value of default locale in messages_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the value (message) of the property key (message ID)
-
getMessage
@Nonnull public static String getMessage(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the localized value in messages_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the message corresponding to the message ID
-
hasMessage
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)
-
getMessageWithItemName
@Nonnull public static String getMessageWithItemName(@RequireNonnull String key, @RequireNonnull String... args) Returns the value of default locale in messagesWithItemNames_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMessageWithItemName
@Nonnull public static String getMessageWithItemName(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull String... args) Returns the localized value in messagesWithItemNames_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMessageWithItemName
@Nonnull public static String getMessageWithItemName(@RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the value of default locale in messagesWithItemNames_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the value (message) of the property key (message ID)
-
getMessageWithItemName
@Nonnull public static String getMessageWithItemName(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the localized value in messagesWithItemNames_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the message corresponding to the message ID
-
hasMessageWithItemName
Returns the existence of the key in messagesWithItemNames_xxx.properties.- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key (message ID)
-
getString
Returns the value in string_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getString
@Nonnull public static String getString(@RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the value in string_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the value (message) of the property key (message ID)
-
hasString
Returns the existence of the key in strings_xxx.properties.- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
getItemName
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
Returns the localized item name in item_names_xxx.properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
hasItemName
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
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
Returns the localized enum name in enum_names_xxx.properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
hasEnumName
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, Object> argMap) Returns the property value of default locale in ValidationMessages[_locale].properties.Usually
ValidationMessages[_locale].propertiesfile 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 propertyargMap- argMap- Returns:
- the value of the property
-
getValidationMessage
@Nonnull public static String getValidationMessage(@Nullable Locale locale, @RequireNonnull String key, @Nullable Map<String, Object> argMap) Returns the localized enum name in ValidationMessages[_locale].properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
getValidationMessageWithItemName
@Nonnull public static String getValidationMessageWithItemName(@RequireNonnull String key, @Nullable Map<String, Object> argMap) Returns the property value of default locale in ValidationMessagesWithItemNames_xxx.properties.Usually
ValidationMessages[_locale].propertiesfile 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 propertyargMap- argMap- Returns:
- the value of the property
-
getValidationMessageWithItemName
@Nonnull public static String getValidationMessageWithItemName(@Nullable Locale locale, @RequireNonnull String key, @Nullable Map<String, Object> argMap) Returns the localized enum name in enum_names_xxx.properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
getValidationMessagePatternDescription
Returns the property value of default locale in ValidationMessages[_locale].properties.Usually
ValidationMessages[_locale].propertiesfile 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- Returns:
- the value of the property
-
getValidationMessagePatternDescription
@Nonnull public static String getValidationMessagePatternDescription(@Nullable Locale locale, @RequireNonnull String key) Returns the localized enum name in ValidationMessages[_locale].properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.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 ofPropertyUtilFileKind(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 ofPropertyUtilFileKind(application, messages, ...)locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
has
Returns the existence of the key.- Parameters:
propertyUtilFileKind- String value ofPropertyUtilFileKind(application, messages, ...)key- the key of the property- Returns:
- the value of the property
-
addResourceBundlePostfix
Adds postfix dinamically.If you add
testfor 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
-
getStringFromArg
@Nonnull public static String getStringFromArg(@Nullable Locale locale, @RequireNonnull PropertyFileUtil.Arg arg) Obtains string fromArg.- Parameters:
locale- locale, may benullwhich means noLocalespecified.arg- message arguments, which can be message ID.- Returns:
- the message corresponding to the message ID or the string set to
Arg.
-