1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.HashSet; |
27 |
|
import java.util.Iterator; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.Set; |
31 |
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
32 |
|
|
33 |
|
import javax.inject.Inject; |
34 |
|
import javax.inject.Named; |
35 |
|
import javax.inject.Provider; |
36 |
|
import javax.inject.Singleton; |
37 |
|
|
38 |
|
import org.slf4j.Logger; |
39 |
|
import org.xwiki.component.annotation.Component; |
40 |
|
import org.xwiki.component.phase.Initializable; |
41 |
|
import org.xwiki.component.phase.InitializationException; |
42 |
|
import org.xwiki.configuration.ConfigurationSource; |
43 |
|
import org.xwiki.query.Query; |
44 |
|
import org.xwiki.query.QueryManager; |
45 |
|
import org.xwiki.watchlist.internal.documents.WatchListClassDocumentInitializer; |
46 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
47 |
|
|
48 |
|
import com.xpn.xwiki.XWikiContext; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
|
55 |
|
@Component |
56 |
|
@Singleton |
|
|
| 76.7% |
Uncovered Elements: 28 (120) |
Complexity: 22 |
Complexity Density: 0.23 |
|
57 |
|
public class DefaultWatchListNotificationCache implements WatchListNotificationCache, Initializable |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public static final String REALTIME_INTERVAL_ID = "realtime"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@Inject |
68 |
|
private Provider<XWikiContext> contextProvider; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@Inject |
74 |
|
private WikiDescriptorManager wikiDescriptorManager; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@Inject |
80 |
|
private QueryManager queryManager; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private Logger logger; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
private Map<String, Set<String>> intervalToSubscribersMap = new HashMap<>(); |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
private List<String> intervals; |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
private ReentrantReadWriteLock subscribersLock = new ReentrantReadWriteLock(); |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
private ReentrantReadWriteLock intervalsLock = new ReentrantReadWriteLock(); |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
@Inject |
112 |
|
@Named("xwikiproperties") |
113 |
|
private ConfigurationSource xwikiProperties; |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
@throws |
119 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
120 |
1 |
@Override... |
121 |
|
public void initialize() throws InitializationException |
122 |
|
{ |
123 |
1 |
XWikiContext context = contextProvider.get(); |
124 |
|
|
125 |
|
|
126 |
1 |
try { |
127 |
1 |
intervals = new ArrayList<String>(); |
128 |
|
|
129 |
1 |
if (xwikiProperties.getProperty("watchlist.realtime.enabled", false)) { |
130 |
|
|
131 |
|
|
132 |
1 |
intervals.add(REALTIME_INTERVAL_ID); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
1 |
Query jobDocumentsQuery = queryManager.getNamedQuery("getWatchlistJobDocuments"); |
137 |
|
|
138 |
1 |
jobDocumentsQuery.setWiki(context.getWikiId()); |
139 |
1 |
List<String> jobDocumentNames = jobDocumentsQuery.execute(); |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
1 |
intervals.addAll(jobDocumentNames); |
145 |
|
} catch (Exception e) { |
146 |
0 |
throw new InitializationException("Failed to initialize the cache of watchlist intervals.", e); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
1 |
for (String jobDocumentName : intervals) { |
151 |
2 |
initSubscribersCache(jobDocumentName); |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@param |
159 |
|
@param |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
161 |
5 |
private void initSubscribersCache(String intervalId)... |
162 |
|
{ |
163 |
|
|
164 |
5 |
List<Object> queryParams = new ArrayList<Object>(); |
165 |
5 |
queryParams.add(WatchListClassDocumentInitializer.DOCUMENT_FULL_NAME); |
166 |
5 |
queryParams.add(intervalId); |
167 |
5 |
queryParams.add(DefaultWatchListStore.USERS_CLASS); |
168 |
|
|
169 |
5 |
Set<String> subscribersForJob = |
170 |
|
globalSearchDocuments(", BaseObject as obj, StringProperty as prop, BaseObject as userobj where" |
171 |
|
+ " doc.fullName=obj.name and obj.className=? and obj.id=prop.id.id and prop.value=?" |
172 |
|
+ " and doc.fullName=userobj.name and userobj.className=?", 0, 0, queryParams); |
173 |
|
|
174 |
5 |
subscribersLock.writeLock().lock(); |
175 |
5 |
try { |
176 |
5 |
intervalToSubscribersMap.put(intervalId, subscribersForJob); |
177 |
|
} finally { |
178 |
5 |
subscribersLock.writeLock().unlock(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@param |
187 |
|
@param |
188 |
|
@param |
189 |
|
@return |
190 |
|
|
|
|
| 78.3% |
Uncovered Elements: 5 (23) |
Complexity: 5 |
Complexity Density: 0.24 |
|
191 |
5 |
private Set<String> globalSearchDocuments(String request, int nb, int start, List<Object> values)... |
192 |
|
{ |
193 |
5 |
Collection<String> wikiServers = new ArrayList<String>(); |
194 |
5 |
Set<String> results = new HashSet<>(); |
195 |
|
|
196 |
5 |
try { |
197 |
5 |
wikiServers = wikiDescriptorManager.getAllIds(); |
198 |
|
} catch (Exception e) { |
199 |
0 |
logger.error("Failed to get the list of wikis", e); |
200 |
|
} |
201 |
|
|
202 |
5 |
try { |
203 |
|
|
204 |
5 |
Query query = queryManager.createQuery(request, Query.HQL); |
205 |
5 |
query.setOffset(start); |
206 |
5 |
query.setLimit(nb); |
207 |
5 |
query.bindValues(values); |
208 |
|
|
209 |
|
|
210 |
5 |
for (String wiki : wikiServers) { |
211 |
5 |
String wikiPrefix = wiki + DefaultWatchListStore.WIKI_SPACE_SEP; |
212 |
5 |
try { |
213 |
5 |
query.setWiki(wiki); |
214 |
5 |
List<String> upDocsInWiki = query.execute(); |
215 |
|
|
216 |
|
|
217 |
5 |
Iterator<String> it = upDocsInWiki.iterator(); |
218 |
5 |
while (it.hasNext()) { |
219 |
0 |
results.add(wikiPrefix + it.next()); |
220 |
|
} |
221 |
|
} catch (Exception e) { |
222 |
0 |
logger.error("Failed to search in wiki [{}]", wiki, e); |
223 |
|
} |
224 |
|
} |
225 |
|
} catch (Exception e) { |
226 |
0 |
logger.error("Failed to create query", e); |
227 |
|
} |
228 |
|
|
229 |
5 |
return results; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
@param |
236 |
|
@param |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
238 |
0 |
private void destroySubscribersCache(String intervalId)... |
239 |
|
{ |
240 |
0 |
subscribersLock.writeLock().lock(); |
241 |
0 |
try { |
242 |
0 |
intervalToSubscribersMap.remove(intervalId); |
243 |
|
} finally { |
244 |
0 |
subscribersLock.writeLock().unlock(); |
245 |
|
} |
246 |
|
} |
247 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
248 |
59 |
@Override... |
249 |
|
public Collection<String> getSubscribers(String intervalId) |
250 |
|
{ |
251 |
59 |
Set<String> result = null; |
252 |
|
|
253 |
59 |
subscribersLock.readLock().lock(); |
254 |
59 |
try { |
255 |
59 |
result = intervalToSubscribersMap.get(intervalId); |
256 |
|
} finally { |
257 |
59 |
subscribersLock.readLock().unlock(); |
258 |
|
} |
259 |
|
|
260 |
59 |
if (result == null) { |
261 |
0 |
return Collections.emptySet(); |
262 |
|
} else { |
263 |
59 |
return new HashSet<>(result); |
264 |
|
} |
265 |
|
} |
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
267 |
8 |
@Override... |
268 |
|
public boolean addSubscriber(String jobId, String user) |
269 |
|
{ |
270 |
8 |
subscribersLock.writeLock().lock(); |
271 |
8 |
try { |
272 |
8 |
Set<String> subscribersForJob = intervalToSubscribersMap.get(jobId); |
273 |
|
|
274 |
8 |
if (subscribersForJob != null) { |
275 |
2 |
return subscribersForJob.add(user); |
276 |
|
} |
277 |
|
|
278 |
6 |
return false; |
279 |
|
} finally { |
280 |
8 |
subscribersLock.writeLock().unlock(); |
281 |
|
} |
282 |
|
} |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
284 |
2 |
@Override... |
285 |
|
public boolean moveSubscriber(String oldIntervalId, String newIntervalId, String user) |
286 |
|
{ |
287 |
|
|
288 |
2 |
subscribersLock.writeLock().lock(); |
289 |
2 |
try { |
290 |
|
|
291 |
2 |
removeSubscriber(oldIntervalId, user); |
292 |
|
|
293 |
|
|
294 |
2 |
return addSubscriber(newIntervalId, user); |
295 |
|
} finally { |
296 |
2 |
subscribersLock.writeLock().unlock(); |
297 |
|
} |
298 |
|
} |
299 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
300 |
4 |
@Override... |
301 |
|
public boolean removeSubscriber(String intervalId, String user) |
302 |
|
{ |
303 |
4 |
subscribersLock.writeLock().lock(); |
304 |
4 |
try { |
305 |
4 |
Set<String> subscribersForJob = intervalToSubscribersMap.get(intervalId); |
306 |
|
|
307 |
4 |
if (subscribersForJob != null) { |
308 |
1 |
return subscribersForJob.remove(user); |
309 |
|
} |
310 |
|
} finally { |
311 |
4 |
subscribersLock.writeLock().unlock(); |
312 |
|
} |
313 |
|
|
314 |
3 |
return false; |
315 |
|
} |
316 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
317 |
115 |
@Override... |
318 |
|
public List<String> getIntervals() |
319 |
|
{ |
320 |
115 |
intervalsLock.readLock().lock(); |
321 |
115 |
try { |
322 |
|
|
323 |
115 |
return new ArrayList<String>(intervals); |
324 |
|
} finally { |
325 |
115 |
intervalsLock.readLock().unlock(); |
326 |
|
} |
327 |
|
} |
328 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
329 |
0 |
@Override... |
330 |
|
public boolean removeInterval(String intervalId) |
331 |
|
{ |
332 |
|
|
333 |
0 |
intervalsLock.writeLock().lock(); |
334 |
0 |
subscribersLock.writeLock().lock(); |
335 |
0 |
try { |
336 |
0 |
if (intervals.remove(intervalId)) { |
337 |
0 |
destroySubscribersCache(intervalId); |
338 |
|
|
339 |
0 |
return true; |
340 |
|
} |
341 |
|
|
342 |
0 |
return false; |
343 |
|
} finally { |
344 |
0 |
subscribersLock.writeLock().unlock(); |
345 |
0 |
intervalsLock.writeLock().unlock(); |
346 |
|
} |
347 |
|
} |
348 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
349 |
3 |
@Override... |
350 |
|
public boolean addInterval(String jobDocument) |
351 |
|
{ |
352 |
|
|
353 |
3 |
intervalsLock.writeLock().lock(); |
354 |
3 |
subscribersLock.writeLock().lock(); |
355 |
3 |
try { |
356 |
3 |
if (intervals.add(jobDocument)) { |
357 |
3 |
initSubscribersCache(jobDocument); |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
3 |
return true; |
362 |
|
} |
363 |
|
|
364 |
0 |
return false; |
365 |
|
} finally { |
366 |
3 |
subscribersLock.writeLock().unlock(); |
367 |
3 |
intervalsLock.writeLock().unlock(); |
368 |
|
} |
369 |
|
} |
370 |
|
} |