1 11 13 package com.sun.jmx.snmp; 14 15 16 import java.util.StringTokenizer ; 19 import java.util.NoSuchElementException ; 20 21 30 31 public class SnmpOid extends SnmpValue { 32 33 38 public SnmpOid() { 39 components = new long[15] ; 40 componentCount = 0 ; 41 } 42 43 47 public SnmpOid(long[] oidComponents) { 48 components = (long[])oidComponents.clone() ; 49 componentCount = components.length ; 50 } 51 52 57 public SnmpOid(long id) { 58 components = new long[1] ; 59 components[0] = id ; 60 componentCount = components.length ; 61 } 62 63 71 public SnmpOid(long id1, long id2, long id3, long id4) { 72 components = new long[4] ; 73 components[0] = id1 ; 74 components[1] = id2 ; 75 components[2] = id3 ; 76 components[3] = id4 ; 77 componentCount = components.length ; 78 } 79 80 90 public SnmpOid(String s) throws IllegalArgumentException { 91 String dotString = s ; 92 93 if (s.startsWith(".") == false) { 94 try { 95 dotString = resolveVarName(s); 96 } catch(SnmpStatusException e) { 97 throw new IllegalArgumentException (e.getMessage()); 98 } 99 } 100 101 StringTokenizer st = new StringTokenizer (dotString, ".", false) ; 102 componentCount= st.countTokens(); 103 104 if (componentCount == 0) { 107 components = new long[15] ; 108 } else { 109 components = new long[componentCount] ; 110 try { 111 for (int i = 0 ; i < componentCount ; i++) { 112 try { 113 components[i] = Long.parseLong(st.nextToken()) ; 114 } 115 catch(NoSuchElementException e) {} 116 } 117 } 118 catch(NumberFormatException e) { 119 throw new IllegalArgumentException (s) ; 120 } 121 } 122 } 123 124 130 public int getLength() { 131 return componentCount ; 132 } 133 134 138 public long[] longValue() { 139 long[] result = new long[componentCount] ; 140 System.arraycopy(components,0,result,0,componentCount); 141 return result ; 142 } 143 144 159 public final long[] longValue(boolean duplicate) { 160 if (duplicate) return longValue(); 161 if (componentCount == components.length) return components ; 162 components = longValue(); 163 componentCount = components.length; 164 return components ; 165 } 166 167 179 public final long getOidArc(int pos) throws SnmpStatusException { 180 try { 181 return components[pos]; 182 } catch(Exception e) { 183 throw new SnmpStatusException(SnmpStatusException.noAccess); 184 } 185 } 186 187 191 public Long toLong() { 192 if (componentCount != 1) { 193 throw new IllegalArgumentException () ; 194 } 195 return new Long (components[0]) ; 196 } 197 198 202 public Integer toInteger() { 203 if ((componentCount != 1) || (components[0] > Integer.MAX_VALUE)) { 204 throw new IllegalArgumentException () ; 205 } 206 return new Integer ((int)components[0]) ; 207 } 208 209 213 public String toString() { 214 String result = "" ; 215 if (componentCount >= 1) { 216 for (int i = 0 ; i < componentCount - 1 ; i++) { 217 result = result + components[i] + "." ; 218 } 219 result = result + components[componentCount - 1] ; 220 } 221 return result ; 222 } 223 224 228 public Boolean toBoolean() { 229 if ((componentCount != 1) && (components[0] != 1) && (components[0] != 2)) { 230 throw new IllegalArgumentException () ; 231 } 232 return new Boolean (components[0] == 1) ; 233 } 234 235 239 public Byte [] toByte() { 240 Byte [] result = new Byte [componentCount] ; 241 for (int i =0 ; i < componentCount ; i++) { 242 if (components[0] > 255) { 243 throw new IllegalArgumentException () ; 244 } 245 result[i] = new Byte ((byte)components[i]) ; 246 } 247 return result ; 248 } 249 250 254 public SnmpOid toOid() { 255 long[] ids = new long[componentCount] ; 256 for (int i = 0 ; i < componentCount ; i++) { 257 ids[i] = components[i] ; 258 } 259 return new SnmpOid(ids) ; 260 } 261 262 271 public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { 272 try { 273 if (index[start] > Integer.MAX_VALUE) { 274 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 275 } 276 int idCount = (int)index[start++] ; 277 long[] ids = new long[idCount] ; 278 for (int i = 0 ; i < idCount ; i++) { 279 ids[i] = index[start + i] ; 280 } 281 return new SnmpOid(ids) ; 282 } 283 catch(IndexOutOfBoundsException e) { 284 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 285 } 286 } 287 288 297 public static int nextOid(long[] index, int start) throws SnmpStatusException { 298 try { 299 if (index[start] > Integer.MAX_VALUE) { 300 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 301 } 302 int idCount = (int)index[start++] ; 303 start += idCount ; 304 if (start <= index.length) { 305 return start ; 306 } 307 else { 308 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 309 } 310 } 311 catch(IndexOutOfBoundsException e) { 312 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 313 } 314 } 315 316 321 public static void appendToOid(SnmpOid source, SnmpOid dest) { 322 dest.append(source.getLength()) ; 323 dest.append(source) ; 324 } 325 326 331 final synchronized public SnmpValue duplicate() { 332 return (SnmpValue)clone() ; 333 } 334 335 339 public Object clone() { 340 try { 341 SnmpOid obj = (SnmpOid)super.clone() ; 342 obj.components = new long[this.componentCount] ; 343 344 System.arraycopy(this.components, 0, obj.components, 0, 345 this.componentCount) ; 346 return obj ; 347 } catch (CloneNotSupportedException e) { 348 throw new InternalError () ; } 350 } 351 352 356 public void insert(long id) { 357 enlargeIfNeeded(1) ; 358 for (int i = componentCount - 1 ; i >= 0 ; i--) { 359 components[i + 1] = components[i] ; 360 } 361 components[0] = id ; 362 componentCount++ ; 363 } 364 365 369 public void insert(int id) { 370 insert((long)id) ; 371 } 372 373 377 public void append(SnmpOid oid) { 378 enlargeIfNeeded(oid.componentCount) ; 379 for (int i = 0 ; i < oid.componentCount ; i++) { 380 components[componentCount + i] = oid.components[i] ; 381 } 382 componentCount += oid.componentCount ; 383 } 384 385 389 public void append(long id) { 390 enlargeIfNeeded(1) ; 391 components[componentCount] = id ; 392 componentCount++ ; 393 } 394 395 403 public void addToOid(String s) throws SnmpStatusException { 404 SnmpOid suffix= new SnmpOid(s); 405 this.append(suffix); 406 } 407 408 413 public void addToOid(long []oid) throws SnmpStatusException { 414 SnmpOid suffix= new SnmpOid(oid); 415 this.append(suffix); 416 } 417 418 422 public boolean isValid() { 423 return ((componentCount >= 2) && 424 ((0 <= components[0]) && (components[0] < 3)) && 425 ((0 <= components[1]) && (components[1] < 40))) ; 426 } 427 428 433 public boolean equals(Object o) { 434 boolean result = false ; 435 436 if (o instanceof SnmpOid) { 437 SnmpOid oid = (SnmpOid)o ; 438 if (oid.componentCount == componentCount) { 439 int i = 0 ; 440 long[] objoid = oid.components; 441 while ((i < componentCount) && (components[i] == objoid[i])) 442 i++ ; 443 result = (i == componentCount) ; 444 } 445 } 446 return result ; 447 } 448 449 453 public int hashCode() { 454 long acc=0; 455 for (int i=0;i<componentCount;i++) { 456 acc = acc*31+components[i]; 457 } 458 return (int)acc; 459 } 460 461 469 public int compareTo(SnmpOid other) { 470 int result = 0 ; 471 int i = 0 ; 472 int cmplen = Math.min(componentCount, other.componentCount) ; 473 long[] otheroid = other.components; 474 475 for (i = 0; i < cmplen; i++) { 476 if (components[i] != otheroid[i]) { 477 break ; 478 } 479 } 480 if ((i == componentCount) && (i == other.componentCount)) { 481 result = 0 ; 482 } 483 else if (i == componentCount) { 484 result = -1 ; 485 } 486 else if (i == other.componentCount) { 487 result = 1 ; 488 } 489 else { 490 result = (components[i] < otheroid[i]) ? -1 : 1 ; 491 } 492 return result ; 493 } 494 495 500 public String resolveVarName(String s) throws SnmpStatusException { 501 int index = s.indexOf('.') ; 502 503 try { 506 return handleLong(s, index); 507 } catch(NumberFormatException e) {} 508 509 if (meta == null) 512 throw new SnmpStatusException(SnmpStatusException.noSuchName); 513 514 if (index <= 0) { 517 SnmpOidRecord rec = meta.resolveVarName(s); 518 return rec.getOid(); 519 520 } else { 521 SnmpOidRecord rec = meta.resolveVarName(s.substring(0, index)); 522 return (rec.getOid()+ s.substring(index)); 523 524 } 525 } 526 527 531 public String getTypeName() { 532 return name ; 533 } 534 535 539 public static SnmpOidTable getSnmpOidTable() { 540 return meta; 541 } 542 543 549 public static void setSnmpOidTable(SnmpOidTable db) { 550 meta = db; 551 } 552 553 557 public String toOctetString() { 558 return new String (tobyte()) ; 559 } 560 561 562 565 568 private byte[] tobyte() { 569 byte[] result = new byte[componentCount] ; 570 for (int i =0 ; i < componentCount ; i++) { 571 if (components[0] > 255) { 572 throw new IllegalArgumentException () ; 573 } 574 result[i] = (byte)components[i] ; 575 } 576 return result ; 577 } 578 579 580 587 private void enlargeIfNeeded(int n) { 588 int neededSize = components.length ; 589 while (componentCount + n > neededSize) { 590 neededSize = neededSize * 2 ; 591 } 592 if (neededSize > components.length) { 593 long[] newComponents = new long[neededSize] ; 594 for (int i = 0 ; i < components.length ; i++) { 595 newComponents[i] = components[i] ; 596 } 597 components = newComponents ; 598 } 599 } 600 601 private String handleLong(String oid, int index) throws NumberFormatException , SnmpStatusException { 604 String str; 605 if (index >0) { 606 str= oid.substring(0, index); 607 } else { 608 str= oid ; 609 } 610 611 Long.parseLong(str); 614 return oid; 615 } 616 617 623 protected long components[] = null ; 624 625 629 protected int componentCount = 0 ; 630 631 634 final static String name = "Object Identifier"; 635 636 640 private static SnmpOidTable meta= null; 641 642 646 static final long serialVersionUID = 8956237235607885096L; 647 } 648 | Popular Tags |