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