KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpVarBind


1 /*
2  * @(#)file SnmpVarBind.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.12
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11 // Copyright (c) 1995-96 by Cisco Systems, Inc.
12

13 package com.sun.jmx.snmp;
14
15 // java imports
16
//
17
import java.io.Serializable JavaDoc;
18
19
20 /**
21  * This class holds information for a MIB variable contained in an {@link com.sun.jmx.snmp.SnmpVarBindList}.
22  * An <CODE>SnmpVarBind</CODE> consists of three parts:<P>
23  * <DL>
24  * <DD>- The corresponding OID object for the MIB variable.
25  * <DD>- The value part associated with that OID instance.
26  * If present, it determines the MIB syntax for the object.
27  * <DD>- The status of the <CODE>SnmpVarBind</CODE> which specifies whether the agent responded with an
28  * exception condition for this variable such as <CODE>noSuchInstance</CODE>, <CODE>endOfMibView</CODE>,
29  * or <CODE>noSuchObject</CODE>.
30  * </DL>
31  * <p><b>This API is a Sun Microsystems internal API and is subject
32  * to change without notice.</b></p>
33  */

34
35 public class SnmpVarBind implements SnmpDataTypeEnums, Cloneable JavaDoc, Serializable JavaDoc {
36
37     // PUBLIC VARIABLES
38
//-----------------
39

40     /**
41      * Keeps the legend for the value part of the <CODE>SnmpVarBind</CODE>.
42      */

43     static final public String JavaDoc statusLegend[] = { "Status Mapper", "Value not initialized",
44                                                   "Valid Value", "No such object",
45                                                   "No such Instance", "End of Mib View" } ;
46
47     /**
48      * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is not initialized.
49      */

50     static final public int stValueUnspecified = 1 ;
51
52     /**
53      * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is valid.
54      */

55     static final public int stValueOk = 2 ;
56
57     /**
58      * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is <CODE>noSuchObject</CODE>.
59      * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
60      */

61     static final public int stValueNoSuchObject = 3 ;
62
63     /**
64      * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is
65      * <CODE>noSuchInstance</CODE>.
66      * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
67      * In the SNMPv1 context, this is appropriate when <CODE>noSuchName</CODE> is returned in response to the
68      * <CODE>SnmpGet</CODE> request.
69      */

70     static final public int stValueNoSuchInstance = 4 ;
71
72     /**
73      * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is <CODE>endOfMibView</CODE>.
74      * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
75      * In the SNMPv1 context, this is appropriate when <CODE>noSuchName</CODE> is returned in response to the
76      * <CODE>SnmpGetNext</CODE> request.
77      */

78     static final public int stValueEndOfMibView = 5 ;
79
80     
81     //
82
// These are predefined values for SNMP V2 variables
83
//
84
/**
85      * Error code value as defined in RFC 1448 for: <CODE>noSuchObject</CODE>.
86      */

87     public final static SnmpNull noSuchObject = new SnmpNull(errNoSuchObjectTag) ;
88
89     /**
90      * Error code value as defined in RFC 1448 for: <CODE>noSuchInstance</CODE>.
91      */

92     public final static SnmpNull noSuchInstance = new SnmpNull(errNoSuchInstanceTag) ;
93
94     /**
95      * Error code value as defined in RFC 1448 for: <CODE>endOfMibView</CODE>.
96      */

97     public final static SnmpNull endOfMibView = new SnmpNull(errEndOfMibViewTag) ;
98     
99     /**
100      * The OID of the <CODE>SnmpVarBind</CODE>.
101      * The default value is null.
102      * <p><b>Reserved for internal use:<b><br>
103      * As of Java Dynamic Management Kit 5.0, use instead <CODE>getOid</CODE> and <CODE>setOid</CODE></p>
104      */

105     public SnmpOid oid = null ;
106
107     /**
108      * The value of the <CODE>SnmpVarBind</CODE>.
109      * The default value is null.
110      * <p><b>Reserved for internal use:<b><br>
111      * As of Java Dynamic Management Kit 5.0, use instead <CODE>getSnmpValue</CODE> and <CODE>setSnmpValue</CODE></p>
112      */

113     public SnmpValue value = null ;
114     
115     /**
116      * Indicates the status of the value in this <CODE>SnmpVarBind</CODE>.
117      * The default value is <CODE>stValueUnspecified</CODE>.
118      * This attribute is updated internally and should not be changed otherwise.
119      */

120     public int status = stValueUnspecified ;
121     
122     
123     // CONSTRUCTORS
124
//-------------
125

126     /**
127      * Default constructor.
128      */

129     public SnmpVarBind() {
130     }
131     
132     /**
133      * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>SnmpOid</CODE> value.
134      * @param oid The OID part of the <CODE>SnmpVarBind</CODE>.
135      */

136     public SnmpVarBind(SnmpOid oid) {
137         this.oid = oid ;
138     }
139
140     /**
141      * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>SnmpOid</CODE> and
142      * <CODE>SnmpValue</CODE>.
143      * @param oid The OID part of the <CODE>SnmpVarBind</CODE>.
144      * @param val The value part of the <CODE>SnmpVarBind</CODE>.
145      */

146     public SnmpVarBind(SnmpOid oid, SnmpValue val) {
147         this.oid = oid ;
148         this.setSnmpValue(val) ;
149     }
150
151     /**
152      * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>String</CODE> value.
153      * If the name is a MIB variable, it resolves the name with the MIB database.
154      * @param name The MIB variable name or a dot-formatted OID <CODE>String</CODE>.
155      * @exception SnmpStatusException An error occurred while resolving the MIB variable name.
156      */

157     public SnmpVarBind(String JavaDoc name) throws SnmpStatusException {
158
159         if (name.startsWith(".")) {
160             this.oid = new SnmpOid(name) ;
161         } else {
162             SnmpOidRecord record= null;
163             try {
164                 int index = name.indexOf('.') ;
165                 handleLong(name, index);
166                 this.oid = new SnmpOid(name);
167             }
168             catch(NumberFormatException JavaDoc e) {
169                 int index = name.indexOf('.') ;
170                 if (index <= 0) {
171                     record = resolveVarName(name) ;
172                     this.oid = new SnmpOid(record.getName()) ;
173                 } else {
174                     record = resolveVarName(name.substring(0, index)) ;
175                     this.oid = new SnmpOid(record.getName() + name.substring(index)) ;
176                 }
177             }
178         }
179     }
180     
181     
182     // GETTER/SETTER
183
//--------------
184

185     /**
186      * Returns the complete OID part associated with this <CODE>SnmpVarBind</CODE>.
187      * @return The <CODE>SnmpOid</CODE> for this variable.
188      */

189     final public SnmpOid getOid() {
190         return this.oid ;
191     }
192
193     /**
194      * Sets the <CODE>SnmpOid</CODE> part associated with this <CODE>SnmpVarBind</CODE> with the specified OID.
195      * The value part of this <CODE>SnmpVarBind</CODE> will automatically be nulled.
196      * @param oid The new OID.
197      */

198     final public void setOid(SnmpOid oid) {
199         this.oid = oid ;
200         clearValue() ;
201     }
202
203     /**
204      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
205      * @return The <CODE>SnmpValue</CODE> for this variable.
206      */

207     final synchronized public SnmpValue getSnmpValue() {
208         return this.value ;
209     }
210   
211     /**
212      * Sets the <CODE>SnmpValue</CODE> part associated with this <CODE>SnmpVarBind</CODE> with the specified value.
213      * The status is updated to indicate that the value is valid.
214      * @param val The new value.
215      */

216     final public void setSnmpValue(SnmpValue val) {
217         this.value= val ;
218         setValueValid();
219     }
220
221     /**
222      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
223      * @return The <CODE>SnmpCounter64</CODE> value for this variable.
224      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
225      * it is not an instance.
226      */

227     final public SnmpCounter64 getSnmpCounter64Value() throws ClassCastException JavaDoc {
228         return (SnmpCounter64)this.value ;
229     }
230
231     /**
232      * Sets the <CODE>SnmpCounter64</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
233      * with the specified counter 64 value.
234      * The status is updated to indicate that the value is valid.
235      * @param val The new counter 64 value.
236      * @exception IllegalArgumentException The specified value is negative or larger than <CODE>Long.MAX_VALUE</CODE>.
237      * @see SnmpCounter64
238      */

239     final public void setSnmpCounter64Value(long val) throws IllegalArgumentException JavaDoc {
240         clearValue() ;
241         this.value = new SnmpCounter64(val) ;
242         setValueValid() ;
243     }
244
245     /**
246      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
247      * @return The <CODE>SnmpInt</CODE> value for this variable.
248      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
249      * it is not an instance.
250      */

251     final public SnmpInt getSnmpIntValue() throws ClassCastException JavaDoc {
252         return (SnmpInt)this.value ;
253     }
254
255     /**
256      * Sets the <CODE>SnmpInt</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
257      * with the specified integer value.
258      * The status is updated to indicate that the value is valid.
259      * @param val The new integer value.
260      * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
261      * or larger than <CODE>Integer.MAX_VALUE</CODE>.
262      * @see SnmpInt
263      */

264     final public void setSnmpIntValue(long val) throws IllegalArgumentException JavaDoc {
265         clearValue() ;
266         this.value = new SnmpInt(val) ;
267         setValueValid() ;
268     }
269
270     /**
271      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
272      * @return The <CODE>SnmpCounter</CODE> value for this variable.
273      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
274      * it is not an instance.
275      */

276     final public SnmpCounter getSnmpCounterValue() throws ClassCastException JavaDoc {
277         return (SnmpCounter)this.value ;
278     }
279
280     /**
281      * Sets the <CODE>SnmpCounter</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
282      * with the specified counter value.
283      * The status is updated to indicate that the value is valid.
284      * @param val The new counter value.
285      * @exception IllegalArgumentException The specified value is negative or larger than
286      * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
287      * @see SnmpCounter
288      */

289     final public void setSnmpCounterValue(long val) throws IllegalArgumentException JavaDoc {
290         clearValue() ;
291         this.value = new SnmpCounter(val) ;
292         setValueValid() ;
293     }
294
295     /**
296      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
297      * @return The <CODE>SnmpGauge</CODE> value for this variable.
298      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
299      * it is not an instance.
300      */

301     final public SnmpGauge getSnmpGaugeValue() throws ClassCastException JavaDoc {
302         return (SnmpGauge)this.value ;
303     }
304
305     /**
306      * Sets the <CODE>SnmpGauge</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
307      * with the specified gauge value.
308      * The status is updated to indicate that the value is valid.
309      * @param val The new gauge value.
310      * @exception IllegalArgumentException The specified value is negative or larger than
311      * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
312      * @see SnmpGauge
313      */

314     final public void setSnmpGaugeValue(long val) throws IllegalArgumentException JavaDoc {
315         clearValue() ;
316         this.value = new SnmpGauge(val) ;
317         setValueValid() ;
318     }
319
320     /**
321      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
322      * @return The <CODE>SnmpTimeticks</CODE> value for this variable.
323      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
324      * it is not an instance.
325      */

326     final public SnmpTimeticks getSnmpTimeticksValue() throws ClassCastException JavaDoc {
327         return (SnmpTimeticks)this.value ;
328     }
329
330     /**
331      * Sets the <CODE>SnmpTimeticks</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
332      * with the specified timeticks value.
333      * The status is updated to indicate that the value is valid.
334      * @param val The new timeticks value.
335      * @exception IllegalArgumentException The specified value is negative or larger than
336      * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
337      * @see SnmpTimeticks
338      */

339     final public void setSnmpTimeticksValue(long val) throws IllegalArgumentException JavaDoc {
340         clearValue() ;
341         this.value = new SnmpTimeticks(val) ;
342         setValueValid() ;
343     }
344
345     /**
346      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
347      * @return The <CODE>SnmpOid</CODE> value for this variable.
348      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
349      * it is not an instance.
350      */

351     final public SnmpOid getSnmpOidValue() throws ClassCastException JavaDoc {
352         return (SnmpOid)this.value ;
353     }
354
355     /**
356      * Sets the <CODE>SnmpOid</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
357      * with the specified OID value.
358      * The status is updated to indicate that the value is valid.
359      * @param val The new OID value.
360      * @exception IllegalArgumentException The specified value is neither a numeric <CODE>String</CODE>
361      * nor a <CODE>String</CODE> of the MIB database.
362      * @see SnmpOid
363      */

364     final public void setSnmpOidValue(String JavaDoc val) throws IllegalArgumentException JavaDoc {
365         clearValue() ;
366         this.value = new SnmpOid(val) ;
367         setValueValid() ;
368     }
369
370     /**
371      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
372      * @return The <CODE>SnmpIpAddress</CODE> value for this variable.
373      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
374      * it is not an instance.
375      */

376     final public SnmpIpAddress getSnmpIpAddressValue() throws ClassCastException JavaDoc {
377         return (SnmpIpAddress)this.value ;
378     }
379
380     /**
381      * Sets the <CODE>SnmpIpAddress</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
382      * with the specified ipAddress value.
383      * The status is updated to indicate that the value is valid.
384      * @param val The new IP address value.
385      * @exception IllegalArgumentException The specified value does not correspond to an IP address.
386      * @see SnmpIpAddress
387      */

388     final public void setSnmpIpAddressValue(String JavaDoc val) throws IllegalArgumentException JavaDoc {
389         clearValue() ;
390         this.value = new SnmpIpAddress(val) ;
391         setValueValid() ;
392     }
393
394     /**
395      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
396      * @return The <CODE>SnmpString</CODE> value for this variable.
397      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
398      * it is not an instance.
399      */

400     final public SnmpString getSnmpStringValue() throws ClassCastException JavaDoc {
401         return (SnmpString)this.value ;
402     }
403
404     /**
405      * Sets the <CODE>SnmpString</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
406      * with the specified string value.
407      * The status is updated to indicate that the value is valid.
408      * @param val The new string value.
409      * @see SnmpString
410      */

411     final public void setSnmpStringValue(String JavaDoc val) {
412         clearValue() ;
413         this.value = new SnmpString(val) ;
414         setValueValid() ;
415     }
416
417     /**
418      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
419      * @return The <CODE>SnmpOpaque</CODE> value for this variable.
420      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
421      * it is not an instance.
422      */

423     final public SnmpOpaque getSnmpOpaqueValue() throws ClassCastException JavaDoc {
424         return (SnmpOpaque)this.value ;
425     }
426
427     /**
428      * Sets the <CODE>SnmpOpaque</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
429      * with the specified bytes array values.
430      * The status is updated to indicate that the value is valid.
431      * @param val The new bytes array value.
432      * @see SnmpOpaque
433      */

434     final public void setSnmpOpaqueValue(byte[] val) {
435         clearValue() ;
436         this.value = new SnmpOpaque(val) ;
437         setValueValid() ;
438     }
439
440     /**
441      * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
442      * @return The <CODE>SnmpStringFixed</CODE> value for this variable.
443      * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
444      * it is not an instance.
445      */

446     final public SnmpStringFixed getSnmpStringFixedValue() throws ClassCastException JavaDoc {
447         return (SnmpStringFixed)this.value ;
448     }
449
450     /**
451      * Sets the <CODE>SnmpStringFixed</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
452      * with the specified string value.
453      * The status is updated to indicate that the value is valid.
454      * @param val The new string value.
455      * @see SnmpStringFixed
456      */

457     final public void setSnmpStringFixedValue(String JavaDoc val) {
458         clearValue() ;
459         this.value = new SnmpStringFixed(val) ;
460         setValueValid() ;
461     }
462     
463     
464     // PUBLIC METHODS
465
//---------------
466

467     /**
468      * Consults the MIB table storage to resolve the name to its OID type structure.
469      * @param name The MIB variable name or a dot-formatted OID <CODE>String</CODE>.
470      * @return The <CODE>SnmpOidRecord</CODE> object containing information on the MIB variable.
471      * @exception SnmpStatusException An error occurred while resolving the MIB variable name.
472      */

473     public SnmpOidRecord resolveVarName(String JavaDoc name) throws SnmpStatusException {
474         
475         SnmpOidTable mibTable = oid.getSnmpOidTable();
476         if (mibTable == null)
477             throw new SnmpStatusException(SnmpStatusException.noSuchName);
478         int index = name.indexOf('.');
479         if (index < 0) {
480             return mibTable.resolveVarName(name);
481         } else {
482             return mibTable.resolveVarOid(name);
483         }
484     }
485     
486     /**
487      * Returns the status of the value associated with this <CODE>SnmpVarBind</CODE> as an integer.
488      * This value is one of {@link #stValueUnspecified}, {@link #stValueOk}, {@link #stValueNoSuchObject},
489      * {@link #stValueNoSuchInstance}, {@link #stValueEndOfMibView}.
490      * @return The status of the associated value.
491      */

492     final public int getValueStatus() {
493         return status ;
494     }
495
496     /**
497      * Returns the status of the value associated with this <CODE>SnmpVarBind</CODE> as a <CODE>String</CODE>.
498      * This value is a displayable representation of the status integer value.
499      * It is one of <CODE>Value not initialized</CODE>, <CODE>Valid Value</CODE>, <CODE>No such object</CODE>,
500      * <CODE>No such Instance</CODE>, <CODE>End of Mib View</CODE>.
501      * @return The status of the associated value.
502      */

503     final public String JavaDoc getValueStatusLegend() {
504         return statusLegend[status] ;
505     }
506     
507     /**
508      * Checks whether the object contains a valid accessible value.
509      * @return <CODE>true</CODE> if the associated value is valid, <CODE>false</CODE> otherwise.
510      */

511     final public boolean isValidValue() {
512         return (status == stValueOk) ;
513     }
514
515     /**
516      * Checks whether the value associated with this <CODE>SnmpVarBind</CODE> is unspecified.
517      * @return <CODE>true</CODE> if the status is unspecified, <CODE>false</CODE> otherwise.
518      */

519     final public boolean isUnspecifiedValue() {
520         return (status == stValueUnspecified) ;
521     }
522
523     /**
524      * Clears the value associated with this <CODE>SnmpVarBind</CODE> and sets the status to
525      * <CODE>stValueUnspecified</CODE>.
526      */

527     final public void clearValue() {
528         this.value = null ;
529         status = stValueUnspecified ;
530     }
531     
532     /**
533      * Checks whether the OID for this variable completely matches the OID part of the specified
534      * <CODE>SnmpVarBind</CODE> object.
535      * @param var The object whose OID part is to be matched.
536      * @return <CODE>true</CODE> if the OID part matches exactly, <CODE>false</CODE> otherwise.
537      */

538     final public boolean isOidEqual(SnmpVarBind var) {
539         return this.oid.equals(var.oid) ;
540     }
541
542     /**
543      * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
544      * Note that there is no <CODE>getInstance</CODE> method.
545      * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
546      * @param inst The sub-identifier to be appended to the OID.
547      */

548     final public void addInstance(long inst) {
549         oid.append(inst) ;
550         return ;
551     }
552
553     /**
554      * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
555      * Note that there is no <CODE>getInstance</CODE> method.
556      * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
557      * @param inst The sub-identifier array to be appended to the OID.
558      * @exception SnmpStatusException An error occurred while accessing a MIB node.
559      */

560     final public void addInstance(long[] inst) throws SnmpStatusException {
561         oid.addToOid(inst) ;
562         return ;
563     }
564
565     /**
566      * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
567      * Note that there is no <CODE>getInstance</CODE> method.
568      * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
569      * @param inst Dot-formatted sub-identifier <CODE>String</CODE> to be appended to the OID.
570      * @exception SnmpStatusException An error occurred while accessing a MIB node.
571      */

572     final public void addInstance(String JavaDoc inst) throws SnmpStatusException {
573         if (inst != null) {
574             oid.addToOid(inst) ;
575         }
576         return ;
577     }
578
579     /**
580      * Inserts a sub-id at the beginning of the OID of this <CODE>SnmpVarBind</CODE>.
581      * @param oid The sub-id to insert.
582      */

583     public void insertInOid(int oid) {
584         this.oid.insert(oid) ;
585     }
586
587     /**
588      * Appends the specified <CODE>SnmpOid</CODE> to the end of the OID of this <CODE>SnmpVarBind</CODE>.
589      * @param oid The OID to append.
590      */

591     public void appendInOid(SnmpOid oid) {
592         this.oid.append(oid) ;
593     }
594     
595     /**
596      * Determines whether the <CODE>SnmpVarBind</CODE> has an SNMP exception
597      * (generated by agent in response to a request).
598      * @return <CODE>true</CODE> if the <CODE>SnmpVarBind</CODE> has an SNMP response exception,
599      * <CODE>false</CODE> otherwise.
600      */

601     final public synchronized boolean hasVarBindException() {
602         switch (status) {
603         case stValueUnspecified :
604         case stValueNoSuchObject :
605         case stValueNoSuchInstance :
606         case stValueEndOfMibView :
607             return true ;
608         }
609         return false ;
610     }
611
612     /**
613      * Clones and copies the OID and value part from another <CODE>SnmpVarBind</CODE> object.
614      * @param var The <CODE>SnmpVarBind</CODE> clone.
615      */

616     public void copyValueAndOid(SnmpVarBind var) {
617         setOid((SnmpOid) (var.oid.clone())) ;
618         copyValue(var) ;
619     }
620
621     /**
622      * Clones and copies only the value part from another <CODE>SnmpVarBind</CODE> object.
623      * @param var The <CODE>SnmpVarBind</CODE> clone.
624      */

625     public void copyValue(SnmpVarBind var) {
626         if (var.isValidValue()) {
627             this.value = var.getSnmpValue().duplicate() ;
628             setValueValid() ;
629         } else {
630             status = var.getValueStatus() ;
631         if (status == stValueEndOfMibView) value=endOfMibView;
632         else if (status == stValueNoSuchObject) value=noSuchObject;
633         else if (status == stValueNoSuchInstance) value=noSuchInstance;
634         }
635     }
636
637     /**
638      * Clones the SNMP variable. It does not clone the value portion.
639      * @return A new object with the value part set to null.
640      */

641     public Object JavaDoc cloneWithoutValue() {
642         SnmpOid noid = (SnmpOid)this.oid.clone() ;
643         return new SnmpVarBind(noid) ;
644     }
645
646     /**
647      * Clones the SNMP variable (including value).
648      * @return The SNMP variable clone.
649      */

650     public Object JavaDoc clone() {
651 // SnmpVarBind v = null ;
652
// try {
653
// v = (SnmpVarBind) super.clone() ;
654
// v.copyValueAndOid(this) ;
655
// } catch (CloneNotSupportedException e) {
656
// throw new InternalError() ;
657
// }
658
// return v ;
659
SnmpVarBind v = new SnmpVarBind() ;
660         v.copyValueAndOid(this) ;
661         return v ;
662     }
663
664     /**
665      * Returns the printable ASCII representation for the corresponding variable value.
666      * @return The printable ASCII representation.
667      */

668     final public String JavaDoc getStringValue() {
669         return this.value.toString() ;
670     }
671
672     /**
673      * Set the value to {@link #noSuchObject}. This is equivalent to
674      * <code>setSnmpValue(SnmpVarBind.noSuchObject)</code>.
675      **/

676     final public void setNoSuchObject() {
677     value=noSuchObject;
678     status=stValueNoSuchObject;
679     }
680
681     /**
682      * Set the value to {@link #noSuchInstance}. This is equivalent to
683      * <code>setSnmpValue(SnmpVarBind.noSuchInstance)</code>.
684      **/

685     final public void setNoSuchInstance() {
686     value=noSuchInstance;
687     status=stValueNoSuchInstance;
688     }
689
690     /**
691      * Set the value to {@link #endOfMibView}. This is equivalent to
692      * <code>setSnmpValue(SnmpVarBind.endOfMibView)</code>.
693      **/

694     final public void setEndOfMibView() {
695     value=endOfMibView;
696     status=stValueEndOfMibView;
697     }
698
699      /**
700      * Returns the printable ASCII representation of this <CODE>SnmpVarBind</CODE>.
701      * @return The printable ASCII representation.
702      */

703     final public String JavaDoc toString() {
704         StringBuffer JavaDoc s = new StringBuffer JavaDoc(400) ;
705         s.append("Object ID : " + this.oid.toString()) ;
706
707         if (isValidValue()) {
708             s.append(" (Syntax : " + this.value.getTypeName() + ")\n") ;
709             s.append("Value : " + this.value.toString()) ;
710         } else {
711             s.append("\n" + "Value Exception : " + getValueStatusLegend()) ;
712         }
713         return s.toString() ;
714     }
715
716     
717     // PRIVATE METHODS
718
//----------------
719

720     /**
721      * Sets the status to indicate that the value for this <CODE>SnmpVarBind</CODE> is valid.
722      */

723     private void setValueValid() {
724     if (value == endOfMibView) status=stValueEndOfMibView;
725     else if (value == noSuchObject) status=stValueNoSuchObject;
726     else if (value == noSuchInstance) status=stValueNoSuchInstance;
727         else status = stValueOk ;
728     }
729
730     private void handleLong(String JavaDoc oid, int index) throws NumberFormatException JavaDoc, SnmpStatusException {
731
732         String JavaDoc str;
733         if (index >0) {
734             str= oid.substring(0, index);
735         } else {
736             str= oid ;
737         }
738
739         // just parse the element.
740
//
741
Long.parseLong(str);
742     }
743 }
744
Popular Tags