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 */
044@Component( role = MojoDescriptorExtractor.class, hint = "bsh" )
045public class BeanshellMojoDescriptorExtractor
046    extends AbstractScriptedMojoDescriptorExtractor
047    implements MojoDescriptorExtractor
048{
049    /**
050     * {@inheritDoc}
051     */
052    protected String getScriptFileExtension( PluginToolsRequest request )
053    {
054        return ".bsh";
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
061                                                           PluginToolsRequest request )
062        throws ExtractionException, InvalidPluginDescriptorException
063    {
064        List<MojoDescriptor> descriptors = new ArrayList<>();
065
066        for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
067        {
068            String basedir = entry.getKey();
069            Set<File> metadataFiles = entry.getValue();
070
071            for ( File scriptFile : metadataFiles )
072            {
073                String relativePath = null;
074
075                if ( basedir.endsWith( "/" ) )
076                {
077                    basedir = basedir.substring( 0, basedir.length() - 2 );
078                }
079
080                relativePath = scriptFile.getPath().substring( basedir.length() );
081
082                relativePath = relativePath.replace( '\\', '/' );
083
084                MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
085                descriptors.add( mojoDescriptor );
086            }
087        }
088
089        return descriptors;
090    }
091
092    /**
093     * @param basedir  not null
094     * @param resource not null
095     * @param request  not null
096     * @return a new Mojo descriptor instance
097     * @throws InvalidPluginDescriptorException
098     *          if any
099     */
100    private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
101        throws InvalidPluginDescriptorException
102    {
103        MojoDescriptor mojoDescriptor = new MojoDescriptor();
104        mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
105
106        mojoDescriptor.setLanguage( "bsh" );
107        mojoDescriptor.setComponentConfigurator( "bsh" );
108
109        mojoDescriptor.setImplementation( resource );
110
111        Interpreter interpreter = new Interpreter();
112
113        try
114        {
115            interpreter.set( "file", new File( basedir, resource ) );
116
117            interpreter.set( "mojoDescriptor", mojoDescriptor );
118
119            interpreter.set( "encoding", "UTF-8" );
120
121            interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), "UTF-8" ) );
122        }
123        catch ( EvalError evalError )
124        {
125            throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
126        }
127        catch ( UnsupportedEncodingException uee )
128        {
129            // should not occur...
130            throw new InvalidPluginDescriptorException( "Unsupported encoding while reading beanshell script", uee );
131        }
132
133        return mojoDescriptor;
134    }
135}