Java Code Examples for java.net.BindException#initCause()

The following examples show how to use java.net.BindException#initCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpServer2.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Create bind exception by wrapping the bind exception thrown.
 * @param listener
 * @param ex
 * @return
 */
private static BindException constructBindException(ServerConnector listener,
    IOException ex) {
  BindException be = new BindException("Port in use: "
      + listener.getHost() + ":" + listener.getPort());
  if (ex != null) {
    be.initCause(ex);
  }
  return be;
}
 
Example 2
Source File: HttpServer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
void openListener() throws Exception {
  if (listener.getLocalPort() != -1) { // it's already bound
    return;
  }
  if (listenerStartedExternally) { // Expect that listener was started securely
    throw new Exception("Expected webserver's listener to be started " +
        "previously but wasn't");
  }
  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();
      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);
  }
}
 
Example 3
Source File: HttpServer2.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
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);
    }
  }
}
 
Example 4
Source File: HttpServer.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
void openListener() throws Exception {
  if (listener.getLocalPort() != -1) { // it's already bound
    return;
  }
  if (listenerStartedExternally) { // Expect that listener was started securely
    throw new Exception("Expected webserver's listener to be started " +
        "previously but wasn't");
  }
  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();
      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);
  }
}
 
Example 5
Source File: HttpServer2.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
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);
    }
  }
}
 
Example 6
Source File: HttpServer2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Create bind exception by wrapping the bind exception thrown.
 * @param listener listener to check
 * @param ex exception to check
 * @return returns the exception
 */
private static BindException constructBindException(ServerConnector listener,
                                                    BindException ex) {
  BindException be = new BindException("Port in use: "
      + listener.getHost() + ":" + listener.getPort());
  if (ex != null) {
    be.initCause(ex);
  }
  return be;
}
 
Example 7
Source File: HttpServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception if the listener cannot be opened or the appropriate port is already in use
 */
@VisibleForTesting
void openListeners() throws Exception {
  for (ListenerInfo li : listeners) {
    ServerConnector listener = li.listener;
    if (!li.isManaged || (li.listener.getLocalPort() != -1 && li.listener.getLocalPort() != -2)) {
      // This listener is either started externally, or has not been opened, or has been closed
      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 (IOException ex) {
        if(!(ex instanceof BindException) && !(ex.getCause() instanceof BindException)) {
          throw 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);
    }
  }
}
 
Example 8
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
/**
 * Create bind exception by wrapping the bind exception thrown.
 * @param listener listener to check
 * @param ex exception to check
 * @return returns the exception
 */
private static BindException constructBindException(ServerConnector listener,
                                                    BindException ex) {
  BindException be = new BindException("Port in use: "
                                           + listener.getHost() + ":" + listener.getPort());
  if (ex != null) {
    be.initCause(ex);
  }
  return be;
}
 
Example 9
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
/**
 * Create bind exception by wrapping the bind exception thrown.
 * @param listener listener to check
 * @param ex exception to check
 * @return returns the exception
 */
private static BindException constructBindException(ServerConnector listener,
                                                    BindException ex) {
  BindException be = new BindException("Port in use: "
                                           + listener.getHost() + ":" + listener.getPort());
  if (ex != null) {
    be.initCause(ex);
  }
  return be;
}
 
Example 10
Source File: JIoEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void bind() throws Exception {

    // Initialize thread count defaults for acceptor
    if (acceptorThreadCount == 0) {
        acceptorThreadCount = 1;
    }
    // Initialize maxConnections
    if (getMaxConnections() == 0) {
        // User hasn't set a value - use the default
        setMaxConnections(getMaxThreadsExecutor(true));
    }

    if (serverSocketFactory == null) {
        if (isSSLEnabled()) {
            serverSocketFactory =
                handler.getSslImplementation().getServerSocketFactory(this);
        } else {
            serverSocketFactory = new DefaultServerSocketFactory(this);
        }
    }

    if (serverSocket == null) {
        try {
            if (getAddress() == null) {
                serverSocket = serverSocketFactory.createSocket(getPort(),
                        getBacklog());
            } else {
                serverSocket = serverSocketFactory.createSocket(getPort(),
                        getBacklog(), getAddress());
            }
        } catch (BindException orig) {
            String msg;
            if (getAddress() == null)
                msg = orig.getMessage() + " <null>:" + getPort();
            else
                msg = orig.getMessage() + " " +
                        getAddress().toString() + ":" + getPort();
            BindException be = new BindException(msg);
            be.initCause(orig);
            throw be;
        }
    }

}
 
Example 11
Source File: JIoEndpoint.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void bind() throws Exception {

    // Initialize thread count defaults for acceptor
    if (acceptorThreadCount == 0) {
        acceptorThreadCount = 1;
    }
    // Initialize maxConnections
    if (getMaxConnections() == 0) {
        // User hasn't set a value - use the default
        setMaxConnections(getMaxThreadsInternal());
    }

    if (serverSocketFactory == null) {
        if (isSSLEnabled()) {
            serverSocketFactory =
                handler.getSslImplementation().getServerSocketFactory(this);
        } else {
            serverSocketFactory = new DefaultServerSocketFactory(this);
        }
    }

    if (serverSocket == null) {
        try {
            if (getAddress() == null) {
                serverSocket = serverSocketFactory.createSocket(getPort(),
                        getBacklog());
            } else {
                serverSocket = serverSocketFactory.createSocket(getPort(),
                        getBacklog(), getAddress());
            }
        } catch (BindException orig) {
            String msg;
            if (getAddress() == null)
                msg = orig.getMessage() + " <null>:" + getPort();
            else
                msg = orig.getMessage() + " " +
                        getAddress().toString() + ":" + getPort();
            BindException be = new BindException(msg);
            be.initCause(orig);
            throw be;
        }
    }

}