org.jnetpcap.util.resolver
Class AbstractResolver

java.lang.Object
  extended by org.jnetpcap.util.resolver.AbstractResolver
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.util.EventListener, Resolver
Direct Known Subclasses:
IEEEOuiPrefixResolver, IpResolver

public abstract class AbstractResolver
extends java.lang.Object
implements Resolver, java.beans.PropertyChangeListener

Default adaptor class for Resovler interface. This abstract class provides the default caching mechanism for positive and negative resolver lookups. It also provides a timeout mechanism to time out lookup results.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jnetpcap.util.resolver.Resolver
Resolver.ResolverType
 
Field Summary
protected static long INFINITE_TIMEOUT
          Timeout of 5 years
protected  java.util.logging.Logger logger
          Logger is supplied from subclass.
 
Fields inherited from interface org.jnetpcap.util.resolver.Resolver
RESOLVER_SEARCH_PATH_PROPERTY
 
Constructor Summary
AbstractResolver(java.util.logging.Logger logger, Resolver.ResolverType type)
           
AbstractResolver(java.util.logging.Logger logger, java.lang.String name)
           
 
Method Summary
 void addToCache(long hash, java.lang.String name)
           
 void addToCache(long hash, java.lang.String name, long timeout)
           
 boolean canBeResolved(byte[] address)
          Checks if a mapping exists or can be made.
 void clearCache()
          Clear cache and timeout queues
protected  void finalize()
           
 int getCacheCapacity()
           
 float getCacheLoadFactor()
           
 long getNegativeTimeout()
           
 long getPositiveTimeout()
           
protected  boolean hasCacheFile()
           
 void initializeIfNeeded()
          Called by JRegistry when resolver when it is being retrieved.
 boolean isCached(byte[] address)
          Checks if resolver already has a mapping made for this particular address.
 int loadCache()
          Load cache entries using default mechanism
 int loadCache(java.lang.String file)
          Load cache entries from file.
 int loadCache(java.net.URL url)
          Attempts to load the cache from the given URL.
 void propertyChange(java.beans.PropertyChangeEvent evt)
           
 java.lang.String resolve(byte[] address)
          Attempts to resole an address to a human readable form.
protected abstract  java.lang.String resolveToName(byte[] address, long hash)
          Resolves an address to a name.
protected abstract  java.lang.String resolveToName(long number, long hash)
          Resolves number to a name.
 int saveCache()
          Save the cache using default mechanism, if set
 int saveCache(java.lang.String file)
          Save the cache to file.
 void setCacheCapacity(int cacheCapacity)
           
 void setCacheLoadFactor(float cacheLoadFactor)
           
 void setNegativeTimeout(long negativeTimeout)
           
 void setPositiveTimeout(long positiveTimeout)
           
protected abstract  long toHashCode(byte[] address)
           
protected  long toHashCode(long number)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INFINITE_TIMEOUT

protected static final long INFINITE_TIMEOUT
Timeout of 5 years

See Also:
Constant Field Values

logger

protected final java.util.logging.Logger logger
Logger is supplied from subclass. This allows Abstract logger to log messages attached to actual logger that was intended for

Constructor Detail

AbstractResolver

public AbstractResolver(java.util.logging.Logger logger,
                        Resolver.ResolverType type)

AbstractResolver

public AbstractResolver(java.util.logging.Logger logger,
                        java.lang.String name)
Method Detail

addToCache

public void addToCache(long hash,
                       java.lang.String name)

addToCache

public void addToCache(long hash,
                       java.lang.String name,
                       long timeout)

canBeResolved

public boolean canBeResolved(byte[] address)
Description copied from interface: Resolver
Checks if a mapping exists or can be made. This operation may trigger a lookup which may take certain amount of time to complete, some times many seconds or even minutes.

Specified by:
canBeResolved in interface Resolver
Parameters:
address - address to check mapping for
Returns:
true the mapping can be made, otherwise false

clearCache

public void clearCache()
Clear cache and timeout queues

Specified by:
clearCache in interface Resolver

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

getCacheCapacity

public final int getCacheCapacity()

