1 20 21 22 23 package org.snmp4j.transport; 24 25 import org.snmp4j.smi.Address; 26 import org.snmp4j.TransportMapping; 27 import java.io.InputStream ; 28 import java.util.Properties ; 29 import java.io.IOException ; 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 import org.snmp4j.log.*; 33 import java.lang.reflect.Constructor ; 34 import org.snmp4j.SNMP4JSettings; 35 36 44 public class TransportMappings { 45 46 private static final LogAdapter logger = LogFactory.getLogger(TransportMappings.class); 47 48 public static final String TRANSPORT_MAPPINGS = 49 "org.snmp4j.transportMappings"; 50 private static final String TRANSPORT_MAPPINGS_DEFAULT = 51 "transports.properties"; 52 53 private static TransportMappings instance = null; 54 private Hashtable transportMappings = null; 55 56 protected TransportMappings() { 57 } 58 59 64 public static TransportMappings getInstance() { 65 if (instance == null) { 66 instance = new TransportMappings(); 67 } 68 return instance; 69 } 70 71 87 public TransportMapping createTransportMapping(Address transportAddress) { 88 if (transportMappings == null) { 89 registerTransportMappings(); 90 } 91 Class c = 92 (Class ) transportMappings.get(transportAddress.getClass().getName()); 93 if (c == null) { 94 return null; 95 } 96 Class [] params = new Class [1]; 97 params[0] = transportAddress.getClass(); 98 Constructor constructor = null; 99 try { 100 constructor = c.getConstructor(params); 101 return (TransportMapping) 102 constructor.newInstance(new Object [] { transportAddress }); 103 } 104 catch (Exception ex) { 105 if (logger.isDebugEnabled()) { 106 ex.printStackTrace(); 107 } 108 logger.error(ex); 109 return null; 110 } 111 } 112 113 protected synchronized void registerTransportMappings() { 114 if (SNMP4JSettings.isExtensibilityEnabled()) { 115 String transports = 116 System.getProperty(TRANSPORT_MAPPINGS, TRANSPORT_MAPPINGS_DEFAULT); 117 InputStream is = TransportMappings.class.getResourceAsStream(transports); 118 if (is == null) { 119 throw new InternalError ("Could not read '" + transports + 120 "' from classpath!"); 121 } 122 Properties props = new Properties (); 123 try { 124 props.load(is); 125 Hashtable t = new Hashtable (props.size()); 126 for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) { 127 String addressClassName = (String ) en.nextElement(); 128 String className = props.getProperty(addressClassName); 129 try { 130 Class c = Class.forName(className); 131 t.put(addressClassName, c); 132 } 133 catch (ClassNotFoundException cnfe) { 134 logger.error(cnfe); 135 } 136 } 137 transportMappings = t; 139 } 140 catch (IOException iox) { 141 String txt = "Could not read '" + transports + "': " + 142 iox.getMessage(); 143 logger.error(txt); 144 throw new InternalError (txt); 145 } 146 finally { 147 try { 148 is.close(); 149 } 150 catch (IOException ex) { 151 logger.warn(ex); 152 } 153 } 154 } 155 else { 156 Hashtable t = new Hashtable (2); 157 t.put("org.snmp4j.smi.UdpAddress", DefaultUdpTransportMapping.class); 158 t.put("org.snmp4j.smi.TcpAddress", DefaultTcpTransportMapping.class); 159 transportMappings = t; 160 } 161 } 162 163 } 164 | Popular Tags |