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 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 /** Returns Returns a bound socket; be sure to close when done. */ 057 private ServerSocket get(String hostname, Supplier<Integer> randomPortMaker) { 058 ServerSocket ss = null; 059 int port = -1; 060 while (true) { 061 port = randomPortMaker.get(); 062 try { 063 ss = new ServerSocket(); 064 ss.bind(new InetSocketAddress(hostname, port)); 065 break; 066 } catch (IOException ioe) { 067 LOG.warn("Failed bind", ioe); 068 try { 069 ss.close(); 070 } catch (IOException ioe2) { 071 LOG.warn("FAILED CLOSE of failed bind socket", ioe2); 072 } 073 } 074 } 075 return ss; 076 } 077 078 @Override 079 public void close() throws IOException { 080 if (this.socket != null) { 081 this.socket.close(); 082 } 083 } 084}