Coverage Report - org.apache.maven.shared.scriptinterpreter.GroovyScriptInterpreter
 
Classes in this File Line Coverage Branch Coverage Complexity
GroovyScriptInterpreter
65%
15/23
50%
4/8
10
 
 1  
 package org.apache.maven.shared.scriptinterpreter;
 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 groovy.lang.Binding;
 23  
 import groovy.lang.GroovyShell;
 24  
 import org.apache.tools.ant.AntClassLoader;
 25  
 import org.codehaus.groovy.control.CompilerConfiguration;
 26  
 import org.codehaus.plexus.component.annotations.Component;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.PrintStream;
 30  
 import java.io.PrintWriter;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * Provides a facade to evaluate Groovy scripts.
 36  
  * 
 37  
  * @author Benjamin Bentmann
 38  
  * @version $Id: GroovyScriptInterpreter.java 1361828 2012-07-15 22:30:27Z hboutemy $
 39  
  */
 40  
 @Component( role = ScriptInterpreter.class, hint = "groovy" )
 41  12
 public class GroovyScriptInterpreter
 42  
     implements ScriptInterpreter
 43  
 {
 44  
 
 45  
     /**
 46  
      * {@inheritDoc}
 47  
      */
 48  
     public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables,
 49  
                                   PrintStream scriptOutput )
 50  
         throws ScriptEvaluationException
 51  
     {
 52  8
         PrintStream origOut = System.out;
 53  8
         PrintStream origErr = System.err;
 54  
 
 55  
         try
 56  
         {
 57  8
             CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
 58  
 
 59  8
             if ( scriptOutput != null )
 60  
             {
 61  8
                 System.setErr( scriptOutput );
 62  8
                 System.setOut( scriptOutput );
 63  8
                 config.setOutput( new PrintWriter( scriptOutput ) );
 64  
             }
 65  
 
 66  8
             ClassLoader loader = null;
 67  8
             if ( classPath != null && !classPath.isEmpty() )
 68  
             {
 69  0
                 AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
 70  0
                 for ( String path : classPath )
 71  
                 {
 72  0
                     childFirstLoader.addPathComponent( new File( path ) );
 73  
                 }
 74  0
                 loader = childFirstLoader;
 75  
             }
 76  
 
 77  8
             Binding binding = new Binding( globalVariables );
 78  
 
 79  8
             GroovyShell interpreter = new GroovyShell( loader, binding, config );
 80  
 
 81  
             try
 82  
             {
 83  8
                 return interpreter.evaluate( script );
 84  
             }
 85  0
             catch ( ThreadDeath e )
 86  
             {
 87  0
                 throw e;
 88  
             }
 89  0
             catch ( Throwable e )
 90  
             {
 91  0
                 throw new ScriptEvaluationException( e );
 92  
             }
 93  
         }
 94  
         finally
 95  
         {
 96  8
             System.setErr( origErr );
 97  8
             System.setOut( origOut );
 98  
         }
 99  
     }
 100  
 
 101  
 }