FileDocCategorySizeDatePackage
NegativeCache.javaAPI DocAndroid 1.5 API5989Wed May 06 22:41:04 BST 2009java.net

NegativeCache

public 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.
see
NegCacheElement
since
Android 1.0

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.

return
hostName name of the host for which the lookup failed.
since
Android 1.0

    // END android-changed

                               
          
        super(initialCapacity, loadFactor, accessOrder);
    
Methods Summary
static synchronized voidcheckCacheExists()
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.StringgetFailedMessage(java.lang.String hostName)
Returns the message of the negative cache if the entry has not yet expired.

param
hostName the name of the host for which we look up the entry.
return
the message which was returned when the host lookup failed if the entry has not yet expired.
since
Android 1.0

        // 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 voidput(java.lang.String hostName, java.lang.String failedMessage)
Adds the host name and the corresponding name lookup fail message to the cache.

param
hostName the name of the host for which the lookup failed.
param
failedMessage the message returned when the lookup fails.
since
Android 1.0

        // BEGIN android-note
        // Made synchronized. copied from a newer version of harmony
        // END android-note
        checkCacheExists();
        negCache.put(hostName, new NegCacheElement(failedMessage));
    
protected booleanremoveEldestEntry(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.

param
eldest the map entry which will be deleted if we return {@code true}.
since
Android 1.0

        return size() > MAX_NEGATIVE_ENTRIES;
    
private static intsecondsToNanos(int ttl)
Multiplies value by 1 billion.

        return ttl * 1000000000;