-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptManager.java
254 lines (237 loc) · 8.23 KB
/
ScriptManager.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.gservlet;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.codehaus.groovy.control.BytecodeProcessor;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.gservlet.annotation.ContextAttributeListener;
import org.gservlet.annotation.ContextListener;
import org.gservlet.annotation.Filter;
import org.gservlet.annotation.RequestAttributeListener;
import org.gservlet.annotation.RequestListener;
import org.gservlet.annotation.Servlet;
import org.gservlet.annotation.SessionActivationListener;
import org.gservlet.annotation.SessionAttributeListener;
import org.gservlet.annotation.SessionBindingListener;
import org.gservlet.annotation.SessionIdListener;
import org.gservlet.annotation.SessionListener;
import groovy.util.GroovyScriptEngine;
import groovy.util.ScriptException;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;
import javassist.NotFoundException;
/**
*
* An specific class to load Groovy scripts
*
* @author Mamadou Lamine Ba
*
*/
public class ScriptManager {
/**
* The scripts folder object
*/
protected final File folder;
/**
* The GroovyScriptEngine object
*/
protected final GroovyScriptEngine engine;
/**
* The logger object
*/
protected final Logger logger = Logger.getLogger(getClass().getName());
/**
* The list of script listeners
*/
protected final List<ScriptListener> listeners = new ArrayList<>();
/**
*
* Constructs a ScriptManager for the given folder
*
* @param folder the scripts folder object
* @throws ScriptException the ScriptException
*
*/
public ScriptManager(File folder) throws ScriptException {
try {
this.folder = folder;
engine = createScriptEngine();
} catch (MalformedURLException e) {
throw new ScriptException(e);
}
}
/**
*
* Creates an object from a groovy script file
*
* @param file the groovy script file
* @return the instantiated object
* @throws ScriptException the ScriptException
*
*/
public Object createObject(File file) throws ScriptException {
try {
Class<?> clazz = loadClass(file);
if (!clazz.isInterface() && Stream.of(clazz.getConstructors()).anyMatch(c -> c.getParameterCount() == 0)) {
Object object = clazz.getConstructor().newInstance();
listeners.forEach(listener -> listener.onCreated(object));
return object;
}
return new Object();
} catch (Exception e) {
throw new ScriptException(e);
}
}
/**
*
* Loads a class from a groovy script file
*
* @param file the groovy script file
* @return the loaded class
* @throws ScriptException the ScriptException
*
*/
public Class<?> loadClass(File file) throws ScriptException {
try {
String name = file.getAbsolutePath().substring(folder.getAbsolutePath().length() + 1);
return engine.loadScriptByName(name);
} catch (Exception e) {
throw new ScriptException(e);
}
}
/**
*
* Creates a GroovyScriptEngine for the given folder
*
* @return the groovy script engine
* @throws MalformedURLException the MalformedURLException
*
*/
protected GroovyScriptEngine createScriptEngine() throws MalformedURLException {
URL[] urls = { folder.toURI().toURL() };
GroovyScriptEngine scriptEngine = new GroovyScriptEngine(urls, this.getClass().getClassLoader());
ClassPool classPool = ClassPool.getDefault();
classPool.insertClassPath(new LoaderClassPath(scriptEngine.getParentClassLoader()));
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.setBytecodePostprocessor(createBytecodeProcessor());
configuration.setTargetBytecode(CompilerConfiguration.JDK8);
scriptEngine.setConfig(configuration);
return scriptEngine;
}
/**
*
* Creates a BytecodeProcessor for the given ClassPool instance
*
* @return the BytecodeProcessor object
*/
protected BytecodeProcessor createBytecodeProcessor() {
return (String name, byte[] original) -> {
ByteArrayInputStream stream = new ByteArrayInputStream(original);
try {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.makeClass(stream);
ctClass.detach();
processClass(ctClass);
return ctClass.toBytecode();
} catch (Exception e) {
logger.log(Level.SEVERE, "exception when processing bytecode", e);
}
return original;
};
}
/**
*
* Makes the given class extend a superclass based on the declared annotation
*
* @param ctClass the class
* @throws CannotCompileException the CannotCompileException
* @throws NotFoundException the NotFoundException
*/
private void processClass(CtClass ctClass) throws CannotCompileException, NotFoundException {
ClassPool classPool = ClassPool.getDefault();
if (ctClass.hasAnnotation(Servlet.class)) {
ctClass.setSuperclass(classPool.get(AbstractServlet.class.getName()));
} else if (ctClass.hasAnnotation(Filter.class)) {
ctClass.setSuperclass(classPool.get(AbstractFilter.class.getName()));
} else if (ctClass.hasAnnotation(ContextListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractContextListener.class.getName()));
} else if (ctClass.hasAnnotation(RequestListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractRequestListener.class.getName()));
} else if (ctClass.hasAnnotation(ContextAttributeListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractContextAttributeListener.class.getName()));
} else if (ctClass.hasAnnotation(RequestAttributeListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractRequestAttributeListener.class.getName()));
} else if (ctClass.hasAnnotation(SessionListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractSessionListener.class.getName()));
} else if (ctClass.hasAnnotation(SessionAttributeListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractSessionAttributeListener.class.getName()));
} else if (ctClass.hasAnnotation(SessionBindingListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractSessionBindingListener.class.getName()));
} else if (ctClass.hasAnnotation(SessionActivationListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractSessionActivationListener.class.getName()));
} else if (ctClass.hasAnnotation(SessionIdListener.class)) {
ctClass.setSuperclass(classPool.get(AbstractSessionIdListener.class.getName()));
}
}
/**
* Registers a new script listener
*
* @param listener the ScriptListener object
*/
public void addScriptListener(ScriptListener listener) {
this.listeners.add(listener);
}
/**
* Registers a list of script listeners
*
* @param listeners the list of script listeners
*
*/
public void addScriptListeners(List<ScriptListener> listeners) {
this.listeners.addAll(listeners);
}
/**
*
* Returns the list of script listeners
*
* @return the list of script listeners
*/
public List<ScriptListener> getScriptListeners() {
return listeners;
}
/**
*
* Returns the scripts folder
*
* @return the scripts folder
*/
public File getFolder() {
return folder;
}
}