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  @Component( role = ResourceManager.class, hint = "license" )
34  public class LicenseResourceManager
35      extends DefaultResourceManager
36  {
37  
38      @Requirement( role = ResourceLoader.class )
39      private Map<String, ResourceLoader> resourceLoaders;
40  
41      @Override
42      public void addSearchPath( String id, String path )
43      {
44          ResourceLoader loader = (ResourceLoader) resourceLoaders.get( id );
45  
46          if ( loader == null )
47          {
48              throw new IllegalArgumentException( "unknown resource loader: " + id );
49          }
50  
51          loader.addSearchPath( path );
52      }
53  
54      @Override
55      public PlexusResource getResource( String name )
56          throws ResourceNotFoundException
57      {
58          for ( ResourceLoader resourceLoader : resourceLoaders.values() )
59          {
60              if ( resourceLoader instanceof ThreadContextClasspathResourceLoader
61                  && !"config/maven-header.txt".equals( name ) )
62              {
63                  // MCHECKSTYLE-219: Don't load the license from the plugin
64                  // classloader, only allow config/maven-header.txt
65                  continue;
66              }
67  
68              try
69              {
70                  PlexusResource resource = resourceLoader.getResource( name );
71  
72                  getLogger().debug( "The resource " + "'" + name + "'" + " was found as " + resource.getName() + "." );
73  
74                  return resource;
75              }
76              catch ( ResourceNotFoundException e )
77              {
78                  getLogger().debug( "The resource " + "'" + name + "'" + " was not found with resourceLoader "
79                                         + resourceLoader.getClass().getName() + "." );
80              }
81          }
82  
83          throw new ResourceNotFoundException( name );
84      }
85  }