Skip to content

Commit

Permalink
Improved: Refactor ‘WebAppCache’ (OFBIZ-10606)
Browse files Browse the repository at this point in the history
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 6be4f5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ public static class Builder {
private String description;
private String menuName;
private String server;
private String mountPoint;
private String mountPoint = "";
private String contextRoot;
private String location;
private String[] basePermissions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -111,32 +110,25 @@ private List<WebappInfo> getAppBarWebInfos(String serverName, Comparator<? super
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);
Expand All @@ -152,19 +144,13 @@ private List<WebappInfo> getAppBarWebInfos(String serverName, Comparator<? super
* @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.
Expand Down

0 comments on commit 6be4f5e

Please sign in to comment.