[ofbiz-framework] branch trunk updated: Improved: Replace getLocation with location in ‘ComponentConfig.WebappInfo’ (OFBIZ-11192)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: Replace getLocation with location in ‘ComponentConfig.WebappInfo’ (OFBIZ-11192)

mthl
This is an automated email from the ASF dual-hosted git repository.

mthl pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new df5dde3  Improved: Replace getLocation with location in ‘ComponentConfig.WebappInfo’ (OFBIZ-11192)
df5dde3 is described below

commit df5dde37d5da5811eb408264ec95f4f1586e327a
Author: Mathieu Lirzin <[hidden email]>
AuthorDate: Sat Dec 7 23:53:55 2019 +0100

    Improved: Replace getLocation with location in ‘ComponentConfig.WebappInfo’
    (OFBIZ-11192)
   
    Use Path instead of String + File.
---
 build.gradle                                       |  2 +-
 .../ofbiz/base/component/ComponentConfig.java      |  4 +--
 .../catalina/container/CatalinaContainer.java      |  3 +-
 .../java/org/apache/ofbiz/webapp/WebAppUtil.java   | 37 ++++++++++++----------
 .../ofbiz/webapp/control/ConfigXMLReader.java      | 14 ++++----
 .../artifactinfo/ComponentList.groovy              |  3 +-
 6 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/build.gradle b/build.gradle
index 3d4b81b..34bd3be 100644
--- a/build.gradle
+++ b/build.gradle
@@ -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
diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java b/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java
index 9ddffbb..7781572 100644
--- a/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java
+++ b/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java
@@ -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() {
diff --git a/framework/catalina/src/main/java/org/apache/ofbiz/catalina/container/CatalinaContainer.java b/framework/catalina/src/main/java/org/apache/ofbiz/catalina/container/CatalinaContainer.java
index 54d2f7d..d50c5f8 100644
--- a/framework/catalina/src/main/java/org/apache/ofbiz/catalina/container/CatalinaContainer.java
+++ b/framework/catalina/src/main/java/org/apache/ofbiz/catalina/container/CatalinaContainer.java
@@ -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);
diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java
index c8edb15..1883b07 100644
--- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppUtil.java
@@ -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;
     }
diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java
index 3c87bb1..ff585a2 100644
--- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java
@@ -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;
diff --git a/framework/webtools/groovyScripts/artifactinfo/ComponentList.groovy b/framework/webtools/groovyScripts/artifactinfo/ComponentList.groovy
index 68dca59..badea7e 100644
--- a/framework/webtools/groovyScripts/artifactinfo/ComponentList.groovy
+++ b/framework/webtools/groovyScripts/artifactinfo/ComponentList.groovy
@@ -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) {