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 import java.util.Set ; 23 24 import javax.management.MBeanServer ; 27 import javax.management.MBeanRegistration ; 28 import javax.management.ObjectName ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.InstanceNotFoundException ; 31 import javax.management.ServiceNotFoundException ; 32 import javax.management.ReflectionException ; 33 import javax.management.MBeanException ; 34 import com.sun.jmx.snmp.SnmpVarBind; 35 import com.sun.jmx.snmp.SnmpDefinitions; 36 import com.sun.jmx.snmp.SnmpStatusException; 37 import com.sun.jmx.snmp.SnmpPdu; 38 import com.sun.jmx.snmp.SnmpOid; 39 import com.sun.jmx.snmp.SnmpEngine; 40 41 52 53 public abstract class SnmpMibAgent 54 implements SnmpMibAgentMBean, MBeanRegistration , Serializable { 55 56 59 public SnmpMibAgent() { 60 } 61 62 66 72 public abstract void init() throws IllegalAccessException ; 73 74 85 public abstract ObjectName preRegister(MBeanServer server, 86 ObjectName name) 87 throws java.lang.Exception ; 88 89 92 public void postRegister (Boolean registrationDone) { 93 } 94 95 98 public void preDeregister() throws java.lang.Exception { 99 } 100 101 104 public void postDeregister() { 105 } 106 107 118 public abstract void get(SnmpMibRequest req) 119 throws SnmpStatusException; 120 121 132 public abstract void getNext(SnmpMibRequest req) 133 throws SnmpStatusException; 134 135 155 public abstract void getBulk(SnmpMibRequest req, int nonRepeat, 156 int maxRepeat) 157 throws SnmpStatusException; 158 159 176 public abstract void set(SnmpMibRequest req) 177 throws SnmpStatusException; 178 179 180 194 public abstract void check(SnmpMibRequest req) 195 throws SnmpStatusException; 196 197 204 public abstract long[] getRootOid(); 205 206 210 217 public MBeanServer getMBeanServer() { 218 return server; 219 } 220 221 227 public SnmpMibHandler getSnmpAdaptor() { 228 return adaptor; 229 } 230 231 237 public void setSnmpAdaptor(SnmpMibHandler stack) { 238 if (adaptor != null) { 239 adaptor.removeMib(this); 240 } 241 adaptor = stack; 242 if (adaptor != null) { 243 adaptor.addMib(this); 244 } 245 } 246 247 257 public void setSnmpAdaptor(SnmpMibHandler stack, SnmpOid[] oids) { 258 if (adaptor != null) { 259 adaptor.removeMib(this); 260 } 261 adaptor = stack; 262 if (adaptor != null) { 263 adaptor.addMib(this, oids); 264 } 265 } 266 267 279 public void setSnmpAdaptor(SnmpMibHandler stack, String contextName) { 280 if (adaptor != null) { 281 adaptor.removeMib(this, contextName); 282 } 283 adaptor = stack; 284 if (adaptor != null) { 285 adaptor.addMib(this, contextName); 286 } 287 } 288 300 public void setSnmpAdaptor(SnmpMibHandler stack, 301 String contextName, 302 SnmpOid[] oids) { 303 if (adaptor != null) { 304 adaptor.removeMib(this, contextName); 305 } 306 adaptor = stack; 307 if (adaptor != null) { 308 adaptor.addMib(this, contextName, oids); 309 } 310 } 311 312 318 public ObjectName getSnmpAdaptorName() { 319 return adaptorName; 320 } 321 322 335 public void setSnmpAdaptorName(ObjectName name) 336 throws InstanceNotFoundException , ServiceNotFoundException { 337 338 if (server == null) { 339 throw new ServiceNotFoundException (mibName + " is not registered in the MBean server"); 340 } 341 if (adaptor != null) { 344 adaptor.removeMib(this); 345 } 346 347 Object [] params = {this}; 350 String [] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent"}; 351 try { 352 adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, 353 signature)); 354 } catch (InstanceNotFoundException e) { 355 throw new InstanceNotFoundException (name.toString()); 356 } catch (ReflectionException e) { 357 throw new ServiceNotFoundException (name.toString()); 358 } catch (MBeanException e) { 359 } 361 362 adaptorName = name; 363 } 364 380 public void setSnmpAdaptorName(ObjectName name, SnmpOid[] oids) 381 throws InstanceNotFoundException , ServiceNotFoundException { 382 383 if (server == null) { 384 throw new ServiceNotFoundException (mibName + " is not registered in the MBean server"); 385 } 386 if (adaptor != null) { 389 adaptor.removeMib(this); 390 } 391 392 Object [] params = {this, oids}; 395 String [] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", 396 oids.getClass().getName()}; 397 try { 398 adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, 399 signature)); 400 } catch (InstanceNotFoundException e) { 401 throw new InstanceNotFoundException (name.toString()); 402 } catch (ReflectionException e) { 403 throw new ServiceNotFoundException (name.toString()); 404 } catch (MBeanException e) { 405 } 407 408 adaptorName = name; 409 } 410 425 public void setSnmpAdaptorName(ObjectName name, String contextName) 426 throws InstanceNotFoundException , ServiceNotFoundException { 427 428 if (server == null) { 429 throw new ServiceNotFoundException (mibName + " is not registered in the MBean server"); 430 } 431 432 if (adaptor != null) { 435 adaptor.removeMib(this, contextName); 436 } 437 438 Object [] params = {this, contextName}; 441 String [] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String"}; 442 try { 443 adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, 444 signature)); 445 } catch (InstanceNotFoundException e) { 446 throw new InstanceNotFoundException (name.toString()); 447 } catch (ReflectionException e) { 448 throw new ServiceNotFoundException (name.toString()); 449 } catch (MBeanException e) { 450 } 452 453 adaptorName = name; 454 } 455 456 472 public void setSnmpAdaptorName(ObjectName name, 473 String contextName, SnmpOid[] oids) 474 throws InstanceNotFoundException , ServiceNotFoundException { 475 476 if (server == null) { 477 throw new ServiceNotFoundException (mibName + " is not registered in the MBean server"); 478 } 479 480 if (adaptor != null) { 483 adaptor.removeMib(this, contextName); 484 } 485 486 Object [] params = {this, contextName, oids}; 489 String [] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String", oids.getClass().getName()}; 490 try { 491 adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, 492 signature)); 493 } catch (InstanceNotFoundException e) { 494 throw new InstanceNotFoundException (name.toString()); 495 } catch (ReflectionException e) { 496 throw new ServiceNotFoundException (name.toString()); 497 } catch (MBeanException e) { 498 } 500 501 adaptorName = name; 502 } 503 504 513 public boolean getBindingState() { 514 if (adaptor == null) 515 return false; 516 else 517 return true; 518 } 519 520 525 public String getMibName() { 526 return mibName; 527 } 528 529 541 public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu, 542 Vector vblist, 543 int version, 544 Object userData) 545 { 546 return new SnmpMibRequestImpl(null, 547 reqPdu, 548 vblist, 549 version, 550 userData, 551 null, 552 SnmpDefinitions.noAuthNoPriv, 553 getSecurityModel(version), 554 null,null); 555 } 556 569 public static SnmpMibRequest newMibRequest(SnmpEngine engine, 570 SnmpPdu reqPdu, 571 Vector vblist, 572 int version, 573 Object userData, 574 String principal, 575 int securityLevel, 576 int securityModel, 577 byte[] contextName, 578 byte[] accessContextName) { 579 return new SnmpMibRequestImpl(engine, 580 reqPdu, 581 vblist, 582 version, 583 userData, 584 principal, 585 securityLevel, 586 securityModel, 587 contextName, 588 accessContextName); 589 } 590 594 616 void getBulkWithGetNext(SnmpMibRequest req, int nonRepeat, int maxRepeat) 617 throws SnmpStatusException { 618 final Vector list = req.getSubList(); 619 620 final int L = list.size() ; 622 final int N = Math.max(Math.min(nonRepeat, L), 0) ; 623 final int M = Math.max(maxRepeat, 0) ; 624 final int R = L - N ; 625 626 if (L != 0) { 631 632 getNext(req); 635 636 Vector repeaters= splitFrom(list, N); 639 SnmpMibRequestImpl repeatedReq = 640 new SnmpMibRequestImpl(req.getEngine(), 641 req.getPdu(), 642 repeaters, 643 SnmpDefinitions.snmpVersionTwo, 644 req.getUserData(), 645 req.getPrincipal(), 646 req.getSecurityLevel(), 647 req.getSecurityModel(), 648 req.getContextName(), 649 req.getAccessContextName()); 650 for (int i = 2 ; i <= M ; i++) { 651 getNext(repeatedReq); 652 concatVector(req, repeaters); 653 } 654 } 655 } 656 657 658 662 669 private Vector splitFrom(Vector original, int limit) { 670 671 int max= original.size(); 672 Vector result= new Vector (max - limit); 673 int i= limit; 674 675 for(Enumeration e= original.elements(); e.hasMoreElements(); --i) { 680 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 681 if (i >0) 682 continue; 683 result.addElement(new SnmpVarBind(var.oid, var.value)); 684 } 685 return result; 686 } 687 688 private void concatVector(SnmpMibRequest req, Vector source) { 689 for(Enumeration e= source.elements(); e.hasMoreElements(); ) { 690 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 691 req.addVarBind(new SnmpVarBind(var.oid, var.value)); 694 } 695 } 696 697 private void concatVector(Vector target, Vector source) { 698 for(Enumeration e= source.elements(); e.hasMoreElements(); ) { 699 SnmpVarBind var= (SnmpVarBind) e.nextElement(); 700 target.addElement(new SnmpVarBind(var.oid, var.value)); 703 } 704 } 705 706 static private Vector vector(Enumeration e) { 707 if (e == null) return null; 708 Vector v = new Vector (); 709 while (e.hasMoreElements()) v.addElement(e.nextElement()); 710 return v; 711 } 712 713 private static int getSecurityModel(int version) { 714 switch(version) { 715 case SnmpDefinitions.snmpVersionOne: 716 return SnmpDefinitions.snmpV1SecurityModel; 717 default: 718 return SnmpDefinitions.snmpV2SecurityModel; 719 } 720 } 721 722 726 730 protected String mibName; 731 732 736 protected MBeanServer server; 737 738 742 746 private ObjectName adaptorName; 747 748 751 private transient SnmpMibHandler adaptor; 752 } 753 | Popular Tags |