Commit df5dde37 authored by Mathieu Lirzin's avatar Mathieu Lirzin

Improved: Replace getLocation with location in ‘ComponentConfig.WebappInfo’

(OFBIZ-11192)

Use Path instead of String + File.
parent 4d4a1ba1
......@@ -285,7 +285,7 @@ checkstyle {
// the sum of errors that were present before introducing the
// ‘checkstyle’ tool present in the framework and in the official
// plugins.
tasks.checkstyleMain.maxErrors = 37729
tasks.checkstyleMain.maxErrors = 37725
// Currently there are a lot of errors so we need to temporarily
// hide them to avoid polluting the terminal output.
showViolations = false
......
......@@ -1181,8 +1181,8 @@ public final class ComponentConfig {
return initParameters;
}
public String getLocation() {
return componentConfig.rootLocation().resolve(location).toString();
public Path location() {
return componentConfig.rootLocation().resolve(location);
}
public String getName() {
......
......@@ -469,7 +469,8 @@ public class CatalinaContainer implements Container {
/* webapp is loaded already (overridden). Therefore, disable
* app bar display on overridden apps and do not load */
appInfo.setAppBarDisplay(false);
Debug.logInfo("Duplicate webapp mount (overridding); not loading : " + appInfo.getName() + " / " + appInfo.getLocation(), module);
Debug.logInfo("Duplicate webapp mount (overridding); not loading : "
+ appInfo.getName() + " / " + appInfo.location(), module);
}
}
ExecutionPool.getAllFutures(futures);
......
......@@ -18,12 +18,14 @@
*******************************************************************************/
package org.apache.ofbiz.webapp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.servlet.ServletContext;
......@@ -64,8 +66,8 @@ public final class WebAppUtil {
public static final String module = WebAppUtil.class.getName();
public static final String CONTROL_MOUNT_POINT = "control";
private static final String webAppFileName = "/WEB-INF/web.xml";
private static final UtilCache<String, WebXml> webXmlCache = UtilCache.createUtilCache("webapp.WebXml");
private static final Path webAppFileName = Paths.get("WEB-INF", "web.xml");
private static final UtilCache<Path, WebXml> webXmlCache = UtilCache.createUtilCache("webapp.WebXml");
/**
* Returns the control servlet path. The path consists of the web application's mount-point
......@@ -94,7 +96,8 @@ public final class WebAppUtil {
}
}
if (servletMapping == null) {
throw new IllegalArgumentException("org.apache.ofbiz.webapp.control.ControlServlet mapping not found in " + webAppInfo.getLocation() + webAppFileName);
throw new IllegalArgumentException("org.apache.ofbiz.webapp.control.ControlServlet mapping not found in "
+ webAppInfo.location().resolve(webAppFileName));
}
servletMapping = servletMapping.replace("*", "");
String servletPath = webAppInfo.contextRoot.concat(servletMapping);
......@@ -233,41 +236,41 @@ public final class WebAppUtil {
*/
private static WebXml getWebXml(WebappInfo webAppInfo) throws IOException, SAXException {
Assert.notNull("webAppInfo", webAppInfo);
String webXmlFileLocation = webAppInfo.getLocation().concat(webAppFileName);
Path webXmlFileLocation = webAppInfo.location().resolve(webAppFileName);
return parseWebXmlFile(webXmlFileLocation, true);
}
/**
* Parses the specified <code>web.xml</code> file into a <code>WebXml</code> instance.
*
* @param webXmlFileLocation
* @param webXmlLocation
* @param validate
* @throws IOException
* @throws SAXException
*/
private static WebXml parseWebXmlFile(String webXmlFileLocation, boolean validate) throws IOException, SAXException {
Assert.notEmpty("webXmlFileLocation", webXmlFileLocation);
WebXml result = webXmlCache.get(webXmlFileLocation);
private static WebXml parseWebXmlFile(Path webXmlLocation, boolean validate) throws IOException, SAXException {
Objects.requireNonNull(webXmlLocation, "webXmlFileLocation");
WebXml result = webXmlCache.get(webXmlLocation);
if (result == null) {
File file = new File(webXmlFileLocation);
if (!file.exists()) {
throw new IllegalArgumentException(webXmlFileLocation + " does not exist.");
if (Files.notExists(webXmlLocation)) {
throw new IllegalArgumentException(webXmlLocation + " does not exist.");
}
boolean namespaceAware = true;
result = new WebXml();
LocalResolver lr = new LocalResolver(new DefaultHandler());
ErrorHandler handler = new LocalErrorHandler(webXmlFileLocation, lr);
ErrorHandler handler = new LocalErrorHandler(webXmlLocation.toString(), lr);
Digester digester = DigesterFactory.newDigester(validate, namespaceAware, new WebRuleSet(), false);
digester.push(result);
digester.setErrorHandler(handler);
try (InputStream is = new FileInputStream(file)) {
try (InputStream is = Files.newInputStream(webXmlLocation)) {
InputSource iso = new InputSource(is);
iso.setSystemId(file.getAbsolutePath());
iso.setSystemId(webXmlLocation.toString());
digester.parse(iso);
} finally {
digester.reset();
}
result = webXmlCache.putIfAbsentAndGet(webXmlFileLocation, result);
result = webXmlCache.putIfAbsentAndGet(webXmlLocation, result);
}
return result;
}
......
......@@ -22,6 +22,8 @@ import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
......@@ -61,7 +63,7 @@ import org.w3c.dom.Element;
public class ConfigXMLReader {
public static final String module = ConfigXMLReader.class.getName();
public static final String controllerXmlFileName = "/WEB-INF/controller.xml";
public static final Path controllerXmlFileName = Paths.get("WEB-INF", "controller.xml");
private static final UtilCache<URL, ControllerConfig> controllerCache = UtilCache.createUtilCache("webapp.ControllerConfig");
private static final UtilCache<String, List<ControllerConfig>> controllerSearchResultsCache = UtilCache.createUtilCache("webapp.ControllerSearchResults");
public static final RequestResponse emptyNoneRequestResponse = RequestResponse.createEmptyNoneRequestResponse();
......@@ -136,11 +138,11 @@ public class ConfigXMLReader {
}
}
public static ControllerConfig getControllerConfig(WebappInfo webAppInfo) throws WebAppConfigurationException, MalformedURLException {
public static ControllerConfig getControllerConfig(WebappInfo webAppInfo)
throws WebAppConfigurationException, MalformedURLException {
Assert.notNull("webAppInfo", webAppInfo);
String filePath = webAppInfo.getLocation().concat(controllerXmlFileName);
File configFile = new File(filePath);
return getControllerConfig(configFile.toURI().toURL());
Path filePath = webAppInfo.location().resolve(controllerXmlFileName);
return getControllerConfig(filePath.toUri().toURL());
}
public static ControllerConfig getControllerConfig(URL url) throws WebAppConfigurationException {
......@@ -153,7 +155,7 @@ public class ConfigXMLReader {
public static URL getControllerConfigURL(ServletContext context) {
try {
return context.getResource(controllerXmlFileName);
return context.getResource("/" + controllerXmlFileName);
} catch (MalformedURLException e) {
Debug.logError(e, "Error Finding XML Config File: " + controllerXmlFileName, module);
return null;
......
......@@ -37,10 +37,9 @@ components.each { component ->
componentMap.enabled = (component.enabled() == true? "Y" : "N")
componentMap.webAppName = webApp.getName()
componentMap.contextRoot = webApp.getContextRoot()
componentMap.location = webApp.getLocation()
componentMap.webAppName = webApp.getName()
componentMap.contextRoot = webApp.getContextRoot()
componentMap.location = webApp.getLocation()
componentMap.location = webApp.location().toString()
componentList.add(componentMap)
}
if (!webApps) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment