01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: FilteredTagProcessorGroovy.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.template;
09:
10: import groovy.lang.Binding;
11: import groovy.lang.GroovyClassLoader;
12: import groovy.lang.GroovyCodeSource;
13: import groovy.lang.Script;
14: import java.io.ByteArrayInputStream;
15: import java.util.Map;
16: import org.codehaus.groovy.control.CompilerConfiguration;
17: import org.codehaus.groovy.runtime.InvokerHelper;
18:
19: public class FilteredTagProcessorGroovy extends FilteredTagProcessor {
20: private static FilteredTagProcessor sInstance = null;
21:
22: private int mScriptCounter = 1;
23: private GroovyClassLoader mLoader = new GroovyClassLoader(
24: FilteredTagProcessorGroovy.class.getClassLoader(),
25: new CompilerConfiguration());
26:
27: public static FilteredTagProcessor getInstance() {
28: if (null == sInstance) {
29: sInstance = new FilteredTagProcessorGroovy();
30: }
31:
32: return sInstance;
33: }
34:
35: public String getLanguage() {
36: return "GROOVY";
37: }
38:
39: public Object processExpression(Template template, Class rootType,
40: String rootName, Object rootValue, String expression,
41: Map<String, Object> context) throws Exception {
42: Class script_class = (Class) template
43: .getCacheObject(expression);
44: if (null == script_class) {
45: GroovyCodeSource code_source = new GroovyCodeSource(
46: new ByteArrayInputStream(expression.getBytes()),
47: "Script" + (mScriptCounter++) + ".groovy",
48: "/groovy/shell");
49: script_class = mLoader.parseClass(code_source);
50: template.cacheObject(expression, script_class);
51: }
52:
53: Binding binding = new Binding(context);
54: Script script = InvokerHelper.createScript(script_class,
55: binding);
56: return script.run();
57: }
58: }
|