org.tinyjee.maven.dim.spi
Class ResourceResolver

java.lang.Object
  extended by org.tinyjee.maven.dim.spi.ResourceResolver

public final class ResourceResolver
extends Object

A collection of utility methods used to resolve resources.

Version:
1.0
Author:
Juergen_Kellerer, 2010-09-06

Method Summary
static List<File> buildDefaultSearchPaths(File basePath, String... proposedPaths)
          Builds a list of paths to search in.
static List<URL> findAllSources(File basePath, String source)
          Finds the given source relative to the specified basePath.
static void findMatchingPaths(List<File> searchPaths, String source, List<URL> results)
          Finds the given source within the specified search paths and adds the results as file URLs to the results list.
static void findMatchingURLs(File basePath, String source, List<URL> results)
          Resolves the non-file URLs and module path expressions that match the source.
static File findSiteSourceDirectory(File baseDir, File proposedSiteDir)
          Finds the source directory for the project site.
static URL findSource(File basePath, String source)
          Finds the given source relative to the specified basePath.
static String getModulePath(String moduleName)
          Returns a module path for the given module name or 'null' if not known.
static Map<String,String> getModulePaths()
          Returns all currently defined module paths.
static boolean isAbsolute(String filePath)
          Returns true if the given filePath string is absolute.
static Class<?> resolveClass(File basePath, String className)
          Resolves the specified class.
static URLClassLoader resolveClassLoader(File basePath)
          Resolves a class loader for the given base path.
static void setModulePath(String groupId, String artifactId, File basePath)
          Sets additional search paths that are used to support source definitions like '[artifactId]:/some-path' or '[groupId:artifactId]:/some-path'.
static String toFilePath(String source)
          Converts the given source to a filePath if possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setModulePath

public static void setModulePath(String groupId,
                                 String artifactId,
                                 File basePath)
Sets additional search paths that are used to support source definitions like '[artifactId]:/some-path' or '[groupId:artifactId]:/some-path'.

This method is primarily called by InitializeMacroMojo and therefore path resolution depends on the Mojo being executed.

Parameters:
groupId - the group id of the module to set.
artifactId - the artifact id of the module to set.
basePath - the base path of the module.

getModulePath

public static String getModulePath(String moduleName)
Returns a module path for the given module name or 'null' if not known.

Parameters:
moduleName - the name of the module, either "groupId:artifactId" or "artifactId".
Returns:
a module path for the given module name or 'null' if not known.

getModulePaths

public static Map<String,String> getModulePaths()
Returns all currently defined module paths.

Returns:
all currently defined module paths.

buildDefaultSearchPaths

public static List<File> buildDefaultSearchPaths(File basePath,
                                                 String... proposedPaths)
Builds a list of paths to search in.

Parameters:
basePath - the base path to lookup the search paths against (if not absolute).
proposedPaths - an optional list of proposed paths (!!these paths are user input!!).
Returns:
an ordered list of paths to use for searching resources.

isAbsolute

public static boolean isAbsolute(String filePath)
Returns true if the given filePath string is absolute.

Parameters:
filePath - the filePath to check.
Returns:
true if the given filePath string is absolute.

toFilePath

public static String toFilePath(String source)
Converts the given source to a filePath if possible.

Parameters:
source - the source to convert.
Returns:
the filePath of the given source or 'null' if the source is an URL like 'ftp://' or 'http://'.

findMatchingPaths

public static void findMatchingPaths(List<File> searchPaths,
                                     String source,
                                     List<URL> results)
Finds the given source within the specified search paths and adds the results as file URLs to the results list.

Parameters:
searchPaths - the paths to search in.
source - the source path to search for.
results - a list to append the results to.

findMatchingURLs

public static void findMatchingURLs(File basePath,
                                    String source,
                                    List<URL> results)
Resolves the non-file URLs and module path expressions that match the source.

Parameters:
basePath - the basePath of the maven project (e.g. containing the target folder).
source - the source URI or module path expression.
results - the result list to write to.

findSource

public static URL findSource(File basePath,
                             String source)
Finds the given source relative to the specified basePath.

Note: Sources are resolved using absolute or relative file paths (the latter is using a list of paths to find the resource inside) OR sources can use any supported URI scheme including FTP and HTTP and lastly using the special scheme "CLASSPATH" in order to use the module's classpath to resolve the source.

Parameters:
basePath - the basePath of the maven project (e.g. containing the target folder).
source - the source path or URI.
Returns:
the resolved URL.
Throws:
IllegalArgumentException - In case of the source was not resolvable.

findAllSources

public static List<URL> findAllSources(File basePath,
                                       String source)
Finds the given source relative to the specified basePath.

Parameters:
basePath - the basePath of the maven project (e.g. containing the target folder).
source - the source path or URI.
Returns:
all URLs that contain this source.

resolveClass

public static Class<?> resolveClass(File basePath,
                                    String className)
Resolves the specified class.

Parameters:
basePath - the basePath of the maven project (e.g. containing the target folder).
className - the name of the class to resolve.
Returns:
The class.
Throws:
IllegalArgumentException - In case of the class was not found.

resolveClassLoader

public static URLClassLoader resolveClassLoader(File basePath)
Resolves a class loader for the given base path.

Parameters:
basePath - the basePath of the maven project (e.g. containing the target folder).
Returns:
a URLClassLoader that is capable of loading all classes defined inside the current module. (without transitive dependencies)

findSiteSourceDirectory

public static File findSiteSourceDirectory(File baseDir,
                                           File proposedSiteDir)
Finds the source directory for the project site.

Parameters:
baseDir - the base directory of the project.
proposedSiteDir - an optional proposal where the site directory may be.
Returns:
the source directory for the project site or 'null' if it wasn't found.