From 4aa65a5b68712cc8cfe31786ad663da9fbbdcabc Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Tue, 26 Feb 2019 22:47:58 +0000 Subject: [PATCH] =?UTF-8?q?Improved:=20Add=20=E2=80=98WebAppCache=E2=80=99?= =?UTF-8?q?=20(OFBIZ-10606)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../ofbiz/base/component/ComponentConfig.java | 72 +------ .../org/apache/ofbiz/webapp/WebAppCache.java | 182 ++++++++++++++++++ .../ofbiz/webapp/control/LoginWorker.java | 10 +- .../template/includes/HomeMenu.ftl | 4 +- .../template/includes/TopAppBar.ftl | 4 +- 5 files changed, 193 insertions(+), 79 deletions(-) create mode 100644 framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java 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 3434e774ce..17495ce019 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 @@ -25,12 +25,10 @@ import java.security.KeyStore; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.TreeMap; import org.apache.ofbiz.base.container.ContainerConfig; import org.apache.ofbiz.base.container.ContainerConfig.Configuration; @@ -55,12 +53,8 @@ public final class ComponentConfig { public static final String module = ComponentConfig.class.getName(); public static final String OFBIZ_COMPONENT_XML_FILENAME = "ofbiz-component.xml"; - /* Note: These Maps are not UtilCache instances because there is no strategy or implementation for reloading components. - * Also, we are using LinkedHashMap to maintain insertion order - which client code depends on. This means - * we will need to use synchronization code because there is no concurrent implementation of LinkedHashMap. - */ + // This map is not a UtilCache instance because there is no strategy or implementation for reloading components. private static final ComponentConfigCache componentConfigCache = new ComponentConfigCache(); - private static final Map> serverWebApps = new LinkedHashMap<>(); public static Boolean componentExists(String componentName) { Assert.notEmpty("componentName", componentName); @@ -187,55 +181,6 @@ public final class ComponentConfig { return webappInfos; } - public static List getAppBarWebInfos(String serverName) { - return ComponentConfig.getAppBarWebInfos(serverName, null, null); - } - - public static List getAppBarWebInfos(String serverName, Comparator comp, String menuName) { - String serverWebAppsKey = serverName + menuName; - List webInfos = null; - synchronized (serverWebApps) { - webInfos = serverWebApps.get(serverWebAppsKey); - } - if (webInfos == null) { - Map 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 : getAllComponents()) { - 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; - } - - public static List getAppBarWebInfos(String serverName, String menuName) { - return getAppBarWebInfos(serverName, null, menuName); - } - public static ComponentConfig getComponentConfig(String globalName) throws ComponentException { // TODO: we need to look up the rootLocation from the container config, or this will blow up return getComponentConfig(globalName, null); @@ -314,21 +259,6 @@ public final class ComponentConfig { return info; } - public static WebappInfo getWebappInfo(String serverName, String webAppName) { - WebappInfo webappInfo = null; - List webappsInfo = getAppBarWebInfos(serverName); - for(WebappInfo currApp : webappsInfo) { - String currWebAppName = currApp.getMountPoint().replace("/", "").replace("*", ""); - if (webAppName.equals(currWebAppName)) { - webappInfo = currApp; - break; - } - } - return webappInfo; - } - - - public static boolean isFileResourceLoader(String componentName, String resourceLoaderName) throws ComponentException { ComponentConfig cc = getComponentConfig(componentName); return cc.isFileResourceLoader(resourceLoaderName); diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java new file mode 100644 index 0000000000..1e52204898 --- /dev/null +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/WebAppCache.java @@ -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}. + *

+ * This improves performance by avoiding to retrieve web applications from + * component configurations each time. + *

+ * This is a cache which doesn't implement any invalidation mechanism. Once a + * web applications is defined, it is memoized because it is not meant + * to change while OFBiz is running. + * + * @see Memoization + */ +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> serverWebApps; + // Source for retrieving components configurations. + private final Supplier> ccs; + + /** + * Constructs an empty web application cache. + * + * @param supplier the source from which components configurations + * are retrieved + */ + public WebAppCache(Supplier>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 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}. + *

+ * 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 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}. + *

