SNMP Stack 4_12

uk.co.westhawk.snmp.stack
Class SnmpContextv3Pool

java.lang.Object
  |
  +--uk.co.westhawk.snmp.stack.SnmpContextv3Pool
All Implemented Interfaces:
SnmpContextBasisFace, SnmpContextv3Face

public class SnmpContextv3Pool
extends java.lang.Object
implements SnmpContextv3Face

This class contains the pool of SNMP v3 contexts. This class reuses the existings contexts instead of creating a new one everytime.

Everytime a property changes the pool is checked for a SnmpContextv3 context that matches all the new properties of this class. If no such context exists, a new one is made. The Pdus associated with the old context remain associated with the old context.

A counter indicates the number of times the context is referenced. The counter is decreased when destroy is called. When the counter reaches zero, the context is released.

Note that because the underlying context can change when a property is changed and the Pdus remain associated with the old context, all properties have to be set BEFORE a Pdu is send.

Version:
$Revision: 3.16 $ $Date: 2002/10/23 09:56:03 $
Author:
Birgit Arkesteijn
See Also:
SnmpContextv3, SnmpContextPool, SnmpContextv2cPool

Field Summary
protected  int authenticationProtocol
           
protected  SnmpContextv3 context
           
protected  byte[] contextEngineId
           
protected  java.lang.String contextName
           
protected static java.util.Hashtable contextPool
           
protected  boolean hasChanged
           
protected  java.lang.String hostAddr
           
protected  int hostPort
           
protected  java.lang.String socketType
           
protected  boolean useAuthentication
           
protected  boolean usePrivacy
           
protected  java.lang.String userAuthenticationPassword
           
protected  java.lang.String userName
           
protected  java.lang.String userPrivacyPassword
           
 
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextv3Face
Default_ContextName, Default_UserName, MD5_PROTOCOL, ProtocolNames, SHA1_PROTOCOL, SNMPv1_Security_Model, SNMPv2c_Security_Model, USM_Security_Model, version_id
 
Fields inherited from interface uk.co.westhawk.snmp.stack.SnmpContextBasisFace
DEFAULT_PORT, KVM_SOCKET, MAXPDU, MSS, NETSCAPE_SOCKET, STANDARD_SOCKET
 
Constructor Summary
SnmpContextv3Pool(java.lang.String host, int port)
          Constructor.
SnmpContextv3Pool(java.lang.String host, int port, java.lang.String typeSocket)
          Constructor.
 
Method Summary
 boolean addDiscoveryPdu(DiscoveryPdu pdu)
          Adds an discovery pdu.
 boolean addPdu(Pdu pdu)
          Adds an pdu.
 void addTrapListener(TrapListener l)
          Adds the specified trap listener.
 void destroy()
          Releases the resources held by this context.
 void dumpContexts(java.lang.String title)
          Dumps the pool of contexts.
 byte[] encodeDiscoveryPacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve)
          Encodes a discovery pdu packet.
 byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve)
          Encodes a pdu packet.
 int getAuthenticationProtocol()
          Returns the protocol to be used for authentication.
 byte[] getContextEngineId()
          Returns the contextEngineID.
 java.lang.String getContextName()
          Returns the contextName.
 java.lang.String getHashKey()
          Returns the hash key.
 java.lang.String getHost()
          Returns the host
