001package org.apache.maven.tools.plugin.scanner;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
023import org.apache.maven.plugin.descriptor.MojoDescriptor;
024import org.apache.maven.tools.plugin.PluginToolsRequest;
025import org.apache.maven.tools.plugin.extractor.ExtractionException;
026import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
027import org.codehaus.plexus.logging.AbstractLogEnabled;
028import org.codehaus.plexus.logging.Logger;
029import org.codehaus.plexus.logging.console.ConsoleLogger;
030import org.codehaus.plexus.util.StringUtils;
031
032import java.util.HashSet;
033import java.util.List;
034import java.util.Map;
035import java.util.Set;
036
037/**
038 * @author jdcasey
039 */
040public class DefaultMojoScanner
041    extends AbstractLogEnabled
042    implements MojoScanner
043{
044
045    private Map<String, MojoDescriptorExtractor> mojoDescriptorExtractors;
046
047    /**
048     * The names of the active extractors
049     */
050    private Set<String> activeExtractors;
051
052    /**
053     * Default constructor
054     *
055     * @param extractors not null
056     */
057    public DefaultMojoScanner( Map<String, MojoDescriptorExtractor> extractors )
058    {
059        this.mojoDescriptorExtractors = extractors;
060
061        this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "standalone-scanner-logger" ) );
062    }
063
064    /**
065     * Empty constructor
066     */
067    public DefaultMojoScanner()
068    {
069        // nop
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    public void populatePluginDescriptor( PluginToolsRequest request )
076        throws ExtractionException, InvalidPluginDescriptorException
077    {
078        Logger logger = getLogger();
079        Set<String> activeExtractorsInternal = getActiveExtractors();
080
081        logger.debug( "Using " + activeExtractorsInternal.size() + " mojo extractors." );
082
083        int numMojoDescriptors = 0;
084
085        for ( String extractorId : activeExtractorsInternal )
086        {
087            MojoDescriptorExtractor extractor = mojoDescriptorExtractors.get( extractorId );
088
089            if ( extractor == null )
090            {
091                throw new ExtractionException( "No mojo extractor with id: " + extractorId );
092            }
093
094            logger.debug( "Applying mojo extractor with id: " + extractorId );
095
096            List<MojoDescriptor> extractorDescriptors = extractor.execute( request );
097
098            logger.info( "Mojo extractor with id: " + extractorId + " found " + extractorDescriptors.size()
099                             + " mojo descriptors." );
100            numMojoDescriptors += extractorDescriptors.size();
101
102            for ( MojoDescriptor descriptor : extractorDescriptors )
103            {
104                logger.debug( "Adding mojo: " + descriptor + " to plugin descriptor." );
105
106                descriptor.setPluginDescriptor( request.getPluginDescriptor() );
107
108                request.getPluginDescriptor().addMojo( descriptor );
109            }
110        }
111
112        if ( numMojoDescriptors == 0 && !request.isSkipErrorNoDescriptorsFound() )
113        {
114            throw new InvalidPluginDescriptorException(
115                "No mojo definitions were found for plugin: " + request.getPluginDescriptor().getPluginLookupKey()
116                    + "." );
117        }
118    }
119
120    /**
121     * Gets the name of the active extractors.
122     *
123     * @return A Set containing the names of the active extractors.
124     */
125    protected Set<String> getActiveExtractors()
126    {
127        Set<String> result = activeExtractors;
128
129        if ( result == null )
130        {
131            result = new HashSet<String>( mojoDescriptorExtractors.keySet() );
132        }
133
134        return result;
135    }
136
137    public void setActiveExtractors( Set<String> extractors )
138    {
139        if ( extractors == null )
140        {
141            this.activeExtractors = null;
142        }
143        else
144        {
145            this.activeExtractors = new HashSet<String>();
146
147            for ( String extractor : extractors )
148            {
149                if ( StringUtils.isNotEmpty( extractor ) )
150                {
151                    this.activeExtractors.add( extractor );
152                }
153            }
154        }
155    }
156
157}