View Javadoc

1   package org.apache.maven.tools.plugin.scanner;
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 org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
23  import org.apache.maven.plugin.descriptor.MojoDescriptor;
24  import org.apache.maven.plugin.descriptor.PluginDescriptor;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.tools.plugin.extractor.ExtractionException;
27  import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
28  import org.codehaus.plexus.logging.AbstractLogEnabled;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.logging.console.ConsoleLogger;
31  
32  import java.util.HashSet;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  /**
39   * @author jdcasey
40   */
41  public class DefaultMojoScanner
42      extends AbstractLogEnabled
43      implements MojoScanner
44  {
45  
46      private Map mojoDescriptorExtractors;
47  
48      /**
49       * The names of the active extractors
50       */
51      private Set/* <String> */activeExtractors;
52  
53      public DefaultMojoScanner( Map extractors )
54      {
55          this.mojoDescriptorExtractors = extractors;
56  
57          this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "standalone-scanner-logger" ) );
58      }
59  
60      public DefaultMojoScanner()
61      {
62      }
63  
64      public void populatePluginDescriptor( MavenProject project, PluginDescriptor pluginDescriptor )
65          throws ExtractionException, InvalidPluginDescriptorException
66      {
67          Logger logger = getLogger();
68          Set activeExtractors = getActiveExtractors();
69  
70          logger.info( "Using " + activeExtractors.size() + " extractors." );
71  
72          for ( Iterator it = activeExtractors.iterator(); it.hasNext(); )
73          {
74              String language = (String) it.next();
75              MojoDescriptorExtractor extractor = (MojoDescriptorExtractor) mojoDescriptorExtractors.get( language );
76  
77              if ( extractor == null )
78              {
79                  throw new ExtractionException( "No extractor for language: " + language );
80              }
81  
82              logger.info( "Applying extractor for language: " + language );
83  
84              List extractorDescriptors = extractor.execute( project, pluginDescriptor );
85  
86              logger.info( "Extractor for language: " + language + " found " + extractorDescriptors.size() +
87                  " mojo descriptors." );
88  
89              for ( Iterator descriptorIt = extractorDescriptors.iterator(); descriptorIt.hasNext(); )
90              {
91                  MojoDescriptor descriptor = (MojoDescriptor) descriptorIt.next();
92  
93                  logger.debug( "Adding mojo: " + descriptor + " to plugin descriptor." );
94  
95                  descriptor.setPluginDescriptor( pluginDescriptor );
96  
97                  pluginDescriptor.addMojo( descriptor );
98              }
99          }
100     }
101 
102     /**
103      * Gets the name of the active extractors.
104      *
105      * @return A Set containing the names of the active extractors.
106      */
107     protected Set/* <String> */getActiveExtractors()
108     {
109         Set/* <String> */result = activeExtractors;
110 
111         if ( result == null )
112         {
113             result = new HashSet/* <String> */( mojoDescriptorExtractors.keySet() );
114         }
115 
116         return result;
117     }
118 
119     /**
120      * @see org.apache.maven.tools.plugin.scanner.ExtendedMojoScanner#setActiveExtractors(java.util.Set)
121      */
122     public void setActiveExtractors( Set/* <String> */extractors )
123     {
124         if ( extractors == null )
125         {
126             this.activeExtractors = null;
127         }
128         else
129         {
130             this.activeExtractors = new HashSet/* <String> */();
131 
132             for ( Iterator i = extractors.iterator(); i.hasNext(); )
133             {
134                 String extractor = (String) i.next();
135 
136                 if ( extractor != null && extractor.length() > 0 )
137                 {
138                     this.activeExtractors.add( extractor );
139                 }
140             }
141         }
142     }
143 }