1 20 package org.jboss.jmx.adaptor.snmp.agent; 21 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.jboss.logging.Logger; 27 import org.opennms.protocols.snmp.SnmpCounter64; 28 import org.opennms.protocols.snmp.SnmpInt32; 29 import org.opennms.protocols.snmp.SnmpObjectId; 30 import org.opennms.protocols.snmp.SnmpOctetString; 31 import org.opennms.protocols.snmp.SnmpVarBind; 32 45 public class SnmpVarBindFactory 46 { 47 48 private static final Logger log = Logger.getLogger(SnmpVarBindFactory.class); 49 50 51 private Map makers = new HashMap (); 52 53 54 private final Maker defaultMaker = new SnmpObjectMaker(); 55 56 60 public SnmpVarBindFactory() 61 { 62 makers.put("java.lang.String", new SnmpOctetStringMaker()); 63 makers.put("java.lang.Integer", new SnmpInt32Maker()); 64 makers.put("java.lang.Long", new SnmpCounter64Maker()); 65 makers.put("java.math.BigInteger", new SnmpCounter64Maker()); 66 makers.put("java.util.Date", new SnmpDateMaker()); 67 } 68 69 74 public SnmpVarBind make(String oid, Object value) 75 throws MappingFailedException 76 { 77 String type = value.getClass().getName(); 79 Maker m = (Maker)this.makers.get(type); 80 81 if (m == null) { 83 log.warn("Value type \"" + type + "\" for OID " + oid + 84 " encountered. Using default VarBind maker"); 85 86 return defaultMaker.make(oid, value); 87 } 88 else 89 return m.make(oid, value); 90 } 91 92 95 interface Maker 96 { 97 public SnmpVarBind make(String oid, Object value) 98 throws MappingFailedException; 99 } 100 101 104 class SnmpInt32Maker 105 implements Maker 106 { 107 public SnmpVarBind make(String oid, Object value) 108 throws MappingFailedException 109 { 110 Integer i = (Integer )value; 111 112 return new SnmpVarBind(new SnmpObjectId(oid), 113 new SnmpInt32(i)); 114 } 115 } 117 120 class SnmpCounter64Maker 121 implements Maker 122 { 123 public SnmpVarBind make(String oid, Object value) 124 throws MappingFailedException 125 { 126 Long l = (Long )value; 127 128 return new SnmpVarBind(new SnmpObjectId(oid), 129 new SnmpCounter64(l.longValue())); 130 } 131 } 133 136 class SnmpOctetStringMaker 137 implements Maker 138 { 139 public SnmpVarBind make(String oid, Object value) 140 throws MappingFailedException 141 { 142 String s = (String )value; 143 144 return new SnmpVarBind(new SnmpObjectId(oid), 145 new SnmpOctetString(s.getBytes())); 146 } 147 } 149 152 class SnmpDateMaker 153 implements Maker 154 { 155 public SnmpVarBind make(String oid, Object value) 156 throws MappingFailedException 157 { 158 Date d = (Date )value; 159 SnmpOctetStringMaker sMaker = new SnmpOctetStringMaker(); 160 161 return sMaker.make(oid, d.toString()); 162 } 163 } 165 168 class SnmpObjectMaker 169 implements Maker 170 { 171 public SnmpVarBind make(String oid, Object value) 172 throws MappingFailedException 173 { 174 SnmpOctetStringMaker sMaker = new SnmpOctetStringMaker(); 175 176 return sMaker.make(oid, value.toString()); 177 } 178 } 180 } 182 183 | Popular Tags |