Coverage Report - org.apache.maven.tools.plugin.extractor.beanshell.BeanshellMojoDescriptorExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanshellMojoDescriptorExtractor
0%
0/33
0%
0/6
3.333
 
 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.java 1338290 2012-05-14 16:54:01Z olamy $
 44  
  * @todo share constants
 45  
  * @todo add example usage tag that can be shown in the doco
 46  
  * @todo need to add validation directives so that systems embedding maven2 can
 47  
  * get validation directives to help users in IDEs.
 48  
  */
 49  
 @Component( role = MojoDescriptorExtractor.class, hint = "bsh" )
 50  0
 public class BeanshellMojoDescriptorExtractor
 51  
     extends AbstractScriptedMojoDescriptorExtractor
 52  
     implements MojoDescriptorExtractor
 53  
 {
 54  
     /**
 55  
      * {@inheritDoc}
 56  
      */
 57  
     protected String getScriptFileExtension( PluginToolsRequest request )
 58  
     {
 59  0
         return ".bsh";
 60  
     }
 61  
 
 62  
     /**
 63  
      * {@inheritDoc}
 64  
      */
 65  
     protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
 66  
                                                            PluginToolsRequest request )
 67  
         throws ExtractionException, InvalidPluginDescriptorException
 68  
     {
 69  0
         List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>();
 70  
 
 71  0
         for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
 72  
         {
 73  0
             String basedir = entry.getKey();
 74  0
             Set<File> metadataFiles = entry.getValue();
 75  
 
 76  0
             for ( File scriptFile : metadataFiles )
 77  
             {
 78  0
                 String relativePath = null;
 79  
 
 80  0
                 if ( basedir.endsWith( "/" ) )
 81  
                 {
 82  0
                     basedir = basedir.substring( 0, basedir.length() - 2 );
 83  
                 }
 84  
 
 85  0
                 relativePath = scriptFile.getPath().substring( basedir.length() );
 86  
 
 87  0
                 relativePath = relativePath.replace( '\\', '/' );
 88  
 
 89  0
                 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
 90  0
                 descriptors.add( mojoDescriptor );
 91  0
             }
 92  0
         }
 93  
 
 94  0
         return descriptors;
 95  
     }
 96  
 
 97  
     /**
 98  
      * @param basedir  not null
 99  
      * @param resource not null
 100  
      * @param request  not null
 101  
      * @return a new Mojo descriptor instance
 102  
      * @throws InvalidPluginDescriptorException
 103  
      *          if any
 104  
      */
 105  
     private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
 106  
         throws InvalidPluginDescriptorException
 107  
     {
 108  0
         MojoDescriptor mojoDescriptor = new MojoDescriptor();
 109  0
         mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
 110  
 
 111  0
         mojoDescriptor.setLanguage( "bsh" );
 112  0
         mojoDescriptor.setComponentConfigurator( "bsh" );
 113  
 
 114  0
         mojoDescriptor.setImplementation( resource );
 115  
 
 116  0
         Interpreter interpreter = new Interpreter();
 117  
 
 118  
         try
 119  
         {
 120  0
             interpreter.set( "file", new File( basedir, resource ) );
 121  
 
 122  0
             interpreter.set( "mojoDescriptor", mojoDescriptor );
 123  
 
 124  0
             interpreter.set( "encoding", "UTF-8" );
 125  
 
 126  0
             interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), "UTF-8" ) );
 127  
         }
 128  0
         catch ( EvalError evalError )
 129  
         {
 130  0
             throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
 131  
         }
 132  0
         catch ( UnsupportedEncodingException uee )
 133  
         {
 134  
             // should not occur...
 135  0
             throw new InvalidPluginDescriptorException( "Unsupported encoding while reading beanshell script", uee );
 136  0
         }
 137  
 
 138  0
         return mojoDescriptor;
 139  
     }
 140  
 }