View Javadoc
1   /*
2    * Copyright (c) 2006-present the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.gmaven.plugin;
17  
18  import java.io.File;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.List;
22  
23  import javax.annotation.Nullable;
24  
25  import org.codehaus.gmaven.adapter.ClassSource;
26  import org.codehaus.gmaven.adapter.ResourceLoader;
27  
28  /**
29   * Mojo {@link ResourceLoader}.
30   *
31   * @since 2.0
32   */
33  public class MojoResourceLoader
34      extends BasicResourceLoader
35  {
36    private final ClassSource classSource;
37  
38    private final List<File> scriptpath;
39  
40    public MojoResourceLoader(final ClassLoader classLoader,
41                              final @Nullable ClassSource classSource,
42                              final @Nullable List<File> scriptpath)
43    {
44      super(classLoader);
45      this.classSource = classSource;
46      this.scriptpath = scriptpath;
47    }
48  
49    public MojoResourceLoader(final ClassLoader classLoader,
50                              final @Nullable List<File> scriptpath)
51    {
52      this(classLoader, null, scriptpath);
53    }
54  
55    @Nullable
56    protected URL resolve(final String className, final ClassLoader classLoader) throws MalformedURLException {
57      String name = resourceName(className);
58  
59      // First check the scriptpath
60      if (scriptpath != null) {
61        for (File path : scriptpath) {
62          File file = new File(path, name);
63          if (file.exists()) {
64            return file.toURI().toURL();
65          }
66        }
67      }
68  
69      // Then look for a resource in the classpath
70      URL url = classLoader.getResource(name);
71  
72      // FIXME: Sort out if this is required to function or if it should be removed
73      // Try w/o leading '/'... ???  Seems that when loading resources the '/' prefix messes things up?
74      if (url == null) {
75        if (name.startsWith("/")) {
76          String tmp = name.substring(1, name.length());
77          url = classLoader.getResource(tmp);
78        }
79      }
80  
81      if (url != null) {
82        return url;
83      }
84  
85      // Check for a class defined in a file next to the main script file
86      if (classSource != null) {
87        File script = classSource.getFile();
88        if (script != null) {
89          File file = new File(script.getParentFile(), name);
90  
91          if (file.exists()) {
92            return file.toURI().toURL();
93          }
94        }
95      }
96  
97      // else fallback to super impl
98      return super.resolve(className, classLoader);
99    }
100 }