Skip to content
Permalink
Browse files

Improved: Refactor ‘WebAppCache’ (OFBIZ-10606)

This makes use of streams and lambdas.



git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854431 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Feb 26, 2019
1 parent d035643 commit 6be4f5e37f2c8f7ac0bdf95dab591cf299d01587
@@ -940,7 +940,7 @@ private WebappInfo(Builder b) {
private String description;
private String menuName;
private String server;
private String mountPoint;
private String mountPoint = "";
private String contextRoot;
private String location;
private String[] basePermissions;
@@ -24,7 +24,6 @@
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 WebAppCache(Supplier<Collection<ComponentConfig>>supplier) {
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 WebAppCache(Supplier<Collection<ComponentConfig>>supplier) {
* @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.

0 comments on commit 6be4f5e

Please sign in to comment.
You can’t perform that action at this time.