Coverage Report - org.apache.maven.plugin.invoker.BeanShellScriptInterpreter
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanShellScriptInterpreter
61%
19/31
50%
6/12
15
 
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.io.PrintStream;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 import bsh.EvalError;
 30  
 import bsh.Interpreter;
 31  
 import bsh.TargetError;
 32  
 
 33  
 /**
 34  
  * Provides a facade to evaluate BeanShell scripts.
 35  
  * 
 36  
  * @author Benjamin Bentmann
 37  
  * @version $Id: BeanShellScriptInterpreter.java 684549 2008-08-10 16:30:43Z bentmann $
 38  
  */
 39  2
 class BeanShellScriptInterpreter
 40  
     implements ScriptInterpreter
 41  
 {
 42  
 
 43  
     /**
 44  
      * {@inheritDoc}
 45  
      */
 46  
     public Object evaluateScript( String script, List classPath, Map globalVariables, PrintStream scriptOutput )
 47  
         throws ScriptEvaluationException
 48  
     {
 49  2
         PrintStream origOut = System.out;
 50  2
         PrintStream origErr = System.err;
 51  
 
 52  
         try
 53  
         {
 54  2
             Interpreter engine = new Interpreter();
 55  
 
 56  2
             if ( scriptOutput != null )
 57  
             {
 58  2
                 System.setErr( scriptOutput );
 59  2
                 System.setOut( scriptOutput );
 60  2
                 engine.setErr( scriptOutput );
 61  2
                 engine.setOut( scriptOutput );
 62  
             }
 63  
 
 64  2
             if ( classPath != null && !classPath.isEmpty() )
 65  
             {
 66  0
                 for ( Iterator it = classPath.iterator(); it.hasNext(); )
 67  
                 {
 68  0
                     String path = (String) it.next();
 69  
                     try
 70  
                     {
 71  0
                         engine.getClassManager().addClassPath( new File( path ).toURI().toURL() );
 72  
                     }
 73  0
                     catch ( IOException e )
 74  
                     {
 75  0
                         throw new RuntimeException( "bad class path: " + path, e );
 76  0
                     }
 77  
                 }
 78  
             }
 79  
 
 80  2
             if ( globalVariables != null )
 81  
             {
 82  1
                 for ( Iterator it = globalVariables.keySet().iterator(); it.hasNext(); )
 83  
                 {
 84  1
                     String variable = (String) it.next();
 85  1
                     Object value = globalVariables.get( variable );
 86  
                     try
 87  
                     {
 88  1
                         engine.set( variable, value );
 89  
                     }
 90  0
                     catch ( EvalError e )
 91  
                     {
 92  0
                         throw new RuntimeException( e );
 93  2
                     }
 94  
                 }
 95  
             }
 96  
 
 97  
             try
 98  
             {
 99  2
                 return engine.eval( script );
 100  
             }
 101  0
             catch ( TargetError e )
 102  
             {
 103  0
                 throw new ScriptEvaluationException( e.getTarget() );
 104  
             }
 105  0
             catch ( Exception e )
 106  
             {
 107  0
                 throw new ScriptEvaluationException( e );
 108  
             }
 109  
         }
 110  
         finally
 111  
         {
 112  2
             System.setErr( origErr );
 113  2
             System.setOut( origOut );
 114  
         }
 115  
     }
 116  
 
 117  
 }