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