View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.checkstyle.resource;
20  
21  import java.util.Map;
22  
23  import org.codehaus.plexus.component.annotations.Component;
24  import org.codehaus.plexus.component.annotations.Requirement;
25  import org.codehaus.plexus.resource.DefaultResourceManager;
26  import org.codehaus.plexus.resource.PlexusResource;
27  import org.codehaus.plexus.resource.ResourceManager;
28  import org.codehaus.plexus.resource.loader.ResourceLoader;
29  import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
30  import org.codehaus.plexus.resource.loader.ThreadContextClasspathResourceLoader;
31  
32  /**
33   * License resource manager, to avoid defaulting license to maven-checkstyle-plugin's own license.
34   *
35   * @since 2.12
36   */
37  @Component(role = ResourceManager.class, hint = "license", instantiationStrategy = "per-lookup")
38  public class LicenseResourceManager extends DefaultResourceManager {
39  
40      @Requirement(role = ResourceLoader.class)
41      private Map<String, ResourceLoader> resourceLoaders;
42  
43      @Override
44      public void addSearchPath(String id, String path) {
45          ResourceLoader loader = resourceLoaders.get(id);
46  
47          if (loader == null) {
48              throw new IllegalArgumentException("unknown resource loader: " + id);
49          }
50  
51          loader.addSearchPath(path);
52      }
53  
54      @Override
55      public PlexusResource getResource(String name) throws ResourceNotFoundException {
56          for (ResourceLoader resourceLoader : resourceLoaders.values()) {
57              if (resourceLoader instanceof ThreadContextClasspathResourceLoader
58                      && !"config/maven-header.txt".equals(name)) {
59                  // MCHECKSTYLE-219: Don't load the license from the plugin
60                  // classloader, only allow config/maven-header.txt
61                  continue;
62              }
63  
64              try {
65                  PlexusResource resource = resourceLoader.getResource(name);
66  
67                  getLogger().debug("The resource '" + name + "' was found as " + resource.getName() + ".");
68  
69                  return resource;
70              } catch (ResourceNotFoundException e) {
71                  getLogger()
72                          .debug("The resource '" + name + "' was not found with resourceLoader "
73                                  + resourceLoader.getClass().getName() + ".");
74              }
75          }
76  
77          throw new ResourceNotFoundException(name);
78      }
79  }