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 bsh.EvalError;
23  import bsh.Interpreter;
24  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.tools.plugin.PluginToolsRequest;
27  import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
28  import org.apache.maven.tools.plugin.extractor.ExtractionException;
29  import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
30  import org.codehaus.plexus.component.annotations.Component;
31  
32  import java.io.File;
33  import java.io.InputStreamReader;
34  import java.io.UnsupportedEncodingException;
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Set;
39  
40  /**
41   * Extracts Mojo descriptors from <a href="http://www.beanshell.org/">BeanShell</a> sources.
42   *
43   * @version $Id: BeanshellMojoDescriptorExtractor.html 995996 2016-08-26 22:31:42Z rfscholte $
44   */
45  @Component( role = MojoDescriptorExtractor.class, hint = "bsh" )
46  public class BeanshellMojoDescriptorExtractor
47      extends AbstractScriptedMojoDescriptorExtractor
48      implements MojoDescriptorExtractor
49  {
50      /**
51       * {@inheritDoc}
52       */
53      protected String getScriptFileExtension( PluginToolsRequest request )
54      {
55          return ".bsh";
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
62                                                             PluginToolsRequest request )
63          throws ExtractionException, InvalidPluginDescriptorException
64      {
65          List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>();
66  
67          for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
68          {
69              String basedir = entry.getKey();
70              Set<File> metadataFiles = entry.getValue();
71  
72              for ( File scriptFile : metadataFiles )
73              {
74                  String relativePath = null;
75  
76                  if ( basedir.endsWith( "/" ) )
77                  {
78                      basedir = basedir.substring( 0, basedir.length() - 2 );
79                  }
80  
81                  relativePath = scriptFile.getPath().substring( basedir.length() );
82  
83                  relativePath = relativePath.replace( '\\', '/' );
84  
85                  MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
86                  descriptors.add( mojoDescriptor );
87              }
88          }
89  
90          return descriptors;
91      }
92  
93      /**
94       * @param basedir  not null
95       * @param resource not null
96       * @param request  not null
97       * @return a new Mojo descriptor instance
98       * @throws InvalidPluginDescriptorException
99       *          if any
100      */
101     private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
102         throws InvalidPluginDescriptorException
103     {
104         MojoDescriptor mojoDescriptor = new MojoDescriptor();
105         mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
106 
107         mojoDescriptor.setLanguage( "bsh" );
108         mojoDescriptor.setComponentConfigurator( "bsh" );
109 
110         mojoDescriptor.setImplementation( resource );
111 
112         Interpreter interpreter = new Interpreter();
113 
114         try
115         {
116             interpreter.set( "file", new File( basedir, resource ) );
117 
118             interpreter.set( "mojoDescriptor", mojoDescriptor );
119 
120             interpreter.set( "encoding", "UTF-8" );
121 
122             interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), "UTF-8" ) );
123         }
124         catch ( EvalError evalError )
125         {
126             throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
127         }
128         catch ( UnsupportedEncodingException uee )
129         {
130             // should not occur...
131             throw new InvalidPluginDescriptorException( "Unsupported encoding while reading beanshell script", uee );
132         }
133 
134         return mojoDescriptor;
135     }
136 }