+ * 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 getAppBarWebInfos(String serverName, Comparator comp, String menuName) { + String serverWebAppsKey = serverName + menuName; + List webInfos = null; + synchronized (serverWebApps) { + webInfos = serverWebApps.get(serverWebAppsKey); + } + if (webInfos == null) { + Map 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 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; + } +} diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java index e63c889774..9f1198ca6d 100644 --- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java @@ -81,6 +81,7 @@ import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ModelService; import org.apache.ofbiz.service.ServiceUtil; +import org.apache.ofbiz.webapp.WebAppCache; import org.apache.ofbiz.webapp.WebAppUtil; import org.apache.ofbiz.webapp.stats.VisitHandler; import org.apache.ofbiz.widget.model.ThemeFactory; @@ -97,6 +98,7 @@ public class LoginWorker { public static final String securityProperties = "security.properties"; private static final String keyValue = UtilProperties.getPropertyValue(securityProperties, "login.secret_key_string"); + private static final WebAppCache webapps = WebAppCache.getShared(); public static StringWrapper makeLoginUrl(PageContext pageContext) { return makeLoginUrl(pageContext, "checkLogin"); @@ -922,9 +924,9 @@ public class LoginWorker { Delegator delegator = (Delegator) request.getAttribute("delegator"); HttpSession session = request.getSession(); GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); - ServletContext context = request.getServletContext(); + String serverId = (String) request.getServletContext().getAttribute("_serverId"); String applicationName = UtilHttp.getApplicationName(request); - WebappInfo webappInfo = ComponentConfig.getWebappInfo((String) context.getAttribute("_serverId"), applicationName); + WebappInfo webappInfo = webapps.getWebappInfo(serverId, applicationName); if (userLogin != null && ((webappInfo != null && webappInfo.isAutologinCookieUsed()) @@ -932,7 +934,7 @@ public class LoginWorker { Cookie autoLoginCookie = new Cookie(getAutoLoginCookieName(request), userLogin.getString("userLoginId")); autoLoginCookie.setMaxAge(60 * 60 * 24 * 365); autoLoginCookie.setDomain(EntityUtilProperties.getPropertyValue("url", "cookie.domain", delegator)); - autoLoginCookie.setPath( applicationName.equals("root") ? "/" : request.getContextPath()); + autoLoginCookie.setPath(applicationName.equals("root") ? "/" : request.getContextPath()); autoLoginCookie.setSecure(true); autoLoginCookie.setHttpOnly(true); response.addCookie(autoLoginCookie); @@ -1368,7 +1370,7 @@ public class LoginWorker { * user is authorized to access */ public static Collection getAppBarWebInfos(Security security, GenericValue userLogin, String serverName, String menuName) { - Collection allInfos = ComponentConfig.getAppBarWebInfos(serverName, menuName); + Collection allInfos = webapps.getAppBarWebInfos(serverName, menuName); Collection allowedInfos = new ArrayList(allInfos.size()); for (ComponentConfig.WebappInfo info : allInfos) { if (hasApplicationPermission(info, security, userLogin)) { diff --git a/themes/rainbowstone/template/includes/HomeMenu.ftl b/themes/rainbowstone/template/includes/HomeMenu.ftl index cc3744fde9..7ed398bc99 100644 --- a/themes/rainbowstone/template/includes/HomeMenu.ftl +++ b/themes/rainbowstone/template/includes/HomeMenu.ftl @@ -21,8 +21,8 @@ under the License. <#if (externalLoginKey)?exists><#assign externalKeyParam = "?externalLoginKey=" + requestAttributes.externalLoginKey?if_exists> <#assign ofbizServerName = application.getAttribute("_serverId")?default("default-server")> <#assign contextPath = request.getContextPath()> -<#assign displayApps = Static["org.apache.ofbiz.base.component.ComponentConfig"].getAppBarWebInfos(ofbizServerName, "main")> -<#assign displaySecondaryApps = Static["org.apache.ofbiz.base.component.ComponentConfig"].getAppBarWebInfos(ofbizServerName, "secondary")> +<#assign displayApps = Static["org.apache.ofbiz.webapp.WebAppCache"].getShared().getAppBarWebInfos(ofbizServerName, "main")> +<#assign displaySecondaryApps = Static["org.apache.ofbiz.webapp.WebAppCache"].getShared().getAppBarWebInfos(ofbizServerName, "secondary")> <#assign avatarList = EntityQuery.use(delegator).from("PartyContent").where("partyId", person.partyId!, "partyContentTypeId", "LGOIMGURL").queryList()!> <#if avatarList?has_content> <#assign avatar = Static["org.apache.ofbiz.entity.util.EntityUtil"].getFirst(avatarList)> diff --git a/themes/rainbowstone/template/includes/TopAppBar.ftl b/themes/rainbowstone/template/includes/TopAppBar.ftl index 18f610a0c7..e82c73b331 100644 --- a/themes/rainbowstone/template/includes/TopAppBar.ftl +++ b/themes/rainbowstone/template/includes/TopAppBar.ftl @@ -20,8 +20,8 @@ under the License. <#if (externalLoginKey)?exists><#assign externalKeyParam = "?externalLoginKey=" + requestAttributes.externalLoginKey?if_exists> <#assign ofbizServerName = application.getAttribute("_serverId")?default("default-server")> <#assign contextPath = request.getContextPath()> -<#assign displayApps = Static["org.apache.ofbiz.base.component.ComponentConfig"].getAppBarWebInfos(ofbizServerName, "main")> -<#assign displaySecondaryApps = Static["org.apache.ofbiz.base.component.ComponentConfig"].getAppBarWebInfos(ofbizServerName, "secondary")> +<#assign displayApps = Static["org.apache.ofbiz.webapp.WebAppCache"].getShared().getAppBarWebInfos(ofbizServerName, "main")> +<#assign displaySecondaryApps = Static["org.apache.ofbiz.webapp.WebAppCache"].getShared().getAppBarWebInfos(ofbizServerName, "secondary")> <#if person?has_content> <#assign avatarList = EntityQuery.use(delegator).from("PartyContent").where("partyId", person.partyId!, "partyContentTypeId", "LGOIMGURL").queryList()!> <#if avatarList?has_content>