1 20 21 package org.snmp4j.smi; 22 23 import java.io.*; 24 import java.util.*; 25 import org.snmp4j.log.*; 26 import org.snmp4j.asn1.*; 27 import org.snmp4j.SNMP4JSettings; 28 import org.snmp4j.PDU; 30 31 49 public abstract class AbstractVariable implements Variable, Serializable { 50 51 private static final long serialVersionUID = 1395840752909725320L; 52 53 public static final String SMISYNTAXES_PROPERTIES = 54 "org.snmp4j.smisyntaxes"; 55 private static final String SMISYNTAXES_PROPERTIES_DEFAULT = 56 "smisyntaxes.properties"; 57 58 private static final Object [][] SYNTAX_NAME_MAPPING = { 59 { "Integer32", new Integer (BER.INTEGER32) }, 60 { "BIT STRING", new Integer (BER.BITSTRING) }, 61 { "OCTET STRING", new Integer (BER.OCTETSTRING) }, 62 { "OBJECT IDENTIFIER", new Integer (BER.OID) }, 63 { "TimeTicks", new Integer (BER.TIMETICKS) }, 64 { "Counter", new Integer (BER.COUNTER) }, 65 { "Counter64", new Integer (BER.COUNTER64) }, 66 { "EndOfMibView", new Integer (BER.ENDOFMIBVIEW) }, 67 { "Gauge", new Integer (BER.GAUGE32) }, 68 { "IpAddress", new Integer (BER.IPADDRESS) }, 69 { "NoSuchInstance", new Integer (BER.NOSUCHINSTANCE) }, 70 { "NoSuchObject", new Integer (BER.NOSUCHOBJECT) }, 71 { "Null", new Integer (BER.NULL) }, 72 { "Opaque", new Integer (BER.OPAQUE) } 73 }; 74 75 private static Hashtable registeredSyntaxes = null; 76 77 private static final LogAdapter logger = 78 LogFactory.getLogger(AbstractVariable.class); 79 80 84 public AbstractVariable() { 85 } 86 87 public abstract boolean equals(Object o); 88 89 public abstract int compareTo(Object o); 90 91 public abstract int hashCode(); 92 93 99 public abstract int getBERLength(); 100 101 public int getBERPayloadLength() { 102 return getBERLength(); 103 } 104 105 112 public abstract void decodeBER(BERInputStream inputStream) throws IOException; 113 114 121 public abstract void encodeBER(OutputStream outputStream) throws IOException; 122 123 135 public static Variable createFromBER(BERInputStream inputStream) throws 136 IOException { 137 if (!inputStream.markSupported()) { 138 throw new IOException( 139 "InputStream for decoding a Variable must support marks"); 140 } 141 if (SNMP4JSettings.isExtensibilityEnabled() && 142 (registeredSyntaxes == null)) { 143 registerSyntaxes(); 144 } 145 inputStream.mark(2); 146 int type = inputStream.read(); 147 Variable variable; 148 if (SNMP4JSettings.isExtensibilityEnabled()) { 149 Class c = (Class ) registeredSyntaxes.get(new Integer (type)); 150 if (c == null) { 151 throw new IOException("Encountered unsupported variable syntax: " + 152 type); 153 } 154 try { 155 variable = (Variable) c.newInstance(); 156 } 157 catch (IllegalAccessException aex) { 158 throw new IOException("Could not access variable syntax class for: " + 159 c.getName()); 160 } 161 catch (InstantiationException iex) { 162 throw new IOException( 163 "Could not instantiate variable syntax class for: " + 164 c.getName()); 165 } 166 } 167 else { 168 variable = createVariable(type); 169 } 170 inputStream.reset(); 171 variable.decodeBER(inputStream); 172 return variable; 173 } 174 175 private static Variable createVariable(int smiSyntax) { 176 switch (smiSyntax) { 177 case SMIConstants.SYNTAX_OBJECT_IDENTIFIER: { 178 return new OID(); 179 } 180 case SMIConstants.SYNTAX_INTEGER: { 181 return new Integer32(); 182 } 183 case SMIConstants.SYNTAX_OCTET_STRING: { 184 return new OctetString(); 185 } 186 case SMIConstants.SYNTAX_GAUGE32: { 187 return new Gauge32(); 188 } 189 case SMIConstants.SYNTAX_COUNTER32: { 190 return new Counter32(); 191 } 192 case SMIConstants.SYNTAX_COUNTER64: { 193 return new Counter64(); 194 } 195 case SMIConstants.SYNTAX_NULL: { 196 return new Null(); 197 } 198 case SMIConstants.SYNTAX_TIMETICKS: { 199 return new TimeTicks(); 200 } 201 case SMIConstants.EXCEPTION_END_OF_MIB_VIEW: { 202 return new Null(SMIConstants.EXCEPTION_END_OF_MIB_VIEW); 203 } 204 case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE: { 205 return new Null(SMIConstants.EXCEPTION_NO_SUCH_INSTANCE); 206 } 207 case SMIConstants.EXCEPTION_NO_SUCH_OBJECT: { 208 return new Null(SMIConstants.EXCEPTION_NO_SUCH_OBJECT); 209 } 210 case SMIConstants.SYNTAX_OPAQUE: { 211 return new Opaque(); 212 } 213 case SMIConstants.SYNTAX_IPADDRESS: { 214 return new IpAddress(); 215 } 216 default: { 217 throw new IllegalArgumentException ("Unsupported variable syntax: " + 218 smiSyntax); 219 } 220 } 221 } 222 223 235 public static Variable createFromSyntax(int smiSyntax) { 236 if (!SNMP4JSettings.isExtensibilityEnabled()) { 237 return createVariable(smiSyntax); 238 } 239 if (registeredSyntaxes == null) { 240 registerSyntaxes(); 241 } 242 Class c = (Class ) registeredSyntaxes.get(new Integer (smiSyntax)); 243 if (c == null) { 244 throw new IllegalArgumentException ("Unsupported variable syntax: " + 245 smiSyntax); 246 } 247 try { 248 Variable variable = (Variable) c.newInstance(); 249 return variable; 250 } 251 catch (IllegalAccessException aex) { 252 throw new RuntimeException ("Could not access variable syntax class for: " + 253 c.getName()); 254 } 255 catch (InstantiationException iex) { 256 throw new RuntimeException ( 257 "Could not instantiate variable syntax class for: " + 258 c.getName()); 259 } 260 } 261 262 268 private synchronized static void registerSyntaxes() { 269 String syntaxes = System.getProperty(SMISYNTAXES_PROPERTIES, 270 SMISYNTAXES_PROPERTIES_DEFAULT); 271 InputStream is = Variable.class.getResourceAsStream(syntaxes); 272 if (is == null) { 273 throw new InternalError ("Could not read '" + syntaxes + 274 "' from classpath!"); 275 } 276 Properties props = new Properties(); 277 try { 278 props.load(is); 279 Hashtable regSyntaxes = new Hashtable(props.size()); 280 for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) { 281 String id = (String ) en.nextElement(); 282 String className = props.getProperty(id); 283 try { 284 Class c = Class.forName(className); 285 regSyntaxes.put(new Integer (id), c); 286 } 287 catch (ClassNotFoundException cnfe) { 288 logger.error(cnfe); 289 } 290 } 291 registeredSyntaxes = regSyntaxes; 293 } 294 catch (IOException iox) { 295 String txt = "Could not read '" + syntaxes + "': " + 296 iox.getMessage(); 297 logger.error(txt); 298 throw new InternalError (txt); 299 } 300 finally { 301 try { 302 is.close(); 303 } 304 catch (IOException ex) { 305 logger.warn(ex); 306 } 307 } 308 } 309 310 317 public abstract int getSyntax(); 318 319 331 public boolean isException() { 332 return Null.isExceptionSyntax(getSyntax()); 333 } 334 335 340 public abstract String toString(); 341 342 352 public abstract int toInt(); 353 354 363 public abstract long toLong(); 364 365 public abstract Object clone(); 366 367 376 public static String getSyntaxString(int syntax) { 377 switch (syntax) { 378 case BER.INTEGER: 379 return "Integer32"; 380 case BER.BITSTRING: 381 return "BIT STRING"; 382 case BER.OCTETSTRING: 383 return "OCTET STRING"; 384 case BER.OID: 385 return "OBJECT IDENTIFIER"; 386 case BER.TIMETICKS: 387 return "TimeTicks"; 388 case BER.COUNTER: 389 return "Counter"; 390 case BER.COUNTER64: 391 return "Counter64"; 392 case BER.ENDOFMIBVIEW: 393 return "EndOfMibView"; 394 case BER.GAUGE32: 395 return "Gauge"; 396 case BER.IPADDRESS: 397 return "IpAddress"; 398 case BER.NOSUCHINSTANCE: 399 return "NoSuchInstance"; 400 case BER.NOSUCHOBJECT: 401 return "NoSuchObject"; 402 case BER.NULL: 403 return "Null"; 404 case BER.OPAQUE: 405 return "Opaque"; 406 } 407 return "?"; 408 } 409 410 418 public final String getSyntaxString() { 419 return getSyntaxString(getSyntax()); 420 } 421 422 431 public static int getSyntaxFromString(String syntaxString) { 432 for (int i=0; i<SYNTAX_NAME_MAPPING.length; i++) { 433 if (SYNTAX_NAME_MAPPING[i][0].equals(syntaxString)) { 434 return ((Integer )SYNTAX_NAME_MAPPING[i][1]).intValue(); 435 } 436 } 437 return BER.NULL; 438 } 439 440 453 public abstract OID toSubIndex(boolean impliedLength); 454 455 467 public abstract void fromSubIndex(OID subIndex, boolean impliedLength); 468 469 483 public boolean isDynamic() { 484 return false; 485 } 486 487 } 488 | Popular Tags |