use of org.xwiki.script.event.ScriptEvaluatedEvent in project xwiki-platform by xwiki.
the class ScriptClassLoaderHandlerListener method getEvents.
@Override
public List<Event> getEvents() {
List<Event> events = new LinkedList<Event>();
events.add(new ScriptEvaluatingEvent());
events.add(new ScriptEvaluatedEvent());
return events;
}
use of org.xwiki.script.event.ScriptEvaluatedEvent in project xwiki-platform by xwiki.
the class ScriptClassLoaderHandlerListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
if (!(data instanceof ScriptMacroParameters)) {
return;
}
if (event instanceof ScriptEvaluatingEvent) {
// Set the context class loader to the script CL to ensure that any script engine using the context
// classloader will work just fine.
// Note: We must absolutely ensure that we always use the same context CL during the whole execution
// request since JSR223 script engines (for example) that create internal class loaders need to
// continue using these class loaders (where classes defined in scripts have been loaded for example).
ScriptMacroParameters parameters = (ScriptMacroParameters) data;
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
this.execution.getContext().setProperty(EXECUTION_CONTEXT_ORIG_CLASSLOADER_KEY, originalClassLoader);
try {
ClassLoader newClassLoader = getClassLoader(parameters.getJars(), originalClassLoader);
Thread.currentThread().setContextClassLoader(newClassLoader);
} catch (Exception exception) {
// abort execution
((CancelableEvent) event).cancel(exception.getMessage());
}
} else if (event instanceof ScriptEvaluatedEvent) {
// Restore original class loader.
ClassLoader originalClassLoader = (ClassLoader) this.execution.getContext().getProperty(EXECUTION_CONTEXT_ORIG_CLASSLOADER_KEY);
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
use of org.xwiki.script.event.ScriptEvaluatedEvent in project xwiki-platform by xwiki.
the class AbstractScriptMacro method execute.
@Override
public List<Block> execute(P parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
List<Block> result = Collections.emptyList();
if (StringUtils.isNotEmpty(content)) {
try {
// send evaluation starts event
ScriptEvaluatingEvent event = new ScriptEvaluatingEvent(getDescriptor().getId().getId());
this.observation.notify(event, context, parameters);
if (event.isCanceled()) {
throw new MacroExecutionException(event.getReason());
}
// 2) Run script engine on macro block content
List<Block> blocks = evaluateBlock(parameters, content, context);
if (parameters.isOutput()) {
result = blocks;
}
} finally {
// send evaluation finished event
this.observation.notify(new ScriptEvaluatedEvent(getDescriptor().getId().getId()), context, parameters);
}
}
return result;
}
Aggregations