FileDocCategorySizeDatePackage
SnmpOid.javaAPI DocJava SE 5 API19010Fri Aug 26 14:55:02 BST 2005com.sun.jmx.snmp

SnmpOid

public class SnmpOid extends SnmpValue
Represents an SNMP Object Identifier (OID).

This API is a Sun Microsystems internal API and is subject to change without notice.

version
4.22 12/19/03
author
Sun Microsystems, Inc
author
Cisco Systems, Inc.

Fields Summary
protected long[]
components
The components' array.
protected int
componentCount
The length of the components' array.
static final String
name
The name of the type.
private static SnmpOidTable
meta
Reference to a mib table. If no mib table is available, the class will not be able to resolve names contained in the Object Identifier.
static final long
serialVersionUID
Ensure serialization compatibility with version 4.1 FCS
Constructors Summary
public SnmpOid()
Constructs a new SnmpOid with no components.

	components = new long[15] ;
	componentCount = 0 ;
    
public SnmpOid(long[] oidComponents)
Constructs a new SnmpOid from the specified component array.

param
oidComponents The initialization component array.

	components = (long[])oidComponents.clone() ;
	componentCount = components.length ;
    
public SnmpOid(long id)
Constructs a new SnmpOid containing one component with the specified value.

param
id The initialization component value.

	components = new long[1] ;
	components[0] = id ;
	componentCount = components.length ;
    
public SnmpOid(long id1, long id2, long id3, long id4)
Constructs a new SnmpOid containing four components with the specified values.

param
id1 The first component value.
param
id2 The second component values.
param
id3 The third component values.
param
id4 The fourth component values.

	components = new long[4] ;
	components[0] = id1 ;
	components[1] = id2 ;
	components[2] = id3 ;
	components[3] = id4 ;
	componentCount = components.length ;
    
public SnmpOid(String s)
Constructs a new SnmpOid from a dot-formatted String or a MIB variable name. It generates an exception if the variable name cannot be resolved, or if the dot-formatted String has an invalid subidentifier. This constructor helps build an OID object with a String like .1.2.3.4 or 1.2.3.4 or ifInOctets or ifInOctets.0.

param
s String or MIB variable of the form .1.2.3 or 1.2.3 or ifInOctets.
exception
IllegalArgumentException The subidentifier is neither a numeric String nor a String of the MIB database.

	String dotString = s ;

	if (s.startsWith(".") == false) {
	    try {
		dotString = resolveVarName(s);
	    } catch(SnmpStatusException e) {
		throw new IllegalArgumentException(e.getMessage());
	    }
	}

	StringTokenizer st = new StringTokenizer(dotString, ".", false) ;
	componentCount= st.countTokens();
    
	// Now extract the ids
	//
	if (componentCount == 0) {
	    components = new long[15] ;
	}  else {
	    components = new long[componentCount] ;
	    try {
		for (int i = 0 ; i < componentCount ; i++) {
		    try {
			components[i] = Long.parseLong(st.nextToken()) ;
		    }
		    catch(NoSuchElementException e) {}
		}
	    }
	    catch(NumberFormatException e) {
		throw new IllegalArgumentException(s) ;
	    }
	}
    
Methods Summary
public voidaddToOid(java.lang.String s)
Adds the specified dot-formatted OID String to the end of this SnmpOid. The subidentifiers can be expressed as a dot-formatted String or a MIB variable name.

param
s Variable name of the form .1.2.3 or 1.2.3 or ifInOctets.
exception
SnmpStatusException An error occurred while accessing a MIB node.

	SnmpOid suffix= new SnmpOid(s);
	this.append(suffix);
    
public voidaddToOid(long[] oid)
Adds the specified array of longs to the end of this SnmpOid.

param
oid An array of longs.
exception
SnmpStatusException An error occurred while accessing a MIB node.

	SnmpOid suffix= new SnmpOid(oid);
	this.append(suffix);
    
