forked from I2P_Developers/i2p.i2p
- Implement getNames()
- Use getNames() for merging to hosts.txt naming services to avoid O(n**2) - Fix naming service selection - Don't merge from master book unless publishing - Add naming service and direct config options
This commit is contained in:
@ -8,8 +8,10 @@
|
||||
package net.i2p.client.naming;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
@ -77,4 +79,23 @@ public class HostsTxtNamingService extends MetaNamingService {
|
||||
public boolean remove(String hostname, Properties options) {
|
||||
return super.remove(hostname.toLowerCase(), options);
|
||||
}
|
||||
|
||||
/**
|
||||
* All services aggregated, unless options contains
|
||||
* the property "file", in which case only for that file
|
||||
*/
|
||||
@Override
|
||||
public Set<String> getNames(Properties options) {
|
||||
String file = null;
|
||||
if (options != null)
|
||||
file = options.getProperty("file");
|
||||
if (file == null)
|
||||
return super.getNames(options);
|
||||
for (NamingService ns : _services) {
|
||||
String name = ns.getName();
|
||||
if (name.equals(file) || name.endsWith('/' + file) || name.endsWith('\\' + file))
|
||||
return ns.getNames(options);
|
||||
}
|
||||
return new HashSet(0);
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,11 @@ import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@ -174,6 +176,18 @@ public class MetaNamingService extends DummyNamingService {
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* All services aggregated
|
||||
*/
|
||||
@Override
|
||||
public Set<String> getNames(Properties options) {
|
||||
Set<String> rv = new HashSet();
|
||||
for (NamingService ns : _services) {
|
||||
rv.addAll(ns.getNames(options));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* All services aggregated
|
||||
*/
|
||||
|
@ -17,6 +17,7 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -365,6 +366,40 @@ public class SingleFileNamingService extends NamingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options ignored
|
||||
* @return all known host names, unsorted
|
||||
*/
|
||||
public Set<String> getNames(Properties options) {
|
||||
if (!_file.exists())
|
||||
return Collections.EMPTY_SET;
|
||||
BufferedReader in = null;
|
||||
getReadLock();
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16*1024);
|
||||
String line = null;
|
||||
Set<String> rv = new HashSet();
|
||||
while ( (line = in.readLine()) != null) {
|
||||
if (line.length() <= 0)
|
||||
continue;
|
||||
if (line.startsWith("#"))
|
||||
continue;
|
||||
int split = line.indexOf('=');
|
||||
if (split <= 0)
|
||||
continue;
|
||||
String key = line.substring(0, split);
|
||||
rv.add(key);
|
||||
}
|
||||
return rv;
|
||||
} catch (IOException ioe) {
|
||||
_log.error("getNames error", ioe);
|
||||
return Collections.EMPTY_SET;
|
||||
} finally {
|
||||
if (in != null) try { in.close(); } catch (IOException ioe) {}
|
||||
releaseReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options ignored
|
||||
*/
|
||||
|
Reference in New Issue
Block a user