Coverage Report - org.apache.maven.plugin.invoker.GroovyScriptInterpreter
 
Classes in this File Line Coverage Branch Coverage Complexity
GroovyScriptInterpreter
62%
15/24
25%
2/8
9
 
 1  
 package org.apache.maven.plugin.invoker;
 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  
 
 25  
 import java.io.File;
 26  
 import java.io.PrintStream;
 27  
 import java.io.PrintWriter;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 import org.apache.tools.ant.AntClassLoader;
 33  
 import org.codehaus.groovy.control.CompilerConfiguration;
 34  
 
 35  
 /**
 36  
  * Provides a facade to evaluate Groovy scripts.
 37  
  * 
 38  
  * @author Benjamin Bentmann
 39  
  * @version $Id: GroovyScriptInterpreter.java 737521 2009-01-25 15:22:26Z bentmann $
 40  
  */
 41  2
 class GroovyScriptInterpreter
 42  
     implements ScriptInterpreter
 43  
 {
 44  
 
 45  
     /**
 46  
      * {@inheritDoc}
 47  
      */
 48  
     public Object evaluateScript( String script, List classPath, Map globalVariables, PrintStream scriptOutput )
 49  
         throws ScriptEvaluationException
 50  
     {
 51  2
         PrintStream origOut = System.out;
 52  2
         PrintStream origErr = System.err;
 53  
 
 54  
         try
 55  
         {
 56  2
             CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
 57  
 
 58  2
             if ( scriptOutput != null )
 59  
             {
 60  2
                 System.setErr( scriptOutput );
 61  2
                 System.setOut( scriptOutput );
 62  2
                 config.setOutput( new PrintWriter( scriptOutput ) );
 63  
             }
 64  
 
 65  2
             ClassLoader loader = null;
 66  2
             if ( classPath != null && !classPath.isEmpty() )
 67  
             {
 68  0
                 AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
 69  0
                 for ( Iterator it = classPath.iterator(); it.hasNext(); )
 70  
                 {
 71  0
                     String path = (String) it.next();
 72  0
                     childFirstLoader.addPathComponent( new File( path ) );
 73  
                 }
 74  0
                 loader = childFirstLoader;
 75  
             }
 76  
 
 77  2
             Binding binding = new Binding( globalVariables );
 78  
 
 79  2
             GroovyShell interpreter = new GroovyShell( loader, binding, config );
 80  
 
 81  
             try
 82  
             {
 83  2
                 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  2
             System.setErr( origErr );
 97  2
             System.setOut( origOut );
 98  
         }
 99  
     }
 100  
 
 101  
 }