Example usage for java.net BindException initCause

List of usage examples for java.net BindException initCause

Introduction

In this page you can find the example usage for java.net BindException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:com.chiralBehaviors.slp.hive.hardtack.configuration.AggregatorConfiguration.java

@Override
public Engine construct() throws IOException {
    NetworkInterface intf = getNetworkInterface();
    DatagramSocket socket;/*from   w  ww  .j a v a  2 s  . c om*/
    InetSocketAddress address;
    if (endpoint.getAddress().isAnyLocalAddress()) {
        address = new InetSocketAddress(Utils.getAddress(intf, ipv4), endpoint.getPort());
    } else {
        address = endpoint;
    }
    try {
        socket = new DatagramSocket(address);
    } catch (BindException e) {
        BindException bindException = new BindException(String.format("Cannot bind to %s", address));
        bindException.initCause(e);
        throw bindException;
    }
    return new AggregatorEngine(socket, getMac(), Executors.newSingleThreadScheduledExecutor(), getFdFactory());
}

From source file:com.taobao.adfs.distributed.rpc.Server.java

/**
 * A convenience method to bind to a given address and report better exceptions if the address is not a valid host.
 * //from   www .j ava2 s .com
 * @param socket
 *          the socket to bind
 * @param address
 *          the address to bind to
 * @param backlog
 *          the number of connections allowed in the queue
 * @throws BindException
 *           if the address can't be bound
 * @throws UnknownHostException
 *           if the address isn't a valid host name
 * @throws IOException
 *           other random errors from bind
 */
public static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        BindException bindException = new BindException(
                "Problem binding to " + address + " : " + e.getMessage());
        bindException.initCause(e);
        throw bindException;
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        } else {
            throw e;
        }
    }
}

From source file:com.hortonworks.hbase.replication.bridge.HBaseServer.java

/**
 * A convenience method to bind to a given address and report
 * better exceptions if the address is not a valid host.
 * @param socket the socket to bind//from  www .j av  a  2  s .  c  o m
 * @param address the address to bind to
 * @param backlog the number of connections allowed in the queue
 * @throws BindException if the address can't be bound
 * @throws UnknownHostException if the address isn't a valid host name
 * @throws IOException other random errors from bind
 */
public static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        BindException bindException = new BindException(
                "Problem binding to " + address + " : " + e.getMessage());
        bindException.initCause(e);
        throw bindException;
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        }
        throw e;
    }
}

From source file:org.apache.geode.internal.net.SocketCreator.java

public ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr,
        List<GatewayTransportFilter> transportFilters, int socketBufferSize) throws IOException {
    if (transportFilters.isEmpty()) {
        return createServerSocket(nport, backlog, bindAddr, socketBufferSize);
    } else {//from w  ww.  j  a v a 2  s .  com
        printConfig();
        ServerSocket result = new TransportFilterServerSocket(transportFilters);
        result.setReuseAddress(true);
        // Set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        result.setReceiveBufferSize(socketBufferSize);
        try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
        } catch (BindException e) {
            BindException throwMe = new BindException(
                    LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
                            .toLocalizedString(new Object[] { bindAddr, Integer.valueOf(nport) }));
            throwMe.initCause(e);
            throw throwMe;
        }
        return result;
    }
}

From source file:org.apache.geode.internal.net.SocketCreator.java

private ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr, int socketBufferSize,
        boolean sslConnection) throws IOException {
    printConfig();//from ww w.jav a2 s.c o  m
    if (sslConnection) {
        if (this.sslContext == null) {
            throw new GemFireConfigException("SSL not configured correctly, Please look at previous error");
        }
        ServerSocketFactory ssf = this.sslContext.getServerSocketFactory();
        SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket();
        serverSocket.setReuseAddress(true);
        // If necessary, set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        if (socketBufferSize != -1) {
            serverSocket.setReceiveBufferSize(socketBufferSize);
        }
        serverSocket.bind(new InetSocketAddress(bindAddr, nport), backlog);
        finishServerSocket(serverSocket);
        return serverSocket;
    } else {
        // log.info("Opening server socket on " + nport, new Exception("SocketCreation"));
        ServerSocket result = new ServerSocket();
        result.setReuseAddress(true);
        // If necessary, set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        if (socketBufferSize != -1) {
            result.setReceiveBufferSize(socketBufferSize);
        }
        try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
        } catch (BindException e) {
            BindException throwMe = new BindException(
                    LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
                            .toLocalizedString(new Object[] { bindAddr, Integer.valueOf(nport) }));
            throwMe.initCause(e);
            throw throwMe;
        }
        return result;
    }
}

