Search in sources :

Example 1 with ExecutionContextException

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

the class DefaultDistributionManager method startWikiJob.

@Override
public DistributionJob startWikiJob(String wiki) {
    try {
        DistributionJob wikiJob = this.componentManager.getInstance(Job.class, DefaultDistributionJob.HINT);
        this.wikiDistributionJobs.put(wiki, wikiJob);
        final DistributionRequest request = new DistributionRequest();
        request.setId(getWikiJobId(wiki));
        request.setWiki(wiki);
        request.setUserReference(this.xcontextProvider.get().getUserReference());
        request.setInteractive(this.configuration.getProperty("distribution.job.interactive.wiki", true));
        Thread distributionJobThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // Create a clean Execution Context
                ExecutionContext context = new ExecutionContext();
                try {
                    executionContextManager.initialize(context);
                } catch (ExecutionContextException e) {
                    throw new RuntimeException("Failed to initialize wiki distribution job execution context", e);
                }
                DistributionJob job = wikiDistributionJobs.get(request.getWiki());
                job.initialize(request);
                job.run();
            }
        });
        distributionJobThread.setDaemon(true);
        distributionJobThread.setName("Distribution initialization of wiki [" + wiki + "]");
        distributionJobThread.start();
        // Wait until the job is ready (or finished)
        wikiJob.awaitReady();
        return wikiJob;
    } catch (Exception e) {
        this.logger.error("Failed to create distribution job for wiki [" + wiki + "]", e);
    }
    return null;
}
Also used : DistributionRequest(org.xwiki.extension.distribution.internal.job.DistributionRequest) ExecutionContext(org.xwiki.context.ExecutionContext) DistributionJob(org.xwiki.extension.distribution.internal.job.DistributionJob) DefaultDistributionJob(org.xwiki.extension.distribution.internal.job.DefaultDistributionJob) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 2 with ExecutionContextException

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

the class PrepareMailRunnable method prepareMail.

/**
 * Prepare the messages to send, persist them and put them on the Mail Sender Queue.
 *
 * @param item the queue item containing all the data for sending the mail
 * @throws org.xwiki.context.ExecutionContextException when the XWiki Context fails to be set up
 */
