Skip to content

Commit 5ea61d1

Browse files
author
Vladimir Kotal
authored
fail early if web app URI is not reachable (#3520)
1 parent 5631d33 commit 5ea61d1

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.opengrok.indexer.logger.LoggerUtil;
8080
import org.opengrok.indexer.util.CtagsUtil;
8181
import org.opengrok.indexer.util.Executor;
82+
import org.opengrok.indexer.util.HostUtil;
8283
import org.opengrok.indexer.util.OptionParser;
8384
import org.opengrok.indexer.util.PlatformUtils;
8485
import org.opengrok.indexer.util.Statistics;
@@ -161,9 +162,18 @@ public static void main(String[] argv) {
161162

162163
boolean createDict = false;
163164

165+
int CONNECT_TIMEOUT = 1000; // in milliseconds
166+
164167
try {
165168
argv = parseOptions(argv);
166169

170+
if (webappURI != null) {
171+
if (!HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
172+
System.err.println(webappURI + " is not reachable.");
173+
System.exit(1);
174+
}
175+
}
176+
167177
/*
168178
* Attend to disabledRepositories here in case exitWithHelp() will
169179
* need to report about repos.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)