|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opengrok.indexer.util; |
| 24 | + |
| 25 | +import org.opengrok.indexer.logger.LoggerFactory; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.InetAddress; |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.net.Socket; |
| 31 | +import java.net.URI; |
| 32 | +import java.net.URISyntaxException; |
| 33 | +import java.net.UnknownHostException; |
| 34 | +import java.util.logging.Level; |
| 35 | +import java.util.logging.Logger; |
| 36 | + |
| 37 | +/** |
| 38 | + * Utility class to provide simple host/address methods. |
| 39 | + */ |
| 40 | +public class HostUtil { |
| 41 | + |
| 42 | + private static final Logger LOGGER = LoggerFactory.getLogger(HostUtil.class); |
| 43 | + |
| 44 | + private HostUtil() { |
| 45 | + // private to enforce static |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param urlStr URI |
| 50 | + * @return port number |
| 51 | + * @throws URISyntaxException on error |
| 52 | + */ |
| 53 | + public static int urlToPort(String urlStr) throws URISyntaxException { |
| 54 | + URI uri = new URI(urlStr); |
| 55 | + return uri.getPort(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param urlStr URI |
| 60 | + * @return hostname |
| 61 | + * @throws URISyntaxException on error |
| 62 | + */ |
| 63 | + public static String urlToHostname(String urlStr) throws URISyntaxException { |
| 64 | + URI uri = new URI(urlStr); |
| 65 | + return uri.getHost(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param addr IP address |
| 70 | + * @param port port number |
| 71 | + * @param timeOutMillis timeout in milliseconds |
| 72 | + * @return true if TCP connect works, false otherwise |
| 73 | + */ |
| 74 | + public static boolean isReachable(InetAddress addr, int port, int timeOutMillis) { |
| 75 | + try (Socket soc = new Socket()) { |
| 76 | + soc.connect(new InetSocketAddress(addr, port), timeOutMillis); |
| 77 | + } catch (IOException e) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + public static boolean isReachable(String webappURI, int timeOutMillis) { |
| 85 | + boolean connectWorks = false; |
| 86 | + |
| 87 | + try { |
| 88 | + int port = HostUtil.urlToPort(webappURI); |
| 89 | + if (port <= 0) { |
| 90 | + LOGGER.log(Level.SEVERE, "invalid port number for " + webappURI); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + for (InetAddress addr : InetAddress.getAllByName(HostUtil.urlToHostname(webappURI))) { |
| 95 | + if (HostUtil.isReachable(addr, port, timeOutMillis)) { |
| 96 | + LOGGER.log(Level.FINE, "URI " + webappURI + " is reachable via " + addr.toString()); |
| 97 | + connectWorks = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } catch (URISyntaxException | UnknownHostException e) { |
| 102 | + LOGGER.log(Level.WARNING, String.format("URI not valid: %s", webappURI), e); |
| 103 | + } |
| 104 | + |
| 105 | + return connectWorks; |
| 106 | + } |
| 107 | +} |
0 commit comments