protected  SnmpContextv3 getMatchingContext()
          Returns a context from the pool.
 int getPort()
          Returns the port number
 java.lang.String getTypeSocket()
          Returns the type socket
 java.lang.String getUserAuthenticationPassword()
          Returns the user authentication password.
 java.lang.String getUserName()
          Returns the username.
 java.lang.String getUserPrivacyPassword()
          Returns the user privacy password.
 int getVersion()
          Return the SNMP version of the context.
 boolean isUseAuthentication()
          Return if authentication is used or not.
 boolean isUsePrivacy()
          Return if privacy is used or not.
 Pdu processIncomingTrap(byte[] message)
          Processes the incoming trap with the current context.
 boolean removePdu(int requestId)
          Removes a pdu.
 void removeTrapListener(TrapListener l)
          Removes the specified trap listener.
 void sendPacket(byte[] packet)
          Pass a correctly encoded SNMP packet, and we'l send it...
 void setAuthenticationProtocol(int protocol)
          Sets the protocol to be used for authentication.
 void setContextEngineId(byte[] newContextEngineId)
          Sets the contextEngineID.
 void setContextName(java.lang.String newContextName)
          Sets the contextName.
 void setUseAuthentication(boolean newUseAuthentication)
          Sets whether authentication has to used.
 void setUsePrivacy(boolean newUsePrivacy)
          Sets whether privacy has to used.
 void setUserAuthenticationPassword(java.lang.String newUserAuthenticationPd)
          Sets the user authentication password.
 void setUserName(java.lang.String newUserName)
          Sets the username.
 void setUserPrivacyPassword(java.lang.String newUserPrivacyPd)
          Sets the user privacy password.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

contextPool

protected static java.util.Hashtable contextPool

hostAddr

protected java.lang.String hostAddr

socketType

protected java.lang.String socketType

hostPort

protected int hostPort

context

protected SnmpContextv3 context

userName

protected java.lang.String userName

useAuthentication

protected boolean useAuthentication

userAuthenticationPassword

protected java.lang.String userAuthenticationPassword

usePrivacy

protected boolean usePrivacy

userPrivacyPassword

protected java.lang.String userPrivacyPassword

authenticationProtocol

protected int authenticationProtocol

contextEngineId

protected byte[] contextEngineId

contextName

protected java.lang.String contextName

hasChanged

protected boolean hasChanged
Constructor Detail

SnmpContextv3Pool

public SnmpContextv3Pool(java.lang.String host,
                         int port)
                  throws java.io.IOException
Constructor.
Parameters:
host - The host to which the Pdu will send
port - The port where the SNMP server will be
See Also:
SnmpContextv3.SnmpContextv3(String, int)

SnmpContextv3Pool

public SnmpContextv3Pool(java.lang.String host,
                         int port,
                         java.lang.String typeSocket)
                  throws java.io.IOException
Constructor.
Parameters:
host - The host to which the Pdu will send
port - The port where the SNMP server will be
typeSocket - The type of socket to use.
See Also:
SnmpContextv3.SnmpContextv3(String, int, String), SnmpContextBasisFace.STANDARD_SOCKET, SnmpContextBasisFace.NETSCAPE_SOCKET, SnmpContextBasisFace.KVM_SOCKET
Method Detail

getVersion

public int getVersion()
Return the SNMP version of the context.
Specified by:
getVersion in interface SnmpContextBasisFace
Returns:
The version

getHost

public java.lang.String getHost()
Returns the host
Specified by:
getHost in interface SnmpContextBasisFace
Returns:
The host

getPort

public int getPort()
Returns the port number
Specified by:
getPort in interface SnmpContextBasisFace
Returns:
The port no

getTypeSocket

public java.lang.String getTypeSocket()
Returns the type socket
Specified by:
getTypeSocket in interface SnmpContextBasisFace
Returns:
The type socket

getUserName

public java.lang.String getUserName()
Returns the username.
Specified by:
getUserName in interface SnmpContextv3Face
Returns:
the username
See Also:
SnmpContextv3.getUserName()

setUserName

public void setUserName(java.lang.String newUserName)
Sets the username.
Specified by:
setUserName in interface SnmpContextv3Face
Parameters:
newUserName - The new username
See Also:
SnmpContextv3.setUserName(String)

isUseAuthentication

public boolean isUseAuthentication()
Return if authentication is used or not.
Specified by:
isUseAuthentication in interface SnmpContextv3Face
Returns:
true if authentication is used, false if not
See Also:
SnmpContextv3.isUseAuthentication()

setUseAuthentication

public void setUseAuthentication(boolean newUseAuthentication)
Sets whether authentication has to used.
Specified by:
setUseAuthentication in interface SnmpContextv3Face
Parameters:
newUseAuthentication - The use of authentication
See Also:
SnmpContextv3.setUseAuthentication(boolean)

getUserAuthenticationPassword

