001package org.apache.maven.tools.plugin.extractor.beanshell;
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 static java.nio.charset.StandardCharsets.UTF_8;
023
024import bsh.EvalError;
025import bsh.Interpreter;
026import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
027import org.apache.maven.plugin.descriptor.MojoDescriptor;
028import org.apache.maven.tools.plugin.PluginToolsRequest;
029import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
030import org.apache.maven.tools.plugin.extractor.ExtractionException;
031import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
032import org.codehaus.plexus.component.annotations.Component;
033
034import java.io.File;
035import java.io.InputStreamReader;
036import java.util.ArrayList;
037import java.util.List;
038import java.util.Map;
039import java.util.Set;
040
041/**
042 * Extracts Mojo descriptors from <a href="http://www.beanshell.org/">BeanShell</a> sources.
043 *
044 */
045@Component( role = MojoDescriptorExtractor.class, hint = "bsh" )
046public class BeanshellMojoDescriptorExtractor
047    extends AbstractScriptedMojoDescriptorExtractor
048    implements MojoDescriptorExtractor
049{
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    protected String getScriptFileExtension( PluginToolsRequest request )
055    {
056        return ".bsh";
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
064                                                           PluginToolsRequest request )
065        throws ExtractionException, InvalidPluginDescriptorException
066    {
067        List<MojoDescriptor> descriptors = new ArrayList<>();
068
069        for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
070        {
071            String basedir = entry.getKey();
072            Set<File> metadataFiles = entry.getValue();
073
074            for ( File scriptFile : metadataFiles )
075            {
076                String relativePath = null;
077
078                if ( basedir.endsWith( "/" ) )
079                {
080                    basedir = basedir.substring( 0, basedir.length() - 2 );
081                }
082
083                relativePath = scriptFile.getPath().substring( basedir.length() );
084
085                relativePath = relativePath.replace( '\\', '/' );
086
087                MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
088                descriptors.add( mojoDescriptor );
089            }
090        }
091
092        return descriptors;
093    }
094
095    /**
096     * @param basedir  not null
097     * @param resource not null
098     * @param request  not null
099     * @return a new Mojo descriptor instance
100     * @throws InvalidPluginDescriptorException
101     *          if any
102     */
103    private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
104        throws InvalidPluginDescriptorException
105    {
106        MojoDescriptor mojoDescriptor = new MojoDescriptor();
107        mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
108
109        mojoDescriptor.setLanguage( "bsh" );
110        mojoDescriptor.setComponentConfigurator( "bsh" );
111
112        mojoDescriptor.setImplementation( resource );
113
114        Interpreter interpreter = new Interpreter();
115
116        try
117        {
118            interpreter.set( "file", new File( basedir, resource ) );
119
120            interpreter.set( "mojoDescriptor", mojoDescriptor );
121
122            interpreter.set( "encoding", "UTF-8" );
123
124            interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), UTF_8 ) );
125        }
126        catch ( EvalError evalError )
127        {
128            throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
129        }
130
131        return mojoDescriptor;
132    }
133}