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 |
|
package info.magnolia.module.groovy.console; |
35 |
|
|
36 |
|
import info.magnolia.context.Context; |
37 |
|
import info.magnolia.context.MgnlContext; |
38 |
|
import info.magnolia.i18nsystem.SimpleTranslator; |
39 |
|
import info.magnolia.module.groovy.support.classes.MgnlGroovyClassLoader; |
40 |
|
import info.magnolia.objectfactory.Components; |
41 |
|
import info.magnolia.ui.api.message.Message; |
42 |
|
import info.magnolia.ui.api.message.MessageType; |
43 |
|
import info.magnolia.ui.framework.message.MessagesManager; |
44 |
|
|
45 |
|
import java.io.ByteArrayInputStream; |
46 |
|
import java.io.IOException; |
47 |
|
import java.io.InputStream; |
48 |
|
import java.io.InputStreamReader; |
49 |
|
import java.io.StringWriter; |
50 |
|
import java.io.Writer; |
51 |
|
import java.util.Objects; |
52 |
|
import java.util.concurrent.Callable; |
53 |
|
import java.util.concurrent.ExecutionException; |
54 |
|
import java.util.concurrent.ExecutorService; |
55 |
|
import java.util.concurrent.Executors; |
56 |
|
import java.util.concurrent.Future; |
57 |
|
|
58 |
|
import org.apache.commons.lang.CharEncoding; |
59 |
|
import org.codehaus.groovy.control.CompilationFailedException; |
60 |
|
import org.codehaus.groovy.runtime.InvokerHelper; |
61 |
|
import org.slf4j.Logger; |
62 |
|
import org.slf4j.LoggerFactory; |
63 |
|
|
64 |
|
import com.vaadin.event.UIEvents.PollEvent; |
65 |
|
import com.vaadin.event.UIEvents.PollListener; |
66 |
|
import com.vaadin.ui.UI; |
67 |
|
|
68 |
|
import groovy.lang.Binding; |
69 |
|
import groovy.lang.GroovyCodeSource; |
70 |
|
import groovy.lang.GroovyShell; |
71 |
|
import groovy.lang.Script; |
72 |
|
|
73 |
|
|
74 |
|
@link |
75 |
|
|
|
|
| 85.2% |
Uncovered Elements: 12 (81) |
Complexity: 20 |
Complexity Density: 0.36 |
|
76 |
|
public class MgnlGroovyConsole extends GroovyShell { |
77 |
|
|
78 |
|
private static final Logger log = LoggerFactory.getLogger(MgnlGroovyConsole.class); |
79 |
|
|
80 |
|
private static final MgnlGroovyClassLoader GROOVY_CLASS_LOADER = new MgnlGroovyClassLoader(); |
81 |
|
|
82 |
|
private static final int POLL_INTERVAL_MS = 1000; |
83 |
|
|
84 |
|
private MessagesManager messagesManager; |
85 |
|
|
86 |
|
private SimpleTranslator i18n; |
87 |
|
|
88 |
|
|
89 |
|
@deprecated@link |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
5 |
public MgnlGroovyConsole(Binding binding) {... |
92 |
5 |
this(binding, Components.getComponent(MessagesManager.class), Components.getComponent(SimpleTranslator.class)); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
95 |
8 |
public MgnlGroovyConsole(Binding binding, MessagesManager messagesManager, SimpleTranslator simpleTranslator) {... |
96 |
8 |
super(binding); |
97 |
8 |
this.messagesManager = messagesManager; |
98 |
8 |
this.i18n = simpleTranslator; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
@link |
103 |
|
@link@link |
104 |
|
|
105 |
|
|
106 |
|
@deprecated@link |
107 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
108 |
3 |
@Deprecated... |
109 |
|
public Object evaluate(InputStream in, String fileName, Writer out) throws CompilationFailedException { |
110 |
3 |
Context originalCtx = MgnlContext.getInstance(); |
111 |
3 |
MgnlGroovyConsoleContext groovyCtx = new MgnlGroovyConsoleContext(originalCtx); |
112 |
3 |
MgnlContext.setInstance(groovyCtx); |
113 |
|
|
114 |
3 |
Script script = null; |
115 |
3 |
try { |
116 |
3 |
script = createScript(in, out); |
117 |
2 |
script.setProperty("ctx", MgnlContext.getInstance()); |
118 |
2 |
return script.run(); |
119 |
|
} catch (IOException e) { |
120 |
0 |
throw new RuntimeException(e); |
121 |
|
} finally { |
122 |
3 |
if (script != null) { |
123 |
2 |
InvokerHelper.removeClass(script.getClass()); |
124 |
|
} |
125 |
3 |
MgnlContext.setInstance(originalCtx); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
@link |
131 |
|
|
132 |
|
|
133 |
|
@link@link |
134 |
|
|
135 |
|
@throws |
136 |
|
@throws |
137 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
138 |
3 |
public void runAsync(String source, UI ui, ScriptCallback callback) throws IOException, InterruptedException {... |
139 |
3 |
Writer out = new StringWriter(); |
140 |
3 |
final Script script = createScript(source, out); |
141 |
|
|
142 |
3 |
final Context originalCtx = MgnlContext.getInstance(); |
143 |
3 |
final MgnlGroovyConsoleContext groovyCtx = new MgnlGroovyConsoleContext(originalCtx); |
144 |
|
|
145 |
3 |
ExecutorService executorService = Executors.newSingleThreadExecutor(); |
146 |
|
|
147 |
3 |
final Future<Object> task = executorService.submit(new Callable<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
148 |
3 |
@Override... |
149 |
|
public Object call() throws Exception { |
150 |
|
|
151 |
|
|
152 |
3 |
MgnlContext.setInstance(groovyCtx); |
153 |
3 |
script.setProperty("ctx", groovyCtx); |
154 |
3 |
return script.run(); |
155 |
|
} |
156 |
|
}); |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
3 |
Thread.sleep(200); |
162 |
3 |
if (task.isDone()) { |
163 |
2 |
handleScriptDone(callback, task, ui, out, messagesManager, i18n, false); |
164 |
2 |
return; |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
1 |
if (ui.getPollInterval() < POLL_INTERVAL_MS) { |
169 |
1 |
ui.setPollInterval(POLL_INTERVAL_MS); |
170 |
|
} |
171 |
1 |
ui.addPollListener(new ScriptDonePollListener(task, ui, out, callback, messagesManager, i18n)); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
3 |
private Script createScript(String code, Writer out) throws CompilationFailedException, IOException {... |
175 |
3 |
return createScript(new ByteArrayInputStream(code.getBytes()), out); |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
178 |
6 |
private Script createScript(InputStream in, Writer out) throws CompilationFailedException, IOException {... |
179 |
6 |
Script script = parse(new InputStreamReader(in, CharEncoding.UTF_8), generateScriptName()); |
180 |
5 |
script.setProperty("out", out); |
181 |
5 |
script.setProperty("err", out); |
182 |
5 |
return script; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@param |
190 |
|
@return |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
6 |
@Override... |
193 |
|
public Script parse(final GroovyCodeSource codeSource) throws CompilationFailedException { |
194 |
6 |
return InvokerHelper.createScript(parseClass(codeSource), getContext()); |
195 |
|
} |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
197 |
6 |
@Override... |
198 |
|
public String generateScriptName() { |
199 |
|
|
200 |
6 |
return super.generateScriptName(); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
6 |
private Class parseClass(final GroovyCodeSource codeSource) throws CompilationFailedException {... |
207 |
|
|
208 |
6 |
return GROOVY_CLASS_LOADER.parseClass(codeSource, false); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
214 |
|
public interface ScriptCallback { |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
|
220 |
|
void onSuccess(String result); |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
@param |
226 |
|
|
227 |
|
void onFailure(Throwable e); |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
|
234 |
|
void onProgress(Writer out); |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
boolean requiresNotificationMessageUponCompletion(); |
240 |
|
} |
241 |
|
|
|
|
| 58.3% |
Uncovered Elements: 10 (24) |
Complexity: 6 |
Complexity Density: 0.38 |
|
242 |
2 |
private static void handleScriptDone(ScriptCallback callback, final Future<Object> scriptTask, UI ui, Writer out, MessagesManager messagesManager, SimpleTranslator i18n, boolean sendNotificationMessage) {... |
243 |
2 |
try { |
244 |
2 |
String resultAsString = scriptTask.get() == null ? "" : Objects.toString(scriptTask.get()); |
245 |
2 |
String completeOutput; |
246 |
2 |
if (out.toString().isEmpty()) { |
247 |
2 |
completeOutput = resultAsString; |
248 |
|
} else { |
249 |
0 |
completeOutput = out.toString().concat("\n").concat(resultAsString); |
250 |
|
} |
251 |
|
|
252 |
2 |
log.debug("Script run successfully with result {}", completeOutput); |
253 |
2 |
callback.onSuccess(completeOutput); |
254 |
2 |
if (sendNotificationMessage) { |
255 |
0 |
messagesManager.sendLocalMessage(new Message(MessageType.INFO, i18n.translate("groovy.script.console.done.success"), completeOutput)); |
256 |
|
} |
257 |
|
|
258 |
|
} catch (InterruptedException | ExecutionException e) { |
259 |
0 |
log.error("An error occurred while running a Groovy script", e); |
260 |
0 |
callback.onFailure(e); |
261 |
|
|
262 |
0 |
if (sendNotificationMessage) { |
263 |
0 |
messagesManager.sendLocalMessage(new Message(MessageType.WARNING, i18n.translate("groovy.script.console.done.error"), e.getMessage())); |
264 |
|
} |
265 |
|
} finally { |
266 |
2 |
log.debug("Stop polling"); |
267 |
2 |
ui.setPollInterval(-1); |
268 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
@SuppressWarnings("serial") |
|
|
| 38.9% |
Uncovered Elements: 11 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
272 |
|
private static final class ScriptDonePollListener implements PollListener { |
273 |
|
private Future<Object> scriptTask; |
274 |
|
private UI ui; |
275 |
|
private ScriptCallback callback; |
276 |
|
private Writer out; |
277 |
|
private MessagesManager messagesManager; |
278 |
|
private SimpleTranslator i18n; |
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
280 |
1 |
public ScriptDonePollListener(Future<Object> scriptTask, UI ui, Writer out, ScriptCallback callback, MessagesManager messagesManager, SimpleTranslator i18n) {... |
281 |
1 |
this.scriptTask = scriptTask; |
282 |
1 |
this.ui = ui; |
283 |
1 |
this.callback = callback; |
284 |
1 |
this.out = out; |
285 |
1 |
this.messagesManager = messagesManager; |
286 |
1 |
this.i18n = i18n; |
287 |
|
} |
288 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
289 |
0 |
@Override... |
290 |
|
public void poll(PollEvent event) { |
291 |
0 |
if (scriptTask.isDone()) { |
292 |
0 |
handleScriptDone(callback, scriptTask, ui, out, messagesManager, i18n, callback.requiresNotificationMessageUponCompletion()); |
293 |
|
|
294 |
0 |
ui.removePollListener(this); |
295 |
|
} else { |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
0 |
if (ui.getPollInterval() < POLL_INTERVAL_MS) { |
300 |
0 |
ui.setPollInterval(POLL_INTERVAL_MS); |
301 |
|
} |
302 |
0 |
callback.onProgress(out); |
303 |
|
} |
304 |
|
} |
305 |
|
} |
306 |
|
} |