Search in sources :

Example 1 with WikiProvisioningJob

use of org.xwiki.wiki.provisioning.WikiProvisioningJob in project xwiki-platform by xwiki.

the class DefaultWikiProvisioningJobExecutorTest method createAndExecuteJob.

@Test
public void createAndExecuteJob() throws Exception {
    // Mocks
    WikiProvisioningJob provisioningJob = mock(WikiProvisioningJob.class);
    mocker.registerComponent(Job.class, "wikiprovisioning.test", provisioningJob);
    ExecutionContextManager executionContextManager = mock(ExecutionContextManager.class);
    mocker.registerComponent(ExecutionContextManager.class, executionContextManager);
    Execution execution = mock(Execution.class);
    mocker.registerComponent(Execution.class, execution);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    when(xcontext.getUserReference()).thenReturn(user);
    // Execute
    WikiProvisioningJob job = mocker.getComponentUnderTest().createAndExecuteJob("wikiid", "wikiprovisioning.test", "templateid");
    // Verify
    // Id of the job.
    List<String> jobId = new ArrayList<String>();
    jobId.add("wiki");
    jobId.add("provisioning");
    jobId.add("wikiprovisioning.test");
    jobId.add("wikiid");
    verify(provisioningJob).initialize(eq(new WikiProvisioningJobRequest(jobId, "wikiid", "templateid", user)));
    Thread.sleep(100);
    verify(provisioningJob).run();
    // getJobs also works
    assertEquals(mocker.getComponentUnderTest().getJob(jobId), job);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ArrayList(java.util.ArrayList) WikiProvisioningJobRequest(org.xwiki.wiki.provisioning.WikiProvisioningJobRequest) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob) Test(org.junit.Test)

Example 2 with WikiProvisioningJob

use of org.xwiki.wiki.provisioning.WikiProvisioningJob in project xwiki-platform by xwiki.

the class WikiTemplateManagerScriptTest method getWikiProvisioningJobStatus.

@Test
public void getWikiProvisioningJobStatus() throws Exception {
    WikiProvisioningJob job = mock(WikiProvisioningJob.class);
    when(wikiTemplateManager.getWikiProvisioningJob(anyList())).thenReturn(job);
    JobStatus status = mock(JobStatus.class);
    when(job.getStatus()).thenReturn(status);
    List<String> jobId = new ArrayList<String>();
    JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId);
    assertEquals(status, result);
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) ArrayList(java.util.ArrayList) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob) Test(org.junit.Test)

Example 3 with WikiProvisioningJob

use of org.xwiki.wiki.provisioning.WikiProvisioningJob in project xwiki-platform by xwiki.

the class DefaultWikiProvisioningJobExecutor method initialize.

@Override
public void initialize() throws InitializationException {
    this.jobs = new HashMap<List<String>, WikiProvisioningJob>();
    // Setup jobs thread
    BasicThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("XWiki provisioning thread").daemon(true).priority(Thread.MIN_PRIORITY).build();
    this.jobExecutor = Executors.newCachedThreadPool(factory);
}
Also used : BasicThreadFactory(org.apache.commons.lang3.concurrent.BasicThreadFactory) ArrayList(java.util.ArrayList) List(java.util.List) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob)

Example 4 with WikiProvisioningJob

use of org.xwiki.wiki.provisioning.WikiProvisioningJob in project xwiki-platform by xwiki.

the class DefaultWikiProvisioningJobExecutor method createAndExecuteJob.

