Search in sources :

Example 1 with Job

use of org.xwiki.job.Job in project xwiki-platform by xwiki.

the class XarExtensionHandler method initJobPackageConfiguration.

private void initJobPackageConfiguration(Request request, boolean defaultConflict) throws InterruptedException {
    ExecutionContext context = this.execution.getContext();
    if (context != null && context.getProperty(CONTEXTKEY_PACKAGECONFIGURATION) == null) {
        Job currentJob = null;
        try {
            currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
        } catch (Exception e) {
            this.logger.error("Failed to lookup JobContext, it will be impossible to do interactive install");
        }
        if (currentJob != null) {
            PackageConfiguration configuration = new PackageConfiguration();
            context.setProperty(CONTEXTKEY_PACKAGECONFIGURATION, configuration);
            DocumentReference userReference = getRequestUserReference(AbstractExtensionValidator.PROPERTY_USERREFERENCE, request);
            configuration.setInteractive(request.isInteractive());
            configuration.setUser(userReference);
            configuration.setVerbose(request.isVerbose());
            configuration.setSkipMandatorytDocuments(true);
            configuration.setXarExtensionPlan(getXARExtensionPlan());
            configuration.setJobStatus(currentJob.getStatus());
            // Non blocker conflicts
            configuration.setConflictAction(ConflictType.CURRENT_DELETED, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_DELETED), GlobalAction.CURRENT);
            configuration.setConflictAction(ConflictType.MERGE_SUCCESS, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_SUCCESS), GlobalAction.MERGED);
            // Blocker conflicts
            configuration.setConflictAction(ConflictType.CURRENT_EXIST, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_EXIST), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.NEXT);
            configuration.setConflictAction(ConflictType.MERGE_FAILURE, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_FAILURE), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.MERGED);
            // If user asked to be asked about conflict behavior
            if (defaultConflict && currentJob.getStatus().getRequest().isInteractive()) {
                XWikiContext xcontext = xcontextProvider.get();
                // Make sure the context has the right user
                xcontext.setUserReference(userReference);
                int extensionConflictSetup = NumberUtils.toInt(xcontext.getWiki().getUserPreference("extensionConflictSetup", xcontext), 0);
                if (extensionConflictSetup == 1) {
                    DefaultConflictActionQuestion question = new DefaultConflictActionQuestion(configuration);
                    currentJob.getStatus().ask(question, 1, TimeUnit.HOURS);
                }
            }
        }
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) DefaultConflictActionQuestion(org.xwiki.extension.xar.question.DefaultConflictActionQuestion) XWikiContext(com.xpn.xwiki.XWikiContext) JobContext(org.xwiki.job.JobContext) Job(org.xwiki.job.Job) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with Job

use of org.xwiki.job.Job in project xwiki-platform by xwiki.

the class XarExtensionHandler method uninstall.

@Override
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) throws UninstallException {
    try {
        initializePagesIndex(request);
        initJobPackageConfiguration(request, false);
    } catch (Exception e) {
        throw new UninstallException("Failed to initialize extension plan index", e);
    }
    // probably not be in an expected state)
    if (!request.isRemote()) {
        Job currentJob;
        try {
            currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
        } catch (ComponentLookupException e) {
            currentJob = null;
        }
        if (currentJob == null) {
            String wiki;
            try {
                wiki = XarHandlerUtils.getWikiFromNamespace(namespace);
            } catch (UnsupportedNamespaceException e) {
                throw new UninstallException("Failed to extract wiki id from namespace", e);
            }
            PackageConfiguration configuration = createPackageConfiguration(null, request, wiki);
            try {
                XarInstalledExtension xarLocalExtension = (XarInstalledExtension) this.xarRepository.resolve(installedExtension.getId());
                Collection<XarEntry> pages = xarLocalExtension.getXarPackage().getEntries();
                this.packager.unimportPages(pages, configuration);
            } catch (Exception e) {
                // Not supposed to be possible
                throw new UninstallException("Failed to get xar extension [" + installedExtension.getId() + "] from xar repository", e);
            }
        } else {
        // The actual delete of pages is done in XarExtensionJobFinishedListener
        }
    }
}
Also used : XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarEntry(org.xwiki.xar.XarEntry) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) JobContext(org.xwiki.job.JobContext) Job(org.xwiki.job.Job) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException) UninstallException(org.xwiki.extension.UninstallException)

Example 3 with Job

use of org.xwiki.job.Job in project xwiki-platform by xwiki.

the class QuestionJobResourceReferenceHandler method handle.

