HADOOP-14850. Read HttpServer2 resources directly from the source tree (if exists). Contributed by Elek, Marton.

This commit is contained in:
Anu Engineer 2017-09-08 10:01:01 -07:00
parent 1f53ae7972
commit e8278b02a4

View File

@ -27,6 +27,7 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.net.BindException; import java.net.BindException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@ -993,14 +994,31 @@ public WebAppContext getWebAppContext(){
* Get the pathname to the webapps files. * Get the pathname to the webapps files.
* @param appName eg "secondary" or "datanode" * @param appName eg "secondary" or "datanode"
* @return the pathname as a URL * @return the pathname as a URL
* @throws FileNotFoundException if 'webapps' directory cannot be found on CLASSPATH. * @throws FileNotFoundException if 'webapps' directory cannot be found
* on CLASSPATH or in the development location.
*/ */
protected String getWebAppsPath(String appName) throws FileNotFoundException { protected String getWebAppsPath(String appName) throws FileNotFoundException {
URL url = getClass().getClassLoader().getResource("webapps/" + appName); URL resourceUrl = null;
if (url == null) File webResourceDevLocation = new File("src/main/webapps", appName);
throw new FileNotFoundException("webapps/" + appName if (webResourceDevLocation.exists()) {
+ " not found in CLASSPATH"); LOG.info("Web server is in development mode. Resources "
String urlString = url.toString(); + "will be read from the source tree.");
try {
resourceUrl = webResourceDevLocation.getParentFile().toURI().toURL();
} catch (MalformedURLException e) {
throw new FileNotFoundException("Mailformed URL while finding the "
+ "web resource dir:" + e.getMessage());
}
} else {
resourceUrl =
getClass().getClassLoader().getResource("webapps/" + appName);
if (resourceUrl == null) {
throw new FileNotFoundException("webapps/" + appName +
" not found in CLASSPATH");
}
}
String urlString = resourceUrl.toString();
return urlString.substring(0, urlString.lastIndexOf('/')); return urlString.substring(0, urlString.lastIndexOf('/'));
} }