From source file:org.apache.hadoop.hbase.http.HttpServer.java

/**
 * Open the main listener for the server
 * @throws Exception/* w  w w  .  j a  v  a2s.  c  o  m*/
 */
void openListeners() throws Exception {
    for (ListenerInfo li : listeners) {
        Connector listener = li.listener;
        if (!li.isManaged || li.listener.getLocalPort() != -1) {
            // This listener is either started externally or has been bound
            continue;
        }
        int port = listener.getPort();
        while (true) {
            // jetty has a bug where you can't reopen a listener that previously
            // failed to open w/o issuing a close first, even if the port is changed
            try {
                listener.close();
                listener.open();
                LOG.info("Jetty bound to port " + listener.getLocalPort());
                break;
            } catch (BindException ex) {
                if (port == 0 || !findPort) {
                    BindException be = new BindException(
                            "Port in use: " + listener.getHost() + ":" + listener.getPort());
                    be.initCause(ex);
                    throw be;
                }
            }
            // try the next port number
            listener.setPort(++port);
            Thread.sleep(100);
        }
    }
}

From source file:org.apache.hadoop.http.HttpServer2.java

/**
 * Open the main listener for the server
 * @throws Exception//w  w  w . j a  va2s  .c o  m
 */
void openListeners() throws Exception {
    for (Connector listener : listeners) {
        if (listener.getLocalPort() != -1) {
            // This listener is either started externally or has been bound
            continue;
        }
        int port = listener.getPort();
        while (true) {
            // jetty has a bug where you can't reopen a listener that previously
            // failed to open w/o issuing a close first, even if the port is changed
            try {
                listener.close();
                listener.open();
                LOG.info("Jetty bound to port " + listener.getLocalPort());
                break;
            } catch (BindException ex) {
                if (port == 0 || !findPort) {
                    BindException be = new BindException(
                            "Port in use: " + listener.getHost() + ":" + listener.getPort());
                    be.initCause(ex);
                    throw be;
                }
            }
            // try the next port number
            listener.setPort(++port);
            Thread.sleep(100);
        }
    }
}

From source file:org.apache.tajo.webapp.HttpServer.java

/**
 * Start the server. Does not wait for the server to start.
 *//*ww  w  .jav  a  2s.c  o  m*/
public void start() throws IOException {
    try {
        if (listenerStartedExternally) { // Expect that listener was started
                                         // securely
            if (listener.getLocalPort() == -1) // ... and verify
                throw new Exception("Exepected webserver's listener to be started " + "previously but wasn't");
            // And skip all the port rolling issues.
            webServer.start();
        } else {
            int port;
            int oriPort = listener.getPort(); // The original requested port
            while (true) {
                try {
                    port = webServer.getConnectors()[0].getLocalPort();
                    LOG.debug("Port returned by webServer.getConnectors()[0]."
                            + "getLocalPort() before open() is " + port + ". Opening the listener on "
                            + oriPort);
                    listener.open();
                    port = listener.getLocalPort();
                    LOG.debug("listener.getLocalPort() returned " + listener.getLocalPort()
                            + " webServer.getConnectors()[0].getLocalPort() returned "
                            + webServer.getConnectors()[0].getLocalPort());
                    // Workaround to handle the problem reported in HADOOP-4744
                    if (port < 0) {
                        Thread.sleep(100);
                        int numRetries = 1;
                        while (port < 0) {
                            LOG.warn("listener.getLocalPort returned " + port);
                            if (numRetries++ > MAX_RETRIES) {
                                throw new Exception(" listener.getLocalPort is returning "
                                        + "less than 0 even after " + numRetries + " resets");
                            }
                            for (int i = 0; i < 2; i++) {
                                LOG.info("Retrying listener.getLocalPort()");
                                port = listener.getLocalPort();
                                if (port > 0) {
                                    break;
                                }
                                Thread.sleep(200);
                            }
                            if (port > 0) {
                                break;
                            }
                            LOG.info("Bouncing the listener");
                            listener.close();
                            Thread.sleep(1000);
                            listener.setPort(oriPort == 0 ? 0 : (oriPort += 1));
                            listener.open();
                            Thread.sleep(100);
                            port = listener.getLocalPort();
                        }
                    } // Workaround end
                    LOG.info("Jetty bound to port " + port);
                    webServer.start();
                    break;
                } catch (IOException ex) {
                    // if this is a bind exception,
                    // then try the next port number.
                    if (ex instanceof BindException) {
                        if (!findPort) {
                            BindException be = new BindException(
                                    "Port in use: " + listener.getHost() + ":" + listener.getPort());
                            be.initCause(ex);
                            throw be;
                        }
                    } else {
                        LOG.info("HttpServer.start() threw a non Bind IOException");
                        throw ex;
                    }
                } catch (MultiException ex) {
                    LOG.info("HttpServer.start() threw a MultiException");
                    throw ex;
                }
                listener.setPort((oriPort += 1));
            }
        }
        // Make sure there is no handler failures.
        Handler[] handlers = webServer.getHandlers();
        for (Handler handler : handlers) {
            if (handler.isFailed()) {
                throw new IOException("Problem in starting http server. Server handlers failed");
            }
        }
    } catch (IOException e) {
        throw e;
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException("Interrupted while starting HTTP server").initCause(e);
    } catch (Exception e) {
        throw new IOException("Problem starting http server", e);
    }
}

