FileDocCategorySizeDatePackage
SnmpRequestTree.javaAPI DocJava SE 5 API36435Fri Aug 26 14:55:04 BST 2005com.sun.jmx.snmp.agent

SnmpRequestTree

public final class SnmpRequestTree extends Object
This class is used to register varbinds from a SNMP varbind list with the SnmpMibNode responsible for handling the requests concerning that varbind. This class holds a hashtable of Handler nodes, whith the involved SnmpMibNode as a key. When the involved SnmpMibNode is a group, the sublist of varbind is directly stored in the Handler node. When the involved SnmpMibNode is a table, the sublist is stored in a sorted array indexed by the OID of the entry involved.

Fields Summary
private Hashtable
hashtable
private SnmpMibRequest
request
private int
version
private boolean
creationflag
private boolean
getnextflag
private int
type
private boolean
setreqflag
Constructors Summary
SnmpRequestTree(SnmpMibRequest req, boolean creationflag, int pdutype)

	this.request = req;
	this.version  = req.getVersion();
	this.creationflag = creationflag;
	this.hashtable = new Hashtable();
	setPduType(pdutype);
    
Methods Summary
public voidadd(SnmpMibNode meta, int depth, com.sun.jmx.snmp.SnmpVarBind varbind)

	registerNode(meta,depth,null,varbind,false,null);
    
public voidadd(SnmpMibNode meta, int depth, com.sun.jmx.snmp.SnmpOid entryoid, com.sun.jmx.snmp.SnmpVarBind varbind, boolean isnew)

	registerNode(meta,depth,entryoid,varbind,isnew,null);
    
public voidadd(SnmpMibNode meta, int depth, com.sun.jmx.snmp.SnmpOid entryoid, com.sun.jmx.snmp.SnmpVarBind varbind, boolean isnew, com.sun.jmx.snmp.SnmpVarBind statusvb)

	registerNode(meta,depth,entryoid,varbind,isnew,statusvb);
    
private static final voiddebug(java.lang.String func, java.lang.String info)

        Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, 
		   "SnmpRequestTree", func, info);
    
private static intfindOid(com.sun.jmx.snmp.SnmpOid[] oids, int count, com.sun.jmx.snmp.SnmpOid oid)

	final int size = count;
        int low= 0;
        int max= size - 1;
        int curr= low + (max-low)/2;
        //System.out.println("Try to retrieve: " + oid.toString());
        while (low <= max) {

            final SnmpOid pos = oids[curr];
      
            //System.out.println("Compare with" + pos.toString());
            // never know ...we might find something ...
            //
            final int comp = oid.compareTo(pos);
            if (comp == 0)
                return curr;
      
            if (oid.equals(pos)) {
                return curr;
            }
            if (comp > 0) {
                low = curr + 1;
            } else {
                max = curr - 1;
            }
            curr = low + (max-low)/2;
        }
        return -1;
    
private com.sun.jmx.snmp.agent.SnmpRequestTree$Handlerget(java.lang.Object key)

	if (key == null) return null;
	if (hashtable == null) return null;
	return (Handler) hashtable.get(key);
    
public java.util.EnumerationgetHandlers()

	return hashtable.elements();
    
private static intgetInsertionPoint(com.sun.jmx.snmp.SnmpOid[] oids, int count, com.sun.jmx.snmp.SnmpOid oid)

	final SnmpOid[] localoids = oids;
	final int size = count;
        int low= 0;
        int max= size - 1;
        int curr= low + (max-low)/2;


        while (low <= max) {
      
            final SnmpOid pos = localoids[curr];
      
            // never know ...we might find something ...
            //
            final int comp= oid.compareTo(pos);

	    // In the calling method we will have to check for this case...
            //    if (comp == 0)
            //       return -1;
	    // Returning curr instead of -1 avoids having to call
	    // findOid() first and getInsertionPoint() afterwards.
	    // We can simply call getInsertionPoint() and then checks whether
	    // there's an OID at the returned position which equals the
	    // given OID.
            if (comp == 0)
                return curr;
      
            if (comp>0) {
                low= curr +1;
            } else {
                max= curr -1;
            }
            curr= low + (max-low)/2;
        }
        return curr;
    
public SnmpMibNodegetMetaNode(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler)

	return handler.meta;
    
public intgetOidDepth(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler)

	return handler.depth;
    
public intgetRequestPduVersion()

	return request.getRequestPduVersion();
    
SnmpMibSubRequestgetSubRequest(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler)

	if (handler == null) return null;
	return new SnmpMibSubRequestImpl(request,handler.getSubList(),
				      null,false,getnextflag,null);
    
SnmpMibSubRequestgetSubRequest(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler, com.sun.jmx.snmp.SnmpOid oid)

	if (handler == null) return null;
	final int pos = handler.getEntryPos(oid);
	if (pos == -1) return null;
	return new SnmpMibSubRequestImpl(request,
					 handler.getEntrySubList(pos),
					 handler.getEntryOid(pos),
					 handler.isNewEntry(pos),
					 getnextflag,
					 handler.getRowStatusVarBind(pos));
    
SnmpMibSubRequestgetSubRequest(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler, int entry)

	if (handler == null) return null;
	return new 
	    SnmpMibSubRequestImpl(request,handler.getEntrySubList(entry),
				  handler.getEntryOid(entry),
				  handler.isNewEntry(entry),getnextflag,
				  handler.getRowStatusVarBind(entry));
    
public java.util.EnumerationgetSubRequests(com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler)

	return new Enum(this,handler);
    
public java.lang.ObjectgetUserData()

 return request.getUserData(); 
public intgetVersion()

	return version;
    
public booleanisCreationAllowed()

	return creationflag;
    
