From 54fdfd823b73df1ef6e89b5190059c4dec370be3 Mon Sep 17 00:00:00 2001 From: zzz Date: Tue, 8 Feb 2011 03:29:18 +0000 Subject: [PATCH] - Drop deprecated NamingServices - Add caching and b32 support to DummyNamingService - BlockfileNamingService Cleanups --- build.xml | 2 +- core/java/src/net/i2p/I2PAppContext.java | 21 --- .../src/net/i2p/client/naming/AddressDB.java | 62 ------ .../client/naming/AddressDBNamingService.java | 47 ----- .../client/naming/BlockfileNamingService.java | 34 +--- .../net/i2p/client/naming/DummyAddressDB.java | 52 ----- .../i2p/client/naming/DummyNamingService.java | 29 ++- .../client/naming/FilesystemAddressDB.java | 132 ------------- .../client/naming/HostsTxtNamingService.java | 25 +-- .../net/i2p/client/naming/NamingService.java | 2 +- .../src/net/i2p/client/naming/PetName.java | 178 ------------------ .../src/net/i2p/client/naming/PetNameDB.java | 117 ------------ .../client/naming/PetNameNamingService.java | 66 ------- 13 files changed, 38 insertions(+), 729 deletions(-) delete mode 100644 core/java/src/net/i2p/client/naming/AddressDB.java delete mode 100644 core/java/src/net/i2p/client/naming/AddressDBNamingService.java delete mode 100644 core/java/src/net/i2p/client/naming/DummyAddressDB.java delete mode 100644 core/java/src/net/i2p/client/naming/FilesystemAddressDB.java delete mode 100644 core/java/src/net/i2p/client/naming/PetName.java delete mode 100644 core/java/src/net/i2p/client/naming/PetNameDB.java delete mode 100644 core/java/src/net/i2p/client/naming/PetNameNamingService.java diff --git a/build.xml b/build.xml index 20616d1c2..4d42cfe90 100644 --- a/build.xml +++ b/build.xml @@ -239,7 +239,7 @@ splitindex="true" doctitle="I2P Javadocs for Release ${release.number} Build ${build.number}" windowtitle="I2P Anonymous Network - Java Documentation - Version ${release.number}"> - + diff --git a/core/java/src/net/i2p/I2PAppContext.java b/core/java/src/net/i2p/I2PAppContext.java index 5cfc37ac5..b54ca7969 100644 --- a/core/java/src/net/i2p/I2PAppContext.java +++ b/core/java/src/net/i2p/I2PAppContext.java @@ -7,7 +7,6 @@ import java.util.Random; import java.util.Set; import net.i2p.client.naming.NamingService; -import net.i2p.client.naming.PetNameDB; import net.i2p.crypto.AESEngine; import net.i2p.crypto.CryptixAESEngine; import net.i2p.crypto.DSAEngine; @@ -72,7 +71,6 @@ public class I2PAppContext { private StatManager _statManager; private SessionKeyManager _sessionKeyManager; private NamingService _namingService; - private PetNameDB _petnameDb; private ElGamalEngine _elGamalEngine; private ElGamalAESEngine _elGamalAESEngine; private AESEngine _AESEngine; @@ -89,7 +87,6 @@ public class I2PAppContext { private volatile boolean _statManagerInitialized; private volatile boolean _sessionKeyManagerInitialized; private volatile boolean _namingServiceInitialized; - private volatile boolean _petnameDbInitialized; private volatile boolean _elGamalEngineInitialized; private volatile boolean _elGamalAESEngineInitialized; private volatile boolean _AESEngineInitialized; @@ -177,7 +174,6 @@ public class I2PAppContext { _statManager = null; _sessionKeyManager = null; _namingService = null; - _petnameDb = null; _elGamalEngine = null; _elGamalAESEngine = null; _logManager = null; @@ -580,23 +576,6 @@ public class I2PAppContext { } } - /** @deprecated unused */ - public PetNameDB petnameDb() { - if (!_petnameDbInitialized) - initializePetnameDb(); - return _petnameDb; - } - - /** @deprecated unused */ - private void initializePetnameDb() { - synchronized (this) { - if (_petnameDb == null) { - _petnameDb = new PetNameDB(); - } - _petnameDbInitialized = true; - } - } - /** * This is the ElGamal engine used within this context. While it doesn't * really have anything substantial that is context specific (the algorithm diff --git a/core/java/src/net/i2p/client/naming/AddressDB.java b/core/java/src/net/i2p/client/naming/AddressDB.java deleted file mode 100644 index c43ada589..000000000 --- a/core/java/src/net/i2p/client/naming/AddressDB.java +++ /dev/null @@ -1,62 +0,0 @@ -package net.i2p.client.naming; - -import java.lang.reflect.Constructor; -import java.util.Collection; - -import net.i2p.I2PAppContext; -import net.i2p.data.Address; -import net.i2p.util.Log; - -/** - * @deprecated unused - */ -public abstract class AddressDB { - - private final static Log _log = new Log(NamingService.class); - protected I2PAppContext _context; - - /** what classname should be used as the address db impl? */ - public static final String PROP_IMPL = "i2p.addressdb.impl"; - private static final String DEFAULT_IMPL = "net.i2p.client.naming.FilesystemAddressDB"; - - /** - * The address db should only be constructed and accessed through the - * application context. This constructor should only be used by the - * appropriate application context itself. - * - */ - protected AddressDB(I2PAppContext context) { - _context = context; - } - - private AddressDB() { // nop - } - - /** - * Get an address db instance. This method ensures that there - * will be only one address db instance (singleton) as well as - * choose the implementation from the "i2p.addressdb.impl" system - * property. - */ - public static final synchronized AddressDB createInstance(I2PAppContext context) { - AddressDB instance = null; - String impl = context.getProperty(PROP_IMPL, DEFAULT_IMPL); - try { - Class cls = Class.forName(impl); - Constructor con = cls.getConstructor(new Class[] { I2PAppContext.class }); - instance = (AddressDB)con.newInstance(new Object[] { context }); - } catch (Exception ex) { - _log.error("Cannot load address db implementation", ex); - instance = new DummyAddressDB(context); // fallback - } - return instance; - } - - public abstract Address get(String hostname); - public abstract Address put(Address address); - public abstract Address remove(String hostname); - public abstract Address remove(Address address); - public abstract boolean contains(Address address); - public abstract boolean contains(String hostname); - public abstract Collection hostnames(); -} diff --git a/core/java/src/net/i2p/client/naming/AddressDBNamingService.java b/core/java/src/net/i2p/client/naming/AddressDBNamingService.java deleted file mode 100644 index 928212c89..000000000 --- a/core/java/src/net/i2p/client/naming/AddressDBNamingService.java +++ /dev/null @@ -1,47 +0,0 @@ -package net.i2p.client.naming; - -import java.util.Iterator; - -import net.i2p.I2PAppContext; -import net.i2p.data.Address; -import net.i2p.data.Destination; - -/** - * @deprecated unused - */ -public class AddressDBNamingService extends NamingService { - - private AddressDB _addressdb; - - public AddressDBNamingService(I2PAppContext context) { - super(context); - _addressdb = AddressDB.createInstance(context); - } - - private AddressDBNamingService() { - super(null); - } - - @Override - public Destination lookup(String hostname) { - Address addr = _addressdb.get(hostname); - if (addr != null) { - return addr.getDestination(); - } else { - // If we can't find hostname in the addressdb, assume it's a key. - return lookupBase64(hostname); - } - } - - @Override - public String reverseLookup(Destination dest) { - Iterator iter = _addressdb.hostnames().iterator(); - while (iter.hasNext()) { - Address addr = _addressdb.get((String)iter.next()); - if (addr != null && addr.getDestination().equals(dest)) { - return addr.getHostname(); - } - } - return null; - } -} diff --git a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java index 42e5d7edd..3f687e17e 100644 --- a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java +++ b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java @@ -65,7 +65,7 @@ import net.metanotion.util.skiplist.SkipList; * * */ -public class BlockfileNamingService extends NamingService { +public class BlockfileNamingService extends DummyNamingService { private final Log _log; private final BlockFile _bf; @@ -77,7 +77,6 @@ public class BlockfileNamingService extends NamingService { private static final Serializer _stringSerializer = new StringSerializer(); private static final Serializer _destSerializer = new DestEntrySerializer(); - private static final int BASE32_HASH_LENGTH = 52; // 1 + Hash.HASH_LENGTH * 8 / 5 private static final String HOSTS_DB = "hostsdb.blockfile"; private static final String PROP_HOSTS_FILE = "i2p.hostsfilelist"; private static final String PROP_B32 = "i2p.naming.hostsTxt.useB32"; @@ -178,8 +177,11 @@ public class BlockfileNamingService extends NamingService { if (split <= 0) continue; String key = line.substring(0, split).toLowerCase(); - if (line.indexOf('#') > 0) // trim off any end of line comment + if (line.indexOf('#') > 0) { // trim off any end of line comment line = line.substring(0, line.indexOf('#')).trim(); + if (line.length() < split + 1) + continue; + } String b64 = line.substring(split+1); //.trim() ?????????????? Destination d = lookupBase64(b64); if (d != null) { @@ -193,7 +195,7 @@ public class BlockfileNamingService extends NamingService { if (in != null) try { in.close(); } catch (IOException ioe) {} } total += count; - _log.error("Added " + count + " hosts from " + file); + _log.logAlways(Log.INFO, "Added " + count + " hosts from " + file); _lists.add(hostsfile); } _log.error("DB init took " + DataHelper.formatDuration(_context.clock().now() - start)); @@ -342,28 +344,10 @@ public class BlockfileNamingService extends NamingService { @Override public Destination lookup(String hostname) { - Destination d = getCache(hostname); + Destination d = super.lookup(hostname); if (d != null) return d; - // If it's long, assume it's a key. - if (hostname.length() >= 516) { - d = lookupBase64(hostname); - // What the heck, cache these too - putCache(hostname, d); - return d; - } - - // Try Base32 decoding - if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.endsWith(".b32.i2p") && - Boolean.valueOf(_context.getProperty(PROP_B32, "true")).booleanValue()) { - d = LookupDest.lookupBase32Hash(_context, hostname.substring(0, BASE32_HASH_LENGTH)); - if (d != null) { - putCache(hostname, d); - return d; - } - } - String key = hostname.toLowerCase(); synchronized(_bf) { if (_isClosed) @@ -504,7 +488,6 @@ public class BlockfileNamingService extends NamingService { public static void main(String[] args) { BlockfileNamingService bns = new BlockfileNamingService(I2PAppContext.getGlobalContext()); - //System.out.println("zzz.i2p: " + bns._lists.get(2).get("zzz.i2p")); System.out.println("zzz.i2p: " + bns.lookup("zzz.i2p")); List names = null; try { @@ -531,9 +514,6 @@ public class BlockfileNamingService extends NamingService { } System.out.println("BFNS took " + DataHelper.formatDuration(System.currentTimeMillis() - start)); System.out.println("found " + found + " notfound " + notfound); -synchronized(bns) { -try { bns.wait(); } catch (InterruptedException ie) {} -} bns.close(); HostsTxtNamingService htns = new HostsTxtNamingService(I2PAppContext.getGlobalContext()); diff --git a/core/java/src/net/i2p/client/naming/DummyAddressDB.java b/core/java/src/net/i2p/client/naming/DummyAddressDB.java deleted file mode 100644 index e18a2b7ad..000000000 --- a/core/java/src/net/i2p/client/naming/DummyAddressDB.java +++ /dev/null @@ -1,52 +0,0 @@ -package net.i2p.client.naming; - -import java.util.Collection; - -import net.i2p.I2PAppContext; -import net.i2p.data.Address; - -/** - * @deprecated unused - */ -public class DummyAddressDB extends AddressDB { - - public DummyAddressDB(I2PAppContext context) { - super(context); - } - - @Override - public Address get(String hostname) { - return null; - } - - @Override - public Address put(Address address) { - return null; - } - - @Override - public Address remove(String hostname) { - return null; - } - - @Override - public Address remove(Address address) { - return null; - } - - @Override - public boolean contains(Address address) { - return false; - } - - @Override - public boolean contains(String hostname) { - return false; - } - - @Override - public Collection hostnames() { - return null; - } - -} diff --git a/core/java/src/net/i2p/client/naming/DummyNamingService.java b/core/java/src/net/i2p/client/naming/DummyNamingService.java index ff0855c27..012fe196d 100644 --- a/core/java/src/net/i2p/client/naming/DummyNamingService.java +++ b/core/java/src/net/i2p/client/naming/DummyNamingService.java @@ -11,9 +11,12 @@ import net.i2p.I2PAppContext; import net.i2p.data.Destination; /** - * A Dummy naming service that can only handle base64 destinations. + * A Dummy naming service that can only handle base64 and b32 destinations. */ class DummyNamingService extends NamingService { + private static final int BASE32_HASH_LENGTH = 52; // 1 + Hash.HASH_LENGTH * 8 / 5 + public final static String PROP_B32 = "i2p.naming.hostsTxt.useB32"; + /** * The naming service should only be constructed and accessed through the * application context. This constructor should only be used by the @@ -25,6 +28,28 @@ class DummyNamingService extends NamingService { @Override public Destination lookup(String hostname) { - return lookupBase64(hostname); + Destination d = getCache(hostname); + if (d != null) + return d; + + // If it's long, assume it's a key. + if (hostname.length() >= 516) { + d = lookupBase64(hostname); + // What the heck, cache these too + putCache(hostname, d); + return d; + } + + // Try Base32 decoding + if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.endsWith(".b32.i2p") && + _context.getBooleanPropertyDefaultTrue(PROP_B32)) { + d = LookupDest.lookupBase32Hash(_context, hostname.substring(0, BASE32_HASH_LENGTH)); + if (d != null) { + putCache(hostname, d); + return d; + } + } + + return null; } } diff --git a/core/java/src/net/i2p/client/naming/FilesystemAddressDB.java b/core/java/src/net/i2p/client/naming/FilesystemAddressDB.java deleted file mode 100644 index 806b9e90a..000000000 --- a/core/java/src/net/i2p/client/naming/FilesystemAddressDB.java +++ /dev/null @@ -1,132 +0,0 @@ -package net.i2p.client.naming; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.Properties; - -import net.i2p.I2PAppContext; -import net.i2p.data.Address; -import net.i2p.data.DataFormatException; -import net.i2p.data.DataHelper; -import net.i2p.util.Log; - -/** - * @deprecated unused - */ -public class FilesystemAddressDB extends AddressDB { - - public final static String PROP_ADDRESS_DIR = "i2p.addressdir"; - public final static String DEFAULT_ADDRESS_DIR = "addressDb"; - private final static Log _log = new Log(FilesystemAddressDB.class); - - public FilesystemAddressDB(I2PAppContext context) { - super(context); - - //If the address db directory doesn't exist, create it, using the - //contents of hosts.txt. - String dir = _context.getProperty(PROP_ADDRESS_DIR, DEFAULT_ADDRESS_DIR); - File addrDir = new File(dir); - if (!addrDir.exists()) { - addrDir.mkdir(); - Properties hosts = new Properties(); - File hostsFile = new File("hosts.txt"); - if (hostsFile.exists() && hostsFile.canRead()) { - try { - DataHelper.loadProps(hosts, hostsFile); - } catch (IOException ioe) { - _log.error("Error loading hosts file " + hostsFile, ioe); - } - } - Iterator iter = hosts.keySet().iterator(); - while (iter.hasNext()) { - String hostname = (String)iter.next(); - Address addr = new Address(); - addr.setHostname(hostname); - addr.setDestination(hosts.getProperty(hostname)); - put(addr); - } - } - } - - @Override - public Address get(String hostname) { - String dir = _context.getProperty(PROP_ADDRESS_DIR, DEFAULT_ADDRESS_DIR); - File f = new File(dir, hostname); - if (f.exists() && f.canRead()) { - Address addr = new Address(); - try { - addr.readBytes(new FileInputStream(f)); - } catch (FileNotFoundException exp) { - return null; - } catch (DataFormatException exp) { - _log.error(f.getPath() + " is not a valid address file."); - return null; - } catch (IOException exp) { - _log.error("Error reading " + f.getPath()); - return null; - } - return addr; - } else { - _log.warn(f.getPath() + " does not exist."); - return null; - } - } - - @Override - public Address put(Address address) { - Address previous = get(address.getHostname()); - - String dir = _context.getProperty(PROP_ADDRESS_DIR, DEFAULT_ADDRESS_DIR); - File f = new File(dir, address.getHostname()); - try { - address.writeBytes(new FileOutputStream(f)); - } catch (Exception exp) { - _log.error("Error writing " + f.getPath(), exp); - } - return previous; - } - - @Override - public Address remove(String hostname) { - Address previous = get(hostname); - - String dir = _context.getProperty(PROP_ADDRESS_DIR, DEFAULT_ADDRESS_DIR); - File f = new File(dir, hostname); - f.delete(); - return previous; - } - - @Override - public Address remove(Address address) { - if (contains(address)) { - return remove(address.getHostname()); - } else { - return null; - } - } - - @Override - public boolean contains(Address address) { - Address inDb = get(address.getHostname()); - return inDb.equals(address); - } - - @Override - public boolean contains(String hostname) { - return hostnames().contains(hostname); - } - - @Override - public Collection hostnames() { - String dir = _context.getProperty(PROP_ADDRESS_DIR, DEFAULT_ADDRESS_DIR); - File f = new File(dir); - return Arrays.asList(f.list()); - } - -} diff --git a/core/java/src/net/i2p/client/naming/HostsTxtNamingService.java b/core/java/src/net/i2p/client/naming/HostsTxtNamingService.java index a6045d6a2..4a899977a 100644 --- a/core/java/src/net/i2p/client/naming/HostsTxtNamingService.java +++ b/core/java/src/net/i2p/client/naming/HostsTxtNamingService.java @@ -29,7 +29,7 @@ import net.i2p.util.Log; /** * A naming service based on the "hosts.txt" file. */ -public class HostsTxtNamingService extends NamingService { +public class HostsTxtNamingService extends DummyNamingService { /** * The naming service should only be constructed and accessed through the @@ -45,7 +45,6 @@ public class HostsTxtNamingService extends NamingService { * given file for hostname=destKey values when resolving names */ public final static String PROP_HOSTS_FILE = "i2p.hostsfilelist"; - public final static String PROP_B32 = "i2p.naming.hostsTxt.useB32"; /** default hosts.txt filename */ public final static String DEFAULT_HOSTS_FILE = @@ -62,32 +61,12 @@ public class HostsTxtNamingService extends NamingService { return rv; } - private static final int BASE32_HASH_LENGTH = 52; // 1 + Hash.HASH_LENGTH * 8 / 5 - @Override public Destination lookup(String hostname) { - Destination d = getCache(hostname); + Destination d = super.lookup(hostname); if (d != null) return d; - // If it's long, assume it's a key. - if (hostname.length() >= 516) { - d = lookupBase64(hostname); - // What the heck, cache these too - putCache(hostname, d); - return d; - } - - // Try Base32 decoding - if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.endsWith(".b32.i2p") && - Boolean.valueOf(_context.getProperty(PROP_B32, "true")).booleanValue()) { - d = LookupDest.lookupBase32Hash(_context, hostname.substring(0, BASE32_HASH_LENGTH)); - if (d != null) { - putCache(hostname, d); - return d; - } - } - List filenames = getFilenames(); for (int i = 0; i < filenames.size(); i++) { String hostsfile = (String)filenames.get(i); diff --git a/core/java/src/net/i2p/client/naming/NamingService.java b/core/java/src/net/i2p/client/naming/NamingService.java index fc9a53414..66afd2c4a 100644 --- a/core/java/src/net/i2p/client/naming/NamingService.java +++ b/core/java/src/net/i2p/client/naming/NamingService.java @@ -96,7 +96,7 @@ public abstract class NamingService { Constructor con = cls.getConstructor(new Class[] { I2PAppContext.class }); instance = (NamingService)con.newInstance(new Object[] { context }); } catch (Exception ex) { - _log.error("Cannot loadNaming service implementation", ex); + _log.error("Cannot load naming service " + impl, ex); instance = new DummyNamingService(context); // fallback } return instance; diff --git a/core/java/src/net/i2p/client/naming/PetName.java b/core/java/src/net/i2p/client/naming/PetName.java deleted file mode 100644 index cb0df4ab9..000000000 --- a/core/java/src/net/i2p/client/naming/PetName.java +++ /dev/null @@ -1,178 +0,0 @@ -package net.i2p.client.naming; - -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; - -import net.i2p.data.DataHelper; - -/** - * deprecated unused but can be instantiated through I2PAppContext - */ -public class PetName { - private String _name; - private String _network; - private String _protocol; - private List _groups; - private boolean _isPublic; - private String _location; - - public PetName() { - this(null, null, null, null); - } - public PetName(String name, String network, String protocol, String location) { - _name = name; - _network = network; - _protocol = protocol; - _location = location; - _groups = new ArrayList(); - _isPublic = false; - } - /** - * @param dbLine name:network:protocol:isPublic:group1,group2,group3:location - */ - public PetName(String dbLine) { - _groups = new ArrayList(); - StringTokenizer tok = new StringTokenizer(dbLine, ":\n", true); - int tokens = tok.countTokens(); - //System.out.println("Tokens: " + tokens); - if (tokens < 7) { - return; - } - String s = tok.nextToken(); - if (":".equals(s)) { - _name = null; - } else { - _name = s; - s = tok.nextToken(); // skip past the : - } - s = tok.nextToken(); - if (":".equals(s)) { - _network = null; - } else { - _network = s; - s = tok.nextToken(); // skip past the : - } - s = tok.nextToken(); - if (":".equals(s)) { - _protocol = null; - } else { - _protocol = s; - s = tok.nextToken(); // skip past the : - } - s = tok.nextToken(); - if (":".equals(s)) { - _isPublic = false; - } else { - if ("true".equals(s)) - _isPublic = true; - else - _isPublic = false; - s = tok.nextToken(); // skip past the : - } - s = tok.nextToken(); - if (":".equals(s)) { - // noop - } else { - StringTokenizer gtok = new StringTokenizer(s, ","); - while (gtok.hasMoreTokens()) - _groups.add(gtok.nextToken().trim()); - s = tok.nextToken(); // skip past the : - } - while (tok.hasMoreTokens()) { - if (_location == null) - _location = tok.nextToken(); - else - _location = _location + tok.nextToken(); - } - } - - public String getName() { return _name; } - public String getNetwork() { return _network; } - public String getProtocol() { return _protocol; } - public String getLocation() { return _location; } - public boolean getIsPublic() { return _isPublic; } - public int getGroupCount() { return _groups.size(); } - public String getGroup(int i) { return (String)_groups.get(i); } - - public void setName(String name) { _name = name; } - public void setNetwork(String network) { _network = network; } - public void setProtocol(String protocol) { _protocol = protocol; } - public void setLocation(String location) { _location = location; } - public void setIsPublic(boolean pub) { _isPublic = pub; } - public void addGroup(String name) { - if ( (name != null) && (name.length() > 0) && (!_groups.contains(name)) ) - _groups.add(name); - } - public void removeGroup(String name) { _groups.remove(name); } - public void setGroups(String groups) { - if (groups != null) { - _groups.clear(); - StringTokenizer tok = new StringTokenizer(groups, ", \t"); - while (tok.hasMoreTokens()) - addGroup(tok.nextToken().trim()); - } else { - _groups.clear(); - } - } - public boolean isMember(String group) { - for (int i = 0; i < getGroupCount(); i++) - if (getGroup(i).equals(group)) - return true; - return false; - } - - @Override - public String toString() { - StringBuilder buf = new StringBuilder(256); - if (_name != null) buf.append(_name.trim()); - buf.append(':'); - if (_network != null) buf.append(_network.trim()); - buf.append(':'); - if (_protocol != null) buf.append(_protocol.trim()); - buf.append(':').append(_isPublic).append(':'); - if (_groups != null) { - for (int i = 0; i < _groups.size(); i++) { - buf.append(((String)_groups.get(i)).trim()); - if (i + 1 < _groups.size()) - buf.append(','); - } - } - buf.append(':'); - if (_location != null) buf.append(_location.trim()); - return buf.toString(); - } - - @Override - public boolean equals(Object obj) { - if ( (obj == null) || !(obj instanceof PetName) ) return false; - PetName pn = (PetName)obj; - return DataHelper.eq(_name, pn._name) && - DataHelper.eq(_location, pn._location) && - DataHelper.eq(_network, pn._network) && - DataHelper.eq(_protocol, pn._protocol); - } - @Override - public int hashCode() { - int rv = 0; - rv += DataHelper.hashCode(_name); - rv += DataHelper.hashCode(_location); - rv += DataHelper.hashCode(_network); - rv += DataHelper.hashCode(_protocol); - return rv; - } - - public static void main(String args[]) { - test("a:b:c:true:e:f"); - test("a:::true::d"); - test("a:::true::"); - test("a:b::true::"); - test(":::trye::"); - test("a:b:c:true:e:http://foo.bar"); - } - private static void test(String line) { - PetName pn = new PetName(line); - String val = pn.toString(); - System.out.println("OK? " + val.equals(line) + ": " + line + " [" + val + "]"); - } -} diff --git a/core/java/src/net/i2p/client/naming/PetNameDB.java b/core/java/src/net/i2p/client/naming/PetNameDB.java deleted file mode 100644 index cff2ab56c..000000000 --- a/core/java/src/net/i2p/client/naming/PetNameDB.java +++ /dev/null @@ -1,117 +0,0 @@ -package net.i2p.client.naming; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - - -/** - * deprecated unused but can be instantiated through I2PAppContext - */ -public class PetNameDB { - /** name (String) to PetName mapping */ - private final Map _names; - private String _path; - - public PetNameDB() { - _names = Collections.synchronizedMap(new HashMap()); - } - - public PetName getByName(String name) { - if ( (name == null) || (name.length() <= 0) ) return null; - return (PetName)_names.get(name.toLowerCase()); - } - public void add(PetName pn) { - if ( (pn == null) || (pn.getName() == null) ) return; - _names.put(pn.getName().toLowerCase(), pn); - } - public void clear() { _names.clear(); } - public boolean contains(PetName pn) { return _names.containsValue(pn); } - public boolean containsName(String name) { - if ( (name == null) || (name.length() <= 0) ) return false; - return _names.containsKey(name.toLowerCase()); - } - public boolean isEmpty() { return _names.isEmpty(); } - public Iterator iterator() { return new ArrayList(_names.values()).iterator(); } - public void remove(PetName pn) { - if (pn != null) _names.remove(pn.getName().toLowerCase()); - } - public void removeName(String name) { - if (name != null) _names.remove(name.toLowerCase()); - } - public int size() { return _names.size(); } - public Set getNames() { return new HashSet(_names.keySet()); } - public List getGroups() { - List rv = new ArrayList(); - for (Iterator iter = iterator(); iter.hasNext(); ) { - PetName name = (PetName)iter.next(); - for (int i = 0; i < name.getGroupCount(); i++) - if (!rv.contains(name.getGroup(i))) - rv.add(name.getGroup(i)); - } - return rv; - } - - public PetName getByLocation(String location) { - if (location == null) return null; - synchronized (_names) { - for (Iterator iter = iterator(); iter.hasNext(); ) { - PetName name = (PetName)iter.next(); - if ( (name.getLocation() != null) && (name.getLocation().trim().equals(location.trim())) ) - return name; - } - } - return null; - } - - public void load(String location) throws IOException { - _path = location; - File f = new File(location); - if (!f.exists()) return; - BufferedReader in = null; - try { - in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); - String line = null; - while ( (line = in.readLine()) != null) { - PetName name = new PetName(line); - if (name.getName() != null) - add(name); - } - } finally { - in.close(); - } - } - - public void store(String location) throws IOException { - Writer out = null; - try { - out = new OutputStreamWriter(new FileOutputStream(location), "UTF-8"); - for (Iterator iter = iterator(); iter.hasNext(); ) { - PetName name = (PetName)iter.next(); - if (name != null) - out.write(name.toString() + "\n"); - } - } finally { - out.close(); - } - } - - public void store() throws IOException { - if (_path != null) { - store(_path); - } - } -} diff --git a/core/java/src/net/i2p/client/naming/PetNameNamingService.java b/core/java/src/net/i2p/client/naming/PetNameNamingService.java deleted file mode 100644 index a052315f3..000000000 --- a/core/java/src/net/i2p/client/naming/PetNameNamingService.java +++ /dev/null @@ -1,66 +0,0 @@ -package net.i2p.client.naming; - -import java.io.IOException; - -import net.i2p.I2PAppContext; -import net.i2p.data.Destination; - -/** - * @deprecated unused - */ -public class PetNameNamingService extends NamingService { - - private PetNameDB _petnameDb; - public final static String PROP_PETNAME_FILE = "i2p.petnamefile"; - public final static String DEFAULT_PETNAME_FILE = "petnames.txt"; - - public PetNameNamingService(I2PAppContext context) { - super(context); - _petnameDb = _context.petnameDb(); - String file = _context.getProperty(PROP_PETNAME_FILE, DEFAULT_PETNAME_FILE); - - //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) { -// } -// } - - try { - _petnameDb.load(file); - } catch (IOException ioe) { - } - } - - @Override - public Destination lookup(String hostname) { - PetName name = _petnameDb.getByName(hostname); - if (name != null && name.getNetwork().equalsIgnoreCase("i2p")) { - return lookupBase64(name.getLocation()); - } else { - return lookupBase64(hostname); - } - } - - @Override - public String reverseLookup(Destination dest) { - return _petnameDb.getByLocation(dest.toBase64()).getName(); - } -}