001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin.extractor.beanshell;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.io.File;
025import java.io.InputStreamReader;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.Map;
029import java.util.Set;
030
031import bsh.EvalError;
032import bsh.Interpreter;
033import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
034import org.apache.maven.plugin.descriptor.MojoDescriptor;
035import org.apache.maven.tools.plugin.PluginToolsRequest;
036import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
037import org.apache.maven.tools.plugin.extractor.ExtractionException;
038import org.apache.maven.tools.plugin.extractor.GroupKey;
039
040import static java.nio.charset.StandardCharsets.UTF_8;
041
042/**
043 * Extracts Mojo descriptors from <a href="http://www.beanshell.org/">BeanShell</a> sources.
044 *
045 * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
046 */
047@Deprecated
048@Named(BeanshellMojoDescriptorExtractor.NAME)
049@Singleton
050public class BeanshellMojoDescriptorExtractor extends AbstractScriptedMojoDescriptorExtractor {
051    public static final String NAME = "bsh";
052
053    private static final GroupKey GROUP_KEY = new GroupKey("bsh", 100);
054
055    @Override
056    public String getName() {
057        return NAME;
058    }
059
060    @Override
061    public GroupKey getGroupKey() {
062        return GROUP_KEY;
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    protected String getScriptFileExtension(PluginToolsRequest request) {
070        return ".bsh";
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    protected List<MojoDescriptor> extractMojoDescriptors(
078            Map<String, Set<File>> scriptFilesKeyedByBasedir, PluginToolsRequest request)
079            throws ExtractionException, InvalidPluginDescriptorException {
080        List<MojoDescriptor> descriptors = new ArrayList<>();
081
082        for (Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet()) {
083            String basedir = entry.getKey();
084            Set<File> metadataFiles = entry.getValue();
085
086            for (File scriptFile : metadataFiles) {
087                String relativePath = null;
088
089                if (basedir.endsWith("/")) {
090                    basedir = basedir.substring(0, basedir.length() - 2);
091                }
092
093                relativePath = scriptFile.getPath().substring(basedir.length());
094
095                relativePath = relativePath.replace('\\', '/');
096
097                MojoDescriptor mojoDescriptor = createMojoDescriptor(basedir, relativePath, request);
098                descriptors.add(mojoDescriptor);
099            }
100        }
101
102        return descriptors;
103    }
104
105    /**
106     * @param basedir  not null
107     * @param resource not null
108     * @param request  not null
109     * @return a new Mojo descriptor instance
110     * @throws InvalidPluginDescriptorException
111     *          if any
112     */
113    private MojoDescriptor createMojoDescriptor(String basedir, String resource, PluginToolsRequest request)
114            throws InvalidPluginDescriptorException {
115        MojoDescriptor mojoDescriptor = new MojoDescriptor();
116        mojoDescriptor.setPluginDescriptor(request.getPluginDescriptor());
117
118        mojoDescriptor.setLanguage("bsh");
119        mojoDescriptor.setComponentConfigurator("bsh");
120
121        mojoDescriptor.setImplementation(resource);
122
123        Interpreter interpreter = new Interpreter();
124
125        try {
126            interpreter.set("file", new File(basedir, resource));
127
128            interpreter.set("mojoDescriptor", mojoDescriptor);
129
130            interpreter.set("encoding", "UTF-8");
131
132            interpreter.eval(new InputStreamReader(getClass().getResourceAsStream("/extractor.bsh"), UTF_8));
133        } catch (EvalError evalError) {
134            throw new InvalidPluginDescriptorException("Error scanning beanshell script", evalError);
135        }
136
137        // FIXME: convert javadocs
138        return mojoDescriptor;
139    }
140}