public void initializeExecution() throws Exception {
    // Initialize the Execution Context
    ExecutionContextManager ecm = getComponentManager().getInstance(ExecutionContextManager.class);

    ExecutionContext ec = new ExecutionContext();

    ecm.initialize(ec);
  }
Exemplo n.º 2
0
  /**
   * Init provided {@link ExportURLFactory} and add rendered documents to ZIP stream.
   *
   * @param zos the ZIP output stream.
   * @param tempdir the directory where to copy attached files.
   * @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, File tempdir, ExportURLFactory urlf, XWikiContext context)
      throws XWikiException, IOException {
    ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
    Execution execution = Utils.getComponent(Execution.class);

    VelocityContext oldVelocityContext = (VelocityContext) context.get("vcontext");

    try {
      XWikiContext renderContext = (XWikiContext) context.clone();
      renderContext.put("action", "view");

      ExecutionContext ec = new ExecutionContext();

      // Bridge with old XWiki Context, required for old code.
      ec.setProperty("xwikicontext", renderContext);

      ecim.initialize(ec);

      // Push a clean new Execution Context since we don't want the main Execution Context to be
      // used for
      // rendering the HTML pages to export. It's cleaner to isolate it as we do. Note that the new
      // Execution Context automatically gets initialized with a new Velocity Context by
      // the VelocityRequestInitializer class.
      execution.pushContext(ec);

      VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);

      // At this stage we have a clean Velocity Context
      VelocityContext vcontext = velocityManager.getVelocityContext();

      urlf.init(this.pages, tempdir, renderContext);
      renderContext.setURLFactory(urlf);

      for (String pageName : this.pages) {
        renderDocument(pageName, zos, renderContext, vcontext);
      }
    } catch (ExecutionContextException e) {
      throw new XWikiException(
          XWikiException.MODULE_XWIKI_EXPORT,
          XWikiException.ERROR_XWIKI_INIT_FAILED,
          "Failed to initialize Execution Context",
          e);
    } finally {
      // We must ensure that the new request we've used is removed so that the current
      // thread can continue to use its original Execution Context.
      execution.popContext();

      context.put("vcontext", oldVelocityContext);
    }
  }