src/org/sonews/feed/PushFeeder.java
changeset 35 ed84c8bdd87b
parent 22 2541bdb54cb2
child 37 74139325d305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/feed/PushFeeder.java	Sun Aug 29 17:28:58 2010 +0200
     1.3 @@ -0,0 +1,118 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.feed;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.util.List;
    1.26 +import java.util.concurrent.ConcurrentLinkedQueue;
    1.27 +import org.sonews.daemon.AbstractDaemon;
    1.28 +import org.sonews.storage.Article;
    1.29 +import org.sonews.storage.Headers;
    1.30 +import org.sonews.storage.StorageBackendException;
    1.31 +import org.sonews.storage.StorageManager;
    1.32 +import org.sonews.util.Log;
    1.33 +import org.sonews.util.io.ArticleWriter;
    1.34 +
    1.35 +/**
    1.36 + * Pushes new articles to remote newsservers. This feeder sleeps until a new
    1.37 + * message is posted to the sonews instance.
    1.38 + * @author Christian Lins
    1.39 + * @since sonews/0.5.0
    1.40 + */
    1.41 +class PushFeeder extends AbstractDaemon
    1.42 +{
    1.43 +  
    1.44 +  private ConcurrentLinkedQueue<Article> articleQueue = 
    1.45 +    new ConcurrentLinkedQueue<Article>();
    1.46 +  
    1.47 +  @Override
    1.48 +  public void run()
    1.49 +  {
    1.50 +    while(isRunning())
    1.51 +    {
    1.52 +      try
    1.53 +      {
    1.54 +        synchronized(this)
    1.55 +        {
    1.56 +          this.wait();
    1.57 +        }
    1.58 +        
    1.59 +        List<Subscription> subscriptions = StorageManager.current()
    1.60 +          .getSubscriptions(FeedManager.TYPE_PUSH);
    1.61 +
    1.62 +        Article  article = this.articleQueue.poll();
    1.63 +        String[] groups  = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
    1.64 +        Log.get().info("PushFeed: " + article.getMessageID());
    1.65 +        for(Subscription sub : subscriptions)
    1.66 +        {
    1.67 +          // Circle check
    1.68 +          if(article.getHeader(Headers.PATH)[0].contains(sub.getHost()))
    1.69 +          {
    1.70 +            Log.get().info(article.getMessageID() + " skipped for host "
    1.71 +              + sub.getHost());
    1.72 +            continue;
    1.73 +          }
    1.74 +
    1.75 +          try
    1.76 +          {
    1.77 +            for(String group : groups)
    1.78 +            {
    1.79 +              if(sub.getGroup().equals(group))
    1.80 +              {
    1.81 +                // Delete headers that may cause problems
    1.82 +                article.removeHeader(Headers.NNTP_POSTING_DATE);
    1.83 +                article.removeHeader(Headers.NNTP_POSTING_HOST);
    1.84 +                article.removeHeader(Headers.X_COMPLAINTS_TO);
    1.85 +                article.removeHeader(Headers.X_TRACE);
    1.86 +                article.removeHeader(Headers.XREF);
    1.87 +                
    1.88 +                // POST the message to remote server
    1.89 +                ArticleWriter awriter = new ArticleWriter(sub.getHost(), sub.getPort());
    1.90 +                awriter.writeArticle(article);
    1.91 +                break;
    1.92 +              }
    1.93 +            }
    1.94 +          }
    1.95 +          catch(IOException ex)
    1.96 +          {
    1.97 +            Log.get().warning(ex.toString());
    1.98 +          }
    1.99 +        }
   1.100 +      }
   1.101 +      catch(StorageBackendException ex)
   1.102 +      {
   1.103 +        Log.get().severe(ex.toString());
   1.104 +      }
   1.105 +      catch(InterruptedException ex)
   1.106 +      {
   1.107 +        Log.get().warning("PushFeeder interrupted: " + ex);
   1.108 +      }
   1.109 +    }
   1.110 +  }
   1.111 +  
   1.112 +  public void queueForPush(Article article)
   1.113 +  {
   1.114 +    this.articleQueue.add(article);
   1.115 +    synchronized(this)
   1.116 +    {
   1.117 +      this.notifyAll();
   1.118 +    }
   1.119 +  }
   1.120 +  
   1.121 +}