Refactor logging to fix NullPointerException.
3 * see AUTHORS for the list of contributors
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.
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.
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/>.
19 package org.sonews.feed;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.net.Socket;
26 import java.net.SocketException;
27 import java.net.UnknownHostException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.logging.Level;
35 import org.sonews.config.Config;
36 import org.sonews.daemon.AbstractDaemon;
37 import org.sonews.util.Log;
38 import org.sonews.storage.StorageBackendException;
39 import org.sonews.storage.StorageManager;
40 import org.sonews.util.Stats;
41 import org.sonews.util.io.ArticleReader;
42 import org.sonews.util.io.ArticleWriter;
45 * The PullFeeder class regularily checks another Newsserver for new
47 * @author Christian Lins
50 class PullFeeder extends AbstractDaemon
53 private Map<Subscription, Integer> highMarks = new HashMap<Subscription, Integer>();
54 private BufferedReader in;
55 private PrintWriter out;
56 private Set<Subscription> subscriptions = new HashSet<Subscription>();
58 private void addSubscription(final Subscription sub)
60 subscriptions.add(sub);
62 if(!highMarks.containsKey(sub))
64 // Set a initial highMark
65 this.highMarks.put(sub, 0);
70 * Changes to the given group and returns its high mark.
74 private int changeGroup(String groupName)
77 this.out.print("GROUP " + groupName + "\r\n");
80 String line = this.in.readLine();
81 if(line.startsWith("211 "))
83 int highmark = Integer.parseInt(line.split(" ")[3]);
88 throw new IOException("GROUP " + groupName + " returned: " + line);
92 private void connectTo(final String host, final int port)
93 throws IOException, UnknownHostException
95 Socket socket = new Socket(host, port);
96 this.out = new PrintWriter(socket.getOutputStream());
97 this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
99 String line = in.readLine();
100 if(!(line.charAt(0) == '2')) // Could be 200 or 2xx if posting is not allowed
102 throw new IOException(line);
105 // Send MODE READER to peer, some newsservers are friendlier then
106 this.out.println("MODE READER\r\n");
108 line = this.in.readLine();
111 private void disconnect()
114 this.out.print("QUIT\r\n");
124 * Uses the OVER or XOVER command to get a list of message overviews that
125 * may be unknown to this feeder and are about to be peered.
128 * @return A list of message ids with potentially interesting messages.
130 private List<String> over(int start, int end)
133 this.out.print("OVER " + start + "-" + end + "\r\n");
136 String line = this.in.readLine();
137 if(line.startsWith("500 ")) // OVER not supported
139 this.out.print("XOVER " + start + "-" + end + "\r\n");
142 line = this.in.readLine();
145 if(line.startsWith("224 "))
147 List<String> messages = new ArrayList<String>();
148 line = this.in.readLine();
149 while(!".".equals(line))
151 String mid = line.split("\t")[4]; // 5th should be the Message-ID
153 line = this.in.readLine();
159 throw new IOException("Server return for OVER/XOVER: " + line);
168 int pullInterval = 1000 *
169 Config.inst().get(Config.FEED_PULLINTERVAL, 3600);
170 String host = "localhost";
173 Log.get().info("Start PullFeeder run...");
177 this.subscriptions.clear();
178 List<Subscription> subsPull = StorageManager.current()
179 .getSubscriptions(FeedManager.TYPE_PULL);
180 for(Subscription sub : subsPull)
182 addSubscription(sub);
185 catch(StorageBackendException ex)
187 Log.get().log(Level.SEVERE, host, ex);
192 for(Subscription sub : this.subscriptions)
194 host = sub.getHost();
195 port = sub.getPort();
199 Log.get().info("Feeding " + sub.getGroup() + " from " + sub.getHost());
202 connectTo(host, port);
204 catch(SocketException ex)
206 Log.get().info("Skipping " + sub.getHost() + ": " + ex);
210 int oldMark = this.highMarks.get(sub);
211 int newMark = changeGroup(sub.getGroup());
213 if(oldMark != newMark)
215 List<String> messageIDs = over(oldMark, newMark);
217 for(String messageID : messageIDs)
219 if(!StorageManager.current().isArticleExisting(messageID))
223 // Post the message via common socket connection
224 ArticleReader aread =
225 new ArticleReader(sub.getHost(), sub.getPort(), messageID);
226 byte[] abuf = aread.getArticleData();
229 Log.get().warning("Could not feed " + messageID
230 + " from " + sub.getHost());
234 Log.get().info("Feeding " + messageID);
235 ArticleWriter awrite = new ArticleWriter(
236 "localhost", Config.inst().get(Config.PORT, 119));
237 awrite.writeArticle(abuf);
240 Stats.getInstance().mailFeeded(sub.getGroup());
242 catch(IOException ex)
244 // There may be a temporary network failure
245 ex.printStackTrace();
246 Log.get().warning("Skipping mail " + messageID + " due to exception.");
250 this.highMarks.put(sub, newMark);
255 catch(StorageBackendException ex)
257 ex.printStackTrace();
259 catch(IOException ex)
261 ex.printStackTrace();
262 Log.get().severe("PullFeeder run stopped due to exception.");
264 } // for(Subscription sub : subscriptions)
266 Log.get().info("PullFeeder run ended. Waiting " + pullInterval / 1000 + "s");
267 Thread.sleep(pullInterval);
269 catch(InterruptedException ex)
271 Log.get().warning(ex.getMessage());