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