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