View Javadoc
1   package com.github.searls.jasmine.mojo;
2   
3   import org.apache.maven.plugin.MojoExecutionException;
4   import org.apache.maven.project.MavenProject;
5   import org.codehaus.plexus.resource.ResourceManager;
6   import org.codehaus.plexus.resource.loader.FileResourceLoader;
7   
8   import java.io.File;
9   
10  public class ResourceRetriever {
11  
12    private final ResourceManager locator;
13  
14    private static final String ERROR_FILE_DNE = "Invalid value for parameter '%s'. File does not exist: %s";
15  
16    public ResourceRetriever(final ResourceManager locator) {
17      this.locator = locator;
18    }
19  
20    public File getResourceAsFile(final String parameter, final String resourceLocation, final MavenProject mavenProject) throws MojoExecutionException {
21      File file = null;
22  
23      if (resourceLocation != null) {
24        locator.addSearchPath("url", "");
25        locator.addSearchPath(FileResourceLoader.ID, mavenProject.getFile().getParentFile().getAbsolutePath());
26  
27        ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
28        try {
29          Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
30          try {
31            file = locator.getResourceAsFile(resourceLocation);
32          } catch (Exception e) {
33            throw new MojoExecutionException(String.format(ERROR_FILE_DNE, parameter, resourceLocation));
34          }
35        } finally {
36          Thread.currentThread().setContextClassLoader(origLoader);
37        }
38      }
39      return file;
40    }
41  }