Package org.grails.io.support
Class GrailsResourceUtils
- java.lang.Object
-
- org.grails.io.support.GrailsResourceUtils
-
public class GrailsResourceUtils extends java.lang.ObjectUtility methods for resource handling / figuring out class names.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCLASS_EXTENSIONstatic java.lang.StringCLASSPATH_URL_PREFIXPseudo URL prefix for loading from the class path: "classpath:"static java.util.regex.Pattern[]COMPILER_ROOT_PATTERNSstatic java.lang.StringDOMAIN_DIR_PATHstatic java.util.regex.PatternDOMAIN_PATH_PATTERNstatic java.lang.StringFILE_URL_PREFIXURL prefix for loading from the file system: "file:"static java.lang.StringGRAILS_APP_DIRThe name of the Grails application directorystatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_EIGHTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_ELEVENTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_FIFTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_FIRST_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_FOURTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_NINTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_SECOND_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_SEVENTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_SIXTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_TENTH_MATCHstatic java.util.regex.PatternGRAILS_RESOURCE_PATTERN_THIRD_MATCHstatic java.util.regex.Pattern[]grailsAppResourcePatternsstatic java.lang.StringJAR_URL_SEPARATORstatic java.util.regex.Pattern[]patternsstatic java.lang.StringREGEX_FILE_SEPARATORstatic java.util.regex.PatternRESOURCE_PATH_PATTERNstatic java.util.regex.PatternSPRING_SCRIPTS_PATH_PATTERNstatic java.lang.StringURL_PROTOCOL_CODE_SOURCEURL protocol for an entry from an OC4J jar file: "code-source"static java.lang.StringURL_PROTOCOL_FILEURL protocol for a file in the file system: "file"static java.lang.StringURL_PROTOCOL_JARURL protocol for an entry from a jar file: "jar"static java.lang.StringURL_PROTOCOL_VFSURL protocol for a JBoss VFS resource: "vfs"static java.lang.StringURL_PROTOCOL_VFSZIPURL protocol for an entry from a JBoss jar file: "vfszip"static java.lang.StringURL_PROTOCOL_WSJARURL protocol for an entry from a WebSphere jar file: "wsjar"static java.lang.StringURL_PROTOCOL_ZIPURL protocol for an entry from a zip file: "zip"static java.lang.StringVIEWS_DIR_PATHThe path to the views directorystatic java.lang.StringVIEWS_DIR_PATH_NO_SLASHThe path to the views directory without a trailing slashstatic java.lang.StringWEB_APP_DIRThe name of the Web app dir within Grailsstatic java.lang.StringWEB_INFThe relative path to the WEB-INF directory
-
Constructor Summary
Constructors Constructor Description GrailsResourceUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringappendPiecesForUri(java.lang.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 uristatic java.lang.StringapplyRelativePath(java.lang.String path, java.lang.String relativePath)Apply the given relative path to the given path, assuming standard Java folder separation (i.e.static java.lang.StringclassPackageAsResourcePath(java.lang.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 ('/').static java.lang.StringcleanPath(java.lang.String path)Normalize the path by suppressing sequences like "path/.." and inner simple dots.static java.net.URLextractJarFileURL(java.net.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).static ResourcegetAppDir(Resource resource)static java.lang.StringgetArtefactDirectory(java.lang.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: domainstatic java.lang.StringgetClassName(java.lang.String path)Returns the class name for a Grails resource.static java.lang.StringgetClassName(Resource resource)Gets the class name of the specified Grails resourcestatic java.lang.StringgetClassNameForClassFile(java.lang.String rootDir, java.lang.String path)Returns the class name for a compiled class filestatic java.io.FilegetFile(java.net.URI resourceUri)Resolve the given resource URI to ajava.io.File, i.e.static java.io.FilegetFile(java.net.URI resourceUri, java.lang.String description)Resolve the given resource URI to ajava.io.File, i.e.static java.io.FilegetFile(java.net.URL resourceUrl, java.lang.String description)Resolve the given resource URL to ajava.io.File, i.e.static java.lang.StringgetFilename(java.lang.String path)Extract the filename from the given path, e.g.static java.lang.StringgetPathFromBaseDir(java.lang.String path)Gets the path relative to the project base directory.static java.lang.StringgetPathFromRoot(java.lang.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 Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy Output: com/mystartup/Book.groovystatic java.lang.StringgetRelativeInsideWebInf(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.static java.lang.StringgetStaticResourcePathForResource(Resource resource, java.lang.String contextPath)Retrieves the static resource path for the given Grails resource artifact (controller/taglib etc.)static ResourcegetViewsDir(Resource resource)static java.lang.ObjectinstantiateFromConfig(groovy.util.ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName)static java.lang.ObjectinstantiateFromFlatConfig(java.util.Map<java.lang.String,java.lang.Object> flatConfig, java.lang.String configKey, java.lang.String defaultClassName)static booleanisDomainClass(java.net.URL url)Checks whether the file referenced by the given url is a domain classstatic booleanisFileURL(java.net.URL url)Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".static booleanisGrailsPath(java.lang.String path)Checks whether the specified path is a Grails path.static booleanisGrailsResource(Resource r)Checks whether the specific resources is a Grails resource.static booleanisJarURL(java.net.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".static booleanisProjectSource(java.lang.String path)Checks whether the specified path is a Grails path.static booleanisProjectSource(Resource r)Checks whether the specified path is a Grails path.static java.net.URItoURI(java.lang.String location)Create a URI instance for the given location String, replacing spaces with "%20" quotes first.static java.net.URItoURI(java.net.URL url)Create a URI instance for the given URL, replacing spaces with "%20" quotes first.static voiduseCachesIfNecessary(java.net.URLConnection con)
-
-
-
Field Detail
-
CLASS_EXTENSION
public static final java.lang.String CLASS_EXTENSION
- See Also:
- Constant Field Values
-
JAR_URL_SEPARATOR
public static final java.lang.String JAR_URL_SEPARATOR
- See Also:
- Constant Field Values
-
CLASSPATH_URL_PREFIX
public static final java.lang.String CLASSPATH_URL_PREFIX
Pseudo URL prefix for loading from the class path: "classpath:"- See Also:
- Constant Field Values
-
FILE_URL_PREFIX
public static final java.lang.String FILE_URL_PREFIX
URL prefix for loading from the file system: "file:"- See Also:
- Constant Field Values
-
URL_PROTOCOL_FILE
public static final java.lang.String URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"- See Also:
- Constant Field Values
-
URL_PROTOCOL_JAR
public static final java.lang.String URL_PROTOCOL_JAR
URL protocol for an entry from a jar file: "jar"- See Also:
- Constant Field Values
-
URL_PROTOCOL_ZIP
public static final java.lang.String URL_PROTOCOL_ZIP
URL protocol for an entry from a zip file: "zip"- See Also:
- Constant Field Values
-
URL_PROTOCOL_VFSZIP
public static final java.lang.String URL_PROTOCOL_VFSZIP
URL protocol for an entry from a JBoss jar file: "vfszip"- See Also:
- Constant Field Values
-
URL_PROTOCOL_VFS
public static final java.lang.String URL_PROTOCOL_VFS
URL protocol for a JBoss VFS resource: "vfs"- See Also:
- Constant Field Values
-
URL_PROTOCOL_WSJAR
public static final java.lang.String URL_PROTOCOL_WSJAR
URL protocol for an entry from a WebSphere jar file: "wsjar"- See Also:
- Constant Field Values
-
URL_PROTOCOL_CODE_SOURCE
public static final java.lang.String URL_PROTOCOL_CODE_SOURCE
URL protocol for an entry from an OC4J jar file: "code-source"- See Also:
- Constant Field Values
-
WEB_INF
public static final java.lang.String WEB_INF
The relative path to the WEB-INF directory- See Also:
- Constant Field Values
-
GRAILS_APP_DIR
public static final java.lang.String GRAILS_APP_DIR
The name of the Grails application directory- See Also:
- Constant Field Values
-
WEB_APP_DIR
public static final java.lang.String WEB_APP_DIR
The name of the Web app dir within Grails- See Also:
- Constant Field Values
-
VIEWS_DIR_PATH
public static final java.lang.String VIEWS_DIR_PATH
The path to the views directory- See Also:
- Constant Field Values
-
VIEWS_DIR_PATH_NO_SLASH
public static final java.lang.String VIEWS_DIR_PATH_NO_SLASH
The path to the views directory without a trailing slash- See Also:
- Constant Field Values
-
DOMAIN_DIR_PATH
public static final java.lang.String DOMAIN_DIR_PATH
- See Also:
- Constant Field Values
-
REGEX_FILE_SEPARATOR
public static final java.lang.String REGEX_FILE_SEPARATOR
- See Also:
- Constant Field Values
-
DOMAIN_PATH_PATTERN
public static java.util.regex.Pattern DOMAIN_PATH_PATTERN
-
RESOURCE_PATH_PATTERN
public static java.util.regex.Pattern RESOURCE_PATH_PATTERN
-
SPRING_SCRIPTS_PATH_PATTERN
public static java.util.regex.Pattern SPRING_SCRIPTS_PATH_PATTERN
-
COMPILER_ROOT_PATTERNS
public static java.util.regex.Pattern[] COMPILER_ROOT_PATTERNS
-
GRAILS_RESOURCE_PATTERN_FIRST_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_FIRST_MATCH
-
GRAILS_RESOURCE_PATTERN_SECOND_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_SECOND_MATCH
-
GRAILS_RESOURCE_PATTERN_THIRD_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_THIRD_MATCH
-
GRAILS_RESOURCE_PATTERN_FOURTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_FOURTH_MATCH
-
GRAILS_RESOURCE_PATTERN_FIFTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_FIFTH_MATCH
-
GRAILS_RESOURCE_PATTERN_SIXTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_SIXTH_MATCH
-
GRAILS_RESOURCE_PATTERN_SEVENTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_SEVENTH_MATCH
-
GRAILS_RESOURCE_PATTERN_EIGHTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_EIGHTH_MATCH
-
GRAILS_RESOURCE_PATTERN_NINTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_NINTH_MATCH
-
GRAILS_RESOURCE_PATTERN_TENTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_TENTH_MATCH
-
GRAILS_RESOURCE_PATTERN_ELEVENTH_MATCH
public static final java.util.regex.Pattern GRAILS_RESOURCE_PATTERN_ELEVENTH_MATCH
-
patterns
public static final java.util.regex.Pattern[] patterns
-
grailsAppResourcePatterns
public static final java.util.regex.Pattern[] grailsAppResourcePatterns
-
-
Method Detail
-
isDomainClass
public static boolean isDomainClass(java.net.URL url)
Checks whether the file referenced by the given url is a domain class- Parameters:
url- The URL instance- Returns:
- true if it is a domain class
-
getFilename
public static java.lang.String getFilename(java.lang.String path)
Extract the filename from the given path, e.g. "mypath/myfile.txt" -> "myfile.txt".- Parameters:
path- the file path (may benull)- Returns:
- the extracted filename, or
nullif none
-
classPackageAsResourcePath
public static java.lang.String classPackageAsResourcePath(java.lang.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 ('/'). 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 toClassLoader.getResource(). For it to be fed toClass.getResourceinstead, a leading slash would also have to be prepended to the returned value.- Parameters:
clazz- the input class. Anullvalue or the default (empty) package will result in an empty string ("") being returned.- Returns:
- a path which represents the package name
- See Also:
ClassLoader.getResource(java.lang.String),Class.getResource(java.lang.String)
-
useCachesIfNecessary
public static void useCachesIfNecessary(java.net.URLConnection con)
-
getClassName
public static java.lang.String getClassName(Resource resource)
Gets the class name of the specified Grails resource- Parameters:
resource- The Spring Resource- Returns:
- The class name or null if the resource is not a Grails class
-
getClassName
public static java.lang.String getClassName(java.lang.String path)
Returns the class name for a Grails resource.- Parameters:
path- The path to check- Returns:
- The class name or null if it doesn't exist
-
getClassNameForClassFile
public static java.lang.String getClassNameForClassFile(java.lang.String rootDir, java.lang.String path)Returns the class name for a compiled class file- Parameters:
path- The path to check- Returns:
- The class name or null if it doesn't exist
-
getFile
public static java.io.File getFile(java.net.URL resourceUrl, java.lang.String description) throws java.io.FileNotFoundExceptionResolve the given resource URL to ajava.io.File, i.e. to a file in the file system.- Parameters:
resourceUrl- the resource URL to resolvedescription- a description of the original resource that the URL was created for (for example, a class path location)- Returns:
- a corresponding File object
- Throws:
java.io.FileNotFoundException- if the URL cannot be resolved to a file in the file system
-
isJarURL
public static boolean isJarURL(java.net.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"."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.
- Parameters:
url- the URL to check- Returns:
- whether the URL has been identified as a JAR URL
-
getFile
public static java.io.File getFile(java.net.URI resourceUri, java.lang.String description) throws java.io.FileNotFoundExceptionResolve the given resource URI to ajava.io.File, i.e. to a file in the file system.- Parameters:
resourceUri- the resource URI to resolvedescription- a description of the original resource that the URI was created for (for example, a class path location)- Returns:
- a corresponding File object
- Throws:
java.io.FileNotFoundException- if the URL cannot be resolved to a file in the file system
-
getFile
public static java.io.File getFile(java.net.URI resourceUri) throws java.io.FileNotFoundExceptionResolve the given resource URI to ajava.io.File, i.e. to a file in the file system.- Parameters:
resourceUri- the resource URI to resolve- Returns:
- a corresponding File object
- Throws:
java.io.FileNotFoundException- if the URL cannot be resolved to a file in the file system
-
toURI
public static java.net.URI toURI(java.net.URL url) throws java.net.URISyntaxExceptionCreate 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.- Parameters:
url- the URL to convert into a URI instance- Returns:
- the URI instance
- Throws:
java.net.URISyntaxException- if the URL wasn't a valid URI- See Also:
URL.toURI()
-
isFileURL
public static boolean isFileURL(java.net.URL url)
Determine whether the given URL points to a resource in the file system, that is, has protocol "file" or "vfs".- Parameters:
url- the URL to check- Returns:
- whether the URL has been identified as a file system URL
-
applyRelativePath
public static java.lang.String applyRelativePath(java.lang.String path, java.lang.String relativePath)Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/" separators).- Parameters:
path- the path to start from (usually a full file path)relativePath- the relative path to apply (relative to the full file path above)- Returns:
- the full file path that results from applying the relative path
-
cleanPath
public static java.lang.String cleanPath(java.lang.String path)
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.
- Parameters:
path- the original path- Returns:
- the normalized path
-
extractJarFileURL
public static java.net.URL extractJarFileURL(java.net.URL jarUrl) throws java.net.MalformedURLExceptionExtract 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).- Parameters:
jarUrl- the original URL- Returns:
- the URL for the actual jar file
- Throws:
java.net.MalformedURLException- if no valid jar file URL could be extracted
-
toURI
public static java.net.URI toURI(java.lang.String location) throws java.net.URISyntaxExceptionCreate a URI instance for the given location String, replacing spaces with "%20" quotes first.- Parameters:
location- the location String to convert into a URI instance- Returns:
- the URI instance
- Throws:
java.net.URISyntaxException- if the location wasn't a valid URI
-
isGrailsPath
public static boolean isGrailsPath(java.lang.String path)
Checks whether the specified path is a Grails path.- Parameters:
path- The path to check- Returns:
- true if it is a Grails path
-
isProjectSource
public static boolean isProjectSource(java.lang.String path)
Checks whether the specified path is a Grails path.- Parameters:
path- The path to check- Returns:
- true if it is a Grails path
-
isProjectSource
public static boolean isProjectSource(Resource r)
Checks whether the specified path is a Grails path.- Parameters:
r- The resoruce to check- Returns:
- true if it is a Grails path
-
isGrailsResource
public static boolean isGrailsResource(Resource r)
Checks whether the specific resources is a Grails resource. A Grails resource is a Groovy or Java class under the grails-app directory- Parameters:
r- The resource to check- Returns:
- True if it is a Grails resource
-
getRelativeInsideWebInf
public static java.lang.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.- Parameters:
resource- The Grails resource, which is a file inside the grails-app dir- Returns:
- The relative URL of the file inside the WEB-INF dir at deployment time or null if it cannot be established
-
getStaticResourcePathForResource
public static java.lang.String getStaticResourcePathForResource(Resource resource, java.lang.String contextPath)
Retrieves the static resource path for the given Grails resource artifact (controller/taglib etc.)- Parameters:
resource- The ResourcecontextPath- The additonal context path to prefix- Returns:
- The resource path
-
getPathFromRoot
public static java.lang.String getPathFromRoot(java.lang.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 Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy Output: com/mystartup/Book.groovy- Parameters:
path- The path to evaluate- Returns:
- The path relative to the root folder grails-app
-
getPathFromBaseDir
public static java.lang.String getPathFromBaseDir(java.lang.String path)
Gets the path relative to the project base directory. Input: /usr/joe/project/grails-app/conf/BootStrap.groovy Output: grails-app/conf/BootStrap.groovy- Parameters:
path- The path- Returns:
- The path relative to the base directory or null if it can't be established
-
getArtefactDirectory
public static java.lang.String getArtefactDirectory(java.lang.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- Parameters:
path- The path- Returns:
- The domain or null if not known
-
appendPiecesForUri
public static java.lang.String appendPiecesForUri(java.lang.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 uriInput: "" 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
- Parameters:
pieces- Strings to concatenate together into a uri- Returns:
- a uri
-
instantiateFromConfig
public static java.lang.Object instantiateFromConfig(groovy.util.ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException, java.lang.LinkageError- Throws:
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.ClassNotFoundExceptionjava.lang.LinkageError
-
instantiateFromFlatConfig
public static java.lang.Object instantiateFromFlatConfig(java.util.Map<java.lang.String,java.lang.Object> flatConfig, java.lang.String configKey, java.lang.String defaultClassName) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException, java.lang.LinkageError- Throws:
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.ClassNotFoundExceptionjava.lang.LinkageError
-
-