2004-12-31 ragnarok

* Integrated latest addressbook changes (2.0.3) which include support for
      deploying as a .war file with no existing addressbook configuration.
    * Updated main build process to bundle the addressbook.war in the
      i2pinstall.jar and i2pupdate.zip.
This commit is contained in:
jrandom
2005-01-01 00:57:01 +00:00
committed by zzz
parent 70d6332bad
commit 84dc7d9d82
8 changed files with 217 additions and 58 deletions

View File

@@ -99,8 +99,9 @@ public class ConfigParser {
public static Map parse(URL url) throws IOException {
InputStream urlStream;
urlStream = url.openConnection().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(urlStream));
return ConfigParser.parse(br);
BufferedReader input = new BufferedReader(new InputStreamReader(
urlStream));
return ConfigParser.parse(input);
}
/**
@@ -114,11 +115,75 @@ public class ConfigParser {
* if file cannot be read.
*/
public static Map parse(File file) throws IOException {
FileInputStream fileStream;
fileStream = new FileInputStream(file);
BufferedReader br = new BufferedReader(
new InputStreamReader(fileStream));
return ConfigParser.parse(br);
FileInputStream fileStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream));
return ConfigParser.parse(input);
}
/**
* Return a Map using the contents of the String string. See
* parseBufferedReader for details of the input format.
*
* @param string
* A String to parse.
* @return A Map containing the key, value pairs from string.
* @throws IOException
* if file cannot be read.
*/
public static Map parse(String string) throws IOException {
StringReader stringReader = new StringReader(string);
BufferedReader input = new BufferedReader(stringReader);
return ConfigParser.parse(input);
}
/**
* Return a Map using the contents of the File file. If file cannot be read,
* use map instead, and write the result to where file should have been.
*
* @param file
* A File to attempt to parse.
* @param map
* A Map to use as the default, if file fails.
* @return A Map containing the key, value pairs from file, or if file
* cannot be read, map.
*/
public static Map parse(File file, Map map) {
Map result = new HashMap();
try {
result = ConfigParser.parse(file);
} catch (IOException exp) {
result = map;
try {
ConfigParser.write(result, file);
} catch (IOException exp2) {
}
}
return result;
}
/**
* Return a List where each element is a line from the BufferedReader input.
*
* @param input
* A BufferedReader to parse.
* @return A List consisting of one element for each line in input.
* @throws IOException
* if input cannot be read.
*/
public static List parseSubscriptions(BufferedReader input)
throws IOException {
List result = new LinkedList();
String inputLine = input.readLine();
while (inputLine != null) {
inputLine = ConfigParser.stripComments(inputLine).trim();
if (inputLine.length() > 0) {
result.add(inputLine);
}
inputLine = input.readLine();
}
input.close();
return result;
}
/**
@@ -132,57 +197,127 @@ public class ConfigParser {
*/
public static List parseSubscriptions(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
BufferedReader br = new BufferedReader(
new InputStreamReader(fileStream));
BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream));
return ConfigParser.parseSubscriptions(input);
}
/**
* Return a List where each element is a line from the String string.
*
* @param string
* A String to parse.
* @return A List consisting of one element for each line in string.
* @throws IOException
* if string cannot be read.
*/
public static List parseSubscriptions(String string) throws IOException {
StringReader stringReader = new StringReader(string);
BufferedReader input = new BufferedReader(stringReader);
return ConfigParser.parseSubscriptions(input);
}
/**
* Return a List using the contents of the File file. If file cannot be
* read, use list instead, and write the result to where file should have
* been.
*
* @param file
* A File to attempt to parse.
* @param string
* A List to use as the default, if file fails.
* @return A List consisting of one element for each line in file, or if
* file cannot be read, list.
*/
public static List parseSubscriptions(File file, List list) {
List result = new LinkedList();
String inputLine = br.readLine();
while (inputLine != null) {
inputLine = ConfigParser.stripComments(inputLine);
if (inputLine.trim().length() > 0) {
result.add(inputLine.trim());
try {
result = ConfigParser.parseSubscriptions(file);
} catch (IOException exp) {
result = list;
try {
ConfigParser.writeSubscriptions(result, file);
} catch (IOException exp2) {
}
inputLine = br.readLine();
}
br.close();
return result;
}
/**
* Write contents of Map hash to BufferedWriter output. Output is written
* Write contents of Map map to BufferedWriter output. Output is written
* with one key, value pair on each line, in the format: key=value.
*
* @param hash
* @param map
* A Map to write to output.
* @param output
* A BufferedWriter to write the Map to.
* @throws IOException
* if the BufferedWriter cannot be written to.
*/
public static void write(Map hash, BufferedWriter output)
throws IOException {
Iterator keyIter = hash.keySet().iterator();
public static void write(Map map, BufferedWriter output) throws IOException {
Iterator keyIter = map.keySet().iterator();
while (keyIter.hasNext()) {
String key = (String) keyIter.next();
output.write(key + "=" + (String) hash.get(key));
output.write(key + "=" + (String) map.get(key));
output.newLine();
}
output.close();
}
/**
* Write contents of Map hash to the file at location. Output is written
* Write contents of Map map to the File file. Output is written
* with one key, value pair on each line, in the format: key=value.
*
* @param hash
* @param map
* A Map to write to file.
* @param file
* A File to write the Map to.
* @throws IOException
* if file cannot be written to.
*/
public static void write(Map hash, File file) throws IOException {
ConfigParser.write(hash,
new BufferedWriter(new FileWriter(file, false)));
public static void write(Map map, File file) throws IOException {
ConfigParser
.write(map, new BufferedWriter(new FileWriter(file, false)));
}
/**
* Write contents of List list to BufferedReader output. Output is written
* with each element of list on a new line.
*
* @param list
* A List to write to file.
* @param output
* A BufferedReader to write list to.
* @throws IOException
* if output cannot be written to.
*/
public static void writeSubscriptions(List list, BufferedWriter output)
throws IOException {
Iterator iter = list.iterator();
while (iter.hasNext()) {
output.write((String) iter.next());
output.newLine();
}
output.close();
}
/**
* Write contents of List list to File file. Output is written with each
* element of list on a new line.
*
* @param list
* A List to write to file.
* @param file
* A File to write list to.
* @throws IOException
* if output cannot be written to.
*/
public static void writeSubscriptions(List list, File file)
throws IOException {
ConfigParser.writeSubscriptions(list, new BufferedWriter(
new FileWriter(file, false)));
}
}

View File

@@ -24,8 +24,9 @@ package addressbook;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.io.File;
import java.io.IOException;
/**
* Main class of addressbook. Performs updates, and runs the main loop.
@@ -91,8 +92,13 @@ public class Daemon {
AddressBook master = new AddressBook(masterFile);
AddressBook router = new AddressBook(routerFile);
List defaultSubs = new LinkedList();
defaultSubs.add("http://dev.i2p/i2p/hosts.txt");
defaultSubs.add("http://duck.i2p/hosts.txt");
SubscriptionList subscriptions = new SubscriptionList(subscriptionFile,
etagsFile, lastModifiedFile);
etagsFile, lastModifiedFile, defaultSubs);
Log log = new Log(logFile);
Daemon.update(master, router, published, subscriptions, log);
@@ -117,11 +123,28 @@ public class Daemon {
} else {
home = ".";
}
try {
settings = ConfigParser.parse(new File(home, settingsLocation));
} catch (IOException exp) {
System.out.println("Could not load " + settingsLocation);
Map defaultSettings = new HashMap();
defaultSettings.put("proxy_host", "localhost");
defaultSettings.put("proxy_port", "4444");
defaultSettings.put("master_addressbook", "myhosts.txt");
defaultSettings.put("router_addressbook", "../userhosts.txt");
defaultSettings.put("published_addressbook", "../eepsite/docroot/hosts.txt");
defaultSettings.put("log", "log.txt");
defaultSettings.put("subscriptions", "subscriptions.txt");
defaultSettings.put("etags", "etags");
defaultSettings.put("last_modified", "last_modified");
defaultSettings.put("update_delay", "1");
File homeFile = new File(home);
if (!homeFile.exists()) {
boolean created = homeFile.mkdirs();
if (created)
System.out.println("INFO: Addressbook directory " + homeFile.getName() + " created");
else
System.out.println("ERROR: Addressbook directory " + homeFile.getName() + " could not be created");
}
settings = ConfigParser.parse(new File(homeFile, settingsLocation), defaultSettings);
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", (String) settings

View File

@@ -52,6 +52,7 @@ public class Servlet extends GenericServlet {
String[] args = new String[1];
args[0] = config.getInitParameter("home");
DaemonThread thread = new DaemonThread(args);
thread.setDaemon(true);
thread.start();
}

View File

@@ -58,7 +58,7 @@ public class SubscriptionList {
* GET. The file is in the format "url=leastmodified".
*/
public SubscriptionList(File locationsFile, File etagsFile,
File lastModifiedFile) {
File lastModifiedFile, List defaultSubs) {
this.subscriptions = new LinkedList();
this.etagsFile = etagsFile;
this.lastModifiedFile = lastModifiedFile;
@@ -66,11 +66,7 @@ public class SubscriptionList {
Map etags;
Map lastModified;
String location;
try {
locations = ConfigParser.parseSubscriptions(locationsFile);
} catch (IOException exp) {
locations = new LinkedList();
}
locations = ConfigParser.parseSubscriptions(locationsFile, defaultSubs);
try {
etags = ConfigParser.parse(etagsFile);
} catch (IOException exp) {
@@ -90,7 +86,7 @@ public class SubscriptionList {
iter = this.iterator();
}
/**
* Return an iterator over the AddressBooks represented by the Subscriptions
* in this SubscriptionList.