public voidappend(com.sun.jmx.snmp.SnmpOid oid)
Appends the specified SnmpOid to the end of this SnmpOid.

param
oid The OID to append.

	enlargeIfNeeded(oid.componentCount) ;
	for (int i = 0 ; i < oid.componentCount ; i++) {
	    components[componentCount + i] = oid.components[i] ;
	}
	componentCount += oid.componentCount ;
    
public voidappend(long id)
Appends the specified long to the end of this SnmpOid.

param
id The long to append.

	enlargeIfNeeded(1) ;
	components[componentCount] = id ;
	componentCount++ ;
    
public static voidappendToOid(com.sun.jmx.snmp.SnmpOid source, com.sun.jmx.snmp.SnmpOid dest)
Appends an SnmpOid representing an SnmpOid to another OID.

param
source An OID representing an SnmpOid value.
param
dest Where source should be appended.

	dest.append(source.getLength()) ;
	dest.append(source) ;
    
public java.lang.Objectclone()
Clones the SnmpOid object, making a copy of its data.

return
The object clone.

	try {
	    SnmpOid obj = (SnmpOid)super.clone() ;
	    obj.components = new long[this.componentCount] ;

	    System.arraycopy(this.components, 0, obj.components, 0,
			     this.componentCount) ;
	    return obj ;
	} catch (CloneNotSupportedException e) {
	    throw new InternalError() ;  // should never happen. VM bug.
	}
    
public intcompareTo(com.sun.jmx.snmp.SnmpOid other)
Compares two OIDs lexicographically.

param
other The OID to be compared.
return
The value 0 if the parameter other is equal to this SnmpOid. A value smaller than 0 if this SnmpOid is lexicographically smaller than other. A value larger than 0 if this SnmpOid is lexicographically larger than other.

	int result = 0 ;
	int i = 0 ;
	int cmplen = Math.min(componentCount, other.componentCount) ;
	long[] otheroid = other.components;

	for (i = 0; i < cmplen; i++) {
	    if (components[i] != otheroid[i]) {
		break ;
	    }
	}
	if ((i == componentCount) && (i == other.componentCount)) {
	    result = 0 ;
	}
	else if (i == componentCount) {
	    result = -1 ;
	}
	else if (i == other.componentCount) {
	    result = 1 ;
	}
	else {
	    result = (components[i] < otheroid[i]) ? -1 : 1 ;
	}
	return result ;
    
public final synchronized SnmpValueduplicate()
Performs a clone action. This provides a workaround for the SnmpValue interface.

return
The SnmpValue clone.

	return (SnmpValue)clone() ;
    
private voidenlargeIfNeeded(int n)
Checks if there is enough space in the components array to insert n new subids. If not, it increases the size of the array. In fact it reallocates a new array and copy the old one into the new one.

param
n The number of subids to insert.

	int neededSize = components.length ;
	while (componentCount + n > neededSize) {
	    neededSize = neededSize * 2 ;
	}
	if (neededSize > components.length) {
	    long[] newComponents = new long[neededSize] ;
	    for (int i = 0 ; i < components.length ; i++) {
		newComponents[i] = components[i] ;
	    }
	    components = newComponents ;
	}
    
public booleanequals(java.lang.Object o)
Checks whether the specified Object is equal to this SnmpOid.

param
o The Object to be compared.
return
true if o is an SnmpOid instance and equal to this, false otherwise.

	boolean result = false ;
    
	if (o instanceof SnmpOid) {
	    SnmpOid oid = (SnmpOid)o ;
	    if (oid.componentCount == componentCount) {
		int i = 0 ;
		long[]  objoid = oid.components;
		while ((i < componentCount) && (components[i] == objoid[i]))
		    i++ ;
		result = (i == componentCount) ;
	    }
	}
	return result ;
    
public intgetLength()
Gets the number of components in this OID.

return
The number of components.

	return componentCount ;
    
public final longgetOidArc(int pos)
Returns the value of the OID arc found at the requested position in the components array. The first element is at position 0.