getCacheLoadFactor

public final float getCacheLoadFactor()

getNegativeTimeout

public final long getNegativeTimeout()

getPositiveTimeout

public final long getPositiveTimeout()

hasCacheFile

protected boolean hasCacheFile()

initializeIfNeeded

public void initializeIfNeeded()
Called by JRegistry when resolver when it is being retrieved. This allows the resolver to do a late initialization, only when its actually called on to do work. This behaviour is JRegistry specific and therefore kept package and subclass accessible.

Specified by:
initializeIfNeeded in interface Resolver

isCached

public boolean isCached(byte[] address)
Description copied from interface: Resolver
Checks if resolver already has a mapping made for this particular address. This operation does not block and returns immediately. The mapping may include a negative lookup, one that failed before. None the less the negative result is cached along with positive results.

Specified by:
isCached in interface Resolver
Parameters:
address - address to check for
Returns:
true if mapping is already cached, otherwise false

loadCache

public int loadCache()
              throws java.io.IOException
Load cache entries using default mechanism

Returns:
number of cache entries read
Throws:
java.io.IOException

loadCache

public int loadCache(java.lang.String file)
              throws java.io.IOException
Load cache entries from file. Each cached entry is saved with a timeout timestamp. If the timeout is already expired, the entry is skipped.

Parameters:
file - file to load cache entries from
Returns:
number of entries saved
Throws:
java.io.IOException - any IO errors

loadCache

public int loadCache(java.net.URL url)
              throws java.io.IOException
Description copied from interface: Resolver
Attempts to load the cache from the given URL. The format of the file retrieved from the url is resolver specified. The default cache file format is attempted at some point in the file scan until the file can be read or the load fails.

Specified by:
loadCache in interface Resolver
Parameters:
url - URL of the resource containing the database to load
Returns:
number of entries cached
Throws:
java.io.IOException - any IO errors

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)
Specified by:
propertyChange in interface java.beans.PropertyChangeListener

resolve

public final java.lang.String resolve(byte[] address)
Description copied from interface: Resolver
Attempts to resole an address to a human readable form. Any possible or required look ups are performed, sometimes taking a long time to complete if neccessary. All results, positive and negative for the lookup, are cached for certain amount of time. Defaults are minutes for positive lookup and minutes for negative.

Specified by:
resolve in interface Resolver
Parameters:
address - address to try and resolve
Returns:
human readable form if lookup succeeded (position) or null if lookup failed to produce a human label (negative)

resolveToName

protected abstract java.lang.String resolveToName(byte[] address,
                                                  long hash)
Resolves an address to a name. Performs any neccessary lookups to try and resolve the name. The method should not access any of the cached information. THis method is called only after the cache has already been checked and failed to produce a positive or negative lookup entry.

Parameters:
address - address to resolve
hash - computed hash code for the address, identifies the address uniquely

resolveToName

protected abstract java.lang.String resolveToName(long number,
                                                  long hash)
Resolves number to a name. Performs any neccessary lookups to try and resolve the name. The method should not access any of the cached information. THis method is called only after the cache has already been checked and failed to produce a positive or negative lookup entry.

Parameters:
number - a number value to resolve
hash - computed hash code for the number, identifies the number uniquely

saveCache

public int saveCache()
              throws java.io.IOException
Save the cache using default mechanism, if set

Specified by:
saveCache in interface Resolver
Returns:
number of cache entries saved
Throws:
java.io.IOException

saveCache

public int saveCache(java.lang.String file)
              throws java.io.IOException
Save the cache to file.

Parameters:
file - file to save to
Returns:
number of entries saved
Throws:
java.io.IOException - any IO errors

setCacheCapacity

public final void setCacheCapacity(int cacheCapacity)

setCacheLoadFactor

public final void setCacheLoadFactor(float cacheLoadFactor)

setNegativeTimeout

public final void setNegativeTimeout(long negativeTimeout)

setPositiveTimeout

public final void setPositiveTimeout(long positiveTimeout)

toHashCode

protected abstract long toHashCode(byte[] address)

toHashCode

protected long toHashCode(long number)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object