Search in sources :

Example 6 with ExecutionContextException

use of org.xwiki.context.ExecutionContextException in project xwiki-platform by xwiki.

the class XWikiStubContextInitializerTest method testWithAndWithoutXWikiContext.

public void testWithAndWithoutXWikiContext() throws Exception {
    XWikiContext xcontext = new XWikiContext();
    xcontext.put("key", "value");
    this.mockXWiki = mock(XWiki.class);
    this.mockXWiki.stubs().method("prepareResources");
    xcontext.setWiki((XWiki) this.mockXWiki.proxy());
    ExecutionContext context = new ExecutionContext();
    xcontext.declareInExecutionContext(context);
    XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
    stubContextProvider.initialize(xcontext);
    final ExecutionContext daemonContext = new ExecutionContext();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                executionContextManager.initialize(daemonContext);
            } catch (ExecutionContextException e) {
                fail("Failed to initialize execution context: " + e.getStackTrace());
            }
        }
    });
    thread.start();
    thread.join();
    XWikiContext daemonXcontext = (XWikiContext) daemonContext.getProperty("xwikicontext");
    assertNotNull(daemonXcontext);
    assertNotSame(xcontext, daemonXcontext);
    assertEquals("value", daemonXcontext.get("key"));
    assertNotNull(daemonXcontext.getWiki());
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 7 with ExecutionContextException

use of org.xwiki.context.ExecutionContextException in project xwiki-platform by xwiki.

the class DefaultPortletContainerInitializer method initializeRequest.

@Override
public void initializeRequest(javax.portlet.PortletRequest portletRequest, Object xwikiContext) throws PortletContainerException {
    // 1) Create an empty request. From this point forward request initializers can use the
    // Container object to get any data they want from the Request.
    this.container.setRequest(new PortletRequest(portletRequest));
    // 2) Create en empty Execution context so that the Container initializers can put things in the
    // execution context when they execute.
    this.execution.setContext(new ExecutionContext());
    // XWikiContext object whereas new code uses the ExecutionContext found in the Execution component.
    if (xwikiContext != null) {
        ExecutionContext ec = this.execution.getContext();
        String key = "xwikicontext";
        if (ec.hasProperty(key)) {
            ec.setProperty(key, xwikiContext);
        } else {
            ec.newProperty(key).inherited().initial(xwikiContext).declare();
        }
    }
    // initializers.
    try {
        this.requestInitializerManager.initializeRequest(this.container.getRequest());
    } catch (RequestInitializerException e) {
        throw new PortletContainerException("Failed to initialize request", e);
    }
    // 5) Call Execution Context initializers to perform further Execution Context initializations
    try {
        this.executionContextManager.initialize(this.execution.getContext());
    } catch (ExecutionContextException e) {
        throw new PortletContainerException("Failed to initialize Execution Context", e);
    }
}
Also used : PortletRequest(org.xwiki.container.portlet.PortletRequest) ExecutionContext(org.xwiki.context.ExecutionContext) RequestInitializerException(org.xwiki.container.RequestInitializerException) PortletContainerException(org.xwiki.container.portlet.PortletContainerException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 8 with ExecutionContextException

use of org.xwiki.context.ExecutionContextException in project xwiki-platform by xwiki.

the class HtmlPackager method renderDocuments.

/**
 * Init provided {@link ExportURLFactory} and add rendered documents to ZIP stream.
 *
 * @param zos the ZIP output stream.
 * @param urlf the {@link com.xpn.xwiki.web.XWikiURLFactory} used to render the documents.
 * @param context the XWiki context.
 * @throws XWikiException error when render documents.
 * @throws IOException error when render documents.
 */
private void renderDocuments(ZipOutputStream zos, ExportURLFactory urlf, XWikiContext context) throws XWikiException, IOException {
    ExecutionContextManager ecm = Utils.getComponent(ExecutionContextManager.class);
    Execution execution = Utils.getComponent(Execution.class);
    for (DocumentReference pageReference : this.pageReferences) {
        try {
            // Isolate and initialize Contexts
            XWikiContext renderContext = initializeContexts(ecm, execution, urlf, context);
            renderDocument(pageReference, zos, urlf.getFilesystemExportContext(), renderContext);
        } catch (ExecutionContextException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_EXPORT, XWikiException.ERROR_XWIKI_INIT_FAILED, "Failed to initialize Execution Context", e);
        } finally {
            // Clean up context
            execution.popContext();
        }
    }
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContextException(org.xwiki.context.ExecutionContextException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 9 with ExecutionContextException

use of org.xwiki.context.ExecutionContextException in project xwiki-platform by xwiki.

the class AbstractDocumentMojo method createXWikiContext.

protected XWikiContext createXWikiContext() throws MojoExecutionException {
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    ecm.initialize(this.getClass().getClassLoader());
    Utils.setComponentManager(ecm);
    // Context component manager is totally useless here
    ecm.unregisterComponent(ComponentManager.class, "context");
    // We need to initialize the Component Manager so that the components can be looked up
    XWikiContext xcontext = new XWikiContext();
    xcontext.put(ComponentManager.class.getName(), ecm);
    // Initialize the Container fields (request, response, session).
    try {
        ExecutionContextManager ecim = ecm.getInstance(ExecutionContextManager.class);
        ExecutionContext econtext = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code.
        xcontext.declareInExecutionContext(econtext);
        ecim.initialize(econtext);
    } catch (ExecutionContextException | ComponentLookupException e) {
        throw new MojoExecutionException("Failed to initialize Execution Context.", e);
    }
    return xcontext;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 10 with ExecutionContextException

use of org.xwiki.context.ExecutionContextException in project xwiki-platform by xwiki.

the class ContextLinkCheckerThreadInitializer method initialize.

@Override
public void initialize() {
    ExecutionContext context = this.execution.getContext();
    if (context == null) {
        // Create a clean Execution Context
        context = new ExecutionContext();
        try {
            this.executionContextManager.initialize(context);
        } catch (ExecutionContextException e) {
            throw new RuntimeException("Failed to initialize the Execution Context", e);
        }
        // Bridge with old XWiki Context, required for old code.
        XWikiContext xwikiContext = this.stubContextProvider.createStubContext();
        xwikiContext.declareInExecutionContext(context);
        this.execution.pushContext(context);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Aggregations

ExecutionContextException (org.xwiki.context.ExecutionContextException)11 ExecutionContext (org.xwiki.context.ExecutionContext)8 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiException (com.xpn.xwiki.XWikiException)4 Execution (org.xwiki.context.Execution)3 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)3 XWiki (com.xpn.xwiki.XWiki)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 EmbeddableComponentManager (org.xwiki.component.embed.EmbeddableComponentManager)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2 InitializationException (org.xwiki.component.phase.InitializationException)2 DefaultDistributionJob (org.xwiki.extension.distribution.internal.job.DefaultDistributionJob)2 DistributionJob (org.xwiki.extension.distribution.internal.job.DistributionJob)2 DistributionRequest (org.xwiki.extension.distribution.internal.job.DistributionRequest)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiConfig (com.xpn.xwiki.XWikiConfig)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)1 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)1