public java.lang.String getUserAuthenticationPassword()
Returns the user authentication password.
Specified by:
getUserAuthenticationPassword in interface SnmpContextv3Face
Returns:
The user authentication password
See Also:
SnmpContextv3.getUserAuthenticationPassword()

setUserAuthenticationPassword

public void setUserAuthenticationPassword(java.lang.String newUserAuthenticationPd)
Sets the user authentication password.
Specified by:
setUserAuthenticationPassword in interface SnmpContextv3Face
Parameters:
newUserAuthenticationPassword - The user authentication password
See Also:
SnmpContextv3.setUserAuthenticationPassword(String)

setAuthenticationProtocol

public void setAuthenticationProtocol(int protocol)
Sets the protocol to be used for authentication.
Specified by:
setAuthenticationProtocol in interface SnmpContextv3Face
Parameters:
protocol - The authentication protocol to be used
See Also:
SnmpContextv3.setAuthenticationProtocol(int)

getAuthenticationProtocol

public int getAuthenticationProtocol()
Returns the protocol to be used for authentication.
Specified by:
getAuthenticationProtocol in interface SnmpContextv3Face
Returns:
The authentication protocol to be used
See Also:
SnmpContextv3.getAuthenticationProtocol()

isUsePrivacy

public boolean isUsePrivacy()
Return if privacy is used or not.
Specified by:
isUsePrivacy in interface SnmpContextv3Face
Returns:
true if privacy is used, false if not
See Also:
SnmpContextv3.isUsePrivacy()

setUsePrivacy

public void setUsePrivacy(boolean newUsePrivacy)
Sets whether privacy has to used.
Specified by:
setUsePrivacy in interface SnmpContextv3Face
Parameters:
newUsePrivacy - The use of privacy
See Also:
SnmpContextv3.setUsePrivacy(boolean)

getUserPrivacyPassword

public java.lang.String getUserPrivacyPassword()
Returns the user privacy password.
Specified by:
getUserPrivacyPassword in interface SnmpContextv3Face
Returns:
The user privacy password
See Also:
SnmpContextv3.getUserPrivacyPassword()

setUserPrivacyPassword

public void setUserPrivacyPassword(java.lang.String newUserPrivacyPd)
Sets the user privacy password.
Specified by:
setUserPrivacyPassword in interface SnmpContextv3Face
Parameters:
newUserPrivacyPassword - The user privacy password
See Also:
SnmpContextv3.setUserPrivacyPassword(String)

setContextEngineId

public void setContextEngineId(byte[] newContextEngineId)
Sets the contextEngineID.
Specified by:
setContextEngineId in interface SnmpContextv3Face
Parameters:
newContextEngineId - The contextEngineID
See Also:
SnmpContextv3.setContextEngineId(byte [])

getContextEngineId

public byte[] getContextEngineId()
Returns the contextEngineID.
Specified by:
getContextEngineId in interface SnmpContextv3Face
Returns:
The contextEngineID
See Also:
SnmpContextv3.getContextEngineId()

setContextName

public void setContextName(java.lang.String newContextName)
Sets the contextName.
Specified by:
setContextName in interface SnmpContextv3Face
Parameters:
newContextName - The contextName
See Also:
SnmpContextv3.setContextName(String)

getContextName

public java.lang.String getContextName()
Returns the contextName.
Specified by:
getContextName in interface SnmpContextv3Face
Returns:
The contextName
See Also:
SnmpContextv3.getContextName()

addDiscoveryPdu

public boolean addDiscoveryPdu(DiscoveryPdu pdu)
                        throws java.io.IOException,
                               PduException,
                               java.lang.IllegalArgumentException
Adds an discovery pdu. This method adds the Pdu without checking if discovery is needed.
Specified by:
addDiscoveryPdu in interface SnmpContextv3Face
Parameters:
pdu - the discovery pdu
Returns:
pdu is succesful added
See Also:
SnmpContextv3.addPdu(Pdu), addPdu(Pdu)

addPdu

public boolean addPdu(Pdu pdu)
               throws java.io.IOException,
                      PduException,
                      java.lang.IllegalArgumentException
