Top Description Fields Constructors Methods
com.sun.faces.application.resource

public Class ResourceManager

extends Object
Class Inheritance
Static Imports
com.sun.faces.util.Util.ensureLeadingSlash

This class is used to lookup ResourceInfo instances and cache any that are successfully looked up to reduce the computational overhead with the scanning/version checking.
Since
2.0

Field Summary

Modifier and TypeField and Description
private final ResourceCache
cache

Cache for storing ResourceInfo instances to reduce the cost of the resource lookups.

private final ClasspathResourceHelper
classpathResourceHelper

ResourceHelper used for looking up classpath-based resources.

private List<Pattern>
compressableTypes

Patterns used to find ResourceInfo instances that may have their content compressed.

private static final Pattern
CONFIG_MIMETYPE_PATTERN

Pattern for valid mime types to configure compression.

private final FaceletWebappResourceHelper
faceletWebappResourceHelper

ResourceHelper used for looking up webapp-based facelets resources.

private final ReentrantLock
lock

This lock is used to ensure the lookup of compressable ResourceInfo instances are atomic to prevent theading issues when writing the compressed content during a lookup.

private static final Logger
private final WebappResourceHelper
webappResourceHelper

ResourceHelper used for looking up webapp-based resources.

Constructor Summary

AccessConstructor and Description
public
public
ResourceManager(Map<String, Object>
the application map
appMap
,
ResourceCache
the resource cache
cache
)

Constructs a new ResourceManager.

Method Summary

Modifier and TypeMethod and Description
private void
addToCache(ResourceInfo
the @{link ResourceInfo} to add.
info
,
List<String>
the contracts
contracts
)

Adds the the specified ResourceInfo to the cache.

private ResourceInfo

Returns:

a ResourceInfo if a resource if found matching the provided arguments, otherwise, return null
doLookup
(String
the name of the library (if any)
libraryName
,
String
the name of the resource
resourceName
,
String
the locale prefix for this resource (if any)
localePrefix
,
boolean
if this resource can be compressed
compressable
,
boolean isViewResource, List<String>
the contracts to consider
contracts
,
FacesContext
the jakarta.faces.context.FacesContext for the current request
ctx
)

Attempt to look up the Resource based on the provided details.

pack-priv LibraryInfo

Returns:

the Library instance for the specified library
findLibrary
(String
the library to find
libraryName
,
String
the prefix for the desired locale
localePrefix
,
String
the contract to use
contract
,
FacesContext
the jakarta.faces.context.FacesContext for the current request
ctx
)

Attempt to lookup and return a LibraryInfo based on the specified arguments.

pack-priv LibraryInfo
findLibraryOnClasspathWithZipDirectoryEntryScan(String libraryName, String localePrefix, String contract, FacesContext ctx, boolean forceScan)

public ResourceInfo

Returns:

a ResourceInfo if a resource if found matching the provided arguments, otherwise, return null
findResource
(String
the name of the library (if any)
libraryName
,
String
the name of the resource
resourceName
,
String
the content type of the resource. This will be used to determine if the resource is compressable
contentType
,
FacesContext
the jakarta.faces.context.FacesContext for the current request
ctx
)

Attempt to lookup a ResourceInfo based on the specified libraryName and resourceName

Implementation Note

Synchronization is necessary when looking up compressed resources.

public ResourceInfo
findResource(String libraryName, String resourceName, String contentType, boolean isViewResource, FacesContext ctx)

private ResourceInfo

Returns:

the Library instance for the specified library
findResource
(LibraryInfo
the library the resource should be found in
library
,
String
the name of the resource
resourceName
,
String
the prefix for the desired locale
localePrefix
,
boolean
true if the resource can be compressed
compressable
,
boolean skipToFaceletResourceHelper, FacesContext
the jakarta.faces.context.FacesContext for the current request
ctx
)

Attempt to lookup and return a ResourceInfo based on the specified arguments.

pack-priv ResourceInfo
findResource(String resourceId)

private ResourceInfo
findResourceCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx)

private ResourceInfo
findResourceNonCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx)

public ResourceInfo
findViewResource(String resourceName, String contentType, FacesContext facesContext)

public String
private ResourceInfo

Returns:

the ResourceInfo from the cache or null if no cached entry is found
getFromCache
(String
the resource name
name
,
String
the library name
library
,
String
the Locale prefix
localePrefix
,
List<String> contracts)

private String

Returns:

the localePrefix based on the current request, or null if no prefix can be determined
getLocalePrefix
(FacesContext
the FacesContext for the current request
context
)

Obtains the application configured message resources for the current request locale.

