-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved: Add ‘WebAppCache’ (OFBIZ-10606)
This improves the cohesion of the ‘ComponentConfig’ class by extracting the webapps cache related methods into a separate ‘WebAppCache’ class. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854429 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
5 changed files
with
193 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.ofbiz.webapp; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
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; | ||
|
||
import org.apache.ofbiz.base.component.ComponentConfig; | ||
import org.apache.ofbiz.base.component.ComponentConfig.WebappInfo; | ||
import org.apache.ofbiz.base.util.UtilValidate; | ||
|
||
/** | ||
* Cache for web applications information retrieved from | ||
* {@linkplain ComponentConfig component configurations}. | ||
* <p> | ||
* This improves performance by avoiding to retrieve web applications from | ||
* component configurations each time. | ||
* <p> | ||
* This is a cache which doesn't implement any invalidation mechanism. Once a | ||
* web applications is defined, it is <b>memoized</b> because it is not meant | ||
* to change while OFBiz is running. | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Memoization">Memoization</a> | ||
*/ | ||
public class WebAppCache { | ||
// Synchronized map storing web applications. | ||
// The LinkedHashMap is used to maintain insertion order (which client code depends on). | ||
// There is no concurrent implementation of LinkedHashMap, so we are using manual synchronization instead. | ||
private final LinkedHashMap<String, List<WebappInfo>> serverWebApps; | ||
// Source for retrieving components configurations. | ||
private final Supplier<Collection<ComponentConfig>> ccs; | ||
|
||
/** | ||
* Constructs an empty web application cache. | ||
* | ||
* @param supplier the source from which components configurations | ||
* are retrieved | ||
*/ | ||
public WebAppCache(Supplier<Collection<ComponentConfig>>supplier) { | ||
ccs = supplier; | ||
serverWebApps = new LinkedHashMap<>(); | ||
} | ||
|
||
/** | ||
* Retrieves the web applications information that must be visible | ||
* in the context of the server {@code serverName}. | ||
* | ||
* @param serverName the name of the server to match | ||
* @return the corresponding web applications information | ||
*/ | ||
public List<WebappInfo> getAppBarWebInfos(String serverName) { | ||
return getAppBarWebInfos(serverName, null, null); | ||
} | ||
|
||
/** | ||
* Retrieves the web applications information that must be visible inside | ||
* the menu {@code menuName} in the context of the server {@code serverName}. | ||
* <p> | ||
* When an empty string or {@code null} is used for {@code menuName}, | ||
* all the web application information corresponding to {@code serverName} are matched. | ||
* | ||
* @param serverName the name of server to match | ||
* @param menuName the name of the menu to match | ||
* @return the corresponding web applications information | ||
* @throws NullPointerException when {@code serverName} is {@code null} | ||
*/ | ||
public List<WebappInfo> getAppBarWebInfos(String serverName, String menuName) { | ||
return getAppBarWebInfos(serverName, null, menuName); | ||
} | ||
|
||
/** | ||
* Retrieves the web applications information that must be visible inside | ||
* the menu {@code menuName} in the context of the server {@code serverName}. | ||
* <p> | ||
* When an empty string or {@code null} is used for {@code menuName}, | ||
* all the web application information corresponding to {@code serverName} are matched. | ||
* | ||
* @param serverName the name of server to match | ||
* @param comp the comparator used for ordering the results | ||
* @param menuName the name of the menu to match | ||
* @return the corresponding web applications information | ||
* @throws NullPointerException when {@code serverName} is {@code null} | ||
*/ | ||
private List<WebappInfo> getAppBarWebInfos(String serverName, Comparator<? super String> comp, String menuName) { | ||
String serverWebAppsKey = serverName + menuName; | ||
List<WebappInfo> webInfos = null; | ||
synchronized (serverWebApps) { | ||
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); | ||
} | ||
} else { | ||
tm.put(key, wInfo); | ||
} | ||
} if (!wInfo.getAppBarDisplay() && UtilValidate.isEmpty(menuName)) { | ||
tm.put(key, wInfo); | ||
} | ||
} | ||
} | ||
webInfos = new ArrayList<>(tm.size()); | ||
webInfos.addAll(tm.values()); | ||
webInfos = Collections.unmodifiableList(webInfos); | ||
synchronized (serverWebApps) { | ||
// We are only preventing concurrent modification, we are not guaranteeing a singleton. | ||
serverWebApps.put(serverWebAppsKey, webInfos); | ||
} | ||
} | ||
return webInfos; | ||
} | ||
|
||
/** | ||
* Retrieves the first web application information which mount point correspond to | ||
* {@code webAppName} in the context of the server {@code serverName}. | ||
* | ||
* @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} | ||
*/ | ||
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; | ||
} | ||
|
||
// Instance of the cache shared by the loginWorker and Freemarker appbar rendering. | ||
// TODO: Find a way to share this cache without relying on a global variable. | ||
private static WebAppCache sharedCache = new WebAppCache(ComponentConfig::getAllComponents); | ||
|
||
/** | ||
* Provides access to a shared instance of the webapp cache. | ||
* | ||
* @return the shared webapp cache. | ||
*/ | ||
public static WebAppCache getShared() { | ||
return sharedCache; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters