svn commit: r1854431 - in /ofbiz/ofbiz-framework/trunk/framework: base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java

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

svn commit: r1854431 - in /ofbiz/ofbiz-framework/trunk/framework: base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java

mthl
Author: mthl
Date: Tue Feb 26 22:57:00 2019
New Revision: 1854431

URL: http://svn.apache.org/viewvc?rev=1854431&view=rev
Log:
Improved: Refactor ‘WebAppCache’ (OFBIZ-10606)

This makes use of streams and lambdas.


Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java
    ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java?rev=1854431&r1=1854430&r2=1854431&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/component/ComponentConfig.java Tue Feb 26 22:57:00 2019
@@ -940,7 +940,7 @@ public final class ComponentConfig {
             private String description;
             private String menuName;
             private String server;
-            private String mountPoint;
+            private String mountPoint = "";
             private String contextRoot;
             private String location;
             private String[] basePermissions;

Modified: ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java?rev=1854431&r1=1854430&r2=1854431&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java Tue Feb 26 22:57:00 2019
@@ -24,7 +24,6 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.TreeMap;
 import java.util.function.Supplier;
 
@@ -111,32 +110,25 @@ public class WebAppCache {
             webInfos = serverWebApps.get(serverWebAppsKey);
         }
         if (webInfos == null) {
-            Map<String, WebappInfo> tm = null;
-            // use a TreeMap to sort the components alpha by title
-            if (comp != null) {
-                tm = new TreeMap<>(comp);
-            } else {
-                tm = new TreeMap<>();
-            }
-            for (ComponentConfig cc : ccs.get()) {
-                for (WebappInfo wInfo : cc.getWebappInfos()) {
-                    String key = UtilValidate.isNotEmpty(wInfo.position) ? wInfo.position : wInfo.title;
-                    if (serverName.equals(wInfo.server) && wInfo.getAppBarDisplay()) {
-                        if (UtilValidate.isNotEmpty(menuName)) {
-                            if (menuName.equals(wInfo.menuName)) {
-                                tm.put(key, wInfo);
-                            }
+            TreeMap<String, WebappInfo> tm = ccs.get().stream()
+                    .flatMap(cc -> cc.getWebappInfos().stream())
+                    .filter(wInfo -> {
+                        if (wInfo.getAppBarDisplay()) {
+                            return serverName.equals(wInfo.server)
+                                    && (UtilValidate.isEmpty(menuName) || menuName.equals(wInfo.menuName));
                         } else {
-                            tm.put(key, wInfo);
+                            return UtilValidate.isEmpty(menuName);
                         }
-                    } if (!wInfo.getAppBarDisplay() && UtilValidate.isEmpty(menuName)) {
-                        tm.put(key, wInfo);
-                    }
-                }
-            }
-            webInfos = new ArrayList<>(tm.size());
-            webInfos.addAll(tm.values());
-            webInfos = Collections.unmodifiableList(webInfos);
+                    })
+                    // Keep only one WebappInfo per title (the last appearing one).
+                    .collect(() -> new TreeMap<>(comp),
+                            (acc, wInfo) -> {
+                                String key = UtilValidate.isNotEmpty(wInfo.position) ? wInfo.position : wInfo.title;
+                                acc.put(key, wInfo);
+                            },
+                            TreeMap::putAll);
+            // Create the list of WebappInfos ordered by their title/position.
+            webInfos = Collections.unmodifiableList(new ArrayList<>(tm.values()));
             synchronized (serverWebApps) {
                 // We are only preventing concurrent modification, we are not guaranteeing a singleton.
                 serverWebApps.put(serverWebAppsKey, webInfos);
@@ -152,19 +144,13 @@ public class WebAppCache {
      * @param serverName the name of the server to match
      * @param webAppName the name of the web application to match
      * @return the corresponding web application information
-     * @throws NullPointerException when {@code serverName} or {@doc webAppName} are {@code null}
+     * @throws NullPointerException when {@code serverName} is {@code null}
      */
     public WebappInfo getWebappInfo(String serverName, String webAppName) {
-        WebappInfo webappInfo = null;
-        List<WebappInfo> webappsInfo = getAppBarWebInfos(serverName);
-        for(WebappInfo currApp : webappsInfo) {
-            String currWebAppName = currApp.getMountPoint().replace("/", "").replace("*", "");
-            if (webAppName.equals(currWebAppName)) {
-                webappInfo = currApp;
-                break;
-            }
-        }
-        return webappInfo;
+        return getAppBarWebInfos(serverName).stream()
+                .filter(app -> app.getMountPoint().replaceAll("[/*]", "").equals(webAppName))
+                .findFirst()
+                .orElse(null);
     }
 
     // Instance of the cache shared by the loginWorker and Freemarker appbar rendering.