Portable retrieval of resource bundles across module path and class path
Mandy Chung
mandy.chung at oracle.com
Thu Jul 22 22:30:56 UTC 2021
On 7/22/21 12:23 PM, Gunnar Morling wrote:
> Hi all,
>
> I'm trying to figure out how a modular application should handle
> cross-module resource bundle look-ups, supporting running on both the
> module path and the class path. At a first look, resource bundle semantics
> in the two modes are at odds with each other:
>
> * When running on the classpath, all modules (or JARs, I should say) would
> have to provide their bundles in one and the same package, so
> ResourceBundle.getBundle() will pick them all up
> * When running on the module path, this would lead to split package issues,
> so each module has to use a distinct package and a ResourceBundleProvider
> which exposes the module's bundles
>
> So what I ended up doing is using the distinct package approach with RBP to
> make things work when running on the module path. When running on the class
> path, I'm using a ResourceBundleControlProvider whose returned Control
> adjusts bundle names to match the package naming pattern used in the
> different modules.
>
> Is this the recommended approach for solving the issue of making resource
> bundles in a modular application functional on module and class path? Or is
> there a simpler approach to achieve the same?
I assume the class path is running on JDK <= 8, right? Otherwise
This is a reasonable approach for the resource bundles to load from both
class path and module path when the resource bundles of a given name is
packaged in multiple modules/JARs.
We considered extending the default ResourceBundle.Control
implementation to search with a repackaged bundle name as
AbstractResourceBundleProvider::toBundleName in addition to the current
default Control::toBundleName. This incurs higher lookup cost (e.g.
when it fails with the search of p.foo.Resource_<lang>, it will search
p.foo.<lang>.Resource.). What you have is reasonable.
Mandy
More information about the jigsaw-dev
mailing list