private ResourceInfo
getResourceInfo(String libraryName, String resourceName, String localePrefix, String contract, boolean compressable, boolean isViewResource, FacesContext ctx, LibraryInfo library)

private List<String>
public Stream<String>
getViewResources(FacesContext facesContext, String path, int maxDepth, ResourceVisitOption... options)

private void
initCompressableTypes(Map<String, Object> appMap)

Init compressableTypes from the configuration.

private boolean

Returns:

true if this resource can be compressed, otherwise false
isCompressable
(String
content-type in question
contentType
,
FacesContext
the @{link FacesContext} for the current request
ctx
)

public boolean
private boolean

Returns:

true if the input matches the expected pattern, otherwise false
isPatternValid
(String
input mime-type pattern from the configuration
input
)

private String
libraryNameFromContracts(String libraryName, List<String> contracts)

pack-priv static boolean
private String

Returns:

the String without a leading slash if it has one.
trimLeadingSlash
(String
input String
s
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private final ResourceCache cache

Cache for storing ResourceInfo instances to reduce the cost of the resource lookups.

classpathResourceHelperback to summary
private final ClasspathResourceHelper classpathResourceHelper

ResourceHelper used for looking up classpath-based resources.

compressableTypesback to summary
private List<Pattern> compressableTypes

Patterns used to find ResourceInfo instances that may have their content compressed.

CONFIG_MIMETYPE_PATTERNback to summary
private static final Pattern CONFIG_MIMETYPE_PATTERN

Pattern for valid mime types to configure compression.

faceletWebappResourceHelperback to summary
private final FaceletWebappResourceHelper faceletWebappResourceHelper

ResourceHelper used for looking up webapp-based facelets resources.

lockback to summary
private final ReentrantLock lock

This lock is used to ensure the lookup of compressable ResourceInfo instances are atomic to prevent theading issues when writing the compressed content during a lookup.

LOGGERback to summary
private static final Logger LOGGER
webappResourceHelperback to summary
private final WebappResourceHelper webappResourceHelper

ResourceHelper used for looking up webapp-based resources.

Constructor Detail

ResourceManagerback to summary
public ResourceManager(ResourceCache cache)
ResourceManagerback to summary
public ResourceManager(Map<String, Object> appMap, ResourceCache cache)

Constructs a new ResourceManager.

Note

if the current ProjectStage is ProjectStage#Development caching or ResourceInfo instances will not occur.

Parameters
appMap:Map<String, Object>

the application map

cache:ResourceCache

the resource cache

Method Detail

addToCacheback to summary
private void addToCache(ResourceInfo info, List<String> contracts)

Adds the the specified ResourceInfo to the cache.

Parameters
info:ResourceInfo

the @{link ResourceInfo} to add.

contracts:List<String>

the contracts

doLookupback to summary
private ResourceInfo doLookup(String libraryName, String resourceName, String localePrefix, boolean compressable, boolean isViewResource, List<String> contracts, FacesContext ctx)

Attempt to look up the Resource based on the provided details.

Parameters
libraryName:String

the name of the library (if any)

resourceName:String

the name of the resource

localePrefix:String

the locale prefix for this resource (if any)

compressable:boolean

if this resource can be compressed

contracts:List<String>

the contracts to consider

ctx:FacesContext

the jakarta.faces.context.FacesContext for the current request

Returns:ResourceInfo

a ResourceInfo if a resource if found matching the provided arguments, otherwise, return null

findLibraryback to summary
pack-priv LibraryInfo findLibrary(String libraryName, String localePrefix, String contract, FacesContext ctx)

Attempt to lookup and return a LibraryInfo based on the specified arguments.

The lookup process will first search the file system of the web application *within the resources directory*. If the library is not found, then it processed to searching the classpath, if not found there, search from the webapp root *excluding* the resources directory.

If a library is found, this method will return a LibraryInfo instance that contains the name, version, and ResourceHelper.

Parameters
libraryName:String

the library to find

localePrefix:String

the prefix for the desired locale

contract:String

the contract to use

ctx:FacesContext

the jakarta.faces.context.FacesContext for the current request

Returns:LibraryInfo

the Library instance for the specified library

findLibraryOnClasspathWithZipDirectoryEntryScanback to summary
pack-priv LibraryInfo findLibraryOnClasspathWithZipDirectoryEntryScan(String libraryName, String localePrefix, String contract, FacesContext ctx, boolean forceScan)
findResourceback to summary
public ResourceInfo findResource(String libraryName, String resourceName, String contentType, FacesContext ctx)

Attempt to lookup a ResourceInfo based on the specified libraryName and resourceName

Implementation Note

Synchronization is necessary when looking up compressed resources. This ensures the atomicity of the content being compressed. As such, the cost of doing this is low as once the resource is in the cache, the lookup won't be performed again until the cache is cleared. That said, it's not a good idea to have caching disabled in a production environment if leveraging compression. If the resource isn't compressable, then we don't worry about creating a few extra copies of ResourceInfo until the cache is populated.

Parameters
libraryName:String

the name of the library (if any)

resourceName:String

the name of the resource

contentType:String

the content type of the resource. This will be used to determine if the resource is compressable

ctx:FacesContext

the jakarta.faces.context.FacesContext for the current request

Returns:ResourceInfo

a ResourceInfo if a resource if found matching the provided arguments, otherwise, return null

findResourceback to summary
public ResourceInfo findResource(String libraryName, String resourceName, String contentType, boolean isViewResource, FacesContext ctx)
findResourceback to summary
private ResourceInfo findResource(LibraryInfo library, String resourceName, String localePrefix, boolean compressable, boolean skipToFaceletResourceHelper, FacesContext ctx)

Attempt to lookup and return a ResourceInfo based on the specified arguments.

The lookup process will first search the file system of the web application. If the library is not found, then it processed to searching the classpath.

If a library is found, this method will return a LibraryInfo instance that contains the name, version, and ResourceHelper.

Parameters
library:LibraryInfo

the library the resource should be found in

resourceName:String

the name of the resource

localePrefix:String

the prefix for the desired locale

compressable:boolean

true if the resource can be compressed

ctx:FacesContext

the jakarta.faces.context.FacesContext for the current request

Returns:ResourceInfo

the Library instance for the specified library

findResourceback to summary
pack-priv ResourceInfo findResource(String resourceId)
findResourceCompressedback to summary
private ResourceInfo findResourceCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx)
findResourceNonCompressedback to summary
private ResourceInfo findResourceNonCompressed(String libraryName, String resourceName, boolean isViewResource, String localePrefix, List<String> contracts, FacesContext ctx)
findViewResourceback to summary
public ResourceInfo findViewResource(String resourceName, String contentType, FacesContext facesContext)
getBaseContractsPathback to summary
public String getBaseContractsPath()
getFromCacheback to summary
private ResourceInfo getFromCache(String name, String library, String localePrefix, List<String> contracts)
Parameters
name:String

