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

File DistributionInitializerListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

16
18
6
1
127
72
15
0.83
3
6
2.5

Classes

Class Line # Actions
DistributionInitializerListener 46 18 0% 15 12
0.770%
 

Contributing tests

No tests hitting this source file were found.

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.extension.distribution.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.bridge.event.ActionExecutingEvent;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.extension.distribution.internal.DistributionManager.DistributionState;
32    import org.xwiki.observation.EventListener;
33    import org.xwiki.observation.event.Event;
34   
35    import com.xpn.xwiki.XWikiContext;
36   
37    /**
38    * Initialize farm distribution job.
39    *
40    * @version $Id: 42dc0b4b706489b2cfc70a75d7cb71281bbcbe74 $
41    * @since 4.2M3
42    */
43    @Component
44    @Named("DistributionInitializerListener")
45    @Singleton
 
46    public class DistributionInitializerListener implements EventListener
47    {
48    /**
49    * The list of events to listen to.
50    */
51    private static final List<Event> EVENTS = Arrays.<Event>asList(new ActionExecutingEvent("view"),
52    new ActionExecutingEvent("distribution"));
53   
54    /**
55    * The component used to get information about the current distribution.
56    */
57    @Inject
58    private DistributionManager distributionManager;
59   
60    @Inject
61    private DistributionConfiguration distributionConfiguration;
62   
 
63  1 toggle @Override
64    public List<Event> getEvents()
65    {
66  1 return EVENTS;
67    }
68   
 
69  4 toggle @Override
70    public String getName()
71    {
72  4 return "DistributionInitializerListener";
73    }
74   
 
75  68 toggle @Override
76    public void onEvent(Event event, Object arg1, Object arg2)
77    {
78  68 XWikiContext xcontext = (XWikiContext) arg2;
79   
80    // Do nothing if the automatic start of DW is disabled
81  68 if (!isAutoDistributionWizardEnabled(xcontext)) {
82  64 return;
83    }
84   
85  4 DistributionState distributionState = this.distributionManager.getFarmDistributionState();
86   
87    // Start the Distribution Wizard only if the current user has the right to access it
88  4 if (distributionState != DistributionState.NONE && this.distributionManager.canDisplayDistributionWizard()) {
89  4 if (xcontext.isMainWiki()) {
90  0 if (this.distributionManager.getFarmJob() == null) {
91  0 startFarmJob();
92    }
93    } else {
94  4 String wiki = xcontext.getWikiId();
95  4 if (this.distributionManager.getWikiJob(wiki) == null) {
96  3 startWikiJob(wiki);
97    }
98    }
99    }
100    }
101   
102    /**
103    * @return if the automatic launch of DW is enabled
104    */
 
105  68 toggle private boolean isAutoDistributionWizardEnabled(XWikiContext xcontext)
106    {
107  68 return xcontext.isMainWiki() ? distributionConfiguration.isAutoDistributionWizardEnabledForMainWiki()
108    : distributionConfiguration.isAutoDistributionWizardEnabledForWiki();
109    }
110   
 
111  0 toggle private synchronized void startFarmJob()
112    {
113  0 if (this.distributionManager.getFarmJob() == null) {
114  0 this.distributionManager.startFarmJob();
115    }
116    }
117   
118    /**
119    * @param wiki the wiki for which to start the distribution job
120    */
 
121  3 toggle private synchronized void startWikiJob(String wiki)
122    {
123  3 if (this.distributionManager.getWikiJob(wiki) == null) {
124  3 this.distributionManager.startWikiJob(wiki);
125    }
126    }
127    }