View Javadoc
1   /*
2    * Copyright (c) 2006-present the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.gmaven.adapter.impl;
17  
18  import java.util.Map;
19  
20  import javax.annotation.Nullable;
21  
22  import groovy.lang.Binding;
23  import groovy.lang.GroovyClassLoader;
24  import groovy.lang.GroovyCodeSource;
25  import groovy.lang.GroovyShell;
26  import org.codehaus.gmaven.adapter.ClassSource;
27  import org.codehaus.gmaven.adapter.ResourceLoader;
28  import org.codehaus.gmaven.adapter.ScriptExecutor;
29  import org.codehaus.groovy.control.CompilerConfiguration;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import static com.google.common.base.Preconditions.checkNotNull;
34  
35  /**
36   * Default {@link ScriptExecutor} implementation.
37   *
38   * @since 2.0
39   */
40  public class ScriptExecutorImpl
41      implements ScriptExecutor
42  {
43    private final Logger log = LoggerFactory.getLogger(getClass());
44  
45    private final GroovyRuntimeImpl runtime;
46  
47    ScriptExecutorImpl(final GroovyRuntimeImpl runtime) {
48      this.runtime = checkNotNull(runtime);
49    }
50  
51    @Override
52    @Nullable
53    public Object execute(final ClassSource classSource,
54                          final ClassLoader classLoader,
55                          final ResourceLoader resourceLoader,
56                          final Map<String, Object> context,
57                          final @Nullable Map<String, Object> options)
58        throws Exception
59    {
60      checkNotNull(classSource);
61      checkNotNull(classLoader);
62      checkNotNull(resourceLoader);
63      checkNotNull(context);
64  
65      log.trace("Execute; class-source: {}, class-loader: {}, resource-loader: {}, context: {}",
66          classSource, classLoader, resourceLoader, context);
67  
68      GroovyClassLoader gcl = runtime.createGroovyClassLoader(classLoader, resourceLoader);
69  
70      CompilerConfiguration cc = new CompilerConfiguration();
71  
72      // TODO: How, if at all, do we want to expose compiler configuration customizations?
73  
74      Binding binding = runtime.createBinding(context);
75      GroovyShell shell = new GroovyShell(gcl, binding, cc);
76  
77      if (options != null) {
78        configureOptions(shell, options);
79      }
80  
81      GroovyCodeSource codeSource = runtime.createGroovyCodeSource(classSource);
82      try {
83        return shell.evaluate(codeSource);
84      }
85      finally {
86        gcl.clearCache();
87      }
88    }
89  
90    private void configureOptions(final GroovyShell shell, final Map<String, Object> options) {
91      // TODO
92    }
93  }