Interface ResourceAccess
public interface ResourceAccess
This interface is used to register Java resources and
ResourceBundles that
should be accessible at run time. An instance of this interface is acquired via
Feature.AfterRegistrationAccess.getResourceAccess().
All methods in ResourceAccess require a AccessCondition as their first parameter.
Resources will be registered for run-time access only if the specified condition is
satisfied.
How to use
ResourceAccess should only be used during Feature.afterRegistration(Feature.AfterRegistrationAccess). Attempts to
register metadata in any other phase of the Feature API will result in an error.
Example:
@Override
public void afterRegistration(AfterRegistrationAccess access) {
ResourceAccess resources = access.getResourceAccess();
AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
resources.register(condition, "example/**");
resources.register(AccessCondition.unconditional(), "example/directory/concreteResource");
resources.register(condition, "example/concreteResourceStar\\*");
try {
ResourceBundle bundle = ResourceBundle.getBundle("bundlePath1");
resources.registerResourceBundle(condition, bundle);
} catch (MissingResourceException e) {
// Log or handle the missing resource
}
}
- Since:
- 25.0.1
-
Method Summary
Modifier and TypeMethodDescriptionvoidregister(AccessCondition condition, Module module, String glob) Registers resources matching the specifiedglobin the providedmodulefor run-time access, if theconditionis satisfied.default voidregister(AccessCondition condition, String glob) Registers resources matching the specifiedglobfrom the classpath for run-time access, if theconditionis satisfied.voidregisterResourceBundle(AccessCondition condition, ResourceBundle... bundles) Registers the providedResourceBundles for run-time access, if theconditionis satisfied.
-
Method Details
-
register
Registers resources matching the specifiedglobin the providedmodulefor run-time access, if theconditionis satisfied.The
globuses a subset of glob-pattern rules for specifying resources.There are several rules to be observed when specifying a resource path:
- The
native-imagetool supports onlystar (*)andglobstar (**)wildcard patterns.- By definition,
starcan match any number of any characters on one level whileglobstarcan match any number of characters at any level. - If there is a need to treat a star literally (without special meaning), it can be escaped
using
\(for example,\*).
- By definition,
- In the glob, a level represents a part of the pattern separated with
/. - When writing glob patterns the following rules must be observed:
- Glob cannot be empty (for example,
"") - Glob cannot end with a trailing slash ('/') (for example,
"foo/bar/") - Glob cannot contain more than two consecutive (non-escaped)
*characters on one level (for example,"foo/***/") - Glob cannot contain empty levels (for example,
"foo//bar") - Glob cannot contain two consecutive globstar wildcards (example,
"foo/**/**") - Glob cannot have other content on the same level as globstar wildcard (for example,
"foo/**bar/x")
- Glob cannot be empty (for example,
Given the following project structure:
app/src/main/resources/{Resource0.txt, Resource1.txt}You can:
- Include all resources with glob
**/Resource*.txt - Include
Resource0.txtwith glob**/Resource0.txt - Include
Resource0.txtandResource1.txtwith globs**/Resource0.txtand**/Resource1.txt
- Parameters:
condition- the condition that must be satisfied to register the target resourcemodule- the Java module instance that contains target resources. If the provided value isnullor an unnamed module, resources are looked up on the classpath instead.glob- the glob that is matched against all resources- Since:
- 25.0.1
- The
-
register
Registers resources matching the specifiedglobfrom the classpath for run-time access, if theconditionis satisfied.The
globuses a subset of glob-pattern rules for specifying resources.There are several rules to be observed when specifying a resource path:
- The
native-imagetool supports onlystar (*)andglobstar (**)wildcard patterns.- By definition,
starcan match any number of any characters on one level whileglobstarcan match any number of characters at any level. - If there is a need to treat a star literally (without special meaning), it can be escaped
using
\(for example,\*).
- By definition,
- In the glob, a level represents a part of the pattern separated with
/. - When writing glob patterns the following rules must be observed:
- Glob cannot be empty (for example,
"") - Glob cannot end with a trailing slash ('/') (for example,
"foo/bar/") - Glob cannot contain more than two consecutive (non-escaped)
*characters on one level (for example,"foo/***/") - Glob cannot contain empty levels (for example,
"foo//bar") - Glob cannot contain two consecutive globstar wildcards (example,
"foo/**/**") - Glob cannot have other content on the same level as globstar wildcard (for example,
"foo/**bar/x")
- Glob cannot be empty (for example,
app/src/main/resources/{Resource0.txt, Resource1.txt}You can:
- Include all resources with glob
**/Resource*.txt - Include
Resource0.txtwith glob**/Resource0.txt - Include
Resource0.txtandResource1.txt}with globs**/Resource0.txtand**/Resource1.txt
- Parameters:
condition- the condition that must be satisfied to register the target resourceglob- the glob that is matched against all resources- Since:
- 25.0.1
- The
-
registerResourceBundle
Registers the providedResourceBundles for run-time access, if theconditionis satisfied. EachResourceBundlemust be obtained using any version of theResourceBundle.getBundle(String)method. A bundle created in any other way will result in an error.Recommended use to avoid crashes during image building:
@Override public void afterRegistration(AfterRegistrationAccess access) { ResourceAccess resources = access.getResourceAccess(); AccessCondition condition = AccessCondition.typeReached(ConditionType.class); try { ResourceBundle bundle1 = ResourceBundle.getBundle("bundlePath1"); ResourceBundle bundle2 = ResourceBundle.getBundle("bundlePath2"); resources.registerResourceBundle(condition, bundle1, bundle2); } catch (MissingResourceException e) { // Log or handle the missing resource } }- Since:
- 25.0.1
-