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 groovy.lang.Binding;
23  import groovy.lang.GroovyShell;
24  import org.codehaus.groovy.control.CompilerConfiguration;
25  import org.codehaus.groovy.runtime.powerassert.PowerAssertionError;
26  import org.codehaus.groovy.tools.RootLoader;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.PrintStream;
31  import java.net.URL;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * Provides a facade to evaluate Groovy scripts.
37   * 
38   * @author Benjamin Bentmann
39   */
40  class GroovyScriptInterpreter
41      implements ScriptInterpreter
42  {
43  
44      /** {@inheritDoc} */
45      @Override
46      public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables,
47                                    PrintStream scriptOutput )
48          throws ScriptEvaluationException
49      {
50          PrintStream origOut = System.out;
51          PrintStream origErr = System.err;
52  
53          try ( RootLoader childFirstLoader = new RootLoader( new URL[] {}, getClass().getClassLoader() ) )
54          {
55  
56              if ( scriptOutput != null )
57              {
58                  System.setErr( scriptOutput );
59                  System.setOut( scriptOutput );
60              }
61  
62              if ( classPath != null && !classPath.isEmpty() )
63              {
64                  for ( String path : classPath )
65                  {
66                      childFirstLoader.addURL( new File( path ).toURI().toURL() );
67                  }
68              }
69  
70              GroovyShell interpreter = new GroovyShell( childFirstLoader,
71                      new Binding( globalVariables ),
72                      new CompilerConfiguration( CompilerConfiguration.DEFAULT ) );
73  
74              try
75              {
76                  return interpreter.evaluate( script );
77              }
78              catch ( ThreadDeath e )
79              {
80                  throw e;
81              }
82              catch ( PowerAssertionError e )
83              {
84                  throw new ScriptEvaluationException( "Assertion Error", e );
85              }
86              catch ( Throwable e )
87              {
88                  throw new ScriptEvaluationException( e );
89              }
90          }
91          catch ( IOException e )
92          {
93              throw new ScriptEvaluationException( e );
94          }
95          finally
96          {
97              System.setErr( origErr );
98              System.setOut( origOut );
99          }
100     }
101 
102 }