001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.net;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.net.InetAddress;
023import java.net.InetSocketAddress;
024import java.net.ServerSocket;
025import java.util.function.Supplier;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Utility to generate a bound socket. Useful testing for BindException.
031 * Use one of the Constructors to create an instance of this class. On creation it will have put
032 * up a ServerSocket on a random port. Get the port it is bound to using {@link #getPort()}. In
033 * your test, then try to start a Server using same port to generate a BindException. Call
034 * {@link #close()} when done to shut down the Socket.
035 */
036public final class BoundSocketMaker implements Closeable {
037  private static final Logger LOG = LoggerFactory.getLogger(BoundSocketMaker.class);
038  private final ServerSocket socket;
039
040  private BoundSocketMaker() {
041    this.socket = null;
042  }
043
044  public BoundSocketMaker(Supplier<Integer> randomPortMaker) {
045    this(InetAddress.getLoopbackAddress().getHostName(), randomPortMaker);
046  }
047
048  public BoundSocketMaker(final String hostname, Supplier<Integer> randomPortMaker) {
049    this.socket = get(hostname, randomPortMaker);
050  }
051
052  public int getPort() {
053    return this.socket.getLocalPort();
054  }
055
056  /**
057   * @return Returns a bound socket; be sure to close when done.
058   */
059  private ServerSocket get(String hostname, Supplier<Integer> randomPortMaker) {
060    ServerSocket ss = null;
061    int port = -1;
062    while (true) {
063      port = randomPortMaker.get();
064      try {
065        ss = new ServerSocket();
066        ss.bind(new InetSocketAddress(hostname, port));
067        break;
068      } catch (IOException ioe) {
069        LOG.warn("Failed bind", ioe);
070        try {
071          ss.close();
072        } catch (IOException ioe2) {
073          LOG.warn("FAILED CLOSE of failed bind socket", ioe2);
074        }
075      }
076    }
077    return ss;
078  }
079
080  @Override public void close() throws IOException {
081    if (this.socket != null) {
082      this.socket.close();
083    }
084  }
085}