org/sonews/feed/PushFeeder.java
author cli
Thu, 20 Aug 2009 21:31:03 +0200
changeset 16 5a4a41cfc0a3
parent 15 f2293e8566f5
child 22 2541bdb54cb2
permissions -rw-r--r--
Issue #538 fixed.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.feed;
chris@1
    20
chris@1
    21
import java.io.IOException;
chris@1
    22
import java.util.concurrent.ConcurrentLinkedQueue;
chris@3
    23
import org.sonews.storage.Article;
chris@3
    24
import org.sonews.storage.Headers;
chris@1
    25
import org.sonews.util.Log;
chris@1
    26
import org.sonews.util.io.ArticleWriter;
chris@1
    27
chris@1
    28
/**
chris@1
    29
 * Pushes new articles to remote newsservers. This feeder sleeps until a new
chris@1
    30
 * message is posted to the sonews instance.
chris@1
    31
 * @author Christian Lins
chris@1
    32
 * @since sonews/0.5.0
chris@1
    33
 */
chris@1
    34
class PushFeeder extends AbstractFeeder
chris@1
    35
{
chris@1
    36
  
chris@1
    37
  private ConcurrentLinkedQueue<Article> articleQueue = 
chris@1
    38
    new ConcurrentLinkedQueue<Article>();
chris@1
    39
  
chris@1
    40
  @Override
chris@1
    41
  public void run()
chris@1
    42
  {
chris@1
    43
    while(isRunning())
chris@1
    44
    {
chris@1
    45
      try
chris@1
    46
      {
chris@1
    47
        synchronized(this)
chris@1
    48
        {
chris@1
    49
          this.wait();
chris@1
    50
        }
chris@1
    51
        
chris@1
    52
        Article  article = this.articleQueue.poll();
chris@1
    53
        String[] groups  = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
cli@15
    54
        Log.get().info("PushFeed: " + article.getMessageID());
chris@1
    55
        for(Subscription sub : this.subscriptions)
chris@1
    56
        {
chris@1
    57
          // Circle check
chris@1
    58
          if(article.getHeader(Headers.PATH)[0].contains(sub.getHost()))
chris@1
    59
          {
cli@15
    60
            Log.get().info(article.getMessageID() + " skipped for host "
cli@15
    61
              + sub.getHost());
chris@1
    62
            continue;
chris@1
    63
          }
chris@1
    64
chris@1
    65
          try
chris@1
    66
          {
chris@1
    67
            for(String group : groups)
chris@1
    68
            {
chris@1
    69
              if(sub.getGroup().equals(group))
chris@1
    70
              {
chris@1
    71
                // Delete headers that may cause problems
chris@1
    72
                article.removeHeader(Headers.NNTP_POSTING_DATE);
chris@1
    73
                article.removeHeader(Headers.NNTP_POSTING_HOST);
chris@1
    74
                article.removeHeader(Headers.X_COMPLAINTS_TO);
chris@1
    75
                article.removeHeader(Headers.X_TRACE);
chris@1
    76
                article.removeHeader(Headers.XREF);
chris@1
    77
                
chris@1
    78
                // POST the message to remote server
chris@1
    79
                ArticleWriter awriter = new ArticleWriter(sub.getHost(), sub.getPort());
chris@1
    80
                awriter.writeArticle(article);
chris@1
    81
                break;
chris@1
    82
              }
chris@1
    83
            }
chris@1
    84
          }
chris@1
    85
          catch(IOException ex)
chris@1
    86
          {
cli@15
    87
            Log.get().warning(ex.toString());
chris@1
    88
          }
chris@1
    89
        }
chris@1
    90
      }
chris@1
    91
      catch(InterruptedException ex)
chris@1
    92
      {
cli@15
    93
        Log.get().warning("PushFeeder interrupted: " + ex);
chris@1
    94
      }
chris@1
    95
    }
chris@1
    96
  }
chris@1
    97
  
chris@1
    98
  public void queueForPush(Article article)
chris@1
    99
  {
chris@1
   100
    this.articleQueue.add(article);
chris@1
   101
    synchronized(this)
chris@1
   102
    {
chris@1
   103
      this.notifyAll();
chris@1
   104
    }
chris@1
   105
  }
chris@1
   106
  
chris@1
   107
}