Refactor logging to fix NullPointerException.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.util;
21 import org.sonews.daemon.AbstractDaemon;
22 import org.sonews.config.Config;
23 import org.sonews.storage.Article;
24 import org.sonews.storage.Headers;
25 import java.util.Date;
26 import java.util.List;
27 import org.sonews.storage.Channel;
28 import org.sonews.storage.Group;
29 import org.sonews.storage.StorageBackendException;
30 import org.sonews.storage.StorageManager;
33 * The purger is started in configurable intervals to search
34 * for messages that can be purged. A message must be deleted if its lifetime
35 * has exceeded, if it was marked as deleted or if the maximum number of
36 * articles in the database is reached.
37 * @author Christian Lins
40 public class Purger extends AbstractDaemon
44 * Loops through all messages and deletes them if their time
57 Thread.sleep(120000); // Sleep for two minutes
60 catch(StorageBackendException ex)
64 catch(InterruptedException ex)
66 Log.get().warning("Purger interrupted: " + ex);
70 private void purgeDeleted()
71 throws StorageBackendException
73 List<Channel> groups = StorageManager.current().getGroups();
74 for(Channel channel : groups)
76 if(!(channel instanceof Group))
79 Group group = (Group)channel;
80 // Look for groups that are marked as deleted
83 List<Long> ids = StorageManager.current().getArticleNumbers(group.getInternalID());
86 StorageManager.current().purgeGroup(group);
87 Log.get().info("Group " + group.getName() + " purged.");
90 for(int n = 0; n < ids.size() && n < 10; n++)
92 Article art = StorageManager.current().getArticle(ids.get(n), group.getInternalID());
93 StorageManager.current().delete(art.getMessageID());
94 Log.get().info("Article " + art.getMessageID() + " purged.");
100 private void purgeOutdated()
101 throws InterruptedException, StorageBackendException
103 long articleMaximum =
104 Config.inst().get("sonews.article.maxnum", Long.MAX_VALUE);
106 Config.inst().get("sonews.article.lifetime", -1);
108 if(lifetime > 0 || articleMaximum < Stats.getInstance().getNumberOfNews())
110 Log.get().info("Purging old messages...");
111 String mid = StorageManager.current().getOldestArticle();
112 if (mid == null) // No articles in the database
117 Article art = StorageManager.current().getArticle(mid);
119 String dateStr = art.getHeader(Headers.DATE)[0];
122 artDate = Date.parse(dateStr) / 1000 / 60 / 60 / 24;
124 catch (IllegalArgumentException ex)
126 Log.get().warning("Could not parse date string: " + dateStr + " " + ex);
129 // Should we delete the message because of its age or because the
130 // article maximum was reached?
131 if (lifetime < 0 || artDate < (new Date().getTime() + lifetime))
133 StorageManager.current().delete(mid);
134 System.out.println("Deleted: " + mid);
138 Thread.sleep(1000 * 60); // Wait 60 seconds
144 Log.get().info("Lifetime purger is disabled");
145 Thread.sleep(1000 * 60 * 30); // Wait 30 minutes