From source file:tajo.webapp.HttpServer.java

/**
 * Start the server. Does not wait for the server to start.
 *///from   www. ja  v  a2 s.c  o  m
public void start() throws IOException {
    try {
        if (listenerStartedExternally) { // Expect that listener was started
                                         // securely
            if (listener.getLocalPort() == -1) // ... and verify
                throw new Exception("Exepected webserver's listener to be started " + "previously but wasn't");
            // And skip all the port rolling issues.
            webServer.start();
        } else {
            int port = 0;
            int oriPort = listener.getPort(); // The original requested port
            while (true) {
                try {
                    port = webServer.getConnectors()[0].getLocalPort();
                    LOG.debug("Port returned by webServer.getConnectors()[0]."
                            + "getLocalPort() before open() is " + port + ". Opening the listener on "
                            + oriPort);
                    listener.open();
                    port = listener.getLocalPort();
                    LOG.debug("listener.getLocalPort() returned " + listener.getLocalPort()
                            + " webServer.getConnectors()[0].getLocalPort() returned "
                            + webServer.getConnectors()[0].getLocalPort());
                    // Workaround to handle the problem reported in HADOOP-4744
                    if (port < 0) {
                        Thread.sleep(100);
                        int numRetries = 1;
                        while (port < 0) {
                            LOG.warn("listener.getLocalPort returned " + port);
                            if (numRetries++ > MAX_RETRIES) {
                                throw new Exception(" listener.getLocalPort is returning "
                                        + "less than 0 even after " + numRetries + " resets");
                            }
                            for (int i = 0; i < 2; i++) {
                                LOG.info("Retrying listener.getLocalPort()");
                                port = listener.getLocalPort();
                                if (port > 0) {
                                    break;
                                }
                                Thread.sleep(200);
                            }
                            if (port > 0) {
                                break;
                            }
                            LOG.info("Bouncing the listener");
                            listener.close();
                            Thread.sleep(1000);
                            listener.setPort(oriPort == 0 ? 0 : (oriPort += 1));
                            listener.open();
                            Thread.sleep(100);
                            port = listener.getLocalPort();
                        }
                    } // Workaround end
                    LOG.info("Jetty bound to port " + port);
                    webServer.start();
                    break;
                } catch (IOException ex) {
                    // if this is a bind exception,
                    // then try the next port number.
                    if (ex instanceof BindException) {
                        if (!findPort) {
                            BindException be = new BindException(
                                    "Port in use: " + listener.getHost() + ":" + listener.getPort());
                            be.initCause(ex);
                            throw be;
                        }
                    } else {
                        LOG.info("HttpServer.start() threw a non Bind IOException");
                        throw ex;
                    }
                } catch (MultiException ex) {
                    LOG.info("HttpServer.start() threw a MultiException");
                    throw ex;
                }
                listener.setPort((oriPort += 1));
            }
        }
        // Make sure there is no handler failures.
        Handler[] handlers = webServer.getHandlers();
        for (int i = 0; i < handlers.length; i++) {
            if (handlers[i].isFailed()) {
                throw new IOException("Problem in starting http server. Server handlers failed");
            }
        }
    } catch (IOException e) {
        throw e;
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException("Interrupted while starting HTTP server").initCause(e);
    } catch (Exception e) {
        throw new IOException("Problem starting http server", e);
    }
}