1. /*
  2. * @(#)file SnmpVarBind.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.12
  5. * @(#)date 04/09/15
  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. package com.sun.jmx.snmp;
  13. // java imports
  14. //
  15. import java.io.Serializable;
  16. /**
  17. * This class holds information for a MIB variable contained in an {@link com.sun.jmx.snmp.SnmpVarBindList}.
  18. * An <CODE>SnmpVarBind</CODE> consists of three parts:<P>
  19. * <DL>
  20. * <DD>- The corresponding OID object for the MIB variable.
  21. * <DD>- The value part associated with that OID instance.
  22. * If present, it determines the MIB syntax for the object.
  23. * <DD>- The status of the <CODE>SnmpVarBind</CODE> which specifies whether the agent responded with an
  24. * exception condition for this variable such as <CODE>noSuchInstance</CODE>, <CODE>endOfMibView</CODE>,
  25. * or <CODE>noSuchObject</CODE>.
  26. * </DL>
  27. * <p><b>This API is a Sun Microsystems internal API and is subject
  28. * to change without notice.</b></p>
  29. */
  30. public class SnmpVarBind implements SnmpDataTypeEnums, Cloneable, Serializable {
  31. // PUBLIC VARIABLES
  32. //-----------------
  33. /**
  34. * Keeps the legend for the value part of the <CODE>SnmpVarBind</CODE>.
  35. */
  36. static final public String statusLegend[] = { "Status Mapper", "Value not initialized",
  37. "Valid Value", "No such object",
  38. "No such Instance", "End of Mib View" } ;
  39. /**
  40. * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is not initialized.
  41. */
  42. static final public int stValueUnspecified = 1 ;
  43. /**
  44. * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is valid.
  45. */
  46. static final public int stValueOk = 2 ;
  47. /**
  48. * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is <CODE>noSuchObject</CODE>.
  49. * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
  50. */
  51. static final public int stValueNoSuchObject = 3 ;
  52. /**
  53. * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is
  54. * <CODE>noSuchInstance</CODE>.
  55. * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
  56. * In the SNMPv1 context, this is appropriate when <CODE>noSuchName</CODE> is returned in response to the
  57. * <CODE>SnmpGet</CODE> request.
  58. */
  59. static final public int stValueNoSuchInstance = 4 ;
  60. /**
  61. * Useful constant indicating that the status of the <CODE>SnmpVarBind</CODE> object is <CODE>endOfMibView</CODE>.
  62. * Status of <CODE>SnmpVarBind</CODE> as returned by the SNMPv2 agent.
  63. * In the SNMPv1 context, this is appropriate when <CODE>noSuchName</CODE> is returned in response to the
  64. * <CODE>SnmpGetNext</CODE> request.
  65. */
  66. static final public int stValueEndOfMibView = 5 ;
  67. //
  68. // These are predefined values for SNMP V2 variables
  69. //
  70. /**
  71. * Error code value as defined in RFC 1448 for: <CODE>noSuchObject</CODE>.
  72. */
  73. public final static SnmpNull noSuchObject = new SnmpNull(errNoSuchObjectTag) ;
  74. /**
  75. * Error code value as defined in RFC 1448 for: <CODE>noSuchInstance</CODE>.
  76. */
  77. public final static SnmpNull noSuchInstance = new SnmpNull(errNoSuchInstanceTag) ;
  78. /**
  79. * Error code value as defined in RFC 1448 for: <CODE>endOfMibView</CODE>.
  80. */
  81. public final static SnmpNull endOfMibView = new SnmpNull(errEndOfMibViewTag) ;
  82. /**
  83. * The OID of the <CODE>SnmpVarBind</CODE>.
  84. * The default value is null.
  85. * <p><b>Reserved for internal use:<b><br>
  86. * As of Java Dynamic Management Kit 5.0, use instead <CODE>getOid</CODE> and <CODE>setOid</CODE></p>
  87. */
  88. public SnmpOid oid = null ;
  89. /**
  90. * The value of the <CODE>SnmpVarBind</CODE>.
  91. * The default value is null.
  92. * <p><b>Reserved for internal use:<b><br>
  93. * As of Java Dynamic Management Kit 5.0, use instead <CODE>getSnmpValue</CODE> and <CODE>setSnmpValue</CODE></p>
  94. */
  95. public SnmpValue value = null ;
  96. /**
  97. * Indicates the status of the value in this <CODE>SnmpVarBind</CODE>.
  98. * The default value is <CODE>stValueUnspecified</CODE>.
  99. * This attribute is updated internally and should not be changed otherwise.
  100. */
  101. public int status = stValueUnspecified ;
  102. // CONSTRUCTORS
  103. //-------------
  104. /**
  105. * Default constructor.
  106. */
  107. public SnmpVarBind() {
  108. }
  109. /**
  110. * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>SnmpOid</CODE> value.
  111. * @param oid The OID part of the <CODE>SnmpVarBind</CODE>.
  112. */
  113. public SnmpVarBind(SnmpOid oid) {
  114. this.oid = oid ;
  115. }
  116. /**
  117. * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>SnmpOid</CODE> and
  118. * <CODE>SnmpValue</CODE>.
  119. * @param oid The OID part of the <CODE>SnmpVarBind</CODE>.
  120. * @param val The value part of the <CODE>SnmpVarBind</CODE>.
  121. */
  122. public SnmpVarBind(SnmpOid oid, SnmpValue val) {
  123. this.oid = oid ;
  124. this.setSnmpValue(val) ;
  125. }
  126. /**
  127. * Constructs a new <CODE>SnmpVarBind</CODE> object from the specified <CODE>String</CODE> value.
  128. * If the name is a MIB variable, it resolves the name with the MIB database.
  129. * @param name The MIB variable name or a dot-formatted OID <CODE>String</CODE>.
  130. * @exception SnmpStatusException An error occurred while resolving the MIB variable name.
  131. */
  132. public SnmpVarBind(String name) throws SnmpStatusException {
  133. if (name.startsWith(".")) {
  134. this.oid = new SnmpOid(name) ;
  135. } else {
  136. SnmpOidRecord record= null;
  137. try {
  138. int index = name.indexOf('.') ;
  139. handleLong(name, index);
  140. this.oid = new SnmpOid(name);
  141. }
  142. catch(NumberFormatException e) {
  143. int index = name.indexOf('.') ;
  144. if (index <= 0) {
  145. record = resolveVarName(name) ;
  146. this.oid = new SnmpOid(record.getName()) ;
  147. } else {
  148. record = resolveVarName(name.substring(0, index)) ;
  149. this.oid = new SnmpOid(record.getName() + name.substring(index)) ;
  150. }
  151. }
  152. }
  153. }
  154. // GETTER/SETTER
  155. //--------------
  156. /**
  157. * Returns the complete OID part associated with this <CODE>SnmpVarBind</CODE>.
  158. * @return The <CODE>SnmpOid</CODE> for this variable.
  159. */
  160. final public SnmpOid getOid() {
  161. return this.oid ;
  162. }
  163. /**
  164. * Sets the <CODE>SnmpOid</CODE> part associated with this <CODE>SnmpVarBind</CODE> with the specified OID.
  165. * The value part of this <CODE>SnmpVarBind</CODE> will automatically be nulled.
  166. * @param oid The new OID.
  167. */
  168. final public void setOid(SnmpOid oid) {
  169. this.oid = oid ;
  170. clearValue() ;
  171. }
  172. /**
  173. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  174. * @return The <CODE>SnmpValue</CODE> for this variable.
  175. */
  176. final synchronized public SnmpValue getSnmpValue() {
  177. return this.value ;
  178. }
  179. /**
  180. * Sets the <CODE>SnmpValue</CODE> part associated with this <CODE>SnmpVarBind</CODE> with the specified value.
  181. * The status is updated to indicate that the value is valid.
  182. * @param val The new value.
  183. */
  184. final public void setSnmpValue(SnmpValue val) {
  185. this.value= val ;
  186. setValueValid();
  187. }
  188. /**
  189. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  190. * @return The <CODE>SnmpCounter64</CODE> value for this variable.
  191. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  192. * it is not an instance.
  193. */
  194. final public SnmpCounter64 getSnmpCounter64Value() throws ClassCastException {
  195. return (SnmpCounter64)this.value ;
  196. }
  197. /**
  198. * Sets the <CODE>SnmpCounter64</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  199. * with the specified counter 64 value.
  200. * The status is updated to indicate that the value is valid.
  201. * @param val The new counter 64 value.
  202. * @exception IllegalArgumentException The specified value is negative or larger than <CODE>Long.MAX_VALUE</CODE>.
  203. * @see SnmpCounter64
  204. */
  205. final public void setSnmpCounter64Value(long val) throws IllegalArgumentException {
  206. clearValue() ;
  207. this.value = new SnmpCounter64(val) ;
  208. setValueValid() ;
  209. }
  210. /**
  211. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  212. * @return The <CODE>SnmpInt</CODE> value for this variable.
  213. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  214. * it is not an instance.
  215. */
  216. final public SnmpInt getSnmpIntValue() throws ClassCastException {
  217. return (SnmpInt)this.value ;
  218. }
  219. /**
  220. * Sets the <CODE>SnmpInt</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  221. * with the specified integer value.
  222. * The status is updated to indicate that the value is valid.
  223. * @param val The new integer value.
  224. * @exception IllegalArgumentException The specified value is smaller than <CODE>Integer.MIN_VALUE</CODE>
  225. * or larger than <CODE>Integer.MAX_VALUE</CODE>.
  226. * @see SnmpInt
  227. */
  228. final public void setSnmpIntValue(long val) throws IllegalArgumentException {
  229. clearValue() ;
  230. this.value = new SnmpInt(val) ;
  231. setValueValid() ;
  232. }
  233. /**
  234. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  235. * @return The <CODE>SnmpCounter</CODE> value for this variable.
  236. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  237. * it is not an instance.
  238. */
  239. final public SnmpCounter getSnmpCounterValue() throws ClassCastException {
  240. return (SnmpCounter)this.value ;
  241. }
  242. /**
  243. * Sets the <CODE>SnmpCounter</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  244. * with the specified counter value.
  245. * The status is updated to indicate that the value is valid.
  246. * @param val The new counter value.
  247. * @exception IllegalArgumentException The specified value is negative or larger than
  248. * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
  249. * @see SnmpCounter
  250. */
  251. final public void setSnmpCounterValue(long val) throws IllegalArgumentException {
  252. clearValue() ;
  253. this.value = new SnmpCounter(val) ;
  254. setValueValid() ;
  255. }
  256. /**
  257. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  258. * @return The <CODE>SnmpGauge</CODE> value for this variable.
  259. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  260. * it is not an instance.
  261. */
  262. final public SnmpGauge getSnmpGaugeValue() throws ClassCastException {
  263. return (SnmpGauge)this.value ;
  264. }
  265. /**
  266. * Sets the <CODE>SnmpGauge</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  267. * with the specified gauge value.
  268. * The status is updated to indicate that the value is valid.
  269. * @param val The new gauge value.
  270. * @exception IllegalArgumentException The specified value is negative or larger than
  271. * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
  272. * @see SnmpGauge
  273. */
  274. final public void setSnmpGaugeValue(long val) throws IllegalArgumentException {
  275. clearValue() ;
  276. this.value = new SnmpGauge(val) ;
  277. setValueValid() ;
  278. }
  279. /**
  280. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  281. * @return The <CODE>SnmpTimeticks</CODE> value for this variable.
  282. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  283. * it is not an instance.
  284. */
  285. final public SnmpTimeticks getSnmpTimeticksValue() throws ClassCastException {
  286. return (SnmpTimeticks)this.value ;
  287. }
  288. /**
  289. * Sets the <CODE>SnmpTimeticks</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  290. * with the specified timeticks value.
  291. * The status is updated to indicate that the value is valid.
  292. * @param val The new timeticks value.
  293. * @exception IllegalArgumentException The specified value is negative or larger than
  294. * <CODE>SnmpUnsignedInt.MAX_VALUE</CODE>.
  295. * @see SnmpTimeticks
  296. */
  297. final public void setSnmpTimeticksValue(long val) throws IllegalArgumentException {
  298. clearValue() ;
  299. this.value = new SnmpTimeticks(val) ;
  300. setValueValid() ;
  301. }
  302. /**
  303. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  304. * @return The <CODE>SnmpOid</CODE> value for this variable.
  305. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  306. * it is not an instance.
  307. */
  308. final public SnmpOid getSnmpOidValue() throws ClassCastException {
  309. return (SnmpOid)this.value ;
  310. }
  311. /**
  312. * Sets the <CODE>SnmpOid</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  313. * with the specified OID value.
  314. * The status is updated to indicate that the value is valid.
  315. * @param val The new OID value.
  316. * @exception IllegalArgumentException The specified value is neither a numeric <CODE>String</CODE>
  317. * nor a <CODE>String</CODE> of the MIB database.
  318. * @see SnmpOid
  319. */
  320. final public void setSnmpOidValue(String val) throws IllegalArgumentException {
  321. clearValue() ;
  322. this.value = new SnmpOid(val) ;
  323. setValueValid() ;
  324. }
  325. /**
  326. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  327. * @return The <CODE>SnmpIpAddress</CODE> value for this variable.
  328. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  329. * it is not an instance.
  330. */
  331. final public SnmpIpAddress getSnmpIpAddressValue() throws ClassCastException {
  332. return (SnmpIpAddress)this.value ;
  333. }
  334. /**
  335. * Sets the <CODE>SnmpIpAddress</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  336. * with the specified ipAddress value.
  337. * The status is updated to indicate that the value is valid.
  338. * @param val The new IP address value.
  339. * @exception IllegalArgumentException The specified value does not correspond to an IP address.
  340. * @see SnmpIpAddress
  341. */
  342. final public void setSnmpIpAddressValue(String val) throws IllegalArgumentException {
  343. clearValue() ;
  344. this.value = new SnmpIpAddress(val) ;
  345. setValueValid() ;
  346. }
  347. /**
  348. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  349. * @return The <CODE>SnmpString</CODE> value for this variable.
  350. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  351. * it is not an instance.
  352. */
  353. final public SnmpString getSnmpStringValue() throws ClassCastException {
  354. return (SnmpString)this.value ;
  355. }
  356. /**
  357. * Sets the <CODE>SnmpString</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  358. * with the specified string value.
  359. * The status is updated to indicate that the value is valid.
  360. * @param val The new string value.
  361. * @see SnmpString
  362. */
  363. final public void setSnmpStringValue(String val) {
  364. clearValue() ;
  365. this.value = new SnmpString(val) ;
  366. setValueValid() ;
  367. }
  368. /**
  369. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  370. * @return The <CODE>SnmpOpaque</CODE> value for this variable.
  371. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  372. * it is not an instance.
  373. */
  374. final public SnmpOpaque getSnmpOpaqueValue() throws ClassCastException {
  375. return (SnmpOpaque)this.value ;
  376. }
  377. /**
  378. * Sets the <CODE>SnmpOpaque</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  379. * with the specified bytes array values.
  380. * The status is updated to indicate that the value is valid.
  381. * @param val The new bytes array value.
  382. * @see SnmpOpaque
  383. */
  384. final public void setSnmpOpaqueValue(byte[] val) {
  385. clearValue() ;
  386. this.value = new SnmpOpaque(val) ;
  387. setValueValid() ;
  388. }
  389. /**
  390. * Returns the value part associated with this <CODE>SnmpVarBind</CODE>.
  391. * @return The <CODE>SnmpStringFixed</CODE> value for this variable.
  392. * @exception ClassCastException An attempt has been made to cast an object to a subclass of which
  393. * it is not an instance.
  394. */
  395. final public SnmpStringFixed getSnmpStringFixedValue() throws ClassCastException {
  396. return (SnmpStringFixed)this.value ;
  397. }
  398. /**
  399. * Sets the <CODE>SnmpStringFixed</CODE> value part associated with this <CODE>SnmpVarBind</CODE>
  400. * with the specified string value.
  401. * The status is updated to indicate that the value is valid.
  402. * @param val The new string value.
  403. * @see SnmpStringFixed
  404. */
  405. final public void setSnmpStringFixedValue(String val) {
  406. clearValue() ;
  407. this.value = new SnmpStringFixed(val) ;
  408. setValueValid() ;
  409. }
  410. // PUBLIC METHODS
  411. //---------------
  412. /**
  413. * Consults the MIB table storage to resolve the name to its OID type structure.
  414. * @param name The MIB variable name or a dot-formatted OID <CODE>String</CODE>.
  415. * @return The <CODE>SnmpOidRecord</CODE> object containing information on the MIB variable.
  416. * @exception SnmpStatusException An error occurred while resolving the MIB variable name.
  417. */
  418. public SnmpOidRecord resolveVarName(String name) throws SnmpStatusException {
  419. SnmpOidTable mibTable = oid.getSnmpOidTable();
  420. if (mibTable == null)
  421. throw new SnmpStatusException(SnmpStatusException.noSuchName);
  422. int index = name.indexOf('.');
  423. if (index < 0) {
  424. return mibTable.resolveVarName(name);
  425. } else {
  426. return mibTable.resolveVarOid(name);
  427. }
  428. }
  429. /**
  430. * Returns the status of the value associated with this <CODE>SnmpVarBind</CODE> as an integer.
  431. * This value is one of {@link #stValueUnspecified}, {@link #stValueOk}, {@link #stValueNoSuchObject},
  432. * {@link #stValueNoSuchInstance}, {@link #stValueEndOfMibView}.
  433. * @return The status of the associated value.
  434. */
  435. final public int getValueStatus() {
  436. return status ;
  437. }
  438. /**
  439. * Returns the status of the value associated with this <CODE>SnmpVarBind</CODE> as a <CODE>String</CODE>.
  440. * This value is a displayable representation of the status integer value.
  441. * It is one of <CODE>Value not initialized</CODE>, <CODE>Valid Value</CODE>, <CODE>No such object</CODE>,
  442. * <CODE>No such Instance</CODE>, <CODE>End of Mib View</CODE>.
  443. * @return The status of the associated value.
  444. */
  445. final public String getValueStatusLegend() {
  446. return statusLegend[status] ;
  447. }
  448. /**
  449. * Checks whether the object contains a valid accessible value.
  450. * @return <CODE>true</CODE> if the associated value is valid, <CODE>false</CODE> otherwise.
  451. */
  452. final public boolean isValidValue() {
  453. return (status == stValueOk) ;
  454. }
  455. /**
  456. * Checks whether the value associated with this <CODE>SnmpVarBind</CODE> is unspecified.
  457. * @return <CODE>true</CODE> if the status is unspecified, <CODE>false</CODE> otherwise.
  458. */
  459. final public boolean isUnspecifiedValue() {
  460. return (status == stValueUnspecified) ;
  461. }
  462. /**
  463. * Clears the value associated with this <CODE>SnmpVarBind</CODE> and sets the status to
  464. * <CODE>stValueUnspecified</CODE>.
  465. */
  466. final public void clearValue() {
  467. this.value = null ;
  468. status = stValueUnspecified ;
  469. }
  470. /**
  471. * Checks whether the OID for this variable completely matches the OID part of the specified
  472. * <CODE>SnmpVarBind</CODE> object.
  473. * @param var The object whose OID part is to be matched.
  474. * @return <CODE>true</CODE> if the OID part matches exactly, <CODE>false</CODE> otherwise.
  475. */
  476. final public boolean isOidEqual(SnmpVarBind var) {
  477. return this.oid.equals(var.oid) ;
  478. }
  479. /**
  480. * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
  481. * Note that there is no <CODE>getInstance</CODE> method.
  482. * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
  483. * @param inst The sub-identifier to be appended to the OID.
  484. */
  485. final public void addInstance(long inst) {
  486. oid.append(inst) ;
  487. return ;
  488. }
  489. /**
  490. * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
  491. * Note that there is no <CODE>getInstance</CODE> method.
  492. * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
  493. * @param inst The sub-identifier array to be appended to the OID.
  494. * @exception SnmpStatusException An error occurred while accessing a MIB node.
  495. */
  496. final public void addInstance(long[] inst) throws SnmpStatusException {
  497. oid.addToOid(inst) ;
  498. return ;
  499. }
  500. /**
  501. * Adds an instance part to the OID in the <CODE>SnmpOid</CODE> object.
  502. * Note that there is no <CODE>getInstance</CODE> method.
  503. * This method will directly add the instance to the <CODE>SnmpOid</CODE> object.
  504. * @param inst Dot-formatted sub-identifier <CODE>String</CODE> to be appended to the OID.
  505. * @exception SnmpStatusException An error occurred while accessing a MIB node.
  506. */
  507. final public void addInstance(String inst) throws SnmpStatusException {
  508. if (inst != null) {
  509. oid.addToOid(inst) ;
  510. }
  511. return ;
  512. }
  513. /**
  514. * Inserts a sub-id at the beginning of the OID of this <CODE>SnmpVarBind</CODE>.
  515. * @param oid The sub-id to insert.
  516. */
  517. public void insertInOid(int oid) {
  518. this.oid.insert(oid) ;
  519. }
  520. /**
  521. * Appends the specified <CODE>SnmpOid</CODE> to the end of the OID of this <CODE>SnmpVarBind</CODE>.
  522. * @param oid The OID to append.
  523. */
  524. public void appendInOid(SnmpOid oid) {
  525. this.oid.append(oid) ;
  526. }
  527. /**
  528. * Determines whether the <CODE>SnmpVarBind</CODE> has an SNMP exception
  529. * (generated by agent in response to a request).
  530. * @return <CODE>true</CODE> if the <CODE>SnmpVarBind</CODE> has an SNMP response exception,
  531. * <CODE>false</CODE> otherwise.
  532. */
  533. final public synchronized boolean hasVarBindException() {
  534. switch (status) {
  535. case stValueUnspecified :
  536. case stValueNoSuchObject :
  537. case stValueNoSuchInstance :
  538. case stValueEndOfMibView :
  539. return true ;
  540. }
  541. return false ;
  542. }
  543. /**
  544. * Clones and copies the OID and value part from another <CODE>SnmpVarBind</CODE> object.
  545. * @param var The <CODE>SnmpVarBind</CODE> clone.
  546. */
  547. public void copyValueAndOid(SnmpVarBind var) {
  548. setOid((SnmpOid) (var.oid.clone())) ;
  549. copyValue(var) ;
  550. }
  551. /**
  552. * Clones and copies only the value part from another <CODE>SnmpVarBind</CODE> object.
  553. * @param var The <CODE>SnmpVarBind</CODE> clone.
  554. */
  555. public void copyValue(SnmpVarBind var) {
  556. if (var.isValidValue()) {
  557. this.value = var.getSnmpValue().duplicate() ;
  558. setValueValid() ;
  559. } else {
  560. status = var.getValueStatus() ;
  561. if (status == stValueEndOfMibView) value=endOfMibView;
  562. else if (status == stValueNoSuchObject) value=noSuchObject;
  563. else if (status == stValueNoSuchInstance) value=noSuchInstance;
  564. }
  565. }
  566. /**
  567. * Clones the SNMP variable. It does not clone the value portion.
  568. * @return A new object with the value part set to null.
  569. */
  570. public Object cloneWithoutValue() {
  571. SnmpOid noid = (SnmpOid)this.oid.clone() ;
  572. return new SnmpVarBind(noid) ;
  573. }
  574. /**
  575. * Clones the SNMP variable (including value).
  576. * @return The SNMP variable clone.
  577. */
  578. public Object clone() {
  579. // SnmpVarBind v = null ;
  580. // try {
  581. // v = (SnmpVarBind) super.clone() ;
  582. // v.copyValueAndOid(this) ;
  583. // } catch (CloneNotSupportedException e) {
  584. // throw new InternalError() ;
  585. // }
  586. // return v ;
  587. SnmpVarBind v = new SnmpVarBind() ;
  588. v.copyValueAndOid(this) ;
  589. return v ;
  590. }
  591. /**
  592. * Returns the printable ASCII representation for the corresponding variable value.
  593. * @return The printable ASCII representation.
  594. */
  595. final public String getStringValue() {
  596. return this.value.toString() ;
  597. }
  598. /**
  599. * Set the value to {@link #noSuchObject}. This is equivalent to
  600. * <code>setSnmpValue(SnmpVarBind.noSuchObject)</code>.
  601. **/
  602. final public void setNoSuchObject() {
  603. value=noSuchObject;
  604. status=stValueNoSuchObject;
  605. }
  606. /**
  607. * Set the value to {@link #noSuchInstance}. This is equivalent to
  608. * <code>setSnmpValue(SnmpVarBind.noSuchInstance)</code>.
  609. **/
  610. final public void setNoSuchInstance() {
  611. value=noSuchInstance;
  612. status=stValueNoSuchInstance;
  613. }
  614. /**
  615. * Set the value to {@link #endOfMibView}. This is equivalent to
  616. * <code>setSnmpValue(SnmpVarBind.endOfMibView)</code>.
  617. **/
  618. final public void setEndOfMibView() {
  619. value=endOfMibView;
  620. status=stValueEndOfMibView;
  621. }
  622. /**
  623. * Returns the printable ASCII representation of this <CODE>SnmpVarBind</CODE>.
  624. * @return The printable ASCII representation.
  625. */
  626. final public String toString() {
  627. StringBuffer s = new StringBuffer(400) ;
  628. s.append("Object ID : " + this.oid.toString()) ;
  629. if (isValidValue()) {
  630. s.append(" (Syntax : " + this.value.getTypeName() + ")\n") ;
  631. s.append("Value : " + this.value.toString()) ;
  632. } else {
  633. s.append("\n" + "Value Exception : " + getValueStatusLegend()) ;
  634. }
  635. return s.toString() ;
  636. }
  637. // PRIVATE METHODS
  638. //----------------
  639. /**
  640. * Sets the status to indicate that the value for this <CODE>SnmpVarBind</CODE> is valid.
  641. */
  642. private void setValueValid() {
  643. if (value == endOfMibView) status=stValueEndOfMibView;
  644. else if (value == noSuchObject) status=stValueNoSuchObject;
  645. else if (value == noSuchInstance) status=stValueNoSuchInstance;
  646. else status = stValueOk ;
  647. }
  648. private void handleLong(String oid, int index) throws NumberFormatException, SnmpStatusException {
  649. String str;
  650. if (index >0) {
  651. str= oid.substring(0, index);
  652. } else {
  653. str= oid ;
  654. }
  655. // just parse the element.
  656. //
  657. Long.parseLong(str);
  658. }
  659. }