org.xwiki.script.internal.DefaultScriptContextManager Maven / Gradle / Ivy
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.script.internal;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.script.ScriptContext;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.xwiki.script.ScriptContextInitializer;
import org.xwiki.script.ScriptContextManager;
/**
* Default implementation of {@link ScriptContextManager}.
*
* @version $Id: 9baebb3902c375615fbac3e667fcbfb2df358a0a $
*/
@Component
@Singleton
public class DefaultScriptContextManager implements ScriptContextManager
{
@Inject
private Logger logger;
/**
* Used to get and insert script context in current execution context.
*/
@Inject
private Execution execution;
/**
* Used to get the list of {@link ScriptContextInitializer} components to initialize the {@link ScriptContext}. We
* use the context {@link ComponentManager} because we want to take into account also
* {@link ScriptContextInitializer} components registered at wiki level (from extensions installed at wiki level).
*/
@Inject
@Named("context")
private Provider contextComponentManagerProvider;
@Override
public ScriptContext getScriptContext()
{
ScriptContext context = getCurrentScriptContext();
if (context != null) {
// We re-initialize the Script Context with all Script Context Initializers. We do this in order to ensure
// that the Script Context always contain correct values even if user scripts or XWiki code have modified
// them. For example the current document in the Script Context could have changed and thus needs to be set
// back. Also note that we don't clone the context since we want that in the same request several script
// executions can share bindings.
for (ScriptContextInitializer scriptContextInitializer : getScriptContextInitializers()) {
try {
scriptContextInitializer.initialize(context);
} catch (Exception e) {
this.logger.warn("Failed to initialize the script context with [{}]. Root cause is [{}].",
scriptContextInitializer, ExceptionUtils.getRootCauseMessage(e));
}
}
}
return context;
}
private List getScriptContextInitializers()
{
try {
return this.contextComponentManagerProvider.get().getInstanceList(ScriptContextInitializer.class);
} catch (ComponentLookupException e) {
this.logger.warn("Failed to get the list of script context initializers. Root cause is [{}].",
ExceptionUtils.getRootCauseMessage(e));
return Collections.emptyList();
}
}
@Override
public ScriptContext getCurrentScriptContext()
{
ExecutionContext executionContext = this.execution.getContext();
if (executionContext != null) {
// The Script Context is set in ScriptExecutionContextInitializer, when the XWiki Execution Context is
// initialized so we are guaranteed it is defined when this method is called.
return (ScriptContext) executionContext.getProperty(ScriptExecutionContextInitializer.SCRIPT_CONTEXT_ID);
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy