📄 snmpmibagent.java
字号:
* @since 1.5 */ public void setSnmpAdaptorName(ObjectName name, SnmpOid[] oids) throws InstanceNotFoundException, ServiceNotFoundException { if (server == null) { throw new ServiceNotFoundException(mibName + " is not registered in the MBean server"); } // First remove the reference on the old adaptor server. // if (adaptor != null) { adaptor.removeMib(this); } // Then update the reference to the new adaptor server. // Object[] params = {this, oids}; String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", oids.getClass().getName()}; try { adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, signature)); } catch (InstanceNotFoundException e) { throw new InstanceNotFoundException(name.toString()); } catch (ReflectionException e) { throw new ServiceNotFoundException(name.toString()); } catch (MBeanException e) { // Should never occur... } adaptorName = name; } /** * Sets the reference to the SNMP protocol adaptor through which the MIB * will be SNMP accessible and add this new MIB in the SNMP MIB handler * associated to the specified <CODE>name</CODE>. * * @param name The name of the SNMP protocol adaptor. * @param contextName The MIB context name. If null is passed, will be registered in the default context. * @exception InstanceNotFoundException The SNMP protocol adaptor does * not exist in the MBean server. * * @exception ServiceNotFoundException This SNMP MIB is not registered * in the MBean server or the requested service is not supported. * * @since 1.5 */ public void setSnmpAdaptorName(ObjectName name, String contextName) throws InstanceNotFoundException, ServiceNotFoundException { if (server == null) { throw new ServiceNotFoundException(mibName + " is not registered in the MBean server"); } // First remove the reference on the old adaptor server. // if (adaptor != null) { adaptor.removeMib(this, contextName); } // Then update the reference to the new adaptor server. // Object[] params = {this, contextName}; String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String"}; try { adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, signature)); } catch (InstanceNotFoundException e) { throw new InstanceNotFoundException(name.toString()); } catch (ReflectionException e) { throw new ServiceNotFoundException(name.toString()); } catch (MBeanException e) { // Should never occur... } adaptorName = name; } /** * Sets the reference to the SNMP protocol adaptor through which the MIB * will be SNMP accessible and add this new MIB in the SNMP MIB handler * associated to the specified <CODE>name</CODE>. * * @param name The name of the SNMP protocol adaptor. * @param contextName The MIB context name. If null is passed, will be registered in the default context. * @param oids The set of OIDs this agent implements. * @exception InstanceNotFoundException The SNMP protocol adaptor does * not exist in the MBean server. * * @exception ServiceNotFoundException This SNMP MIB is not registered * in the MBean server or the requested service is not supported. * * @since 1.5 */ public void setSnmpAdaptorName(ObjectName name, String contextName, SnmpOid[] oids) throws InstanceNotFoundException, ServiceNotFoundException { if (server == null) { throw new ServiceNotFoundException(mibName + " is not registered in the MBean server"); } // First remove the reference on the old adaptor server. // if (adaptor != null) { adaptor.removeMib(this, contextName); } // Then update the reference to the new adaptor server. // Object[] params = {this, contextName, oids}; String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String", oids.getClass().getName()}; try { adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params, signature)); } catch (InstanceNotFoundException e) { throw new InstanceNotFoundException(name.toString()); } catch (ReflectionException e) { throw new ServiceNotFoundException(name.toString()); } catch (MBeanException e) { // Should never occur... } adaptorName = name; } /** * Indicates whether or not the MIB module is bound to a SNMP protocol * adaptor. * As a reminder, only bound MIBs can be accessed through SNMP protocol * adaptor. * * @return <CODE>true</CODE> if the MIB module is bound, * <CODE>false</CODE> otherwise. */ public boolean getBindingState() { if (adaptor == null) return false; else return true; } /** * Gets the MIB name. * * @return The MIB name. */ public String getMibName() { return mibName; } /** * This is a factory method for creating new SnmpMibRequest objects. * @param reqPdu The received PDU. * @param vblist The vector of SnmpVarBind objects in which the * MIB concerned by this request is involved. * @param version The protocol version of the SNMP request. * @param userData User allocated contextual data. * * @return A new SnmpMibRequest object. * * @since 1.5 **/ public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu, Vector vblist, int version, Object userData) { return new SnmpMibRequestImpl(null, reqPdu, vblist, version, userData, null, SnmpDefinitions.noAuthNoPriv, getSecurityModel(version), null,null); } /** * This is a factory method for creating new SnmpMibRequest objects. * @param engine The local engine. * @param reqPdu The received pdu. * @param vblist The vector of SnmpVarBind objects in which the * MIB concerned by this request is involved. * @param version The protocol version of the SNMP request. * @param userData User allocated contextual data. * * @return A new SnmpMibRequest object. * * @since 1.5 **/ public static SnmpMibRequest newMibRequest(SnmpEngine engine, SnmpPdu reqPdu, Vector vblist, int version, Object userData, String principal, int securityLevel, int securityModel, byte[] contextName, byte[] accessContextName) { return new SnmpMibRequestImpl(engine, reqPdu, vblist, version, userData, principal, securityLevel, securityModel, contextName, accessContextName); } // --------------------------------------------------------------------- // PACKAGE METHODS // --------------------------------------------------------------------- /** * Processes a <CODE>getBulk</CODE> operation using call to * <CODE>getNext</CODE>. * The method implements the <CODE>getBulk</CODE> operation by calling * appropriately the <CODE>getNext</CODE> method. * * @param req The SnmpMibRequest containing the variable list to be * retrieved. * * @param nonRepeat The number of variables, starting with the first * variable in the variable-bindings, for which a single lexicographic * successor is requested. * * @param maxRepeat The number of lexicographic successors * requested for each of the last R variables. R is the number of * variables following the first nonRepeat variables for which * multiple lexicographic successors are requested. * * @return The variable list containing returned values. * * @exception SnmpStatusException An error occured during the operation. */ void getBulkWithGetNext(SnmpMibRequest req, int nonRepeat, int maxRepeat) throws SnmpStatusException { final Vector list = req.getSubList(); // RFC 1905, Section 4.2.3, p14 final int L = list.size() ; final int N = Math.max(Math.min(nonRepeat, L), 0) ; final int M = Math.max(maxRepeat, 0) ; final int R = L - N ; // Let's build the varBindList for the response pdu // // int errorStatus = SnmpDefinitions.snmpRspNoError ; // int errorIndex = 0 ; if (L != 0) { // Non-repeaters and first row of repeaters // getNext(req); // Now the remaining repeaters // Vector repeaters= splitFrom(list, N); SnmpMibRequestImpl repeatedReq = new SnmpMibRequestImpl(req.getEngine(), req.getPdu(), repeaters, SnmpDefinitions.snmpVersionTwo, req.getUserData(), req.getPrincipal(), req.getSecurityLevel(), req.getSecurityModel(), req.getContextName(), req.getAccessContextName()); for (int i = 2 ; i <= M ; i++) { getNext(repeatedReq); concatVector(req, repeaters); } } } // --------------------------------------------------------------------- // PRIVATE METHODS // --------------------------------------------------------------------- /** * This method creates a new Vector which does not contain the first * element up to the specified limit. * * @param original The original vector. * @param limit The limit. */ private Vector splitFrom(Vector original, int limit) { int max= original.size(); Vector result= new Vector(max - limit); int i= limit; // Ok the loop looks a bit strange. But in order to improve the // perf, we try to avoid reference to the limit variable from // within the loop ... // for(Enumeration e= original.elements(); e.hasMoreElements(); --i) { SnmpVarBind var= (SnmpVarBind) e.nextElement(); if (i >0) continue; result.addElement(new SnmpVarBind(var.oid, var.value)); } return result; } private void concatVector(SnmpMibRequest req, Vector source) { for(Enumeration e= source.elements(); e.hasMoreElements(); ) { SnmpVarBind var= (SnmpVarBind) e.nextElement(); // We need to duplicate the SnmpVarBind otherwise it is going // to be overloaded by the next get Next ... req.addVarBind(new SnmpVarBind(var.oid, var.value)); } } private void concatVector(Vector target, Vector source) { for(Enumeration e= source.elements(); e.hasMoreElements(); ) { SnmpVarBind var= (SnmpVarBind) e.nextElement(); // We need to duplicate the SnmpVarBind otherwise it is going // to be overloaded by the next get Next ... target.addElement(new SnmpVarBind(var.oid, var.value)); } } static private Vector vector(Enumeration e) { if (e == null) return null; Vector v = new Vector(); while (e.hasMoreElements()) v.addElement(e.nextElement()); return v; } private static int getSecurityModel(int version) { switch(version) { case SnmpDefinitions.snmpVersionOne: return SnmpDefinitions.snmpV1SecurityModel; default: return SnmpDefinitions.snmpV2SecurityModel; } } // --------------------------------------------------------------------- // PROTECTED VARIABLES // --------------------------------------------------------------------- /** * The object name of the MIB. * @serial */ protected String mibName; /** * The reference to the MBean server. * @serial */ protected MBeanServer server; // --------------------------------------------------------------------- // PRIVATE VARIABLES // --------------------------------------------------------------------- /** * The object name of the SNMP protocol adaptor. * @serial */ private ObjectName adaptorName; /** * The reference to the SNMP stack. */ private transient SnmpMibHandler adaptor;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -