package org.snmp4j.transport;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import org.snmp4j.SNMP4JSettings;
import org.snmp4j.TransportMapping;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.TlsAddress;
import org.snmp4j.smi.UdpAddress;

/* JADX INFO: loaded from: classes.dex */
public class TransportMappings {
    public static final String TRANSPORT_MAPPINGS = "org.snmp4j.transportMappings";
    private static final String TRANSPORT_MAPPINGS_DEFAULT = "transports.properties";
    private Hashtable<String, Class<? extends TransportMapping>> transportMappings = null;
    private static final LogAdapter logger = LogFactory.getLogger(TransportMappings.class);
    private static TransportMappings instance = null;

    protected TransportMappings() {
    }

    public static TransportMappings getInstance() {
        if (instance == null) {
            instance = new TransportMappings();
        }
        return instance;
    }

    public TransportMapping<? extends Address> createTransportMapping(Address address) {
        if (this.transportMappings == null) {
            registerTransportMappings();
        }
        Class<? extends TransportMapping> cls = this.transportMappings.get(address.getClass().getName());
        if (cls == null) {
            return null;
        }
        Class<?>[] clsArr = {address.getClass()};
        try {
            try {
                return cls.getConstructor(clsArr).newInstance(address);
            } catch (NoSuchMethodException e2) {
                for (Constructor<?> constructor : cls.getConstructors()) {
                    Class<?>[] parameterTypes = constructor.getParameterTypes();
                    if (parameterTypes.length == 1 && parameterTypes[0].isAssignableFrom(clsArr[0])) {
                        return (TransportMapping) constructor.newInstance(address);
                    }
                }
                logger.error("NoSuchMethodException while instantiating " + cls.getName(), e2);
                return null;
            }
        } catch (InvocationTargetException e3) {
            if (logger.isDebugEnabled()) {
                e3.printStackTrace();
            }
            logger.error(e3);
            throw new RuntimeException(e3.getTargetException());
        } catch (Exception e4) {
            if (logger.isDebugEnabled()) {
                e4.printStackTrace();
            }
            logger.error(e4);
            return null;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    protected synchronized void registerTransportMappings() {
        if (SNMP4JSettings.isExtensibilityEnabled()) {
            String property = System.getProperty(TRANSPORT_MAPPINGS, TRANSPORT_MAPPINGS_DEFAULT);
            InputStream resourceAsStream = TransportMappings.class.getResourceAsStream(property);
            if (resourceAsStream == null) {
                throw new InternalError("Could not read '" + property + "' from classpath!");
            }
            Properties properties = new Properties();
            try {
                try {
                    properties.load(resourceAsStream);
                    Hashtable<String, Class<? extends TransportMapping>> hashtable = new Hashtable<>(properties.size());
                    Enumeration<?> enumerationPropertyNames = properties.propertyNames();
                    while (enumerationPropertyNames.hasMoreElements()) {
                        String string = enumerationPropertyNames.nextElement().toString();
                        try {
                            hashtable.put(string, Class.forName(properties.getProperty(string)));
                        } catch (ClassNotFoundException e2) {
                            logger.error(e2);
                        }
                    }
                    this.transportMappings = hashtable;
                } catch (IOException e3) {
                    String str = "Could not read '" + property + "': " + e3.getMessage();
                    logger.error(str);
                    throw new InternalError(str);
                }
            } finally {
                try {
                    resourceAsStream.close();
                } catch (IOException e4) {
                    logger.warn(e4);
                }
            }
        } else {
            Hashtable<String, Class<? extends TransportMapping>> hashtable2 = new Hashtable<>(2);
            hashtable2.put(UdpAddress.class.getName(), DefaultUdpTransportMapping.class);
            hashtable2.put(TcpAddress.class.getName(), DefaultTcpTransportMapping.class);
            hashtable2.put(TlsAddress.class.getName(), TLSTM.class);
            this.transportMappings = hashtable2;
        }
    }
}
