1 11 13 package com.sun.jmx.snmp; 14 15 import java.io.Serializable ; 18 19 20 34 35 public class SnmpVarBind implements SnmpDataTypeEnums, Cloneable , Serializable { 36 37 40 43 static final public String statusLegend[] = { "Status Mapper", "Value not initialized", 44 "Valid Value", "No such object", 45 "No such Instance", "End of Mib View" } ; 46 47 50 static final public int stValueUnspecified = 1 ; 51 52 55 static final public int stValueOk = 2 ; 56 57 61 static final public int stValueNoSuchObject = 3 ; 62 63 70 static final public int stValueNoSuchInstance = 4 ; 71 72 78 static final public int stValueEndOfMibView = 5 ; 79 80 81 87 public final static SnmpNull noSuchObject = new SnmpNull(errNoSuchObjectTag) ; 88 89 92 public final static SnmpNull noSuchInstance = new SnmpNull(errNoSuchInstanceTag) ; 93 94 97 public final static SnmpNull endOfMibView = new SnmpNull(errEndOfMibViewTag) ; 98 99 105 public SnmpOid oid = null ; 106 107 113 public SnmpValue value = null ; 114 115 120 public int status = stValueUnspecified ; 121 122 123 126 129 public SnmpVarBind() { 130 } 131 132 136 public SnmpVarBind(SnmpOid oid) { 137 this.oid = oid ; 138 } 139 140 146 public SnmpVarBind(SnmpOid oid, SnmpValue val) { 147 this.oid = oid ; 148 this.setSnmpValue(val) ; 149 } 150 151 157 public SnmpVarBind(String 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 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 185 189 final public SnmpOid getOid() { 190 return this.oid ; 191 } 192 193 198 final public void setOid(SnmpOid oid) { 199 this.oid = oid ; 200 clearValue() ; 201 } 202 203 207 final synchronized public SnmpValue getSnmpValue() { 208 return this.value ; 209 } 210 211 216 final public void setSnmpValue(SnmpValue val) { 217 this.value= val ; 218 setValueValid(); 219 } 220 221 227 final public SnmpCounter64 getSnmpCounter64Value() throws ClassCastException { 228 return (SnmpCounter64)this.value ; 229 } 230 231 239 final public void setSnmpCounter64Value(long val) throws IllegalArgumentException { 240 clearValue() ; 241 this.value = new SnmpCounter64(val) ; 242 setValueValid() ; 243 } 244 245 251 final public SnmpInt getSnmpIntValue() throws ClassCastException { 252 return (SnmpInt)this.value ; 253 } 254 255 264 final public void setSnmpIntValue(long val) throws IllegalArgumentException { 265 clearValue() ; 266 this.value = new SnmpInt(val) ; 267 setValueValid() ; 268 } 269 270 276 final public SnmpCounter getSnmpCounterValue() throws ClassCastException { 277 return (SnmpCounter)this.value ; 278 } 279 280 289 final public void setSnmpCounterValue(long val) throws IllegalArgumentException { 290 clearValue() ; 291 this.value = new SnmpCounter(val) ; 292 setValueValid() ; 293 } 294 295 301 final public SnmpGauge getSnmpGaugeValue() throws ClassCastException { 302 return (SnmpGauge)this.value ; 303 } 304 305 314 final public void setSnmpGaugeValue(long val) throws IllegalArgumentException { 315 clearValue() ; 316 this.value = new SnmpGauge(val) ; 317 setValueValid() ; 318 } 319 320 326 final public SnmpTimeticks getSnmpTimeticksValue() throws ClassCastException { 327 return (SnmpTimeticks)this.value ; 328 } 329 330 339 final public void setSnmpTimeticksValue(long val) throws IllegalArgumentException { 340 clearValue() ; 341 this.value = new SnmpTimeticks(val) ; 342 setValueValid() ; 343 } 344 345 351 final public SnmpOid getSnmpOidValue() throws ClassCastException { 352 return (SnmpOid)this.value ; 353 } 354 355 364 final public void setSnmpOidValue(String val) throws IllegalArgumentException { 365 clearValue() ; 366 this.value = new SnmpOid(val) ; 367 setValueValid() ; 368 } 369 370 376 final public SnmpIpAddress getSnmpIpAddressValue() throws ClassCastException { 377 return (SnmpIpAddress)this.value ; 378 } 379 380 388 final public void setSnmpIpAddressValue(String val) throws IllegalArgumentException { 389 clearValue() ; 390 this.value = new SnmpIpAddress(val) ; 391 setValueValid() ; 392 } 393 394 400 final public SnmpString getSnmpStringValue() throws ClassCastException { 401 return (SnmpString)this.value ; 402 } 403 404 411 final public void setSnmpStringValue(String val) { 412 clearValue() ; 413 this.value = new SnmpString(val) ; 414 setValueValid() ; 415 } 416 417 423 final public SnmpOpaque getSnmpOpaqueValue() throws ClassCastException { 424 return (SnmpOpaque)this.value ; 425 } 426 427 434 final public void setSnmpOpaqueValue(byte[] val) { 435 clearValue() ; 436 this.value = new SnmpOpaque(val) ; 437 setValueValid() ; 438 } 439 440 446 final public SnmpStringFixed getSnmpStringFixedValue() throws ClassCastException { 447 return (SnmpStringFixed)this.value ; 448 } 449 450 457 final public void setSnmpStringFixedValue(String val) { 458 clearValue() ; 459 this.value = new SnmpStringFixed(val) ; 460 setValueValid() ; 461 } 462 463 464 467 473 public SnmpOidRecord resolveVarName(String 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 492 final public int getValueStatus() { 493 return status ; 494 } 495 496 503 final public String getValueStatusLegend() { 504 return statusLegend[status] ; 505 } 506 507 511 final public boolean isValidValue() { 512 return (status == stValueOk) ; 513 } 514 515 519 final public boolean isUnspecifiedValue() { 520 return (status == stValueUnspecified) ; 521 } 522 523 527 final public void clearValue() { 528 this.value = null ; 529 status = stValueUnspecified ; 530 } 531 532 538 final public boolean isOidEqual(SnmpVarBind var) { 539 return this.oid.equals(var.oid) ; 540 } 541 542 548 final public void addInstance(long inst) { 549 oid.append(inst) ; 550 return ; 551 } 552 553 560 final public void addInstance(long[] inst) throws SnmpStatusException { 561 oid.addToOid(inst) ; 562 return ; 563 } 564 565 572 final public void addInstance(String inst) throws SnmpStatusException { 573 if (inst != null) { 574 oid.addToOid(inst) ; 575 } 576 return ; 577 } 578 579 583 public void insertInOid(int oid) { 584 this.oid.insert(oid) ; 585 } 586 587 591 public void appendInOid(SnmpOid oid) { 592 this.oid.append(oid) ; 593 } 594 595 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 616 public void copyValueAndOid(SnmpVarBind var) { 617 setOid((SnmpOid) (var.oid.clone())) ; 618 copyValue(var) ; 619 } 620 621 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 641 public Object cloneWithoutValue() { 642 SnmpOid noid = (SnmpOid)this.oid.clone() ; 643 return new SnmpVarBind(noid) ; 644 } 645 646 650 public Object clone() { 651 SnmpVarBind v = new SnmpVarBind() ; 660 v.copyValueAndOid(this) ; 661 return v ; 662 } 663 664 668 final public String getStringValue() { 669 return this.value.toString() ; 670 } 671 672 676 final public void setNoSuchObject() { 677 value=noSuchObject; 678 status=stValueNoSuchObject; 679 } 680 681 685 final public void setNoSuchInstance() { 686 value=noSuchInstance; 687 status=stValueNoSuchInstance; 688 } 689 690 694 final public void setEndOfMibView() { 695 value=endOfMibView; 696 status=stValueEndOfMibView; 697 } 698 699 703 final public String toString() { 704 StringBuffer s = new StringBuffer (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 720 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 oid, int index) throws NumberFormatException , SnmpStatusException { 731 732 String str; 733 if (index >0) { 734 str= oid.substring(0, index); 735 } else { 736 str= oid ; 737 } 738 739 Long.parseLong(str); 742 } 743 } 744 | Popular Tags |