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