@Override
public void handle(ParentResourceReference reference) throws ResourceReferenceHandlerException {
    List<String> jobId = reference.getPathSegments();
    Job job = this.executor.getJob(jobId);
    if (job == null) {
        throw new ResourceReferenceHandlerException("Cannot find any running job with id " + jobId);
    }
    Object question = job.getStatus().getQuestion();
    if (question == null) {
        throw new ResourceReferenceHandlerException("The job with id " + jobId + " does not have any question");
    }
    Request request = this.container.getRequest();
    String prefix = "question/";
    String contentType = "text/html; charset=utf-8";
    // POST request means answer
    if (request instanceof ServletRequest) {
        HttpServletRequest httpRequest = ((ServletRequest) request).getHttpServletRequest();
        if (httpRequest.getMethod().equals("POST")) {
            String token = httpRequest.getParameter("form_token");
            // TODO: should probably move this check in some filter triggered by an annotation
            if (this.csrf.isTokenValid(token)) {
                answer(httpRequest, job, jobId, question);
                prefix = "answer/";
                contentType = "application/json";
            } else {
            // TODO: Throw some exception
            }
        }
    }
    String jobType = job.getType();
    // Provide informations about the job to the template
    this.scriptContextManager.getCurrentScriptContext().setAttribute("job", job, ScriptContext.ENGINE_SCOPE);
    String[] templates = getTemplates(question.getClass(), jobType, prefix);
    if (!tryTemplates(contentType, templates)) {
        throw new ResourceReferenceHandlerException("Cannot find any template for the job with id" + jobId + " (" + Arrays.toString(templates) + ")");
    }
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Request(org.xwiki.container.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Job(org.xwiki.job.Job)

Example 4 with Job

use of org.xwiki.job.Job in project xwiki-platform by xwiki.

the class DeleteAction method deleteToRecycleBin.

protected boolean deleteToRecycleBin(EntityReference entityReference, XWikiContext context) throws XWikiException {
    Job deleteJob = startDeleteJob(entityReference, context);
    // If the user have asked for an asynchronous delete action...
    if (isAsync(context.getRequest())) {
        List<String> jobId = deleteJob.getRequest().getId();
        // We don't redirect to the delete action because by the time the redirect request reaches the server the
        // specified entity may be already deleted and the current user may not have the delete right anymore (e.g.
        // the current user is no longer the creator).
        sendRedirect(context.getResponse(), Utils.getRedirect("view", "xpage=delete&jobId=" + serializeJobId(jobId), context));
        // A redirect has been performed.
        return true;
    }
    // Otherwise...
    try {
        deleteJob.join();
    } catch (InterruptedException e) {
        throw new XWikiException(String.format("Failed to delete [%s]", entityReference), e);
    }
    // No redirect has been performed.
    return false;
}
Also used : Job(org.xwiki.job.Job) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with Job

use of org.xwiki.job.Job in project xwiki-platform by xwiki.

the class AbstractJobTest method run.

protected Job run(Request request) throws Throwable {
    Job job = getMocker().getComponentUnderTest();
    job.initialize(request);
    job.run();
    Throwable error = job.getStatus().getError();
    if (job.getStatus().getError() != null) {
        throw error;
    }
    return job;
}
Also used : Job(org.xwiki.job.Job) AbstractJob(org.xwiki.job.AbstractJob)

Aggregations

Job (org.xwiki.job.Job)41 Test (org.junit.Test)11 JobStatus (org.xwiki.job.event.status.JobStatus)10 AbstractExtensionJob (org.xwiki.extension.job.internal.AbstractExtensionJob)8 InstallJob (org.xwiki.extension.job.internal.InstallJob)8 UninstallJob (org.xwiki.extension.job.internal.UninstallJob)8 JobException (org.xwiki.job.JobException)8 DocumentReference (org.xwiki.model.reference.DocumentReference)8 InstallRequest (org.xwiki.extension.job.InstallRequest)7 UpgradePlanJob (org.xwiki.extension.job.internal.UpgradePlanJob)7 InstallPlanJob (org.xwiki.extension.job.internal.InstallPlanJob)6 UninstallPlanJob (org.xwiki.extension.job.internal.UninstallPlanJob)6 XWikiException (com.xpn.xwiki.XWikiException)5 LogEvent (org.xwiki.logging.event.LogEvent)5 InstallException (org.xwiki.extension.InstallException)4 UninstallException (org.xwiki.extension.UninstallException)4 XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)4 EntityReference (org.xwiki.model.reference.EntityReference)4 WikiReference (org.xwiki.model.reference.WikiReference)4 IOException (java.io.IOException)3