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    @Override
076    public void populatePluginDescriptor( PluginToolsRequest request )
077        throws ExtractionException, InvalidPluginDescriptorException
078    {
079        Logger logger = getLogger();
080        Set<String> activeExtractorsInternal = getActiveExtractors();
081
082        logger.debug( "Using " + activeExtractorsInternal.size() + " mojo extractors." );
083
084        int numMojoDescriptors = 0;
085
086        for ( String extractorId : activeExtractorsInternal )
087        {
088            MojoDescriptorExtractor extractor = mojoDescriptorExtractors.get( extractorId );
089
090            if ( extractor == null )
091            {
092                throw new ExtractionException( "No mojo extractor with '" + extractorId + "' id." );
093            }
094
095            logger.debug( "Applying " + extractorId + " mojo extractor" );
096
097            List<MojoDescriptor> extractorDescriptors = extractor.execute( request );
098
099            logger.info( extractorId + " mojo extractor found " + extractorDescriptors.size()
100                             + " mojo descriptor" + ( extractorDescriptors.size() > 1 ? "s" : "" ) + "." );
101            numMojoDescriptors += extractorDescriptors.size();
102
103            for ( MojoDescriptor descriptor : extractorDescriptors )
104            {
105                logger.debug( "Adding mojo: " + descriptor + " to plugin descriptor." );
106
107                descriptor.setPluginDescriptor( request.getPluginDescriptor() );
108
109                request.getPluginDescriptor().addMojo( descriptor );
110            }
111        }
112
113        if ( numMojoDescriptors == 0 && !request.isSkipErrorNoDescriptorsFound() )
114        {
115            throw new InvalidPluginDescriptorException(
116                "No mojo definitions were found for plugin: " + request.getPluginDescriptor().getPluginLookupKey()
117                    + "." );
118        }
119    }
120
121    /**
122     * Gets the name of the active extractors.
123     *
124     * @return A Set containing the names of the active extractors.
125     */
126    protected Set<String> getActiveExtractors()
127    {
128        Set<String> result = activeExtractors;
129
130        if ( result == null )
131        {
132            result = new HashSet<>( mojoDescriptorExtractors.keySet() );
133        }
134
135        return result;
136    }
137
138    @Override
139    public void setActiveExtractors( Set<String> extractors )
140    {
141        if ( extractors == null )
142        {
143            this.activeExtractors = null;
144        }
145        else
146        {
147            this.activeExtractors = new HashSet<>();
148
149            for ( String extractor : extractors )
150            {
151                if ( StringUtils.isNotEmpty( extractor ) )
152                {
153                    this.activeExtractors.add( extractor );
154                }
155            }
156        }
157    }
158
159}