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. Use one of the Constructors 031 * to create an instance of this class. On creation it will have put up a ServerSocket on a random 032 * port. Get the port it is bound to using {@link #getPort()}. In your test, then try to start a 033 * Server using same port to generate a BindException. Call {@link #close()} when done to shut down 034 * 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 public BoundSocketMaker(Supplier<Integer> randomPortMaker) { 041 this(InetAddress.getLoopbackAddress().getHostName(), randomPortMaker); 042 } 043 044 public BoundSocketMaker(final String hostname, Supplier<Integer> randomPortMaker) { 045 this.socket = get(hostname, randomPortMaker); 046 } 047 048 public int getPort() { 049 return this.socket.getLocalPort(); 050 } 051 052 /** Returns Returns a bound socket; be sure to close when done. */ 053 private ServerSocket get(String hostname, Supplier<Integer> randomPortMaker) { 054 ServerSocket ss = null; 055 int port = -1; 056 while (true) { 057 port = randomPortMaker.get(); 058 try { 059 ss = new ServerSocket(); 060 ss.bind(new InetSocketAddress(hostname, port)); 061 break; 062 } catch (IOException ioe) { 063 LOG.warn("Failed bind", ioe); 064 try { 065 ss.close(); 066 } catch (IOException ioe2) { 067 LOG.warn("FAILED CLOSE of failed bind socket", ioe2); 068 } 069 } 070 } 071 return ss; 072 } 073 074 @Override 075 public void close() throws IOException { 076 if (this.socket != null) { 077 this.socket.close(); 078 } 079 } 080}