Example usage for javax.net ServerSocketFactory getDefault

List of usage examples for javax.net ServerSocketFactory getDefault

Introduction

In this page you can find the example usage for javax.net ServerSocketFactory getDefault.

Prototype

public static ServerSocketFactory getDefault() 

Source Link

Document

Returns a copy of the environment's default socket factory.

Usage

From source file:de.hshannover.f4.trust.iron.mapserver.contentauth.RemotePDP.java

private static void runServer(int port, LocalSunXacml pdp, int threads) {
    Executor clientExec = Executors.newFixedThreadPool(threads);
    ServerSocketFactory sockFac = ServerSocketFactory.getDefault();

    try {//from  w  w w  .  ja va  2s.c  om
        ServerSocket sock = sockFac.createServerSocket(port);
        while (true) {
            Socket clientSock = sock.accept();
            Runnable cmd = new HandleXacmlRequest(clientSock, pdp);
            System.out.println(nameDate() + "new client " + clientSock.getInetAddress());
            clientExec.execute(cmd);
        }
    } catch (IOException e) {
        System.err.println(nameDate() + e.toString());
        System.exit(1);
    }
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static ServerSocket createServerSocket(int startPort, int endPort) throws BindException {
    ServerSocket ssoc = null;//  www  . ja  v  a 2  s  . co m
    ServerSocketFactory factory = ServerSocketFactory.getDefault();

    int port = startPort;
    while (true) {
        try {
            ssoc = factory.createServerSocket(port);
            break;
        } catch (IOException e) {
            if (port >= endPort)
                throw new BindException(
                        "No available port to bind to in range [" + startPort + " .. " + endPort + "]");
            port++;
        }
    }

    return ssoc;
}

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

public String openActiveMode() throws IOException {
    InetAddress addr = NetUtils.getMachineAddress(true);
    String addrStr = addr != null ? addr.getHostAddress() : "127.0.0.1";
    ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(0, 1, addr);
    sock.setSoTimeout(10000);/*from  w  w w  .  ja v  a 2s .  co m*/

    Pattern pattern = Pattern.compile("^([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)$");
    Matcher matcher = pattern.matcher(addrStr);
    if (!matcher.matches()) {
        throw new IOException("Invalid address: " + addrStr);
    }
    int p1 = (sock.getLocalPort() >>> FtpConstants.BYTE_LENGTH) & FtpConstants.BYTE_MASK;
    int p2 = sock.getLocalPort() & FtpConstants.BYTE_MASK;

    StringBuffer sb = new StringBuffer();
    sb.append("PORT ");
    sb.append(matcher.group(1));
    sb.append(",");
    sb.append(matcher.group(2));
    sb.append(",");
    sb.append(matcher.group(3));
    sb.append(",");
    sb.append(matcher.group(4));
    sb.append(",");
    sb.append(p1);
    sb.append(",");
    sb.append(p2);

    resetDataSockets();
    activeModeServerSocket = sock;

    sendCommand(sb.toString());

    return getResponse();

}

From source file:com.cloudant.tests.CloudantClientTests.java

/**
 * Check that the connection timeout throws a SocketTimeoutException when it can't connect
 * within the timeout./*from w w w .  j a  va2 s  .c om*/
 */
@Test(expected = SocketTimeoutException.class)
public void connectionTimeout() throws Throwable {

    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1);

    //block the single connection to our server
    Socket socket = new Socket();
    socket.connect(serverSocket.getLocalSocketAddress());

    //now try to connect, but should timeout because there is no connection available
    try {
        CloudantClient c = ClientBuilder.url(new URL("http://127.0.0.1:" + serverSocket.getLocalPort()))
                .connectTimeout(100, TimeUnit.MILLISECONDS).build();

        // Make a request
        c.getAllDbs();
    } catch (CouchDbException e) {
        //unwrap the CouchDbException
        if (e.getCause() != null) {
            //whilst it would be really nice to actually assert that this was a connect
            //exception and not some other SocketTimeoutException there are JVM differences in
            //this respect (i.e. OpenJDK does not appear to distinguish between read/connect)
            //in its exception messages
            throw e.getCause();
        } else {
            throw e;
        }
    } finally {
        //make sure we close the sockets
        IOUtils.closeQuietly(serverSocket);
        IOUtils.closeQuietly(socket);
    }
}

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

public String openExtendedActiveMode() throws IOException {
    StringBuffer params = new StringBuffer();

    params.append("|1|");
    InetAddress addr = NetUtils.getMachineAddress(true);
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1, addr);
    params.append(addr.getHostAddress());
    params.append("|");
    params.append(serverSocket.getLocalPort());
    params.append("|");

    sendCommand("EPRT " + params.toString());
    String response = getResponse();

    if (passiveModeSocket != null) {
        passiveModeSocket.close();//w  w  w  .j  a  va  2s. co m
    }
    activeModeServerSocket = serverSocket;
    return response;
}

