org/sonews/feed/PushFeeder.java
author cli
Wed, 12 May 2010 11:18:02 +0200
changeset 31 087ef6fe6a1a
parent 22 2541bdb54cb2
permissions -rw-r--r--
Group.getByName() removed. Channel.getByName() does no longer catch StorageBackendExceptions but throw them further.
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;
cli@22
    22
import java.util.List;
chris@1
    23
import java.util.concurrent.ConcurrentLinkedQueue;
cli@22
    24
import org.sonews.daemon.AbstractDaemon;
chris@3
    25
import org.sonews.storage.Article;
chris@3
    26
import org.sonews.storage.Headers;
cli@22
    27
import org.sonews.storage.StorageBackendException;
cli@22
    28
import org.sonews.storage.StorageManager;
chris@1
    29
import org.sonews.util.Log;
chris@1
    30
import org.sonews.util.io.ArticleWriter;
chris@1
    31
chris@1
    32
/**
chris@1
    33
 * Pushes new articles to remote newsservers. This feeder sleeps until a new
chris@1
    34
 * message is posted to the sonews instance.
chris@1
    35
 * @author Christian Lins
chris@1
    36
 * @since sonews/0.5.0
chris@1
    37
 */
cli@22
    38
class PushFeeder extends AbstractDaemon
chris@1
    39
{
chris@1
    40
  
chris@1
    41
  private ConcurrentLinkedQueue<Article> articleQueue = 
chris@1
    42
    new ConcurrentLinkedQueue<Article>();
chris@1
    43
  
chris@1
    44
  @Override
chris@1
    45
  public void run()
chris@1
    46
  {
chris@1
    47
    while(isRunning())
chris@1
    48
    {
chris@1
    49
      try
chris@1
    50
      {
chris@1
    51
        synchronized(this)
chris@1
    52
        {
chris@1
    53
          this.wait();
chris@1
    54
        }
chris@1
    55
        
cli@22
    56
        List<Subscription> subscriptions = StorageManager.current()
cli@22
    57
          .getSubscriptions(FeedManager.TYPE_PUSH);
cli@22
    58
chris@1
    59
        Article  article = this.articleQueue.poll();
chris@1
    60
        String[] groups  = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
cli@15
    61
        Log.get().info("PushFeed: " + article.getMessageID());
cli@22
    62
        for(Subscription sub : subscriptions)
chris@1
    63
        {
chris@1
    64
          // Circle check
chris@1
    65
          if(article.getHeader(Headers.PATH)[0].contains(sub.getHost()))
chris@1
    66
          {
cli@15
    67
            Log.get().info(article.getMessageID() + " skipped for host "
cli@15
    68
              + sub.getHost());
chris@1
    69
            continue;
chris@1
    70
          }
chris@1
    71
chris@1
    72
          try
chris@1
    73
          {
chris@1
    74
            for(String group : groups)
chris@1
    75
            {
chris@1
    76
              if(sub.getGroup().equals(group))
chris@1
    77
              {
chris@1
    78
                // Delete headers that may cause problems
chris@1
    79
                article.removeHeader(Headers.NNTP_POSTING_DATE);
chris@1
    80
                article.removeHeader(Headers.NNTP_POSTING_HOST);
chris@1
    81
                article.removeHeader(Headers.X_COMPLAINTS_TO);
chris@1
    82
                article.removeHeader(Headers.X_TRACE);
chris@1
    83
                article.removeHeader(Headers.XREF);
chris@1
    84
                
chris@1
    85
                // POST the message to remote server
chris@1
    86
                ArticleWriter awriter = new ArticleWriter(sub.getHost(), sub.getPort());
chris@1
    87
                awriter.writeArticle(article);
chris@1
    88
                break;
chris@1
    89
              }
chris@1
    90
            }
chris@1
    91
          }
chris@1
    92
          catch(IOException ex)
chris@1
    93
          {
cli@15
    94
            Log.get().warning(ex.toString());
chris@1
    95
          }
chris@1
    96
        }
chris@1
    97
      }
cli@22
    98
      catch(StorageBackendException ex)
cli@22
    99
      {
cli@22
   100
        Log.get().severe(ex.toString());
cli@22
   101
      }
chris@1
   102
      catch(InterruptedException ex)
chris@1
   103
      {
cli@15
   104
        Log.get().warning("PushFeeder interrupted: " + ex);
chris@1
   105
      }
chris@1
   106
    }
chris@1
   107
  }
chris@1
   108
  
chris@1
   109
  public void queueForPush(Article article)
chris@1
   110
  {
chris@1
   111
    this.articleQueue.add(article);
chris@1
   112
    synchronized(this)
chris@1
   113
    {
chris@1
   114
      this.notifyAll();
chris@1
   115
    }
chris@1
   116
  }
chris@1
   117
  
chris@1
   118
}