param
pos The position at which the OID arc should be peeked.
return
The OID arc found at the requested position.
exception
SnmpStatusException No OID arc was found at the requested position.

	try {
	    return components[pos];
	} catch(Exception e) {
	    throw new SnmpStatusException(SnmpStatusException.noAccess);
	}
    
public static SnmpOidTablegetSnmpOidTable()
Returns the MIB table used for resolving MIB variable names.

return
The MIB table.

	return meta;
    
public java.lang.StringgetTypeName()
Returns a textual description of the type object.

return
ASN.1 textual description.

	return name ;
    
private java.lang.StringhandleLong(java.lang.String oid, int index)

	String str;
	if (index >0) {
	    str= oid.substring(0, index);
	} else {
	    str= oid ;
	}
    
	// just parse the element.
	//
	Long.parseLong(str);
	return oid;
    
public inthashCode()
The hashCode is computed from the OID components.

return
a hashCode for this SnmpOid.

	long acc=0;
	for (int i=0;i<componentCount;i++) {
	    acc = acc*31+components[i];
	}
	return (int)acc;
    
public voidinsert(long id)
Inserts a subid at the beginning of this SnmpOid.

param
id The long subid to insert.

	enlargeIfNeeded(1) ;
	for (int i = componentCount - 1 ; i >= 0 ; i--) {
	    components[i + 1] = components[i] ;
	}
	components[0] = id ;
	componentCount++ ;
    
public voidinsert(int id)
Inserts a subid at the beginning of this SnmpOid.

param
id The integer subid to insert.

	insert((long)id) ;
    
public booleanisValid()
Checks the validity of the OID.

return
true if the OID is valid, false otherwise.

	return ((componentCount >= 2) &&
		((0 <= components[0]) && (components[0] < 3)) &&
		((0 <= components[1]) && (components[1] < 40))) ;
    
public long[]longValue()
Returns a copy of the components array of this SnmpOid.

return
The copy of the components array.

	long[] result = new long[componentCount] ;
	System.arraycopy(components,0,result,0,componentCount);
	return result ;
    
public final long[]longValue(boolean duplicate)
Returns the components array of this SnmpOid. If duplicate is true, a copy is returned. Otherwise, a reference to the internal array is returned, in which case the caller shall not modify this array. This method is provided to optimize processing in those cases where the caller needs only to read the components array.