private static final booleanisDebugOn()

        return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
    
public booleanisSetRequest()

	return setreqflag;
    
public static intmapGetException(int errorStatus, int version)


	final int errorCode = errorStatus;
	if (version == SnmpDefinitions.snmpVersionOne)
	    return errorCode;
	
	int mappedErrorCode = errorCode;
	
	// Now take care of V2 errorCodes that can be stored
	// in the varbind itself:
	if (errorCode == 
	    SnmpStatusException.noSuchObject)
	    // noSuchObject => noSuchObject
	    mappedErrorCode = errorCode;
	
	else if (errorCode == 
		 SnmpStatusException.noSuchInstance)
	    // noSuchInstance => noSuchInstance
	    mappedErrorCode = errorCode;
	
	// Now we're going to try to transform every other
	// global code in either noSuchInstance or noSuchObject,
	// so that the get can return a partial result.
	//
	// Only noSuchInstance or noSuchObject can be stored
	// in the varbind itself.
	//
	
	// According to RFC 1905: noAccess is emitted when the
	// the access is denied because it is not in the MIB view...
	// 
	else if (errorCode ==
		 SnmpStatusException.noAccess)
	    // noAccess => noSuchInstance
	    mappedErrorCode = SnmpStatusException.noSuchInstance;
	
	// According to RFC 1905: (my interpretation because it is not
	// really clear) The specified variable name exists - but the
	// variable does not exists and cannot be created under the
	// present circumstances (probably because the request specifies
	// another variable/value which is incompatible, or because the
	// value of some other variable in the MIB prevents the creation)
	//
	// Note that this error should never be raised in a GET context
	// but who knows?
	//
	else if (errorCode == SnmpStatusException.snmpRspInconsistentName)
	    // inconsistentName => noSuchInstance
	    mappedErrorCode = SnmpStatusException.noSuchInstance;
	
	// All the errors comprised between snmpRspWrongType and
	// snmpRspInconsistentValue concern values: so we're going
	// to assume the OID was correct, and reply with noSuchInstance.
	//
	// Note that this error should never be raised in a GET context
	// but who knows?
	//
	else if ((errorCode >= SnmpStatusException.snmpRspWrongType) &&
		 (errorCode <= SnmpStatusException.snmpRspInconsistentValue))
	    mappedErrorCode = SnmpStatusException.noSuchInstance;
	
	// We're going to assume the OID was correct, and reply 
	// with noSuchInstance.
	//
	else if (errorCode == SnmpStatusException.readOnly)
	    mappedErrorCode = SnmpStatusException.noSuchInstance;
	
	// For all other errors but genErr, we're going to reply with
	// noSuchObject
	//
	else if (errorCode != SnmpStatusException.snmpRspAuthorizationError &&
		 errorCode != SnmpStatusException.snmpRspGenErr)
	    mappedErrorCode = SnmpStatusException.noSuchObject;
	
	// Only genErr will abort the GET and be returned as global
	// error.
	//
	return mappedErrorCode;
	
    
public static intmapSetException(int errorStatus, int version)


	final int errorCode = errorStatus;

	if (version == SnmpDefinitions.snmpVersionOne) 
	    return errorCode;
	
	int mappedErrorCode = errorCode;
	
	// Now take care of V2 errorCodes that can be stored
	// in the varbind itself:
	if (errorCode == SnmpStatusException.noSuchObject)
	    // noSuchObject => notWritable
	    mappedErrorCode = SnmpStatusException.snmpRspNotWritable;
	
	else if (errorCode == SnmpStatusException.noSuchInstance)
	    // noSuchInstance => notWritable
	    mappedErrorCode = SnmpStatusException.snmpRspNotWritable;

	return mappedErrorCode;
    
private voidput(java.lang.Object key, com.sun.jmx.snmp.agent.SnmpRequestTree$Handler handler)

	if (handler == null) return;
	if (key == null) return;
	if (hashtable == null) hashtable = new Hashtable();
	hashtable.put(key,handler);
    
private voidregisterNode(SnmpMibNode meta, int depth, com.sun.jmx.snmp.SnmpOid entryoid, com.sun.jmx.snmp.SnmpVarBind varbind, boolean isnew, com.sun.jmx.snmp.SnmpVarBind statusvb)

	if (meta == null) {
	    if (isDebugOn())
		debug("registerNode","meta-node is null!!!");
	    return;
	}
	if (varbind == null) {
	    if (isDebugOn())
		debug("registerNode","varbind is null!!!");
	    return ;
	}

	final Object key = meta;

	// retrieve the handler node associated with the given meta, 
	// if any
	Handler handler = get(key);

	// If no handler node was found for that meta, create one.
	if (handler == null) {
	    // if (isDebugOn()) 
            //    debug("registerNode", "adding node for " +
	    //          varbind.oid.toString());
	    handler = new Handler(type);
	    handler.meta  = meta;
	    handler.depth = depth;
	    put(key,handler);
	} 
	// else {
	//   if (isDebugOn()) 
	//	debug("registerNode","found node for " +
	//	      varbind.oid.toString());
	// }

	// Adds the varbind in the handler node's sublist.
	if (entryoid == null) 
	    handler.addVarbind(varbind);
	else 
	    handler.addVarbind(varbind,entryoid,isnew,statusvb);
	return ;
    
voidsetGetNextFlag()

	getnextflag = true;
    
voidsetPduType(int pduType)

	type = pduType;
	setreqflag = ((pduType == SnmpDefinitions.pduWalkRequest) ||
	    (pduType == SnmpDefinitions.pduSetRequestPdu));
    
voidswitchCreationFlag(boolean flag)

	creationflag = flag;