1 11 13 package com.sun.jmx.snmp; 14 15 16 import java.util.Vector ; 17 import java.util.Enumeration ; 18 19 20 28 29 public class SnmpVarBindList extends Vector { 30 31 35 public String identity = "VarBindList " ; 37 43 Timestamp timestamp ; 44 45 46 49 53 public SnmpVarBindList() { 54 super(5, 5) ; 55 } 56 57 61 public SnmpVarBindList(int initialCapacity) { 62 super(initialCapacity) ; 63 } 64 65 69 public SnmpVarBindList(String name) { 70 super(5, 5) ; 71 identity = name ; 72 } 73 74 79 public SnmpVarBindList(SnmpVarBindList list) { 80 super(list.size(), 5) ; 81 list.copyInto(elementData) ; 82 elementCount = list.size() ; 83 } 84 85 90 public SnmpVarBindList(Vector list) { 91 super(list.size(), 5); 92 for (Enumeration e = list.elements(); e.hasMoreElements();) { 93 final SnmpVarBind varBind = (SnmpVarBind)e.nextElement(); 94 addElement((SnmpVarBind)varBind.clone()); 95 } 96 } 97 98 104 public SnmpVarBindList(String name, Vector list) { 105 this(list); 106 identity = name; 107 } 108 109 110 113 117 public Timestamp getTimestamp() { 118 return timestamp ; 119 } 120 121 127 public void setTimestamp(Timestamp tstamp) { 128 timestamp = tstamp ; 129 } 130 131 137 public final synchronized SnmpVarBind getVarBindAt(int pos) { 138 return (SnmpVarBind)(elementAt(pos)) ; 139 } 140 141 145 public synchronized int getVarBindCount() { 146 return size() ; 147 } 148 149 154 public synchronized Enumeration getVarBindList() { 155 return elements() ; 156 } 157 158 165 public final synchronized void setVarBindList(Vector list) { 166 setVarBindList(list, false) ; 167 } 168 169 177 public final synchronized void setVarBindList(Vector list, boolean copy) { 178 synchronized (list) { 179 final int max = list.size(); 180 setSize(max) ; 181 list.copyInto(this.elementData) ; 182 if (copy) { for (int i = 0; i < max ; i++) { 184 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 185 elementData[i] = avar.clone() ; 186 } 187 } 188 } 189 } 190 191 192 195 199 public synchronized void addVarBindList(SnmpVarBindList list) { 200 ensureCapacity(list.size() + size()) ; 201 for (int i = 0; i < list.size(); i++) { 202 addElement(list.getVarBindAt(i)) ; 203 } 204 } 205 206 213 public synchronized boolean removeVarBindList(SnmpVarBindList list) { 214 boolean result = true; 215 for (int i = 0; i < list.size(); i++) { 216 result = removeElement(list.getVarBindAt(i)) ; 217 } 218 return result; 219 } 220 221 227 public final synchronized void replaceVarBind(SnmpVarBind var, int pos) { 228 setElementAt(var, pos) ; 229 } 230 231 237 public final synchronized void addVarBind(String list[], String inst) throws SnmpStatusException { 238 for (int i = 0; i < list.length; i++) { 239 SnmpVarBind avar = new SnmpVarBind(list[i]) ; 240 avar.addInstance(inst) ; 241 addElement(avar) ; 242 } 243 } 244 245 253 public synchronized boolean removeVarBind(String list[], String inst) throws SnmpStatusException { 254 boolean result = true; 255 for (int i = 0; i < list.length; i++) { 256 SnmpVarBind avar = new SnmpVarBind(list[i]) ; 257 avar.addInstance(inst) ; 258 int indexOid = indexOfOid(avar) ; 259 try { 260 removeElementAt(indexOid) ; 261 } catch (ArrayIndexOutOfBoundsException e) { 262 result = false ; 263 } 264 } 265 return result ; 266 } 267 268 280 public synchronized void addVarBind(String list[]) throws SnmpStatusException { 281 addVarBind(list, null) ; 282 } 283 284 291 public synchronized boolean removeVarBind(String list[]) throws SnmpStatusException { 292 return removeVarBind(list, null) ; 293 } 294 295 302 public synchronized void addVarBind(String name) throws SnmpStatusException { 303 SnmpVarBind avar ; 304 avar = new SnmpVarBind(name) ; 305 addVarBind(avar) ; 306 } 307 308 316 public synchronized boolean removeVarBind(String name) throws SnmpStatusException { 317 SnmpVarBind avar ; 318 int indexOid ; 319 avar = new SnmpVarBind(name) ; 320 indexOid = indexOfOid(avar) ; 321 try { 322 removeElementAt(indexOid) ; 323 return true ; 324 } catch (ArrayIndexOutOfBoundsException e) { 325 return false ; 326 } 327 } 328 329 334 public synchronized void addVarBind(SnmpVarBind var) { 335 addElement(var) ; 336 } 337 338 344 public synchronized boolean removeVarBind(SnmpVarBind var) { 345 return removeElement(var) ; 346 } 347 348 354 public synchronized void addInstance(String inst) throws SnmpStatusException { 355 int max= size(); 356 for (int i = 0; i < max; i++) { 357 ((SnmpVarBind)elementData[i]).addInstance(inst) ; 358 } 359 } 360 361 366 final public synchronized void concat(Vector list) { 367 ensureCapacity(size() + list.size()) ; 368 for (Enumeration e = list.elements() ; e.hasMoreElements() ; ) { 369 addElement(e.nextElement()) ; 370 } 371 } 372 373 378 public synchronized boolean checkForValidValues() { 379 int max= this.size(); 380 for (int i = 0; i < max ; i++) { 381 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 382 if (avar.isValidValue() == false) 383 return false ; 384 } 385 return true ; 386 } 387 388 392 public synchronized boolean checkForUnspecifiedValue() { 393 int max= this.size(); 394 for (int i = 0; i < max ; i++) { 395 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 396 if (avar.isUnspecifiedValue()) 397 return true ; 398 } 399 return false ; 400 } 401 402 407 public synchronized SnmpVarBindList splitAt(int pos) { 408 SnmpVarBindList splitVb = null ; 409 if (pos > elementCount) 410 return splitVb ; 411 splitVb = new SnmpVarBindList() ; int max= size(); 413 for (int i = pos; i < max ; i++) 414 splitVb.addElement(elementData[i]) ; 415 elementCount = pos ; 416 trimToSize() ; 417 return splitVb ; 418 } 419 420 429 public synchronized int indexOfOid(SnmpVarBind var, int min, int max) { 430 SnmpOid oidarg = var.getOid() ; 431 for (int i = min; i < max ; i++) { 432 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 433 if (oidarg.equals(avar.getOid())) 434 return i ; 435 } 436 return -1 ; 437 } 438 439 444 public synchronized int indexOfOid(SnmpVarBind var) { 445 return indexOfOid(var, 0, size()) ; 446 } 447 448 453 public synchronized int indexOfOid(SnmpOid oid) { 454 int max = size(); 455 for (int i = 0; i < max ; i++) { 456 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 457 if (oid.equals(avar.getOid())) 458 return i ; 459 } 460 return -1 ; 461 } 462 463 468 public synchronized SnmpVarBindList cloneWithValue() { 469 SnmpVarBindList newvb = new SnmpVarBindList() ; 470 newvb.setTimestamp(this.getTimestamp()) ; 471 newvb.ensureCapacity(this.size()) ; 472 for (int i = 0; i < this.size() ; i++) { 473 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 474 newvb.addElement(avar.clone()) ; 475 } 476 return newvb ; 477 } 478 479 484 public synchronized SnmpVarBindList cloneWithoutValue() { 485 SnmpVarBindList newvb = new SnmpVarBindList() ; 486 int max = this.size(); 487 newvb.ensureCapacity(max) ; 488 for (int i = 0; i < max ; i++) { 489 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 490 newvb.addElement(avar.cloneWithoutValue()) ; 491 } 492 return newvb ; 493 } 494 495 500 public synchronized Object clone() { 501 return cloneWithValue() ; 502 } 503 504 511 public synchronized Vector toVector(boolean copy) { 512 final int count = elementCount; 513 if (copy == false) return (Vector ) super.clone(); 514 Vector result = new Vector (count,5); 515 for (int i = 0; i < count ; i++) { 516 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 517 result.addElement(avar.clone()) ; 518 } 519 return result; 520 } 521 522 526 public String oidListToString() { 527 StringBuffer s = new StringBuffer (300) ; 528 for (int i = 0 ; i < elementCount ; i++) { 529 SnmpVarBind avar = (SnmpVarBind)elementData[i] ; 530 s.append(avar.getOid().toString() + "\n") ; 531 } 532 return s.toString() ; 533 } 534 535 540 public synchronized String varBindListToString() { 541 StringBuffer s = new StringBuffer (300) ; 542 for (int i = 0; i < elementCount ; i++) { 543 s.append(elementData[i].toString() + "\n") ; 544 } 545 return s.toString() ; 546 } 547 548 554 public void finalize() { 555 removeAllElements() ; 556 } 557 } 558 | Popular Tags |