Example #1
0
 /**
  * Converts variable to right object value
  *
  * @param variable snmp var
  * @param clazz class type to narrow to
  * @return object value
  * @throws ClassCastException if error
  * @throws java.net.UnknownHostException if error
  */
 @SuppressWarnings("UnnecessaryBoxing")
 private <T> T convert(Variable variable, Class<T> clazz)
     throws ClassCastException, UnknownHostException {
   switch (variable.getSyntax()) {
     case org.snmp4j.smi.SMIConstants.EXCEPTION_END_OF_MIB_VIEW:
     case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:
     case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_OBJECT:
       return clazz.cast(new Exception(variable.toString()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_BITS:
     case org.snmp4j.smi.SMIConstants.SYNTAX_OCTET_STRING:
       if (variable instanceof BitString) {
         BitString bitString = (BitString) variable;
         return clazz.cast(bitString.toString());
       }
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER32:
       return clazz.cast(new Integer(variable.toInt()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER64:
       return clazz.cast(new Long(variable.toLong()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_GAUGE32:
     case org.snmp4j.smi.SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       if (variable instanceof Gauge32) {
         Gauge32 bitString = (Gauge32) variable;
         return clazz.cast(new Long(bitString.toLong()));
       }
       return clazz.cast(new Long(variable.toLong()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_INTEGER:
     case org.snmp4j.smi.SMIConstants.SYNTAX_INTEGER32:
       return clazz.cast(new Integer(variable.toInt()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_IPADDRESS:
       IpAddress ipAddress = (IpAddress) variable;
       return clazz.cast(InetAddress.getByAddress(ipAddress.toByteArray()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_NULL:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_OPAQUE:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_TIMETICKS:
       return clazz.cast(new Long(variable.toLong()));
     default:
       throw new IllegalStateException();
   }
 }
Example #2
0
 private Object convertVar(Variable valueAsVar) {
   Object retvalue = null;
   if (valueAsVar != null) {
     int type = valueAsVar.getSyntax();
     if (valueAsVar instanceof OID) {
       retvalue = valueAsVar;
     } else if (valueAsVar instanceof UnsignedInteger32) {
       if (valueAsVar instanceof TimeTicks) {
         long epochcentisecond = valueAsVar.toLong();
         retvalue = new Double(epochcentisecond / 100.0);
       } else retvalue = valueAsVar.toLong();
     } else if (valueAsVar instanceof Integer32) retvalue = valueAsVar.toInt();
     else if (valueAsVar instanceof Counter64) retvalue = valueAsVar.toLong();
     else if (valueAsVar instanceof OctetString) {
       if (valueAsVar instanceof Opaque) {
         retvalue = resolvOpaque((Opaque) valueAsVar);
       } else {
         // It might be a C string, try to remove the last 0;
         // But only if the new string is printable
         OctetString octetVar = (OctetString) valueAsVar;
         int length = octetVar.length();
         if (length > 1 && octetVar.get(length - 1) == 0) {
           OctetString newVar = octetVar.substring(0, length - 1);
           if (newVar.isPrintable()) {
             valueAsVar = newVar;
             logger.debug("Convertion an octet stream from " + octetVar + " to " + valueAsVar);
           }
         }
         retvalue = valueAsVar.toString();
       }
     } else if (valueAsVar instanceof Null) {
       retvalue = null;
     } else if (valueAsVar instanceof IpAddress) {
       retvalue = ((IpAddress) valueAsVar).getInetAddress();
     } else {
       logger.warn("Unknown syntax " + AbstractVariable.getSyntaxString(type));
     }
   }
   return retvalue;
 }