param
duplicate Indicates whether a copy or a reference must be returned:
  • true if a copy must be returned,
  • false if a reference on the internal data can be returned.
  • return
    A copy of (or a reference on) the components array.

    	if (duplicate) return longValue();
    	if (componentCount == components.length) return components ;
    	components =  longValue();
    	componentCount = components.length;
    	return components ;
        
    public static intnextOid(long[] index, int start)
    Scans an index OID, skips the OID value and returns the position of the next value.

    param
    index The index array.
    param
    start The position in the index array.
    return
    The position of the next value.
    exception
    SnmpStatusException There is no OID value available at the start position.

    	try {
    	    if (index[start] > Integer.MAX_VALUE) {
    		throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
    	    }
    	    int idCount = (int)index[start++] ;
    	    start += idCount ;
    	    if (start <= index.length) {
    		return start ;
    	    }
    	    else {
    		throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
    	    }
    	}
    	catch(IndexOutOfBoundsException e) {
    	    throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
    	}
        
    public java.lang.StringresolveVarName(java.lang.String s)
    Resolves a MIB variable String with the MIB database.

    param
    s The variable name to resolve.
    exception
    SnmpStatusException If the variable is not found in the MIB database.

    	int index = s.indexOf('.") ;
        
    	// First handle the case where oid is expressed as 1.2.3.4
    	//
    	try {
    	    return handleLong(s, index);
    	} catch(NumberFormatException e) {}
        
    	// if we are here, it means we have something to resolve..
    	//
    	if (meta == null)
    	  throw new SnmpStatusException(SnmpStatusException.noSuchName);
        
    	// Ok assume there is a variable name to resolve ...
    	//
    	if (index <= 0) {          
    	    SnmpOidRecord rec = meta.resolveVarName(s);
    	    return rec.getOid();
    	
    	} else {
    	    SnmpOidRecord rec = meta.resolveVarName(s.substring(0, index));
    	    return (rec.getOid()+ s.substring(index));
    
    	}
        
    public static voidsetSnmpOidTable(SnmpOidTable db)
    Sets the MIB table to use for resolving MIB variable names. If no mib table is available, the class will not be able to resolve names contained in the Object Identifier.

    param
    db The MIB table to use.

    	meta = db;
        
    public java.lang.BooleantoBoolean()
    Converts the OID value to its Boolean form.

    return
    The Boolean representation of the value.

    	if ((componentCount != 1) && (components[0] != 1) && (components[0] != 2)) {
    	    throw new IllegalArgumentException() ;
    	}
    	return new Boolean(components[0] == 1) ;
        
    public java.lang.Byte[]toByte()
    Converts the OID value to its array of Bytes form.

    return
    The array of Bytes representation of the value.

    	Byte[] result = new Byte[componentCount] ;
    	for (int i =0 ; i < componentCount ; i++) {
    	    if (components[0] > 255) {
    		throw new IllegalArgumentException() ;
    	    }
    	    result[i] = new Byte((byte)components[i]) ;
    	}
    	return result ;
        
    public java.lang.IntegertoInteger()
    Converts the OID value to its Integer form.

    return
    The Integer representation of the value.

    	if ((componentCount != 1) || (components[0] > Integer.MAX_VALUE)) {
    	    throw new IllegalArgumentException() ;
    	}
    	return new Integer((int)components[0]) ;
        
    public java.lang.LongtoLong()
    Converts the OID value to its Long form.

    return
    The Long representation of the value.

    	if (componentCount != 1) {
    	    throw new IllegalArgumentException() ;
    	}
    	return new Long(components[0]) ;
        
    public java.lang.StringtoOctetString()
    Converts an OID index converted string back to a DisplayString

     
    	return new String(tobyte()) ; 
        
    public com.sun.jmx.snmp.SnmpOidtoOid()
    Converts the OID value to its SnmpOid form.

    return
    The OID representation of the value.

    	long[] ids = new long[componentCount] ;
    	for (int i = 0 ; i < componentCount ; i++) {
    	    ids[i] = components[i] ;
    	}
    	return new SnmpOid(ids) ;
        
    public static com.sun.jmx.snmp.SnmpOidtoOid(long[] index, int start)
    Extracts the OID from an index OID and returns its value converted as an SnmpOid.

    param
    index The index array.
    param
    start The position in the index array.
    return
    The OID representing the OID value.
    exception
    SnmpStatusException There is no OID value available at the start position.

    	try {
    	    if (index[start] > Integer.MAX_VALUE) {
    		throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
    	    }
    	    int idCount = (int)index[start++] ;
    	    long[] ids = new long[idCount] ;
    	    for (int i = 0 ; i < idCount ; i++) {
    		ids[i] = index[start + i] ;
    	    }
    	    return new SnmpOid(ids) ;
    	}
    	catch(IndexOutOfBoundsException e) {
    	    throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
    	}
        
    public java.lang.StringtoString()
    Converts the OID value to its String form.

    return
    The String representation of the value.

    	String result = "" ;
    	if (componentCount >= 1) {
    	    for (int i = 0 ; i < componentCount - 1 ; i++) {
    		result = result + components[i] + "." ;
    	    }
    	    result = result + components[componentCount - 1] ;
    	}
    	return result ;
        
    private byte[]tobyte()
    convert the components array into a byte array

     
    	byte[] result = new byte[componentCount] ; 
    	for (int i =0 ; i < componentCount ; i++) { 
    	    if (components[0] > 255) { 
    		throw new IllegalArgumentException() ; 
    	    } 
    	    result[i] = (byte)components[i] ; 
    	} 
    	return result ;