This commit is contained in:
zzz
2011-03-11 02:01:20 +00:00
parent 41fc9cf4ca
commit b048b016ad
5 changed files with 63 additions and 77 deletions

View File

@@ -42,7 +42,7 @@ class AddressBook {
private String location; private String location;
private Map addresses; private Map<String, String> addresses;
private boolean modified; private boolean modified;
@@ -53,7 +53,7 @@ class AddressBook {
* A Map containing human readable addresses as keys, mapped to * A Map containing human readable addresses as keys, mapped to
* base64 i2p destinations. * base64 i2p destinations.
*/ */
public AddressBook(Map addresses) { public AddressBook(Map<String, String> addresses) {
this.addresses = addresses; this.addresses = addresses;
} }
@@ -139,7 +139,7 @@ class AddressBook {
* is a human readable name, and the value is a base64 i2p * is a human readable name, and the value is a base64 i2p
* destination. * destination.
*/ */
public Map getAddresses() { public Map<String, String> getAddresses() {
return this.addresses; return this.addresses;
} }

View File

@@ -30,7 +30,6 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.StringReader; import java.io.StringReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -85,8 +84,8 @@ class ConfigParser {
* if the BufferedReader cannot be read. * if the BufferedReader cannot be read.
* *
*/ */
public static Map parse(BufferedReader input) throws IOException { public static Map<String, String> parse(BufferedReader input) throws IOException {
Map result = new HashMap(); Map<String, String> result = new HashMap();
String inputLine; String inputLine;
inputLine = input.readLine(); inputLine = input.readLine();
while (inputLine != null) { while (inputLine != null) {
@@ -111,11 +110,11 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if file cannot be read. * if file cannot be read.
*/ */
public static Map parse(File file) throws IOException { public static Map<String, String> parse(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file); FileInputStream fileStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader( BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream)); fileStream));
Map rv = ConfigParser.parse(input); Map<String, String> rv = ConfigParser.parse(input);
try { try {
fileStream.close(); fileStream.close();
} catch (IOException ioe) {} } catch (IOException ioe) {}
@@ -132,7 +131,7 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if file cannot be read. * if file cannot be read.
*/ */
public static Map parse(String string) throws IOException { public static Map<String, String> parse(String string) throws IOException {
StringReader stringReader = new StringReader(string); StringReader stringReader = new StringReader(string);
BufferedReader input = new BufferedReader(stringReader); BufferedReader input = new BufferedReader(stringReader);
return ConfigParser.parse(input); return ConfigParser.parse(input);
@@ -149,14 +148,13 @@ class ConfigParser {
* @return A Map containing the key, value pairs from file, or if file * @return A Map containing the key, value pairs from file, or if file
* cannot be read, map. * cannot be read, map.
*/ */
public static Map parse(File file, Map map) { public static Map<String, String> parse(File file, Map<String, String> map) {
Map result; Map<String, String> result;
try { try {
result = ConfigParser.parse(file); result = ConfigParser.parse(file);
for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { for (Map.Entry<String, String> entry : map.entrySet()) {
String key = (String) iter.next(); if (!result.containsKey(entry.getKey()))
if (!result.containsKey(key)) result.put(entry.getKey(), entry.getValue());
result.put(key, map.get(key));
} }
} catch (IOException exp) { } catch (IOException exp) {
result = map; result = map;
@@ -177,9 +175,9 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if input cannot be read. * if input cannot be read.
*/ */
public static List parseSubscriptions(BufferedReader input) public static List<String> parseSubscriptions(BufferedReader input)
throws IOException { throws IOException {
List result = new LinkedList(); List<String> result = new LinkedList();
String inputLine = input.readLine(); String inputLine = input.readLine();
while (inputLine != null) { while (inputLine != null) {
inputLine = ConfigParser.stripComments(inputLine).trim(); inputLine = ConfigParser.stripComments(inputLine).trim();
@@ -201,11 +199,11 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if file cannot be read. * if file cannot be read.
*/ */
public static List parseSubscriptions(File file) throws IOException { public static List<String> parseSubscriptions(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file); FileInputStream fileStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader( BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream)); fileStream));
List rv = ConfigParser.parseSubscriptions(input); List<String> rv = ConfigParser.parseSubscriptions(input);
try { try {
fileStream.close(); fileStream.close();
} catch (IOException ioe) {} } catch (IOException ioe) {}
@@ -221,7 +219,7 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if string cannot be read. * if string cannot be read.
*/ */
public static List parseSubscriptions(String string) throws IOException { public static List<String> parseSubscriptions(String string) throws IOException {
StringReader stringReader = new StringReader(string); StringReader stringReader = new StringReader(string);
BufferedReader input = new BufferedReader(stringReader); BufferedReader input = new BufferedReader(stringReader);
return ConfigParser.parseSubscriptions(input); return ConfigParser.parseSubscriptions(input);
@@ -238,8 +236,8 @@ class ConfigParser {
* @return A List consisting of one element for each line in file, or if * @return A List consisting of one element for each line in file, or if
* file cannot be read, list. * file cannot be read, list.
*/ */
public static List parseSubscriptions(File file, List list) { public static List<String> parseSubscriptions(File file, List<String> list) {
List result; List<String> result;
try { try {
result = ConfigParser.parseSubscriptions(file); result = ConfigParser.parseSubscriptions(file);
} catch (IOException exp) { } catch (IOException exp) {
@@ -263,12 +261,9 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if the BufferedWriter cannot be written to. * if the BufferedWriter cannot be written to.
*/ */
public static void write(Map map, BufferedWriter output) throws IOException { public static void write(Map<String, String> map, BufferedWriter output) throws IOException {
Iterator keyIter = map.keySet().iterator(); for (Map.Entry<String, String> entry : map.entrySet()) {
output.write(entry.getKey() + '=' + entry.getValue());
while (keyIter.hasNext()) {
String key = (String) keyIter.next();
output.write(key + "=" + (String) map.get(key));
output.newLine(); output.newLine();
} }
output.close(); output.close();
@@ -288,7 +283,7 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if file cannot be written to. * if file cannot be written to.
*/ */
public static void write(Map map, File file) throws IOException { public static void write(Map<String, String> map, File file) throws IOException {
boolean success = false; boolean success = false;
if (!isWindows) { if (!isWindows) {
File tmp = SecureFile.createTempFile("temp-", ".tmp", file.getAbsoluteFile().getParentFile()); File tmp = SecureFile.createTempFile("temp-", ".tmp", file.getAbsoluteFile().getParentFile());
@@ -318,12 +313,10 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if output cannot be written to. * if output cannot be written to.
*/ */
public static void writeSubscriptions(List list, BufferedWriter output) public static void writeSubscriptions(List<String> list, BufferedWriter output)
throws IOException { throws IOException {
Iterator iter = list.iterator(); for (String s : list) {
output.write(s);
while (iter.hasNext()) {
output.write((String) iter.next());
output.newLine(); output.newLine();
} }
output.close(); output.close();
@@ -340,7 +333,7 @@ class ConfigParser {
* @throws IOException * @throws IOException
* if output cannot be written to. * if output cannot be written to.
*/ */
public static void writeSubscriptions(List list, File file) public static void writeSubscriptions(List<String> list, File file)
throws IOException { throws IOException {
ConfigParser.writeSubscriptions(list, new BufferedWriter( ConfigParser.writeSubscriptions(list, new BufferedWriter(
new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8"))); new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8")));

View File

@@ -83,26 +83,26 @@ public class Daemon {
* @param home * @param home
* The directory containing addressbook's configuration files. * The directory containing addressbook's configuration files.
*/ */
public void update(Map settings, String home) { public void update(Map<String, String> settings, String home) {
File masterFile = new File(home, (String) settings File masterFile = new File(home, settings
.get("master_addressbook")); .get("master_addressbook"));
File routerFile = new File(home, (String) settings File routerFile = new File(home, settings
.get("router_addressbook")); .get("router_addressbook"));
File published = null; File published = null;
if ("true".equals(settings.get("should_publish"))) if ("true".equals(settings.get("should_publish")))
published = new File(home, (String) settings published = new File(home, settings
.get("published_addressbook")); .get("published_addressbook"));
File subscriptionFile = new File(home, (String) settings File subscriptionFile = new File(home, settings
.get("subscriptions")); .get("subscriptions"));
File logFile = new File(home, (String) settings.get("log")); File logFile = new File(home, settings.get("log"));
File etagsFile = new File(home, (String) settings.get("etags")); File etagsFile = new File(home, settings.get("etags"));
File lastModifiedFile = new File(home, (String) settings File lastModifiedFile = new File(home, settings
.get("last_modified")); .get("last_modified"));
File lastFetchedFile = new File(home, (String) settings File lastFetchedFile = new File(home, settings
.get("last_fetched")); .get("last_fetched"));
long delay; long delay;
try { try {
delay = Long.parseLong((String) settings.get("update_delay")); delay = Long.parseLong(settings.get("update_delay"));
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
delay = 12; delay = 12;
} }
@@ -111,13 +111,13 @@ public class Daemon {
AddressBook master = new AddressBook(masterFile); AddressBook master = new AddressBook(masterFile);
AddressBook router = new AddressBook(routerFile); AddressBook router = new AddressBook(routerFile);
List defaultSubs = new LinkedList(); List<String> defaultSubs = new LinkedList();
// defaultSubs.add("http://i2p/NF2RLVUxVulR3IqK0sGJR0dHQcGXAzwa6rEO4WAWYXOHw-DoZhKnlbf1nzHXwMEJoex5nFTyiNMqxJMWlY54cvU~UenZdkyQQeUSBZXyuSweflUXFqKN-y8xIoK2w9Ylq1k8IcrAFDsITyOzjUKoOPfVq34rKNDo7fYyis4kT5bAHy~2N1EVMs34pi2RFabATIOBk38Qhab57Umpa6yEoE~rbyR~suDRvD7gjBvBiIKFqhFueXsR2uSrPB-yzwAGofTXuklofK3DdKspciclTVzqbDjsk5UXfu2nTrC1agkhLyqlOfjhyqC~t1IXm-Vs2o7911k7KKLGjB4lmH508YJ7G9fLAUyjuB-wwwhejoWqvg7oWvqo4oIok8LG6ECR71C3dzCvIjY2QcrhoaazA9G4zcGMm6NKND-H4XY6tUWhpB~5GefB3YczOqMbHq4wi0O9MzBFrOJEOs3X4hwboKWANf7DT5PZKJZ5KorQPsYRSq0E3wSOsFCSsdVCKUGsAAAA/i2p/hosts.txt"); // defaultSubs.add("http://i2p/NF2RLVUxVulR3IqK0sGJR0dHQcGXAzwa6rEO4WAWYXOHw-DoZhKnlbf1nzHXwMEJoex5nFTyiNMqxJMWlY54cvU~UenZdkyQQeUSBZXyuSweflUXFqKN-y8xIoK2w9Ylq1k8IcrAFDsITyOzjUKoOPfVq34rKNDo7fYyis4kT5bAHy~2N1EVMs34pi2RFabATIOBk38Qhab57Umpa6yEoE~rbyR~suDRvD7gjBvBiIKFqhFueXsR2uSrPB-yzwAGofTXuklofK3DdKspciclTVzqbDjsk5UXfu2nTrC1agkhLyqlOfjhyqC~t1IXm-Vs2o7911k7KKLGjB4lmH508YJ7G9fLAUyjuB-wwwhejoWqvg7oWvqo4oIok8LG6ECR71C3dzCvIjY2QcrhoaazA9G4zcGMm6NKND-H4XY6tUWhpB~5GefB3YczOqMbHq4wi0O9MzBFrOJEOs3X4hwboKWANf7DT5PZKJZ5KorQPsYRSq0E3wSOsFCSsdVCKUGsAAAA/i2p/hosts.txt");
defaultSubs.add("http://www.i2p2.i2p/hosts.txt"); defaultSubs.add("http://www.i2p2.i2p/hosts.txt");
SubscriptionList subscriptions = new SubscriptionList(subscriptionFile, SubscriptionList subscriptions = new SubscriptionList(subscriptionFile,
etagsFile, lastModifiedFile, lastFetchedFile, delay, defaultSubs, (String) settings etagsFile, lastModifiedFile, lastFetchedFile, delay, defaultSubs, settings
.get("proxy_host"), Integer.parseInt((String) settings.get("proxy_port"))); .get("proxy_host"), Integer.parseInt(settings.get("proxy_port")));
Log log = new Log(logFile); Log log = new Log(logFile);
update(master, router, published, subscriptions, log); update(master, router, published, subscriptions, log);
@@ -149,7 +149,7 @@ public class Daemon {
homeFile = new SecureDirectory(System.getProperty("user.dir")); homeFile = new SecureDirectory(System.getProperty("user.dir"));
} }
Map defaultSettings = new HashMap(); Map<String, String> defaultSettings = new HashMap();
defaultSettings.put("proxy_host", "127.0.0.1"); defaultSettings.put("proxy_host", "127.0.0.1");
defaultSettings.put("proxy_port", "4444"); defaultSettings.put("proxy_port", "4444");
defaultSettings.put("master_addressbook", "../userhosts.txt"); defaultSettings.put("master_addressbook", "../userhosts.txt");
@@ -173,7 +173,7 @@ public class Daemon {
File settingsFile = new File(homeFile, settingsLocation); File settingsFile = new File(homeFile, settingsLocation);
Map settings = ConfigParser.parse(settingsFile, defaultSettings); Map<String, String> settings = ConfigParser.parse(settingsFile, defaultSettings);
// wait // wait
try { try {
Thread.sleep(5*60*1000 + I2PAppContext.getGlobalContext().random().nextLong(5*60*1000)); Thread.sleep(5*60*1000 + I2PAppContext.getGlobalContext().random().nextLong(5*60*1000));
@@ -181,7 +181,7 @@ public class Daemon {
} catch (InterruptedException ie) {} } catch (InterruptedException ie) {}
while (_running) { while (_running) {
long delay = Long.parseLong((String) settings.get("update_delay")); long delay = Long.parseLong(settings.get("update_delay"));
if (delay < 1) { if (delay < 1) {
delay = 1; delay = 1;
} }

View File

@@ -35,9 +35,9 @@ import net.i2p.data.DataHelper; // debug
* *
* @author Ragnarok * @author Ragnarok
*/ */
class SubscriptionIterator implements Iterator { class SubscriptionIterator implements Iterator<AddressBook> {
private Iterator subIterator; private Iterator<Subscription> subIterator;
private String proxyHost; private String proxyHost;
private int proxyPort; private int proxyPort;
private final long delay; private final long delay;
@@ -51,7 +51,7 @@ class SubscriptionIterator implements Iterator {
* @param proxyHost proxy hostname * @param proxyHost proxy hostname
* @param proxyPort proxt port number * @param proxyPort proxt port number
*/ */
public SubscriptionIterator(List subscriptions, long delay, String proxyHost, int proxyPort) { public SubscriptionIterator(List<Subscription> subscriptions, long delay, String proxyHost, int proxyPort) {
this.subIterator = subscriptions.iterator(); this.subIterator = subscriptions.iterator();
this.delay = delay; this.delay = delay;
this.proxyHost = proxyHost; this.proxyHost = proxyHost;
@@ -72,8 +72,8 @@ class SubscriptionIterator implements Iterator {
* see java.util.Iterator#next() * see java.util.Iterator#next()
* @return an AddressBook (empty if the minimum delay has not been met) * @return an AddressBook (empty if the minimum delay has not been met)
*/ */
public Object next() { public AddressBook next() {
Subscription sub = (Subscription) this.subIterator.next(); Subscription sub = this.subIterator.next();
if (sub.getLastFetched() + this.delay < I2PAppContext.getGlobalContext().clock().now()) { if (sub.getLastFetched() + this.delay < I2PAppContext.getGlobalContext().clock().now()) {
//System.err.println("Fetching addressbook from " + sub.getLocation()); //System.err.println("Fetching addressbook from " + sub.getLocation());
return new AddressBook(sub, this.proxyHost, this.proxyPort); return new AddressBook(sub, this.proxyHost, this.proxyPort);

View File

@@ -24,7 +24,6 @@ package net.i2p.addressbook;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -37,7 +36,7 @@ import java.util.Map;
*/ */
class SubscriptionList { class SubscriptionList {
private List subscriptions; private List<Subscription> subscriptions;
private File etagsFile; private File etagsFile;
@@ -68,7 +67,7 @@ class SubscriptionList {
* @param proxyPort proxy port number * @param proxyPort proxy port number
*/ */
public SubscriptionList(File locationsFile, File etagsFile, public SubscriptionList(File locationsFile, File etagsFile,
File lastModifiedFile, File lastFetchedFile, long delay, List defaultSubs, String proxyHost, File lastModifiedFile, File lastFetchedFile, long delay, List<String> defaultSubs, String proxyHost,
int proxyPort) { int proxyPort) {
this.subscriptions = new LinkedList(); this.subscriptions = new LinkedList();
this.etagsFile = etagsFile; this.etagsFile = etagsFile;
@@ -77,11 +76,10 @@ class SubscriptionList {
this.delay = delay; this.delay = delay;
this.proxyHost = proxyHost; this.proxyHost = proxyHost;
this.proxyPort = proxyPort; this.proxyPort = proxyPort;
Map etags; Map<String, String> etags;
Map lastModified; Map<String, String> lastModified;
Map lastFetched; Map<String, String> lastFetched;
String location; List<String> locations = ConfigParser.parseSubscriptions(locationsFile,
List locations = ConfigParser.parseSubscriptions(locationsFile,
defaultSubs); defaultSubs);
try { try {
etags = ConfigParser.parse(etagsFile); etags = ConfigParser.parse(etagsFile);
@@ -98,12 +96,10 @@ class SubscriptionList {
} catch (IOException exp) { } catch (IOException exp) {
lastFetched = new HashMap(); lastFetched = new HashMap();
} }
Iterator iter = locations.iterator(); for (String location : locations) {
while (iter.hasNext()) { this.subscriptions.add(new Subscription(location, etags.get(location),
location = (String) iter.next(); lastModified.get(location),
this.subscriptions.add(new Subscription(location, (String) etags.get(location), lastFetched.get(location)));
(String) lastModified.get(location),
(String) lastFetched.get(location)));
} }
} }
@@ -125,13 +121,10 @@ class SubscriptionList {
* won't be read back correctly; the '=' should be escaped. * won't be read back correctly; the '=' should be escaped.
*/ */
public void write() { public void write() {
Iterator iter = this.subscriptions.iterator(); Map<String, String> etags = new HashMap();
Subscription sub; Map<String, String> lastModified = new HashMap();
Map etags = new HashMap(); Map<String, String> lastFetched = new HashMap();
Map lastModified = new HashMap(); for (Subscription sub : this.subscriptions) {
Map lastFetched = new HashMap();
while (iter.hasNext()) {
sub = (Subscription) iter.next();
if (sub.getEtag() != null) { if (sub.getEtag() != null) {
etags.put(sub.getLocation(), sub.getEtag()); etags.put(sub.getLocation(), sub.getEtag());
} }