1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.feed

File UpdateThread.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

10
51
16
1
196
139
24
0.47
3.19
16
1.5

Classes

Class Line # Actions
UpdateThread 32 51 0% 24 77
0.00%
 

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 com.xpn.xwiki.plugin.feed;
21   
22    import java.util.Date;
23   
24    import org.xwiki.context.Execution;
25    import org.xwiki.context.ExecutionContext;
26   
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.XWikiException;
29    import com.xpn.xwiki.util.AbstractXWikiRunnable;
30    import com.xpn.xwiki.web.Utils;
31   
 
32    public class UpdateThread extends AbstractXWikiRunnable
33    {
34    protected boolean fullContent;
35   
36    protected String space;
37   
38    protected FeedPlugin feedPlugin;
39   
40    protected int scheduleTimer;
41   
42    protected boolean updateInProgress = false;
43   
44    protected boolean forceUpdate = false;
45   
46    protected boolean stopUpdate = false;
47   
48    protected Date startDate;
49   
50    protected Date endDate;
51   
52    protected int nbLoadedArticles;
53   
54    protected int nbLoadedFeeds;
55   
56    protected int nbLoadedFeedsErrors;
57   
58    protected Exception exception;
59   
60    private XWikiContext xwikiContext;
61   
 
62  0 toggle public UpdateThread(String spaceReference, boolean fullContent, int scheduleTimer, FeedPlugin feedPlugin,
63    XWikiContext context)
64    {
65  0 this.xwikiContext = context.clone();
66  0 this.fullContent = fullContent;
67  0 this.space = spaceReference;
68  0 this.feedPlugin = feedPlugin;
69  0 this.scheduleTimer = scheduleTimer;
70    }
71   
 
72  0 toggle @Override
73    protected void declareProperties(ExecutionContext executionContext)
74    {
75  0 xwikiContext.declareInExecutionContext(executionContext);
76  0 xwikiContext = null;
77    }
78   
79   
 
80  0 toggle public void update()
81    {
82  0 if (!stopUpdate) {
83  0 if (updateInProgress == false) {
84  0 updateInProgress = true;
85  0 nbLoadedFeeds = 0;
86  0 nbLoadedFeedsErrors = 0;
87  0 exception = null;
88  0 nbLoadedArticles = 0;
89  0 endDate = null;
90  0 startDate = new Date();
91  0 XWikiContext context = getXWikiContext();
92  0 try {
93    // Make sure store sessions are cleaned up
94  0 context.getWiki().getStore().cleanUp(context);
95    // update the feeds
96  0 nbLoadedArticles = feedPlugin.updateFeedsInSpace(space, fullContent, true, false, context);
97    } catch (XWikiException e) {
98  0 exception = e;
99  0 e.printStackTrace();
100    } finally {
101  0 updateInProgress = false;
102  0 endDate = new Date();
103  0 context.getWiki().getStore().cleanUp(context);
104    }
105    // an update has been schedule..
106  0 if ((forceUpdate == true) && (stopUpdate == false)) {
107  0 forceUpdate = false;
108  0 update();
109    }
110    } else {
111    // let's schedule an update at the end of the current update
112  0 forceUpdate = true;
113    }
114    }
115    }
116   
 
117  0 toggle private XWikiContext getXWikiContext()
118    {
119  0 return (XWikiContext) Utils.getComponent(Execution.class).getContext().getProperty("xwikicontext");
120    }
121   
 
122  0 toggle public String getSpace()
123    {
124  0 return space;
125    }
126   
 
127  0 toggle public boolean isUpdateInProgress()
128    {
129  0 return updateInProgress;
130    }
131   
 
132  0 toggle public Date getStartDate()
133    {
134  0 return startDate;
135    }
136   
 
137  0 toggle public Date getEndDate()
138    {
139  0 return endDate;
140    }
141   
 
142  0 toggle public int getNbLoadedArticles()
143    {
144  0 return nbLoadedArticles;
145    }
146   
 
147  0 toggle public Exception getException()
148    {
149  0 return exception;
150    }
151   
 
152  0 toggle public void stopUpdate()
153    {
154  0 if (!updateInProgress) {
155  0 feedPlugin.removeUpdateThread(space, this, getXWikiContext());
156    }
157  0 stopUpdate = true;
158    }
159   
 
160  0 toggle public int getNbLoadedFeeds()
161    {
162  0 return nbLoadedFeeds;
163    }
164   
 
165  0 toggle public void setNbLoadedFeeds(int nbLoadedFeeds)
166    {
167  0 this.nbLoadedFeeds = nbLoadedFeeds;
168    }
169   
 
170  0 toggle public int getNbLoadedFeedsErrors()
171    {
172  0 return nbLoadedFeedsErrors;
173    }
174   
 
175  0 toggle public void setNbLoadedFeedsErrors(int nbLoadedFeedsErrors)
176    {
177  0 this.nbLoadedFeedsErrors = nbLoadedFeedsErrors;
178    }
179   
 
180  0 toggle @Override
181    protected void runInternal()
182    {
183  0 while (true) {
184  0 update();
185  0 if (stopUpdate) {
186  0 feedPlugin.removeUpdateThread(space, this, getXWikiContext());
187  0 break;
188    }
189  0 try {
190  0 Thread.sleep(scheduleTimer);
191    } catch (InterruptedException e) {
192  0 break;
193    }
194    }
195    }
196    }