public final class GrailsResourceUtils extends Object
Utility methods for resource handling / figuring out class names.
Modifiers | Name | Description |
---|---|---|
static String |
CLASSPATH_URL_PREFIX |
Pseudo URL prefix for loading from the class path: "classpath:" |
static String |
CLASS_EXTENSION |
|
static Pattern[] |
COMPILER_ROOT_PATTERNS |
|
static String |
DOMAIN_DIR_PATH |
|
static Pattern |
DOMAIN_PATH_PATTERN |
Domain path is always matched against the normalized File representation of the URL and can therefore work with slashes as separators. |
static Pattern |
DOMAIN_PATH_PATTERN_NEW |
|
static String |
FILE_URL_PREFIX |
URL prefix for loading from the file system: "file:" |
static String |
GRAILS_APP_DIR |
The name of the Grails application directory |
static Pattern |
GRAILS_RESOURCE_PATTERN_EIGHTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_ELEVENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_FIFTEENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_FIFTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_FIRST_MATCH |
Resources are resolved against the platform specific path and must therefore obey the specific File.separator. |
static Pattern |
GRAILS_RESOURCE_PATTERN_FOURTEENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_FOURTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_NINTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_SECOND_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_SEVENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_SIXTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_TENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_THIRD_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_THIRTEENTH_MATCH |
|
static Pattern |
GRAILS_RESOURCE_PATTERN_TWELFTH_MATCH |
|
static String |
JAR_URL_SEPARATOR |
|
static String |
REGEX_FILE_SEPARATOR |
backslashes need escaping in regexes |
static Pattern |
RESOURCE_PATH_PATTERN |
This pattern will match any resource within a given directory inside grails-app |
static Pattern |
RESOURCE_PATH_PATTERN_NEW |
|
static Pattern |
SPRING_SCRIPTS_PATH_PATTERN |
|
static Pattern |
SPRING_SCRIPTS_PATH_PATTERN_NEW |
|
static String |
URL_PROTOCOL_CODE_SOURCE |
URL protocol for an entry from an OC4J jar file: "code-source" |
static String |
URL_PROTOCOL_FILE |
URL protocol for a file in the file system: "file" |
static String |
URL_PROTOCOL_JAR |
URL protocol for an entry from a jar file: "jar" |
static String |
URL_PROTOCOL_VFS |
URL protocol for a JBoss VFS resource: "vfs" |
static String |
URL_PROTOCOL_VFSZIP |
URL protocol for an entry from a JBoss jar file: "vfszip" |
static String |
URL_PROTOCOL_WSJAR |
URL protocol for an entry from a WebSphere jar file: "wsjar" |
static String |
URL_PROTOCOL_ZIP |
URL protocol for an entry from a zip file: "zip" |
static String |
VIEWS_DIR_PATH |
The path to the views directory |
static String |
VIEWS_DIR_PATH_NO_SLASH |
The path to the views directory without a trailing slash |
static String |
WEB_APP_DIR |
The name of the Web app dir within Grails |
static String |
WEB_INF |
The relative path to the WEB-INF directory |
static Pattern[] |
grailsAppResourcePatterns |
|
static Pattern[] |
patterns |
Type Params | Return Type | Name and description |
---|---|---|
|
public static String |
appendPiecesForUri(String pieces) Takes any number of Strings and appends them into a uri, making sure that a forward slash is inserted between each piece and making sure that no duplicate slashes are in the uri |
|
public static String |
applyRelativePath(String path, String relativePath) Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators). |
|
public Object |
call(Object args) |
|
public static String |
classPackageAsResourcePath(Class<?> clazz) Given an input class object, return a string which consists of the class's package name as a pathname, i.e., all dots ('.') are replaced by slashes ('/'). |
|
public static String |
cleanPath(String path) Normalize the path by suppressing sequences like "path/.." and inner simple dots. |
|
public static URL |
extractJarFileURL(URL jarUrl) Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself). |
|
public static Resource |
getAppDir(Resource resource) |
|
public static String |
getArtefactDirectory(String path) Takes a file path and returns the name of the folder under grails-app i.e: Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy Output: domain |
|
public static String |
getClassName(Resource resource) Gets the class name of the specified Grails resource |
|
public static String |
getClassName(String path) Returns the class name for a Grails resource. |
|
public static String |
getClassNameForClassFile(String rootDir, String path) Returns the class name for a compiled class file |
|
public static File |
getFile(URL resourceUrl, String description) Resolve the given resource URL to a java.io.File ,
i.e. to a file in the file system. |
|
public static File |
getFile(URI resourceUri, String description) Resolve the given resource URI to a java.io.File ,
i.e. to a file in the file system. |
|
public static File |
getFile(URI resourceUri) Resolve the given resource URI to a java.io.File ,
i.e. to a file in the file system. |
|
public static String |
getFilename(String path) Extract the filename from the given path, e.g. |
|
public static String |
getPathFromBaseDir(String path) Gets the path relative to the project base directory. |
|
public static String |
getPathFromRoot(String path) Get the path relative to an artefact folder under grails-app i.e: Input: /usr/joe/project/grails-app/conf/BootStrap.groovy Output: BootStrap.groovy |
|
public static String |
getRelativeInsideWebInf(Resource resource) Takes a Grails resource (one located inside the grails-app dir) and gets its relative path inside the WEB-INF directory when deployed. |
|
public static String |
getStaticResourcePathForResource(Resource resource, String contextPath) Retrieves the static resource path for the given Grails resource artifact (controller/taglib etc.) |
|
public static Resource |
getViewsDir(Resource resource) |
|
public static Object |
instantiateFromConfig(ConfigObject config, String configKey, String defaultClassName) |
|
public static Object |
instantiateFromFlatConfig(Map<String, Object> flatConfig, String configKey, String defaultClassName) |
|
public static boolean |
isDomainClass(URL url) |
|
public static boolean |
isFileURL(URL url) Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs". |
|
public static boolean |
isGrailsPath(String path) Checks whether the specified path is a Grails path. |
|
public static boolean |
isGrailsResource(Resource r) Checks whether the specific resources is a Grails resource. |
|
public static boolean |
isJarURL(URL url) Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source". |
|
public static boolean |
isProjectSource(String path) Checks whether the specified path is a Grails path. |
|
public static boolean |
isProjectSource(Resource r) Checks whether the specified path is a Grails path. |
|
protected boolean |
removeEldestEntry(Map.Entry eldest) |
|
protected boolean |
removeEldestEntry(Map.Entry<String, Boolean> eldest) |
|
public static URI |
toURI(URL url) Create a URI instance for the given URL, replacing spaces with "%20" quotes first. |
|
public static URI |
toURI(String location) Create a URI instance for the given location String, replacing spaces with "%20" quotes first. |
|
public static void |
useCachesIfNecessary(URLConnection con) |
Pseudo URL prefix for loading from the class path: "classpath:"
Domain path is always matched against the normalized File representation of the URL and can therefore work with slashes as separators.
URL prefix for loading from the file system: "file:"
The name of the Grails application directory
Resources are resolved against the platform specific path and must therefore obey the specific File.separator.
backslashes need escaping in regexes
This pattern will match any resource within a given directory inside grails-app
URL protocol for an entry from an OC4J jar file: "code-source"
URL protocol for a file in the file system: "file"
URL protocol for an entry from a jar file: "jar"
URL protocol for a JBoss VFS resource: "vfs"
URL protocol for an entry from a JBoss jar file: "vfszip"
URL protocol for an entry from a WebSphere jar file: "wsjar"
URL protocol for an entry from a zip file: "zip"
The path to the views directory
The path to the views directory without a trailing slash
The name of the Web app dir within Grails
The relative path to the WEB-INF directory
Takes any number of Strings and appends them into a uri, making sure that a forward slash is inserted between each piece and making sure that no duplicate slashes are in the uri
Input: "" Output: "" Input: "/alpha", "/beta", "/gamma" Output: "/alpha/beta/gamma Input: "/alpha/, "/beta/", "/gamma" Output: "/alpha/beta/gamma Input: "/alpha/", "/beta/", "/gamma/" Output "/alpha/beta/gamma/ Input: "alpha", "beta", "gamma" Output: "alpha/beta/gamma
pieces
- Strings to concatenate together into a uriApply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators).
path
- the path to start from (usually a full file path)relativePath
- the relative path to apply
(relative to the full file path above) Given an input class object, return a string which consists of the
class's package name as a pathname, i.e., all dots ('.') are replaced by
slashes ('/'). Neither a leading nor trailing slash is added. The result
could be concatenated with a slash and the name of a resource and fed
directly to ClassLoader.getResource()
. For it to be fed to
Class.getResource
instead, a leading slash would also have
to be prepended to the returned value.
clazz
- the input class. A null
value or the default
(empty) package will result in an empty string ("") being returned.Normalize the path by suppressing sequences like "path/.." and inner simple dots.
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
path
- the original pathExtract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
jarUrl
- the original URLTakes a file path and returns the name of the folder under grails-app i.e:
Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy Output: domain
path
- The pathGets the class name of the specified Grails resource
resource
- The Spring ResourceReturns the class name for a Grails resource.
path
- The path to checkReturns the class name for a compiled class file
path
- The path to check Resolve the given resource URL to a java.io.File
,
i.e. to a file in the file system.
resourceUrl
- the resource URL to resolvedescription
- a description of the original resource that
the URL was created for (for example, a class path location) Resolve the given resource URI to a java.io.File
,
i.e. to a file in the file system.
resourceUri
- the resource URI to resolvedescription
- a description of the original resource that
the URI was created for (for example, a class path location) Resolve the given resource URI to a java.io.File
,
i.e. to a file in the file system.
resourceUri
- the resource URI to resolveExtract the filename from the given path, e.g. "mypath/myfile.txt" -> "myfile.txt".
path
- the file path (maybe null
)null
if noneGets the path relative to the project base directory.
Input: /usr/joe/project/grails-app/conf/BootStrap.groovy Output: grails-app/conf/BootStrap.groovy
path
- The pathGet the path relative to an artefact folder under grails-app i.e:
Input: /usr/joe/project/grails-app/conf/BootStrap.groovy Output: BootStrap.groovy Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy Output: com/mystartup/Book.groovy
path
- The path to evaluateTakes a Grails resource (one located inside the grails-app dir) and gets its relative path inside the WEB-INF directory when deployed.
resource
- The Grails resource, which is a file inside the grails-app dirRetrieves the static resource path for the given Grails resource artifact (controller/taglib etc.)
resource
- The ResourcecontextPath
- The additional context path to prefixDetermine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".
url
- the URL to checkChecks whether the specified path is a Grails path.
path
- The path to checkChecks whether the specific resources is a Grails resource. A Grails resource is a Groovy or Java class under the grails-app directory
r
- The resource to checkDetermine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip", "wsjar" or "code-source".
"zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively, but can be treated like jar files. The same applies to "code-source" URLs on Oracle OC4J, provided that the path contains a jar separator.
url
- the URL to checkChecks whether the specified path is a Grails path.
path
- The path to checkChecks whether the specified path is a Grails path.
r
- The resource to checkCreate a URI instance for the given URL, replacing spaces with "%20" quotes first.
Furthermore, this method works on JDK 1.4 as well,
in contrast to the URL.toURI()
method.
url
- the URL to convert into a URI instanceCreate a URI instance for the given location String, replacing spaces with "%20" quotes first.
location
- the location String to convert into a URI instance