1 11 12 13 package com.sun.jmx.snmp.agent; 14 15 16 17 import java.io.Serializable ; 20 import java.util.Vector ; 21 import java.util.Enumeration ; 22 23 import com.sun.jmx.snmp.SnmpOid; 26 import com.sun.jmx.snmp.SnmpValue; 27 import com.sun.jmx.snmp.SnmpVarBind; 28 import com.sun.jmx.snmp.SnmpStatusException; 29 30 44 45 public class SnmpMibOid extends SnmpMibNode implements Serializable { 46 47 50 public SnmpMibOid() { 51 } 52 53 56 69 public void get(SnmpMibSubRequest req, int depth) 70 throws SnmpStatusException { 71 for (Enumeration e= req.getElements(); e.hasMoreElements();) { 72 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 73 SnmpStatusException x = 74 new SnmpStatusException(SnmpStatusException.noSuchObject); 75 req.registerGetException(var,x); 76 } 77 } 78 79 92 public void set(SnmpMibSubRequest req, int depth) 93 throws SnmpStatusException { 94 for (Enumeration e= req.getElements(); e.hasMoreElements();) { 95 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 96 SnmpStatusException x = 97 new SnmpStatusException(SnmpStatusException.noAccess); 98 req.registerSetException(var,x); 99 } 100 } 101 102 115 public void check(SnmpMibSubRequest req, int depth) 116 throws SnmpStatusException { 117 for (Enumeration e= req.getElements(); e.hasMoreElements();) { 118 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 119 SnmpStatusException x = 120 new SnmpStatusException(SnmpStatusException.noAccess); 121 req.registerCheckException(var,x); 122 } 123 } 124 125 126 127 void findHandlingNode(SnmpVarBind varbind, 134 long[] oid, int depth, 135 SnmpRequestTree handlers) 136 throws SnmpStatusException { 137 138 139 final int length = oid.length; 140 SnmpMibNode node = null; 141 142 if (handlers == null) 143 throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr); 144 145 if (depth > length) { 146 throw noSuchObjectException; 148 149 } else if (depth == length) { 150 throw noSuchInstanceException; 152 153 } else { 154 final SnmpMibNode child= getChild(oid[depth]); 158 159 if (child == null) 169 handlers.add(this,depth,varbind); 170 else 171 child.findHandlingNode(varbind,oid,depth+1,handlers); 172 } 173 } 174 175 long[] findNextHandlingNode(SnmpVarBind varbind, 182 long[] oid, int pos, int depth, 183 SnmpRequestTree handlers, 184 AcmChecker checker) 185 throws SnmpStatusException { 186 187 188 final int length = oid.length; 189 SnmpMibNode node = null; 190 long[] result = null; 191 if (handlers == null) 192 throw noSuchObjectException; 197 198 final Object data = handlers.getUserData(); 199 final int pduVersion = handlers.getRequestPduVersion(); 200 201 if (pos >= length) { 202 long[] newOid= new long[1]; 203 newOid[0]= getNextVarId(-1,data,pduVersion); 204 result = findNextHandlingNode(varbind,newOid,0,depth,handlers, 205 checker); 206 return result; 207 } 208 209 long[] newOid= new long[1]; 212 long index= oid[pos]; 213 214 while (true) { 215 216 try { 217 final SnmpMibNode child = getChild(index); 218 if (child == null) { 220 throw noSuchObjectException; 222 } else { 226 checker.add(depth, index); 227 try { 228 result = child.findNextHandlingNode(varbind,oid,pos+1, 229 depth+1,handlers, 230 checker); 231 } finally { 232 checker.remove(depth); 233 } 234 } 235 236 result[depth] = index; 238 return result; 239 240 } catch(SnmpStatusException e) { 241 index= getNextVarId(index,data,pduVersion); 244 245 newOid[0]=index; 247 pos= 1; 248 oid=newOid; 249 } 250 } 251 } 252 253 254 257 public void getRootOid(Vector result) { 258 259 if (nbChildren != 1) 263 return; 264 265 result.addElement(new Integer (varList[0])); 266 267 ((SnmpMibNode)children.firstElement()).getRootOid(result); 270 271 } 272 273 276 public void registerNode(String oidString ,SnmpMibNode node) 277 throws IllegalAccessException { 278 SnmpOid oid= new SnmpOid(oidString); 279 registerNode(oid.longValue(), 0, node); 280 } 281 282 285 288 void registerNode(long[] oid, int cursor ,SnmpMibNode node) 289 throws IllegalAccessException { 290 291 if (cursor >= oid.length) 292 throw new IllegalAccessException (); 293 294 long var= oid[cursor]; 297 298 301 int pos = retrieveIndex(var); 302 if (pos == nbChildren) { 303 nbChildren++; 304 varList= new int[nbChildren]; 305 varList[0]= (int) var; 306 pos =0; 307 if ( (cursor + 1) == oid.length) { 308 311 children.insertElementAt(node,pos); 314 return; 315 } 316 317 SnmpMibOid child= new SnmpMibOid(); 320 children.insertElementAt(child, pos); 321 child.registerNode(oid, cursor + 1, node); 322 return; 323 } 324 if (pos == -1) { 325 int[] tmp= new int[nbChildren + 1]; 328 tmp[nbChildren]= (int) var; 329 System.arraycopy(varList, 0, tmp, 0, nbChildren); 330 varList= tmp; 331 nbChildren++; 332 SnmpMibNode.sort(varList); 333 int newPos = retrieveIndex(var); 334 varList[newPos]= (int) var; 335 if ( (cursor + 1) == oid.length) { 336 339 children.insertElementAt(node, newPos); 342 return; 343 } 344 SnmpMibOid child= new SnmpMibOid(); 345 children.insertElementAt(child, newPos); 348 child.registerNode(oid, cursor + 1, node); 349 return; 350 } 351 else { 352 SnmpMibNode child= (SnmpMibNode) children.elementAt(pos); 355 if ( (cursor + 1) == oid.length ) { 356 if (child == node) return; 359 if (child != null && node != null) { 360 367 if (node instanceof SnmpMibGroup) { 368 ((SnmpMibOid)child).exportChildren((SnmpMibOid)node); 372 children.setElementAt(node,pos); 373 return; 374 375 } else if ((node instanceof SnmpMibOid) && 376 (child instanceof SnmpMibGroup)) { 377 ((SnmpMibOid)node).exportChildren((SnmpMibOid)child); 382 return; 383 } else if (node instanceof SnmpMibOid) { 384 ((SnmpMibOid)child).exportChildren((SnmpMibOid)node); 389 children.setElementAt(node,pos); 390 return; 391 } 392 } 393 children.setElementAt(node,pos); 394 return; 395 } else { 396 if (child == null) 397 throw new IllegalAccessException (); 398 ((SnmpMibOid)child).registerNode(oid, cursor + 1, node); 399 } 400 } 401 } 402 403 410 void exportChildren(SnmpMibOid brother) 411 throws IllegalAccessException { 412 413 if (brother == null) return; 414 final long[] oid = new long[1]; 415 for (int i=0; i<nbChildren; i++) { 416 final SnmpMibNode child = (SnmpMibNode)children.elementAt(i); 417 if (child == null) continue; 418 oid[0] = varList[i]; 419 brother.registerNode(oid,0,child); 420 } 421 } 422 423 426 SnmpMibNode getChild(long id) throws SnmpStatusException { 427 428 final int pos= getInsertAt(id); 431 if (pos >= nbChildren) 432 throw noSuchObjectException; 433 434 if (varList[pos] != (int) id) 435 throw noSuchObjectException; 436 437 SnmpMibNode child = null; 440 try { 441 child = (SnmpMibNode) children.elementAtNonSync(pos); 442 } catch(ArrayIndexOutOfBoundsException e) { 443 throw noSuchObjectException; 444 } 445 if (child == null) 446 throw noSuchInstanceException; 447 return child; 448 } 449 450 private int retrieveIndex(long val) { 451 452 int low= 0; 453 int cursor= (int) val; 454 if (varList == null || varList.length < 1) 455 return nbChildren; 456 457 int max= varList.length -1 ; 458 int curr= low + (max-low)/2; 459 int elmt= 0; 460 while (low <= max) { 461 elmt= varList[curr]; 462 if (cursor == elmt) { 463 return curr; 466 } 467 if (elmt < cursor) { 468 low= curr +1; 469 } else { 470 max= curr -1; 471 } 472 curr= low + (max-low)/2; 473 } 474 return -1; 475 } 476 477 private int getInsertAt(long val) { 478 479 int low= 0; 480 final int index= (int) val; 481 if (varList == null) 482 return -1; 483 int max= varList.length -1 ; 484 int elmt=0; 485 487 490 491 int curr= low + (max-low)/2; 492 while (low <= max) { 493 494 elmt= varList[curr]; 495 496 if (index == elmt) 499 return curr; 500 501 if (elmt < index) { 502 low= curr +1; 503 } else { 504 max= curr -1; 505 } 506 curr= low + (max-low)/2; 507 } 508 509 return curr; 510 } 511 512 515 518 private NonSyncVector children= new NonSyncVector(1); 519 520 523 private int nbChildren= 0; 524 525 526 class NonSyncVector extends Vector { 531 532 public NonSyncVector(int size) { 533 super(size); 534 } 535 536 final void addNonSyncElement(Object obj) { 537 ensureCapacity(elementCount + 1); 538 elementData[elementCount++] = obj; 539 } 540 541 final Object elementAtNonSync(int index) { 542 return elementData[index]; 543 } 544 545 } 546 } 547 | Popular Tags |