Search in sources :

Example 1 with ScriptEvaluatedEvent

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;
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Event(org.xwiki.observation.event.Event) LinkedList(java.util.LinkedList)

Example 2 with ScriptEvaluatedEvent

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);
    }
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptMacroParameters(org.xwiki.rendering.macro.script.ScriptMacroParameters) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) ExtendedURLClassLoader(org.xwiki.classloader.ExtendedURLClassLoader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 3 with ScriptEvaluatedEvent

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;
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block)

Aggregations

ScriptEvaluatedEvent (org.xwiki.script.event.ScriptEvaluatedEvent)3 ScriptEvaluatingEvent (org.xwiki.script.event.ScriptEvaluatingEvent)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)2 LinkedList (java.util.LinkedList)1 ExtendedURLClassLoader (org.xwiki.classloader.ExtendedURLClassLoader)1 CancelableEvent (org.xwiki.observation.event.CancelableEvent)1 Event (org.xwiki.observation.event.Event)1 Block (org.xwiki.rendering.block.Block)1 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 ScriptMacroParameters (org.xwiki.rendering.macro.script.ScriptMacroParameters)1