trunk/com/so/news/storage/Purger.java
author chris <chris@marvin>
Tue, 20 Jan 2009 10:21:03 +0100
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
chris@0
     1
/*
chris@0
     2
 *   StarOffice News Server
chris@0
     3
 *   see AUTHORS for the list of contributors
chris@0
     4
 *
chris@0
     5
 *   This program is free software: you can redistribute it and/or modify
chris@0
     6
 *   it under the terms of the GNU General Public License as published by
chris@0
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@0
     8
 *   (at your option) any later version.
chris@0
     9
 *
chris@0
    10
 *   This program is distributed in the hope that it will be useful,
chris@0
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@0
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@0
    13
 *   GNU General Public License for more details.
chris@0
    14
 *
chris@0
    15
 *   You should have received a copy of the GNU General Public License
chris@0
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@0
    17
 */
chris@0
    18
chris@0
    19
package com.so.news.storage;
chris@0
    20
chris@0
    21
import java.util.Date;
chris@0
    22
chris@0
    23
import com.so.news.Config;
chris@0
    24
import com.so.news.Debug;
chris@0
    25
chris@0
    26
/**
chris@0
    27
 * The purger is started in configurable intervals to search
chris@0
    28
 * for old messages that can be purged.
chris@0
    29
 * @author Christian Lins
chris@0
    30
 */
chris@0
    31
public class Purger extends Thread
chris@0
    32
{
chris@0
    33
  private int interval;
chris@0
    34
  
chris@0
    35
  public Purger()
chris@0
    36
  {
chris@0
    37
    setDaemon(true); // Daemons run only along with the main thread
chris@0
    38
    setPriority(Thread.MIN_PRIORITY);
chris@0
    39
chris@0
    40
    this.interval = Config.getInstance().get("n3tpd.article.lifetime", 30) * 24 * 60 * 60 * 1000; // Milliseconds
chris@0
    41
    if(this.interval < 0)
chris@0
    42
      this.interval = Integer.MAX_VALUE;
chris@0
    43
  }
chris@0
    44
  
chris@0
    45
  /**
chris@0
    46
   * Runloop of this Purger class.
chris@0
    47
   */
chris@0
    48
  @Override
chris@0
    49
  public void run()
chris@0
    50
  {
chris@0
    51
    for(;;)
chris@0
    52
    {
chris@0
    53
      purge();
chris@0
    54
chris@0
    55
      try
chris@0
    56
      {
chris@0
    57
        sleep(interval);
chris@0
    58
      }
chris@0
    59
      catch(InterruptedException e)
chris@0
    60
      {
chris@0
    61
        e.printStackTrace(Debug.getInstance().getStream());
chris@0
    62
      }
chris@0
    63
    }
chris@0
    64
  }
chris@0
    65
chris@0
    66
  /**
chris@0
    67
   * Loops through all messages and deletes them if their time
chris@0
    68
   * has come.
chris@0
    69
   */
chris@0
    70
  private void purge()
chris@0
    71
  {
chris@0
    72
    Debug.getInstance().log("Purging old messages...");
chris@0
    73
chris@0
    74
    try
chris@0
    75
    {
chris@0
    76
      for(;;)
chris@0
    77
      {
chris@0
    78
        Article art = null; //Database.getInstance().getOldestArticle();
chris@0
    79
        if(art == null) // No articles in the database
chris@0
    80
          break;
chris@0
    81
        
chris@0
    82
        if(art.getDate().getTime() < (new Date().getTime() + this.interval))
chris@0
    83
        {
chris@0
    84
          Database.getInstance().delete(art);
chris@0
    85
          Debug.getInstance().log("Deleted: " + art);
chris@0
    86
        }
chris@0
    87
        else
chris@0
    88
          break;
chris@0
    89
      }
chris@0
    90
    }
chris@0
    91
    catch(Exception ex)
chris@0
    92
    {
chris@0
    93
      ex.printStackTrace();
chris@0
    94
    }
chris@0
    95
  }
chris@0
    96
}