private void bindAddress(final InetAddress hostAddress) { PortsRange portsRange = new PortsRange(port); final AtomicReference<Exception> lastException = new AtomicReference<>(); final AtomicReference<SocketAddress> boundSocket = new AtomicReference<>(); boolean success = portsRange.iterate( new PortsRange.PortCallback() { @Override public boolean onPortNumber(int portNumber) { try { synchronized (serverChannels) { Channel channel = serverBootstrap.bind(new InetSocketAddress(hostAddress, portNumber)); serverChannels.add(channel); boundSocket.set(channel.getLocalAddress()); } } catch (Exception e) { lastException.set(e); return false; } return true; } }); if (!success) { throw new BindHttpException("Failed to bind to [" + port + "]", lastException.get()); } logger.info("Bound http to address [{}]", boundSocket.get()); }
@Override protected void doStart() throws ElasticsearchException { // Bind and start to accept incoming connections. InetAddress hostAddressX; try { hostAddressX = networkService.resolveBindHostAddress(bindHost); } catch (IOException e) { throw new BindHttpException("Failed to resolve host [" + bindHost + "]", e); } final InetAddress hostAddress = hostAddressX; InetAddress publishAddressHostX; try { publishAddressHostX = networkService.resolvePublishHostAddress(publishHost); } catch (IOException e) { throw new BindHttpException( "Failed to resolve publish address host [" + publishHost + "]", e); } final InetAddress publishAddressHost = publishAddressHostX; capiBehavior = new ElasticSearchCAPIBehavior(client, logger, bucketUUIDCache, pluginSettings); couchbaseBehavior = new ElasticSearchCouchbaseBehavior(client, logger, bucketUUIDCache, pluginSettings); PortsRange portsRange = new PortsRange(port); final AtomicReference<Exception> lastException = new AtomicReference<Exception>(); boolean success = portsRange.iterate( new PortsRange.PortCallback() { @Override public boolean onPortNumber(int portNumber) { try { server = new CAPIServer( capiBehavior, couchbaseBehavior, new InetSocketAddress(hostAddress, portNumber), CouchbaseCAPITransportImpl.this.username, CouchbaseCAPITransportImpl.this.password, numVbuckets); if (publishAddressHost != null) { server.setPublishAddress(publishAddressHost); } server.start(); } catch (Exception e) { lastException.set(e); return false; } return true; } }); if (!success) { throw new BindHttpException("Failed to bind to [" + port + "]", lastException.get()); } InetSocketAddress boundAddress = server.getBindAddress(); InetSocketAddress publishAddress = new InetSocketAddress(publishAddressHost, boundAddress.getPort()); this.boundAddress = new BoundTransportAddress( new InetSocketTransportAddress(boundAddress), new InetSocketTransportAddress(publishAddress)); }