@Override
public WikiProvisioningJob createAndExecuteJob(String wikiId, String provisioningJobName, Object parameter) throws WikiProvisioningJobException {
    try {
        // Get the context
        XWikiContext xcontext = xcontextProvider.get();
        // Create the job
        WikiProvisioningJob job = componentManager.getInstance(Job.class, provisioningJobName);
        // Id of the new job
        List<String> jobId = new ArrayList<String>();
        jobId.add("wiki");
        jobId.add("provisioning");
        jobId.add(provisioningJobName);
        jobId.add(wikiId);
        // Initialize it
        job.initialize(new WikiProvisioningJobRequest(jobId, wikiId, parameter, xcontext.getUserReference()));
        // Add it to the list of jobs
        jobs.put(jobId, job);
        // Pass it to the executor
        jobExecutor.execute(job);
        // Return the job
        return job;
    } catch (ComponentLookupException e) {
        throw new WikiProvisioningJobException(String.format("Failed to lookup provisioning job component for role [%s]", provisioningJobName), e);
    }
}
Also used : WikiProvisioningJobException(org.xwiki.wiki.provisioning.WikiProvisioningJobException) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) WikiProvisioningJobRequest(org.xwiki.wiki.provisioning.WikiProvisioningJobRequest) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob)

Example 5 with WikiProvisioningJob

use of org.xwiki.wiki.provisioning.WikiProvisioningJob in project xwiki-platform by xwiki.

the class DefaultWikiManagerREST method createWiki.

@Override
@POST
public Response createWiki(@QueryParam("template") String template, Wiki wiki) throws XWikiRestException {
    XWikiContext xcontext = getXWikiContext();
    WikiDescriptor descriptor = null;
    try {
        // Create the wiki
        descriptor = wikiManager.create(wiki.getId(), wiki.getId(), true);
        // Change the descriptor
        if (wiki.getOwner() != null) {
            descriptor.setOwnerId(wiki.getOwner());
        } else {
            descriptor.setOwnerId(entityReferenceSerializer.serialize(xcontext.getUserReference()));
        }
        descriptor.setPrettyName(wiki.getName());
        descriptor.setDescription(wiki.getDescription());
        // Save the descriptor
        wikiDescriptorManager.saveDescriptor(descriptor);
        // Apply a template (if needed)
        if (template != null) {
            WikiProvisioningJob job = wikiTemplateManager.applyTemplate(descriptor.getId(), template);
            job.join();
        }
        // Build the response
        Wiki result = createWiki(objectFactory, uriInfo.getBaseUri(), wiki.getId(), wiki.getOwner(), wiki.getDescription());
        // Add a link to the WebHome of the newly created wiki.
        Link home = objectFactory.createLink();
        home.setRel(Relations.HOME);
        home.setHref(xcontext.getWiki().getURL(descriptor.getMainPageReference(), "view", xcontext));
        result.getLinks().add(home);
        // Return the final response
        return Response.created(UriBuilder.fromUri(uriInfo.getBaseUri()).path(WikiResource.class).build(wiki.getId())).entity(result).build();
    } catch (Exception e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) XWikiContext(com.xpn.xwiki.XWikiContext) Wiki(org.xwiki.rest.model.jaxb.Wiki) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Link(org.xwiki.rest.model.jaxb.Link) XWikiRestException(org.xwiki.rest.XWikiRestException) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob) POST(javax.ws.rs.POST)

Aggregations

WikiProvisioningJob (org.xwiki.wiki.provisioning.WikiProvisioningJob)5 ArrayList (java.util.ArrayList)4 XWikiContext (com.xpn.xwiki.XWikiContext)2 Test (org.junit.Test)2 WikiProvisioningJobRequest (org.xwiki.wiki.provisioning.WikiProvisioningJobRequest)2 List (java.util.List)1 POST (javax.ws.rs.POST)1 BasicThreadFactory (org.apache.commons.lang3.concurrent.BasicThreadFactory)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 Execution (org.xwiki.context.Execution)1 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)1 JobStatus (org.xwiki.job.event.status.JobStatus)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1 Link (org.xwiki.rest.model.jaxb.Link)1 Wiki (org.xwiki.rest.model.jaxb.Wiki)1 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)1 WikiProvisioningJobException (org.xwiki.wiki.provisioning.WikiProvisioningJobException)1