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 javax.inject.Inject;
22  
23  import java.util.Map;
24  
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.component.annotations.Requirement;
27  import org.codehaus.plexus.resource.DefaultResourceManager;
28  import org.codehaus.plexus.resource.PlexusResource;
29  import org.codehaus.plexus.resource.ResourceManager;
30  import org.codehaus.plexus.resource.loader.ResourceLoader;
31  import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
32  import org.codehaus.plexus.resource.loader.ThreadContextClasspathResourceLoader;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * License resource manager, to avoid defaulting license to maven-checkstyle-plugin's own license.
38   *
39   * @since 2.12
40   */
41  @Component(role = ResourceManager.class, hint = "license", instantiationStrategy = "per-lookup")
42  public class LicenseResourceManager extends DefaultResourceManager {
43  
44      private static final Logger LOGGER = LoggerFactory.getLogger(LicenseResourceManager.class);
45  
46      @Requirement(role = ResourceLoader.class)
47      private Map<String, ResourceLoader> resourceLoaders;
48  
49      @Inject
50      public LicenseResourceManager(Map<String, ResourceLoader> resourceLoaders) {
51          super(resourceLoaders);
52      }
53  
54      @Override
55      public void addSearchPath(String id, String path) {
56          ResourceLoader loader = resourceLoaders.get(id);
57  
58          if (loader == null) {
59              throw new IllegalArgumentException("unknown resource loader: " + id);
60          }
61  
62          loader.addSearchPath(path);
63      }
64  
65      @Override
66      public PlexusResource getResource(String name) throws ResourceNotFoundException {
67          for (ResourceLoader resourceLoader : resourceLoaders.values()) {
68              if (resourceLoader instanceof ThreadContextClasspathResourceLoader
69                      && !"config/maven-header.txt".equals(name)) {
70                  // MCHECKSTYLE-219: Don't load the license from the plugin
71                  // classloader, only allow config/maven-header.txt
72                  continue;
73              }
74  
75              try {
76                  PlexusResource resource = resourceLoader.getResource(name);
77  
78                  LOGGER.debug("The resource '" + name + "' was found as " + resource.getName() + ".");
79  
80                  return resource;
81              } catch (ResourceNotFoundException e) {
82                  LOGGER.debug("The resource '" + name + "' was not found with resourceLoader "
83                          + resourceLoader.getClass().getName() + ".");
84              }
85          }
86  
87          throw new ResourceNotFoundException(name);
88      }
89  }