View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.tools.plugin.extractor.beanshell;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.io.InputStreamReader;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import bsh.EvalError;
32  import bsh.Interpreter;
33  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
34  import org.apache.maven.plugin.descriptor.MojoDescriptor;
35  import org.apache.maven.tools.plugin.PluginToolsRequest;
36  import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
37  import org.apache.maven.tools.plugin.extractor.ExtractionException;
38  import org.apache.maven.tools.plugin.extractor.GroupKey;
39  
40  import static java.nio.charset.StandardCharsets.UTF_8;
41  
42  /**
43   * Extracts Mojo descriptors from <a href="http://www.beanshell.org/">BeanShell</a> sources.
44   *
45   * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
46   */
47  @Deprecated
48  @Named(BeanshellMojoDescriptorExtractor.NAME)
49  @Singleton
50  public class BeanshellMojoDescriptorExtractor extends AbstractScriptedMojoDescriptorExtractor {
51      public static final String NAME = "bsh";
52  
53      private static final GroupKey GROUP_KEY = new GroupKey("bsh", 100);
54  
55      @Override
56      public String getName() {
57          return NAME;
58      }
59  
60      @Override
61      public GroupKey getGroupKey() {
62          return GROUP_KEY;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      protected String getScriptFileExtension(PluginToolsRequest request) {
70          return ".bsh";
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected List<MojoDescriptor> extractMojoDescriptors(
78              Map<String, Set<File>> scriptFilesKeyedByBasedir, PluginToolsRequest request)
79              throws ExtractionException, InvalidPluginDescriptorException {
80          List<MojoDescriptor> descriptors = new ArrayList<>();
81  
82          for (Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet()) {
83              String basedir = entry.getKey();
84              Set<File> metadataFiles = entry.getValue();
85  
86              for (File scriptFile : metadataFiles) {
87                  String relativePath = null;
88  
89                  if (basedir.endsWith("/")) {
90                      basedir = basedir.substring(0, basedir.length() - 2);
91                  }
92  
93                  relativePath = scriptFile.getPath().substring(basedir.length());
94  
95                  relativePath = relativePath.replace('\\', '/');
96  
97                  MojoDescriptor mojoDescriptor = createMojoDescriptor(basedir, relativePath, request);
98                  descriptors.add(mojoDescriptor);
99              }
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 }