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)
          Instantiates a new abstract resolver.
AbstractResolver(java.util.logging.Logger logger, java.lang.String name)
          Instantiates a new abstract resolver.
 
Method Summary
 void addToCache(long hash, java.lang.String name)
          Adds the to cache.
 void addToCache(long hash, java.lang.String name, long timeout)
          Adds the to cache.
 boolean canBeResolved(byte[] address)
          Can be resolved.
 void clearCache()
          Clear cache and timeout queues.
protected  void finalize()
          Finalize.
 int getCacheCapacity()
          Gets the just an initial map size.
 float getCacheLoadFactor()
          Gets the cache load factor.
 long getNegativeTimeout()
          Gets the when failed to resolve to a name, store the failure information in cache and set the negative hit timeout.
 long getPositiveTimeout()
          Gets the resolved to a name.
protected  boolean hasCacheFile()
          Checks for cache file.
 void initializeIfNeeded()
          Called by JRegistry when resolver when it is being retrieved.
 boolean isCached(byte[] address)
          Checks if is cached.
 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)
          Load cache.
 void propertyChange(java.beans.PropertyChangeEvent evt)
          Property change.
 java.lang.String resolve(byte[] address)
          Resolve.
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)
          Sets the just an initial map size.
 void setCacheLoadFactor(float cacheLoadFactor)
          Sets the cache load factor.
 void setNegativeTimeout(long negativeTimeout)
          Sets the when failed to resolve to a name, store the failure information in cache and set the negative hit timeout.
 void setPositiveTimeout(long positiveTimeout)
          Sets the resolved to a name.
protected abstract  long toHashCode(byte[] address)
          To hash code.
protected  long toHashCode(long number)
          To hash code.
 java.lang.String toString()
          To string.
 
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)
Instantiates a new abstract resolver.

Parameters:
logger - the logger
type - the type

AbstractResolver

public AbstractResolver(java.util.logging.Logger logger,
                        java.lang.String name)
Instantiates a new abstract resolver.

Parameters:
logger - the logger
name - the name
Method Detail

addToCache

public void addToCache(long hash,
                       java.lang.String name)
Adds the to cache.

Parameters:
hash - the hash
name - the name

addToCache

public void addToCache(long hash,
                       java.lang.String name,
                       long timeout)
Adds the to cache.

Parameters:
hash - the hash
name - the name
timeout - the timeout

canBeResolved

public boolean canBeResolved(byte[] address)
Can be resolved.

Specified by:
canBeResolved in interface Resolver
Parameters:
address - the address
Returns:
true, if successful
See Also:
Resolver.canBeResolved(byte[])

clearCache

public void clearCache()
Clear cache and timeout queues.

Specified by:
clearCache in interface Resolver

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalize.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - the throwable
See Also:
Object.finalize()

getCacheCapacity

public final int getCacheCapacity()
Gets the just an initial map size.

Returns:
the just an initial map size

getCacheLoadFactor

public final float getCacheLoadFactor()
Gets the cache load factor.

Returns:
the cache load factor

getNegativeTimeout

public final long getNegativeTimeout()
Gets the when failed to resolve to a name, store the failure information in cache and set the negative hit timeout.

Returns:
the when failed to resolve to a name, store the failure information in cache and set the negative hit timeout

getPositiveTimeout

public final long getPositiveTimeout()
Gets the resolved to a name.

Returns:
the resolved to a name

hasCacheFile

protected boolean hasCacheFile()
Checks for cache file.

Returns:
true, if successful

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)
Checks if is cached.

Specified by:
isCached in interface Resolver
Parameters:
address - the address
Returns:
true, if is cached
See Also:
Resolver.isCached(byte[])

loadCache

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

Returns:
number of cache entries read
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

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
Load cache.

Specified by:
loadCache in interface Resolver
Parameters:
url - the url
Returns:
the int
Throws:
java.io.IOException - Signals that an I/O exception has occurred.
See Also:
Resolver.loadCache(java.net.URL)

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)
Property change.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Parameters:
evt - the evt
See Also:
PropertyChangeListener.propertyChange(java.beans.PropertyChangeEvent)

resolve

public final java.lang.String resolve(byte[] address)
Resolve.

Specified by:
resolve in interface Resolver
Parameters:
address - the address
Returns:
the string
See Also:
Resolver.resolve(byte[])

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
Returns:
the string

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
Returns:
the string

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 - Signals that an I/O exception has occurred.

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)
Sets the just an initial map size.

Parameters:
cacheCapacity - the new just an initial map size

setCacheLoadFactor

public final void setCacheLoadFactor(float cacheLoadFactor)
Sets the cache load factor.

Parameters:
cacheLoadFactor - the new cache load factor

setNegativeTimeout

public final void setNegativeTimeout(long negativeTimeout)
Sets the when failed to resolve to a name, store the failure information in cache and set the negative hit timeout.

Parameters:
negativeTimeout - the new when failed to resolve to a name, store the failure information in cache and set the negative hit timeout

setPositiveTimeout

public final void setPositiveTimeout(long positiveTimeout)
Sets the resolved to a name.

Parameters:
positiveTimeout - the new resolved to a name

toHashCode

protected abstract long toHashCode(byte[] address)
To hash code.

Parameters:
address - the address
Returns:
the long

toHashCode

protected long toHashCode(long number)
To hash code.

Parameters:
number - the number
Returns:
the long

toString

public java.lang.String toString()
To string.

Overrides:
toString in class java.lang.Object
Returns:
the string
See Also:
Object.toString()