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