From source file:com.emc.xtest.analyzer.dao.xDBProvider.XDBManager.java

public synchronized void startDatabase() throws XDBEngineException {
    String nodeName = XhiveFederationIf.PRIMARY_NODE_NAME;

    m_driverToLocalServer = XhiveDriverFactory.getDriver(bootstrapFile.getAbsolutePath(), nodeName);
    if (!m_driverToLocalServer.isInitialized()) {

        int cachePages = getCachePages();
        m_driverToLocalServer.init(cachePages);
        logger.info("Initialize xDB driver to local server with cache pages: " + String.valueOf(cachePages));
    }//from   w  ww .  j a v  a2  s . c om
    if (logger.isDebugEnabled())
        m_driverToLocalServer.setStoreStackTraceInLock(true);
    ServerSocket socket;
    ServerSocketFactory factory = ServerSocketFactory.getDefault();
    int port = getPortFromXhiveFederation(nodeName);
    try {
        socket = factory.createServerSocket(port, 0);
        m_driverToLocalServer.startListenerThread(socket);
    } catch (UnknownHostException e) {
        throw new XDBEngineException("startDatabase UnknownHostException:" + e);
    } catch (IOException e) {
        throw new XDBEngineException("startDatabase IOException:" + e);
    }

    if (XhiveFederationIf.PRIMARY_NODE_NAME.equals(nodeName)) {
        initializeDriverForPrimaryServer();
        try {
            setKeepXDBTransactionalLog(false);
        } catch (Throwable t) {
            logger.warn("Failed to synchronize keep-xdb-transactional-log option", t);
        }
    }
    m_started = true;
}

From source file:ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection.java

SocketFactory getSocketFactory() {
    return new SocketFactory() {

        @Override//w ww.  j  a  va  2  s .  c  o m
        public Socket createTlsSocket() throws IOException {
            try {
                if (getTransport() == TransportStyleEnum.HL7_OVER_HTTP && getTlsKeystore() != null) {
                    return createHohSocketFactory().createClientSocket();
                }
            } catch (KeyStoreException e) {
                throw new IOException(e.getMessage(), e);
            }
            return SSLSocketFactory.getDefault().createSocket();
        }

        @Override
        public ServerSocket createTlsServerSocket() throws IOException {
            try {
                if (getTransport() == TransportStyleEnum.HL7_OVER_HTTP && getHohSignatureKeystore_() != null) {
                    return createHohSocketFactory().createServerSocket();
                }
            } catch (KeyStoreException e) {
                throw new IOException(e.getMessage(), e);
            }
            return SSLServerSocketFactory.getDefault().createServerSocket();
        }

        private CustomCertificateTlsSocketFactory createHohSocketFactory() throws KeyStoreException {
            KeyStore keystore = getTlsKeystore();
            String keystorePassword = getTlsKeystorePassword();
            CustomCertificateTlsSocketFactory sf = new CustomCertificateTlsSocketFactory(keystore,
                    keystorePassword);
            return sf;
        }

        @Override
        public Socket createSocket() throws IOException {
            return javax.net.SocketFactory.getDefault().createSocket();
        }

        @Override
        public ServerSocket createServerSocket() throws IOException {
            return ServerSocketFactory.getDefault().createServerSocket();
        }

        @Override
        public void configureNewAcceptedSocket(Socket theSocket) throws SocketException {
            // nothing
        }
    };
}

From source file:com.atomicleopard.thundr.ftp.commons.SocketClient.java

/**
 * Sets the ServerSocketFactory used by the SocketClient to open ServerSocket
 * connections.  If the factory value is null, then a default
 * factory is used (only do this to reset the factory after having
 * previously altered it)./*  ww w . jav  a  2 s. co m*/
 * <p>
 * @param factory  The new ServerSocketFactory the SocketClient should use.
 * @since 2.0
 */
public void setServerSocketFactory(ServerSocketFactory factory) {
    if (factory == null) {
        _serverSocketFactory_ = ServerSocketFactory.getDefault();
    } else {
        _serverSocketFactory_ = factory;
    }
}

From source file:net.timewalker.ffmq4.jmx.rmi.JMXOverRMIServerSocketFactory.java

private synchronized ServerSocketFactory getSocketFactory() {
    if (socketFactory == null)
        socketFactory = ServerSocketFactory.getDefault();

    return socketFactory;
}

From source file:org.asynchttpclient.test.TestUtils.java

public static synchronized int findFreePort() throws IOException {
    try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(0)) {
        return socket.getLocalPort();
    }/*from w ww. j  a va  2 s  .  co m*/
}