package org.xbib.net.socket.v4;

import org.xbib.net.socket.v4.datagram.DatagramSocket;

import java.lang.reflect.InvocationTargetException;

public class SocketFactory {

    public static final int SOCK_DGRAM = 2;

    private SocketFactory() {
    }

    public static DatagramSocket createDatagramSocket(int protocol, int port)
            throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<? extends DatagramSocket> implementationClass = Class.forName(getImplementationClassName())
                .asSubclass(DatagramSocket.class);
        return implementationClass
                .getDeclaredConstructor(Integer.TYPE, Integer.TYPE, Integer.TYPE)
                .newInstance(SOCK_DGRAM, protocol, port);
    }


    private static String getImplementationClassName() {
        return "org.xbib.net.socket.v4.unix.NativeDatagramSocket";
    }
}
