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