protected void prepareMail(PrepareMailQueueItem item) throws ExecutionContextException {
    Iterator<? extends MimeMessage> messageIterator = item.getMessages().iterator();
    MailListener listener = item.getListener();
    if (listener != null) {
        listener.onPrepareBegin(item.getBatchId(), Collections.<String, Object>emptyMap());
    }
    // Count the total number of messages to process
    long messageCounter = 0;
    try {
        boolean shouldStop = false;
        while (!shouldStop) {
            // Note that we need to have the hasNext() call after the context is ready since the implementation can
            // need a valid XWiki Context.
            prepareContext(item.getContext());
            try {
                if (messageIterator.hasNext()) {
                    MimeMessage mimeMessage = messageIterator.next();
                    prepareSingleMail(mimeMessage, item);
                    messageCounter++;
                } else {
                    shouldStop = true;
                }
            } finally {
                removeContext();
            }
        }
    } catch (Exception e) {
        if (listener != null) {
            listener.onPrepareFatalError(e, Collections.<String, Object>emptyMap());
        }
    } finally {
        if (listener != null) {
            MailStatusResult result = listener.getMailStatusResult();
            // so that waiting process have a chance to see an end.
            if (result instanceof UpdateableMailStatusResult) {
                ((UpdateableMailStatusResult) result).setTotalSize(messageCounter);
            }
            listener.onPrepareEnd(Collections.<String, Object>emptyMap());
        }
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MessagingException(javax.mail.MessagingException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 3 with ExecutionContextException

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

the class AbstractJob method execute.

@Override
public final void execute(JobExecutionContext jobContext) throws JobExecutionException {
    JobDataMap data = jobContext.getJobDetail().getJobDataMap();
    // The XWiki context was saved in the Job execution data map. Get it as we'll retrieve
    // the script to execute from it.
    this.xcontext = (XWikiContext) data.get("context");
    // Clone the XWikiContex to have a new one for each run
    this.xcontext = this.xcontext.clone();
    // Init execution context
    Execution execution;
    try {
        ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
        execution = Utils.getComponent(Execution.class);
        ExecutionContext context = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code
        this.xcontext.declareInExecutionContext(context);
        ecim.initialize(context);
    } catch (ExecutionContextException e) {
        throw new JobExecutionException("Fail to initialize execution context", e);
    }
    try {
        // Execute the job
        executeJob(jobContext);
    } finally {
        // We must ensure we clean the ThreadLocal variables located in the Execution
        // component as otherwise we will have a potential memory leak.
        execution.removeContext();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Execution(org.xwiki.context.Execution) JobExecutionContext(org.quartz.JobExecutionContext) ExecutionContext(org.xwiki.context.ExecutionContext) JobExecutionException(org.quartz.JobExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 4 with ExecutionContextException

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

the class OldCoreHelper method createXWikiContext.

/**
 * @param wikiId id of the wiki for which to prepare the XWiki Context (e.g. {@code xwiki})
 * @param hibernateConfig the Hibernate config fill containing the database definition (JDBC driver, username and
 *            password, etc)
 * @return a valid XWikiContext using the passed Hibernate configuration and passed database name
 * @throws Exception failed to initialize context.
 */
// TODO: Replace the Hibernate config file with a list of parameters required for the packaging operation
private XWikiContext createXWikiContext() throws Exception {
    Utils.setComponentManager(this.componentManager);
    this.xcontext = new XWikiContext();
    this.xcontext.put(ComponentManager.class.getName(), this.componentManager);
    // Initialize the Container fields (request, response, session).
    try {
        ExecutionContext econtext = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code.
        this.xcontext.declareInExecutionContext(econtext);
        this.ecim.initialize(econtext);
    } catch (ExecutionContextException e) {
        throw new Exception("Failed to initialize Execution Context.", e);
    }
    this.xcontext.setWikiId(wikiId);
    this.xcontext.setMainXWiki(wikiId);
    // Use a dummy Request/Response even in daemon mode so that XWiki's initialization can create a Servlet URL
    // Factory and any code requiring those objects will work.
    this.xcontext.setRequest(new XWikiServletRequestStub());
    this.xcontext.setResponse(new XWikiServletResponseStub());
    // Use a dummy URL so that XWiki's initialization can create a Servlet URL Factory. We could also have
    // registered a custom XWikiURLFactory against XWikiURLFactoryService but it's more work.
    this.xcontext.setURL(new URL("http://localhost/xwiki/bin/DummyAction/DumySpace/DummyPage"));
    // Set a dummy Document in the context to act as the current document since when a document containing
    // objects is imported it'll generate Object diff events and the algorithm to compute an object diff
    // currently requires rendering object properties, which requires a current document in the context.
    this.xcontext.setDoc(new XWikiDocument(new DocumentReference(wikiId, "dummySpace", "dummyPage")));
    XWikiConfig config = new XWikiConfig();
    config.put("xwiki.store.class", "com.xpn.xwiki.store.XWikiHibernateStore");
    // The XWikiConfig object requires path to be in unix format (i.e. with forward slashes)
    String hibernateConfigInUnixFormat = hibernateConfig.getPath().replace('\\', '/');
    config.put("xwiki.store.hibernate.path", hibernateConfigInUnixFormat);
    config.put("xwiki.store.hibernate.updateschema", "1");
    // Enable backlinks so that when documents are imported their backlinks will be saved too
    config.put("xwiki.backlinks", "1");
    XWiki xwiki = new XWiki(config, this.xcontext, null, true);
    this.xcontext.setUserReference(new DocumentReference("xwiki", "XWiki", "superadmin"));
    try {
        this.xcontext.setURLFactory(new XWikiServletURLFactory(new URL("http://localhost:8080"), "xwiki/", "bin/"));
    } catch (MalformedURLException e) {
        // doesn't work with external code.
        throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to set up URL Factory", e);
    }
    // Trigger extensions that need to initialize the database (create classes, etc.)
    xwiki.initializeWiki(this.xcontext.getMainXWiki(), true, this.xcontext);
    return this.xcontext;
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletURLFactory(com.xpn.xwiki.web.XWikiServletURLFactory) MalformedURLException(java.net.MalformedURLException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextException(org.xwiki.context.ExecutionContextException) URL(java.net.URL) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiConfig(com.xpn.xwiki.XWikiConfig) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with ExecutionContextException

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

the class DefaultDistributionManager method startFarmJob.

@Override
public DefaultDistributionJob startFarmJob() {
    try {
        this.farmDistributionJob = this.componentManager.getInstance(Job.class, DefaultDistributionJob.HINT);
        XWikiContext xcontext = this.xcontextProvider.get();
        final DistributionRequest request = new DistributionRequest();
        request.setId(getFarmJobId());
        request.setWiki(xcontext.getMainXWiki());
        request.setUserReference(xcontext.getUserReference());
        request.setInteractive(this.configuration.getProperty("distribution.job.interactive", true));
        Thread distributionJobThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // Create a clean Execution Context
                ExecutionContext context = new ExecutionContext();
                try {
                    executionContextManager.initialize(context);
                } catch (ExecutionContextException e) {
                    throw new RuntimeException("Failed to initialize farm distribution job execution context", e);
                }
                farmDistributionJob.initialize(request);
                farmDistributionJob.run();
            }
        });
        distributionJobThread.setDaemon(true);
        distributionJobThread.setName("Farm distribution initialization");
        distributionJobThread.start();
        // Wait until the job is ready (or finished)
        this.farmDistributionJob.awaitReady();
        return this.farmDistributionJob;
    } catch (Exception e) {
        this.logger.error("Failed to create farm distribution job", e);
    }
    return null;
}
Also used : DistributionRequest(org.xwiki.extension.distribution.internal.job.DistributionRequest) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) DistributionJob(org.xwiki.extension.distribution.internal.job.DistributionJob) Job(org.xwiki.job.Job) DefaultDistributionJob(org.xwiki.extension.distribution.internal.job.DefaultDistributionJob) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) 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