View Javadoc
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  /**
34   * Provides a facade to evaluate BeanShell scripts.
35   *
36   * @author Benjamin Bentmann
37   */
38  class BeanShellScriptInterpreter
39      implements ScriptInterpreter
40  {
41  
42      /** {@inheritDoc} */
43      @Override
44      public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables,
45                                    PrintStream scriptOutput )
46          throws ScriptEvaluationException
47      {
48          PrintStream origOut = System.out;
49          PrintStream origErr = System.err;
50  
51          try
52          {
53              Interpreter engine = new Interpreter();
54  
55              if ( scriptOutput != null )
56              {
57                  System.setErr( scriptOutput );
58                  System.setOut( scriptOutput );
59                  engine.setErr( scriptOutput );
60                  engine.setOut( scriptOutput );
61              }
62  
63              if ( !Capabilities.haveAccessibility() )
64              {
65                  try
66                  {
67                      Capabilities.setAccessibility( true );
68                  }
69                  catch ( Exception e )
70                  {
71                      if ( scriptOutput != null )
72                      {
73                          e.printStackTrace( scriptOutput );
74                      }
75                  }
76              }
77  
78              if ( classPath != null && !classPath.isEmpty() )
79              {
80                  for ( String path : classPath )
81                  {
82                      try
83                      {
84                          engine.getClassManager().addClassPath( new File( path ).toURI().toURL() );
85                      }
86                      catch ( IOException e )
87                      {
88                          throw new RuntimeException( "bad class path: " + path, e );
89                      }
90                  }
91              }
92  
93              if ( globalVariables != null )
94              {
95                  for ( Map.Entry<String, ?> entry : globalVariables.entrySet() )
96                  {
97                      try
98                      {
99                          engine.set( entry.getKey(), entry.getValue() );
100                     }
101                     catch ( EvalError e )
102                     {
103                         throw new RuntimeException( e );
104                     }
105                 }
106             }
107 
108             try
109             {
110                 return engine.eval( script );
111             }
112             catch ( TargetError e )
113             {
114                 throw new ScriptEvaluationException( e.getTarget() );
115             }
116             catch ( ThreadDeath e )
117             {
118                 throw e;
119             }
120             catch ( Throwable e )
121             {
122                 throw new ScriptEvaluationException( e );
123             }
124         }
125         finally
126         {
127             System.setErr( origErr );
128             System.setOut( origOut );
129         }
130     }
131 
132 }