Adds an pdu. This method adds the Pdu and block until it has all the discovery parameters it needs.
Specified by:
addPdu in interface SnmpContextBasisFace
Parameters:
pdu - the pdu
Returns:
pdu is succesful added
See Also:
SnmpContextv3.addPdu(Pdu), addDiscoveryPdu(DiscoveryPdu)

removePdu

public boolean removePdu(int requestId)
Removes a pdu. This removes the Pdu from the AbstractSnmpContext and clear the link with the SNMPv3 msgId.
Specified by:
removePdu in interface SnmpContextBasisFace
Parameters:
rid - the Pdu request id
Returns:
whether the pdu has been successfully removed
See Also:
SnmpContextv3.removePdu(int)

encodeDiscoveryPacket

public byte[] encodeDiscoveryPacket(byte msg_type,
                                    int rId,
                                    int errstat,
                                    int errind,
                                    java.util.Enumeration ve)
                             throws java.io.IOException,
                                    EncodingException
Encodes a discovery pdu packet. This methods encodes without checking is the discovery paramters are all known.
Specified by:
encodeDiscoveryPacket in interface SnmpContextv3Face
Following copied from interface: uk.co.westhawk.snmp.stack.SnmpContextv3Face
Returns:
The encoded packet

encodePacket

public byte[] encodePacket(byte msg_type,
                           int rId,
                           int errstat,
                           int errind,
                           java.util.Enumeration ve)
                    throws java.io.IOException,
                           EncodingException
Encodes a pdu packet. This methods first checks if all the discovery paramters are all known, if not it will throw an EncodingException. If so, it encodes and returns the bytes.
Specified by:
encodePacket in interface SnmpContextBasisFace
Following copied from interface: uk.co.westhawk.snmp.stack.SnmpContextBasisFace
Returns:
The encoded packet

sendPacket

public void sendPacket(byte[] packet)
Pass a correctly encoded SNMP packet, and we'l send it...
Specified by:
sendPacket in interface SnmpContextBasisFace
Following copied from interface: uk.co.westhawk.snmp.stack.SnmpContextBasisFace
Parameters:
packet - The encoded packet

destroy

public void destroy()
Releases the resources held by this context. This method will decrement the reference counter. When the reference counter reaches zero the actual context is removed from the pool and destroyed.
Specified by:
destroy in interface SnmpContextBasisFace

getMatchingContext

protected SnmpContextv3 getMatchingContext()
                                    throws java.io.IOException,
                                           java.lang.IllegalArgumentException
Returns a context from the pool. This methods checks for an existing context that matches all our properties. If such a context does not exist a new one is created and added to the pool.
Returns:
A context from the pool
See Also:
getHashKey()

dumpContexts

public void dumpContexts(java.lang.String title)
Dumps the pool of contexts. This is for debug purposes.
Parameters:
title - The title of the dump

getHashKey

public java.lang.String getHashKey()
Returns the hash key. This key is built out of all properties. It serves as key for the hashtable of (v3) contexts.
Returns:
The hash key

addTrapListener

public void addTrapListener(TrapListener l)
                     throws java.io.IOException
Adds the specified trap listener. The listener will be added to the current context, not to all the contexts in the hashtable.
Specified by:
addTrapListener in interface SnmpContextBasisFace
See Also:
AbstractSnmpContext.addTrapListener(uk.co.westhawk.snmp.event.TrapListener)

removeTrapListener

public void removeTrapListener(TrapListener l)
                        throws java.io.IOException
Removes the specified trap listener. The listener will be removed from the current context, not from all the contexts in the hashtable.
Specified by:
removeTrapListener in interface SnmpContextBasisFace
See Also:
AbstractSnmpContext.removeTrapListener(uk.co.westhawk.snmp.event.TrapListener)

processIncomingTrap

public Pdu processIncomingTrap(byte[] message)
                        throws DecodingException,
                               java.io.IOException
Processes the incoming trap with the current context.
Specified by:
processIncomingTrap in interface SnmpContextBasisFace
See Also:
SnmpContextv3.processIncomingTrap(byte[])

toString

public java.lang.String toString()
Returns a string representation of the object.
Overrides:
toString in class java.lang.Object
Returns:
The string

SNMP Stack 4_12