Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

执行Groovy脚本的工具类

groovy带缓存的编译运行工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
import groovy.lang.Script;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class GroovyCompiler {

private static Map<String, GroovyClass> groovyCaches = new ConcurrentHashMap<String, GroovyClass>();

private static ClassLoader cl = GroovyCompiler.class.getClassLoader();
private static GroovyClassLoader groovyCl = new GroovyClassLoader(cl);

public static GroovyClass getClass(File script){

if(script == null || !script.exists()){
return null;
}

try {

String fileName = script.getPath() ;
GroovyClass groovyClazz = groovyCaches.get(fileName);

//cached,check if modified
if(groovyClazz != null){
long lastModified = script.lastModified();

//newest
if(lastModified <= groovyClazz.getLastmodified() ){
return groovyClazz;
}

//update cache
groovyClazz = null;
}

//need new a Instance
//not use GroovyClassLoader cache
Class<?> groovyClass = groovyCl.parseClass(new GroovyCodeSource(script, "UTF-8"),false);
groovyClazz = new GroovyClass((Script)groovyClass.newInstance(),script.lastModified());

groovyCaches.put(fileName, groovyClazz);

return groovyClazz;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static GroovyClass getClass(URL script){

return getClass(new File(script.getFile()));
}

public static GroovyClass getClass(String script){

return getClass(cl.getResource(script));
}

public static class GroovyClass {

public GroovyClass(){

}

public GroovyClass(Script script,long lastmodified){
this.script = script;
this.lastmodified = lastmodified;
}

Script script;

long lastmodified = 0;


public Script getScript() {
return script;
}

public void setScript(Script script) {
this.script = script;
}

public long getLastmodified() {
return lastmodified;
}

public void setLastmodified(long lastmodified) {
this.lastmodified = lastmodified;
}

public Object invoke(String methosName, Object[] args) {
return script.invokeMethod(methosName, args);
}

public Object get() {
return script.run();
}

public Object get(Map binds) {
script.getBinding().getVariables().putAll(binds);
Object obj = script.run();
return obj;
}

}

}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class GroovyKest {

public static void main(String[] args) throws Exception {

GroovyClass groovy = GroovyCompiler.getClass("script/view.groovy");

Map <String,String> context = new HashMap<>();
context.put("name", "kan");

Object obj = groovy.get(context);

System.out.println(obj);

}

}