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.BERInputStream; 27 import org.snmp4j.SNMP4JSettings; 28 29 40 public class GenericAddress extends SMIAddress { 41 42 static final long serialVersionUID = -6102594326202231511L; 43 44 47 public static final String TYPE_UDP = "udp"; 48 51 public static final String TYPE_TCP = "tcp"; 52 55 public static final String TYPE_IP = "ip"; 56 57 public static final String ADDRESS_TYPES_PROPERTIES = 58 "org.snmp4j.addresses"; 59 private static final String ADDRESS_TYPES_PROPERTIES_DEFAULT = 60 "address.properties"; 61 62 private static final LogAdapter logger = LogFactory.getLogger(GenericAddress.class); 63 64 private SMIAddress address; 65 private static Map knownAddressTypes = null; 66 67 public GenericAddress() { 68 } 69 70 public GenericAddress(SMIAddress address) { 71 this.address = address; 72 } 73 74 public int getSyntax() { 75 return address.getSyntax(); 76 } 77 78 public boolean isValid() { 79 if (address == null) { 80 return false; 81 } 82 return address.isValid(); 83 } 84 85 public String toString() { 86 return address.toString(); 87 } 88 89 public int hashCode() { 90 return address.hashCode(); 91 } 92 93 public int compareTo(Object o) { 94 return address.compareTo(o); 95 } 96 97 public boolean equals(Object o) { 98 return address.equals(o); 99 } 100 101 public void decodeBER(BERInputStream inputStream) throws java.io.IOException { 102 throw new UnsupportedOperationException (); 103 } 104 public void encodeBER(OutputStream outputStream) throws java.io.IOException { 105 address.encodeBER(outputStream); 106 } 107 108 public int getBERLength() { 109 return address.getBERLength(); 110 } 111 112 public void setAddress(SMIAddress address) { 113 this.address = address; 114 } 115 116 public Address getAddress() { 117 return address; 118 } 119 120 126 private synchronized static void registerAddressTypes() { 127 if (SNMP4JSettings.isExtensibilityEnabled()) { 128 String addresses = System.getProperty(ADDRESS_TYPES_PROPERTIES, 129 ADDRESS_TYPES_PROPERTIES_DEFAULT); 130 InputStream is = Variable.class.getResourceAsStream(addresses); 131 if (is == null) { 132 throw new InternalError ("Could not read '" + addresses + 133 "' from classpath!"); 134 } 135 Properties props = new Properties(); 136 try { 137 props.load(is); 138 Map h = new TreeMap(); 139 for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) { 140 String id = (String ) en.nextElement(); 141 String className = props.getProperty(id); 142 try { 143 Class c = Class.forName(className); 144 h.put(id, c); 145 } 146 catch (ClassNotFoundException cnfe) { 147 logger.error(cnfe); 148 } 149 } 150 knownAddressTypes = h; 151 } 152 catch (IOException iox) { 153 String txt = "Could not read '" + addresses + "': " + iox.getMessage(); 154 logger.error(txt); 155 throw new InternalError (txt); 156 } 157 finally { 158 try { 159 is.close(); 160 } 161 catch (IOException ex) { 162 logger.warn(ex); 164 } 165 } 166 } 167 else { 168 Map h = new TreeMap(); 169 h.put(TYPE_UDP, UdpAddress.class); 170 h.put(TYPE_TCP, TcpAddress.class); 171 h.put(TYPE_IP, IpAddress.class); 172 knownAddressTypes = h; 173 } 174 } 175 176 194 public static Address parse(String address) { 195 if (knownAddressTypes == null) { 196 registerAddressTypes(); 197 } 198 String type = TYPE_UDP; 199 int sep = address.indexOf(':'); 200 if (sep > 0) { 201 type = address.substring(0, sep); 202 address = address.substring(sep+1); 203 } 204 type = type.toLowerCase(); 205 Class c = (Class )knownAddressTypes.get(type); 206 if (c == null) { 207 throw new IllegalArgumentException ("Address type " + type + " unknown"); 208 } 209 try { 210 Address addr = (Address)c.newInstance(); 211 if (addr.parseAddress(address)) { 212 return addr; 213 } 214 return null; 215 } 216 catch (Exception ex) { 217 logger.warn(ex); 218 } 219 return null; 220 } 221 222 229 public boolean parseAddress(String address) { 230 Address addr = parse(address); 231 if (addr instanceof SMIAddress) { 232 setAddress((SMIAddress)addr); 233 return true; 234 } 235 return false; 236 } 237 238 public Object clone() { 239 return new GenericAddress(address); 240 } 241 242 public int toInt() { 243 throw new UnsupportedOperationException (); 244 } 245 246 public long toLong() { 247 throw new UnsupportedOperationException (); 248 } 249 250 public OID toSubIndex(boolean impliedLength) { 251 throw new UnsupportedOperationException (); 252 } 253 254 public void fromSubIndex(OID subIndex, boolean impliedLength) { 255 throw new UnsupportedOperationException (); 256 } 257 258 public void setValue(String value) { 259 if (!parseAddress(value)) { 260 throw new IllegalArgumentException (value+" cannot be parsed by "+ 261 getClass().getName()); 262 } 263 } 264 } 265 266 | Popular Tags |