NegativeCachepublic class NegativeCache extends LinkedHashMap This class is used to maintain the negative name lookup cache, which caches
host names which could not be resolved, as a security feature. |
Fields Summary |
---|
private static final long | serialVersionUID | private static NegativeCache | negCache | private static final int | MAX_NEGATIVE_ENTRIES | private static final float | LOADING |
Constructors Summary |
---|
NegativeCache(int initialCapacity, float loadFactor, boolean accessOrder)Returns the hostname for the cache element.
// END android-changed
super(initialCapacity, loadFactor, accessOrder);
|
Methods Summary |
---|
static synchronized void | checkCacheExists()This method checks whether the cache was already created and if not
creates it.
// BEGIN android-note
// Made synchronized. copied from a newer version of harmony
// END android-note
if (negCache == null) {
/*
* Create with the access order set so ordering is based on when the
* entries were last accessed. We make the default cache size one
* greater than the maximum number of entries as we will grow to one
* larger and then delete the LRU entry
*/
negCache = new NegativeCache<String, NegCacheElement>(
MAX_NEGATIVE_ENTRIES + 1, LOADING, true);
}
| static synchronized java.lang.String | getFailedMessage(java.lang.String hostName)Returns the message of the negative cache if the entry has not yet
expired.
// BEGIN android-note
// Made synchronized. copied from a newer version of harmony
// END android-note
checkCacheExists();
NegCacheElement element = negCache.get(hostName);
if (element != null) {
// check if element is still valid
String ttlValue = AccessController
.doPrivileged(new PriviAction<String>(
"networkaddress.cache.negative.ttl")); //$NON-NLS-1$
int ttl = 10;
try {
if (ttlValue != null) {
ttl = Integer.decode(ttlValue).intValue();
}
} catch (NumberFormatException e) {
}
if (ttl == 0) {
negCache.clear();
element = null;
} else if (ttl != -1) {
// BEGIN android-changed
long delta = System.nanoTime() - element.nanoTimeAdded;
if (delta > secondsToNanos(ttl)) {
// remove the element from the cache and return null
negCache.remove(hostName);
element = null;
}
// END android-changed
}
}
if (element != null) {
// BEGIN android-changed
// Copied from a newer version of harmony
return element.hostName;
// END android-changed
}
return null;
| static synchronized void | put(java.lang.String hostName, java.lang.String failedMessage)Adds the host name and the corresponding name lookup fail message to the
cache.
// BEGIN android-note
// Made synchronized. copied from a newer version of harmony
// END android-note
checkCacheExists();
negCache.put(hostName, new NegCacheElement(failedMessage));
| protected boolean | removeEldestEntry(java.util.Map$Entry eldest)Returns whether the eldest entry should be removed. It is removed if the
size has grown beyond the maximum size allowed for the cache. A {@code
LinkedHashMap} is created such that the least recently used entry is
deleted.
return size() > MAX_NEGATIVE_ENTRIES;
| private static int | secondsToNanos(int ttl)Multiplies value by 1 billion.
return ttl * 1000000000;
|
|