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