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