View Javadoc

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  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          PrintStream origOut = System.out;
51          PrintStream origErr = System.err;
52  
53          try
54          {
55              Interpreter engine = new Interpreter();
56  
57              if ( scriptOutput != null )
58              {
59                  System.setErr( scriptOutput );
60                  System.setOut( scriptOutput );
61                  engine.setErr( scriptOutput );
62                  engine.setOut( scriptOutput );
63              }
64  
65              if ( !Capabilities.haveAccessibility() )
66              {
67                  try
68                  {
69                      Capabilities.setAccessibility( true );
70                  }
71                  catch ( Exception e )
72                  {
73                      if ( scriptOutput != null )
74                      {
75                          e.printStackTrace( scriptOutput );
76                      }
77                  }
78              }
79  
80              if ( classPath != null && !classPath.isEmpty() )
81              {
82                  for ( Iterator it = classPath.iterator(); it.hasNext(); )
83                  {
84                      String path = (String) it.next();
85                      try
86                      {
87                          engine.getClassManager().addClassPath( new File( path ).toURI().toURL() );
88                      }
89                      catch ( IOException e )
90                      {
91                          throw new RuntimeException( "bad class path: " + path, e );
92                      }
93                  }
94              }
95  
96              if ( globalVariables != null )
97              {
98                  for ( Iterator it = globalVariables.keySet().iterator(); it.hasNext(); )
99                  {
100                     String variable = (String) it.next();
101                     Object value = globalVariables.get( variable );
102                     try
103                     {
104                         engine.set( variable, value );
105                     }
106                     catch ( EvalError e )
107                     {
108                         throw new RuntimeException( e );
109                     }
110                 }
111             }
112 
113             try
114             {
115                 return engine.eval( script );
116             }
117             catch ( TargetError e )
118             {
119                 throw new ScriptEvaluationException( e.getTarget() );
120             }
121             catch ( ThreadDeath e )
122             {
123                 throw e;
124             }
125             catch ( Throwable e )
126             {
127                 throw new ScriptEvaluationException( e );
128             }
129         }
130         finally
131         {
132             System.setErr( origErr );
133             System.setOut( origOut );
134         }
135     }
136 
137 }