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 groovy.lang.Binding;
23  import groovy.lang.GroovyShell;
24  
25  import java.io.File;
26  import java.io.PrintStream;
27  import java.io.PrintWriter;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.tools.ant.AntClassLoader;
33  import org.codehaus.groovy.control.CompilerConfiguration;
34  
35  /**
36   * Provides a facade to evaluate Groovy scripts.
37   * 
38   * @author Benjamin Bentmann
39   * @version $Id: GroovyScriptInterpreter.java 684549 2008-08-10 16:30:43Z bentmann $
40   */
41  class GroovyScriptInterpreter
42      implements ScriptInterpreter
43  {
44  
45      /**
46       * {@inheritDoc}
47       */
48      public Object evaluateScript( String script, List classPath, Map globalVariables, PrintStream scriptOutput )
49          throws ScriptEvaluationException
50      {
51          PrintStream origOut = System.out;
52          PrintStream origErr = System.err;
53  
54          try
55          {
56              CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
57  
58              if ( scriptOutput != null )
59              {
60                  System.setErr( scriptOutput );
61                  System.setOut( scriptOutput );
62                  config.setOutput( new PrintWriter( scriptOutput ) );
63              }
64  
65              ClassLoader loader = null;
66              if ( classPath != null && !classPath.isEmpty() )
67              {
68                  AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
69                  for ( Iterator it = classPath.iterator(); it.hasNext(); )
70                  {
71                      String path = (String) it.next();
72                      childFirstLoader.addPathComponent( new File( path ) );
73                  }
74                  loader = childFirstLoader;
75              }
76  
77              Binding binding = new Binding( globalVariables );
78  
79              GroovyShell interpreter = new GroovyShell( loader, binding, config );
80  
81              try
82              {
83                  return interpreter.evaluate( script );
84              }
85              catch ( Exception e )
86              {
87                  throw new ScriptEvaluationException( e );
88              }
89          }
90          finally
91          {
92              System.setErr( origErr );
93              System.setOut( origOut );
94          }
95      }
96  
97  }