* Implemented a MetaNamingService.

This commit is contained in:
ragnarok
2005-09-18 08:50:56 +00:00
committed by zzz
parent 2bdea23986
commit edf04f07c9
3 changed files with 89 additions and 23 deletions

View File

@ -0,0 +1,60 @@
package net.i2p.client.naming;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.StringTokenizer;
import net.i2p.I2PAppContext;
import net.i2p.data.Destination;
public class MetaNamingService extends NamingService {
private final static String PROP_NAME_SERVICES = "i2p.nameservicelist";
private final static String DEFAULT_NAME_SERVICES =
"net.i2p.client.naming.PetNameNamingService,net.i2p.client.naming.HostsTxtNamingService";
private List _services;
public MetaNamingService(I2PAppContext context) {
super(context);
String list = _context.getProperty(PROP_NAME_SERVICES, DEFAULT_NAME_SERVICES);
StringTokenizer tok = new StringTokenizer(list, ",");
_services = new ArrayList(tok.countTokens());
while (tok.hasMoreTokens()) {
try {
Class cls = Class.forName(tok.nextToken());
Constructor con = cls.getConstructor(new Class[] { I2PAppContext.class });
_services.add(con.newInstance(new Object[] { context }));
} catch (Exception ex) {
_services.add(new DummyNamingService(context)); // fallback
}
}
}
public Destination lookup(String hostname) {
Iterator iter = _services.iterator();
while (iter.hasNext()) {
NamingService ns = (NamingService)iter.next();
Destination dest = ns.lookup(hostname);
if (dest != null) {
return dest;
}
}
return lookupBase64(hostname);
}
public String reverseLookup(Destination dest) {
Iterator iter = _services.iterator();
while (iter.hasNext()) {
NamingService ns = (NamingService)iter.next();
String hostname = ns.reverseLookup(dest);
if (hostname != null) {
return hostname;
}
}
return null;
}
}

View File

@ -22,27 +22,27 @@ public class PetNameNamingService extends NamingService {
//If the petnamedb file doesn't exist, create it, using the
//contents of hosts.txt.
File nameFile = new File(file);
if (!nameFile.exists()) {
Properties hosts = new Properties();
File hostsFile = new File("hosts.txt");
if (hostsFile.exists() && hostsFile.canRead()) {
try {
DataHelper.loadProps(hosts, hostsFile);
} catch (IOException ioe) {
}
}
Iterator iter = hosts.keySet().iterator();
while (iter.hasNext()) {
String hostname = (String)iter.next();
PetName pn = new PetName(hostname, "i2p", "http", hosts.getProperty(hostname));
_petnameDb.set(hostname, pn);
}
try {
_petnameDb.store(file);
} catch (IOException ioe) {
}
}
// File nameFile = new File(file);
// if (!nameFile.exists()) {
// Properties hosts = new Properties();
// File hostsFile = new File("hosts.txt");
// if (hostsFile.exists() && hostsFile.canRead()) {
// try {
// DataHelper.loadProps(hosts, hostsFile);
// } catch (IOException ioe) {
// }
// }
// Iterator iter = hosts.keySet().iterator();
// while (iter.hasNext()) {
// String hostname = (String)iter.next();
// PetName pn = new PetName(hostname, "i2p", "http", hosts.getProperty(hostname));
// _petnameDb.set(hostname, pn);
// }
// try {
// _petnameDb.store(file);
// } catch (IOException ioe) {
// }
// }
try {
_petnameDb.load(file);