the resource name

library:String

the library name

localePrefix:String

the Locale prefix

Returns:ResourceInfo

the ResourceInfo from the cache or null if no cached entry is found

getLocalePrefixback to summary
private String getLocalePrefix(FacesContext context)

Obtains the application configured message resources for the current request locale. If a ResourceBundle is found and contains the key jakarta.faces.resource.localePrefix, use the value associated with that key as the prefix for locale specific resources.

For example, say the request locale is en_US, and jakarta.faces.resourceLocalePrefix is found with a value of en, a resource path within a web application might look like /resources/en/corp/images/greetings.jpg

Parameters
context:FacesContext

the FacesContext for the current request

Returns:String

the localePrefix based on the current request, or null if no prefix can be determined

getResourceInfoback to summary
private ResourceInfo getResourceInfo(String libraryName, String resourceName, String localePrefix, String contract, boolean compressable, boolean isViewResource, FacesContext ctx, LibraryInfo library)
getResourceLibraryContractsback to summary
private List<String> getResourceLibraryContracts(FacesContext context)
getViewResourcesback to summary
public Stream<String> getViewResources(FacesContext facesContext, String path, int maxDepth, ResourceVisitOption... options)
initCompressableTypesback to summary
private void initCompressableTypes(Map<String, Object> appMap)

Init compressableTypes from the configuration.

isCompressableback to summary
private boolean isCompressable(String contentType, FacesContext ctx)
Parameters
contentType:String

content-type in question

ctx:FacesContext

the @{link FacesContext} for the current request

Returns:boolean

true if this resource can be compressed, otherwise false

isContractsResourceback to summary
public boolean isContractsResource(String path)
isPatternValidback to summary
private boolean isPatternValid(String input)
Parameters
input:String

input mime-type pattern from the configuration

Returns:boolean

true if the input matches the expected pattern, otherwise false

libraryNameFromContractsback to summary
private String libraryNameFromContracts(String libraryName, List<String> contracts)
nameContainsForbiddenSequenceback to summary
pack-priv static boolean nameContainsForbiddenSequence(String name)
trimLeadingSlashback to summary
private String trimLeadingSlash(String s)
Parameters
s:String

input String

Returns:String

the String without a leading slash if it has one.