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

File ActivityStreamCleaner.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

18
54
7
1
218
109
17
0.31
7.71
7
2.43

Classes

Class Line # Actions
ActivityStreamCleaner 40 54 0% 17 65
0.1772151917.7%
 

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.activitystream.impl;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.slf4j.Logger;
24    import org.slf4j.LoggerFactory;
25    import org.xwiki.rendering.syntax.Syntax;
26   
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.XWikiException;
29    import com.xpn.xwiki.doc.XWikiDocument;
30    import com.xpn.xwiki.objects.BaseObject;
31    import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin;
32    import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin;
33   
34    /**
35    * Manager for the activitystream cleaning feature. The cleaning consist in deleting old events to prevent infinite
36    * growth of the activitystream table in the database.
37    *
38    * @version $Id: 68d6a99dbf149ddf38d8d3360530e352a21880f3 $
39    */
 
40    public final class ActivityStreamCleaner
41    {
42    /**
43    * Logger.
44    */
45    private static final Logger LOGGER = LoggerFactory.getLogger(ActivityStreamCleaner.class);
46   
47    /**
48    * Document holding the cleaner Job.
49    */
50    private static final String CLEANER_JOB_DOCNAME = "Scheduler.ActivityStreamCleaner";
51   
52    /**
53    * Document holding the cleaner Job.
54    */
55    private static final String CLEANER_JOB_NAME = "ActivityStream cleaner";
56   
57    /**
58    * Document holding the cleaner Job.
59    */
60    private static final String CLEANER_JOB_CRON = "0 0 0 ? * SUN";
61   
62    /**
63    * XWiki Default Admin account.
64    */
65    private static final String XWIKI_DEFAULT_ADMIN = "XWiki.Admin";
66   
67    /**
68    * XWiki Rights class name.
69    */
70    private static final String XWIKI_RIGHTS_CLASS = "XWiki.XWikiRights";
71   
72    /**
73    * Unique instance of ActivityStreamCleaner.
74    */
75    private static ActivityStreamCleaner instance;
76   
77    /**
78    * Hidden constructor of ActivityStreamCleaner only access via getInstance().
79    */
 
80  1 toggle private ActivityStreamCleaner()
81    {
82    }
83   
84    /**
85    * @return a unique instance of ActivityStreamCleaner. Thread safe.
86    */
 
87  1 toggle public static ActivityStreamCleaner getInstance()
88    {
89  1 synchronized (ActivityStreamCleaner.class) {
90  1 if (instance == null) {
91  1 instance = new ActivityStreamCleaner();
92    }
93    }
94   
95  1 return instance;
96    }
97   
98    /**
99    * @param context the XWiki context
100    * @return the number of days activitystream events should be kept (default: infinite duration).
101    */
 
102  1 toggle public static int getNumberOfDaysToKeep(XWikiContext context)
103    {
104  1 ActivityStreamPlugin plugin =
105    (ActivityStreamPlugin) context.getWiki().getPlugin(ActivityStreamPlugin.PLUGIN_NAME, context);
106  1 String pref = plugin.getActivityStreamPreference("daystokeepevents", "0", context);
107  1 return Integer.parseInt(pref);
108    }
109   
110    /**
111    * Set cleaner common documents fields.
112    *
113    * @param doc document to modify
114    * @return true if the fields have been modified, false otherwise
115    */
 
116  0 toggle private boolean setCleanerCommonDocumentsFields(XWikiDocument doc)
117    {
118  0 boolean needsUpdate = false;
119   
120  0 if (StringUtils.isBlank(doc.getAuthor())) {
121  0 needsUpdate = true;
122  0 doc.setAuthor(XWIKI_DEFAULT_ADMIN);
123    }
124   
125  0 if (StringUtils.isBlank(doc.getCreator())) {
126  0 needsUpdate = true;
127  0 doc.setCreator(XWIKI_DEFAULT_ADMIN);
128    }
129   
130  0 if (StringUtils.isBlank(doc.getParent())) {
131  0 needsUpdate = true;
132  0 doc.setParent("Scheduler.WebHome");
133    }
134   
135  0 return needsUpdate;
136    }
137   
138    /**
139    * Create the XWiki rights object in the cleaner job document.
140    *
141    * @param doc Cleaner job document
142    * @param context the XWiki context
143    * @return true if the document has been updated, false otherwise
144    * @throws XWikiException if the object creation fails
145    */
 
146  0 toggle private boolean createWatchListJobRightsObject(XWikiDocument doc, XWikiContext context) throws XWikiException
147    {
148  0 BaseObject rights = doc.getObject(XWIKI_RIGHTS_CLASS);
149  0 if (rights == null) {
150  0 int index = doc.createNewObject(XWIKI_RIGHTS_CLASS, context);
151  0 rights = doc.getObject(XWIKI_RIGHTS_CLASS, index);
152  0 rights.setLargeStringValue("groups", "XWiki.XWikiAdminGroup");
153  0 rights.setStringValue("levels", "edit,delete");
154  0 rights.setIntValue("allow", 1);
155  0 return true;
156    }
157   
158  0 return false;
159    }
160   
161    /**
162    * Create the cleaner job document in the wiki.
163    *
164    * @param context the XWiki context
165    * @throws XWikiException if the job creation fails
166    */
 
167  0 toggle private void initCleanerJob(XWikiContext context) throws XWikiException
168    {
169  0 XWikiDocument doc;
170  0 boolean needsUpdate = false;
171  0 BaseObject job = null;
172   
173  0 try {
174  0 doc = context.getWiki().getDocument(CLEANER_JOB_DOCNAME, context);
175  0 needsUpdate = setCleanerCommonDocumentsFields(doc);
176   
177  0 job = doc.getXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE);
178  0 if (job == null) {
179  0 needsUpdate = true;
180  0 job = doc.newXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE, context);
181  0 job.setStringValue("jobName", CLEANER_JOB_NAME);
182  0 job.setStringValue("jobClass", ActivityStreamCleanerJob.class.getName());
183  0 job.setStringValue("cron", CLEANER_JOB_CRON);
184  0 job.setStringValue("contextUser", XWIKI_DEFAULT_ADMIN);
185  0 job.setStringValue("contextLang", "en");
186  0 job.setStringValue("contextDatabase", "xwiki");
187    }
188   
189  0 needsUpdate = createWatchListJobRightsObject(doc, context);
190   
191  0 if (StringUtils.isBlank(doc.getContent())) {
192  0 needsUpdate = true;
193  0 doc.setContent("{{include reference=\"XWiki.SchedulerJobSheet\"/}}");
194  0 doc.setSyntax(Syntax.XWIKI_2_0);
195    }
196   
197  0 if (needsUpdate) {
198  0 context.getWiki().saveDocument(doc, "", true, context);
199  0 ((SchedulerPlugin) context.getWiki().getPlugin("scheduler", context)).scheduleJob(job, context);
200    }
201    } catch (Exception e) {
202  0 LOGGER.error("Cannot initialize ActivityStreamCleanerJob", e);
203    }
204    }
205   
206    /**
207    * Method that must be called on plugin init. Create the scheduler job.
208    *
209    * @param context the XWiki context
210    * @throws XWikiException if the job creation failed
211    */
 
212  1 toggle public void init(XWikiContext context) throws XWikiException
213    {
214  1 if (getNumberOfDaysToKeep(context) > 0) {
215  0 initCleanerJob(context);
216    }
217    }
218    }