chris@0: /* chris@0: * StarOffice News Server chris@0: * see AUTHORS for the list of contributors chris@0: * chris@0: * This program is free software: you can redistribute it and/or modify chris@0: * it under the terms of the GNU General Public License as published by chris@0: * the Free Software Foundation, either version 3 of the License, or chris@0: * (at your option) any later version. chris@0: * chris@0: * This program is distributed in the hope that it will be useful, chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@0: * GNU General Public License for more details. chris@0: * chris@0: * You should have received a copy of the GNU General Public License chris@0: * along with this program. If not, see . chris@0: */ chris@0: chris@0: package com.so.news.storage; chris@0: chris@0: import java.util.Date; chris@0: chris@0: import com.so.news.Config; chris@0: import com.so.news.Debug; chris@0: chris@0: /** chris@0: * The purger is started in configurable intervals to search chris@0: * for old messages that can be purged. chris@0: * @author Christian Lins chris@0: */ chris@0: public class Purger extends Thread chris@0: { chris@0: private int interval; chris@0: chris@0: public Purger() chris@0: { chris@0: setDaemon(true); // Daemons run only along with the main thread chris@0: setPriority(Thread.MIN_PRIORITY); chris@0: chris@0: this.interval = Config.getInstance().get("n3tpd.article.lifetime", 30) * 24 * 60 * 60 * 1000; // Milliseconds chris@0: if(this.interval < 0) chris@0: this.interval = Integer.MAX_VALUE; chris@0: } chris@0: chris@0: /** chris@0: * Runloop of this Purger class. chris@0: */ chris@0: @Override chris@0: public void run() chris@0: { chris@0: for(;;) chris@0: { chris@0: purge(); chris@0: chris@0: try chris@0: { chris@0: sleep(interval); chris@0: } chris@0: catch(InterruptedException e) chris@0: { chris@0: e.printStackTrace(Debug.getInstance().getStream()); chris@0: } chris@0: } chris@0: } chris@0: chris@0: /** chris@0: * Loops through all messages and deletes them if their time chris@0: * has come. chris@0: */ chris@0: private void purge() chris@0: { chris@0: Debug.getInstance().log("Purging old messages..."); chris@0: chris@0: try chris@0: { chris@0: for(;;) chris@0: { chris@0: Article art = null; //Database.getInstance().getOldestArticle(); chris@0: if(art == null) // No articles in the database chris@0: break; chris@0: chris@0: if(art.getDate().getTime() < (new Date().getTime() + this.interval)) chris@0: { chris@0: Database.getInstance().delete(art); chris@0: Debug.getInstance().log("Deleted: " + art); chris@0: } chris@0: else chris@0: break; chris@0: } chris@0: } chris@0: catch(Exception ex) chris@0: { chris@0: ex.printStackTrace(); chris@0: } chris@0: } chris@0: }