Coverage Report - org.apache.maven.tools.plugin.scanner.DefaultMojoScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMojoScanner
89 %
35/39
94 %
17/18
2,833
 
 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.DefaultPluginToolsRequest;
 27  
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 28  
 import org.apache.maven.tools.plugin.extractor.ExtractionException;
 29  
 import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
 30  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 31  
 import org.codehaus.plexus.logging.Logger;
 32  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 
 35  
 import java.util.HashSet;
 36  
 import java.util.List;
 37  
 import java.util.Map;
 38  
 import java.util.Set;
 39  
 
 40  
 /**
 41  
  * @author jdcasey
 42  
  */
 43  
 public class DefaultMojoScanner
 44  
     extends AbstractLogEnabled
 45  
     implements MojoScanner
 46  
 {
 47  
 
 48  
     private Map<String, MojoDescriptorExtractor> mojoDescriptorExtractors;
 49  
 
 50  
     /**
 51  
      * The names of the active extractors
 52  
      */
 53  
     private Set<String> activeExtractors;
 54  
 
 55  
     /**
 56  
      * Default constructor
 57  
      *
 58  
      * @param extractors not null
 59  
      */
 60  
     public DefaultMojoScanner( Map<String, MojoDescriptorExtractor> extractors )
 61  5
     {
 62  5
         this.mojoDescriptorExtractors = extractors;
 63  
 
 64  5
         this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "standalone-scanner-logger" ) );
 65  5
     }
 66  
 
 67  
     /**
 68  
      * Empty constructor
 69  
      */
 70  
     public DefaultMojoScanner()
 71  0
     {
 72  
         // nop
 73  0
     }
 74  
 
 75  
     /**
 76  
      * {@inheritDoc}
 77  
      */
 78  
     public void populatePluginDescriptor( MavenProject project, PluginDescriptor pluginDescriptor )
 79  
         throws ExtractionException, InvalidPluginDescriptorException
 80  
     {
 81  0
         populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
 82  0
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public void populatePluginDescriptor( PluginToolsRequest request )
 88  
         throws ExtractionException, InvalidPluginDescriptorException
 89  
     {
 90  5
         Logger logger = getLogger();
 91  5
         Set<String> activeExtractorsInternal = getActiveExtractors();
 92  
 
 93  5
         logger.debug( "Using " + activeExtractorsInternal.size() + " mojo extractors." );
 94  
 
 95  5
         int numMojoDescriptors = 0;
 96  
 
 97  5
         for ( String language : activeExtractorsInternal )
 98  
         {
 99  9
             MojoDescriptorExtractor extractor = mojoDescriptorExtractors.get( language );
 100  
 
 101  9
             if ( extractor == null )
 102  
             {
 103  1
                 throw new ExtractionException( "No mojo extractor for language: " + language );
 104  
             }
 105  
 
 106  8
             logger.info( "Applying mojo extractor for language: " + language );
 107  
 
 108  8
             List<MojoDescriptor> extractorDescriptors = extractor.execute( request );
 109  
 
 110  8
             logger.info( "Mojo extractor for language: " + language + " found " + extractorDescriptors.size()
 111  
                              + " mojo descriptors." );
 112  8
             numMojoDescriptors += extractorDescriptors.size();
 113  
 
 114  8
             for ( MojoDescriptor descriptor : extractorDescriptors )
 115  
             {
 116  8
                 logger.debug( "Adding mojo: " + descriptor + " to plugin descriptor." );
 117  
 
 118  8
                 descriptor.setPluginDescriptor( request.getPluginDescriptor() );
 119  
 
 120  8
                 request.getPluginDescriptor().addMojo( descriptor );
 121  
             }
 122  8
         }
 123  
 
 124  4
         if ( numMojoDescriptors == 0 && !request.isSkipErrorNoDescriptorsFound() )
 125  
         {
 126  1
             throw new InvalidPluginDescriptorException(
 127  
                 "No mojo definitions were found for plugin: " + request.getPluginDescriptor().getPluginLookupKey()
 128  
                     + "." );
 129  
         }
 130  3
     }
 131  
 
 132  
     /**
 133  
      * Gets the name of the active extractors.
 134  
      *
 135  
      * @return A Set containing the names of the active extractors.
 136  
      */
 137  
     protected Set<String> getActiveExtractors()
 138  
     {
 139  5
         Set<String> result = activeExtractors;
 140  
 
 141  5
         if ( result == null )
 142  
         {
 143  2
             result = new HashSet<String>( mojoDescriptorExtractors.keySet() );
 144  
         }
 145  
 
 146  5
         return result;
 147  
     }
 148  
 
 149  
     public void setActiveExtractors( Set<String> extractors )
 150  
     {
 151  4
         if ( extractors == null )
 152  
         {
 153  1
             this.activeExtractors = null;
 154  
         }
 155  
         else
 156  
         {
 157  3
             this.activeExtractors = new HashSet<String>();
 158  
 
 159  3
             for ( String extractor : extractors )
 160  
             {
 161  5
                 if ( StringUtils.isNotEmpty( extractor ) )
 162  
                 {
 163  3
                     this.activeExtractors.add( extractor );
 164  
                 }
 165  
             }
 166  
         }
 167  4
     }
 168  
 
 169  
 }