1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.provisioning

File DefaultWikiProvisioningJobExecutor.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
18
3
1
127
69
4
0.22
6
3
1.33

Classes

Class Line # Actions
DefaultWikiProvisioningJobExecutor 55 18 0% 4 1
0.9523809695.2%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.wiki.internal.provisioning;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.concurrent.ExecutorService;
27    import java.util.concurrent.Executors;
28   
29    import javax.inject.Inject;
30    import javax.inject.Provider;
31    import javax.inject.Singleton;
32   
33    import org.apache.commons.lang3.concurrent.BasicThreadFactory;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentLookupException;
36    import org.xwiki.component.manager.ComponentManager;
37    import org.xwiki.component.phase.Initializable;
38    import org.xwiki.component.phase.InitializationException;
39    import org.xwiki.job.Job;
40    import org.xwiki.wiki.provisioning.WikiProvisioningJob;
41    import org.xwiki.wiki.provisioning.WikiProvisioningJobException;
42    import org.xwiki.wiki.provisioning.WikiProvisioningJobExecutor;
43    import org.xwiki.wiki.provisioning.WikiProvisioningJobRequest;
44   
45    import com.xpn.xwiki.XWikiContext;
46   
47    /**
48    * Default implementation for {@link org.xwiki.wiki.provisioning.WikiProvisioningJobExecutor}.
49    *
50    * @since 6.0M1
51    * @version $Id: 4c70af9add2ae6de2dee1316e1695249092d61e8 $
52    */
53    @Component
54    @Singleton
 
55    public class DefaultWikiProvisioningJobExecutor implements WikiProvisioningJobExecutor, Initializable
56    {
57    /**
58    * List of all the jobs.
59    */
60    private Map<List<String>, WikiProvisioningJob> jobs;
61   
62    /**
63    * Job Executor.
64    */
65    // TODO: use JobManager instead when it support several threads
66    private ExecutorService jobExecutor;
67   
68    /**
69    * Component manager used to get metadata extractors.
70    */
71    @Inject
72    private ComponentManager componentManager;
73   
74    /**
75    * Provider to get the xwiki context.
76    */
77    @Inject
78    private Provider<XWikiContext> xcontextProvider;
79   
 
80  3 toggle @Override
81    public void initialize() throws InitializationException
82    {
83  3 this.jobs = new HashMap<List<String>, WikiProvisioningJob>();
84   
85    // Setup jobs thread
86  3 BasicThreadFactory factory =
87    new BasicThreadFactory.Builder().namingPattern("XWiki provisioning thread").daemon(true)
88    .priority(Thread.MIN_PRIORITY).build();
89  3 this.jobExecutor = Executors.newCachedThreadPool(factory);
90    }
91   
 
92  1 toggle @Override
93    public WikiProvisioningJob createAndExecuteJob(String wikiId, String provisioningJobName, Object parameter) throws
94    WikiProvisioningJobException
95    {
96  1 try {
97    // Get the context
98  1 XWikiContext xcontext = xcontextProvider.get();
99    // Create the job
100  1 WikiProvisioningJob job = componentManager.getInstance(Job.class, provisioningJobName);
101    // Id of the new job
102  1 List<String> jobId = new ArrayList<String>();
103  1 jobId.add("wiki");
104  1 jobId.add("provisioning");
105  1 jobId.add(provisioningJobName);
106  1 jobId.add(wikiId);
107    // Initialize it
108  1 job.initialize(new WikiProvisioningJobRequest(jobId, wikiId, parameter, xcontext.getUserReference()));
109    // Add it to the list of jobs
110  1 jobs.put(jobId, job);
111    // Pass it to the executor
112  1 jobExecutor.execute(job);
113    // Return the job
114  1 return job;
115    } catch (ComponentLookupException e) {
116  0 throw new WikiProvisioningJobException(
117    String.format("Failed to lookup provisioning job component for role [%s]", provisioningJobName), e);
118    }
119    }
120   
 
121  2 toggle @Override
122    public WikiProvisioningJob getJob(List<String> jobId) throws WikiProvisioningJobException
123    {
124  2 WikiProvisioningJob job = jobs.get(jobId);
125  2 return job;
126    }
127    }