diff --git a/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java b/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java index ed231380b..ec6b35600 100644 --- a/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java +++ b/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java @@ -36,6 +36,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.logging.Level; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; import javax.faces.FacesException; @@ -64,6 +65,7 @@ * @author Stephan Rauh */ public class AddResourcesListener implements SystemEventListener { + private static final Map> viewMaps = new ConcurrentHashMap<>(); private static final Logger LOGGER = Logger.getLogger("net.bootsfaces.listeners.AddResourcesListener"); @@ -129,15 +131,20 @@ public boolean isListenerForSource(Object source) { * Trigger adding the resources if and only if the event has been fired by * UIViewRoot. */ + @Override public void processEvent(SystemEvent event) throws AbortProcessingException { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); // render the resources only if there is at least one bsf component - if (ensureExistBootsfacesComponent(root, context)) { - addCSS(root, context); - addJavascript(root, context); - addMetaTags(root, context); + try { + if (ensureExistBootsfacesComponent(root, context)) { + addCSS(root, context); + addJavascript(root, context); + addMetaTags(root, context); + } + } finally { + viewMaps.remove(getUniqueViewId(root)); } } @@ -154,7 +161,7 @@ public void processEvent(SystemEvent event) throws AbortProcessingException { * @return */ private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext context) { - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); // check explicit js request if (viewMap.get(RESOURCE_KEY) != null) { @@ -284,7 +291,7 @@ private void addCSS(UIViewRoot root, FacesContext context) { // 2) Font Awesome if (useCDNImportForFontAwesome) { InternalFALink output = new InternalFALink(); - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); if (viewMap.containsKey(FONTAWESOME_USED)) { String version = (String) viewMap.get(FONTAWESOME_VERSION); if (version != null) { @@ -300,7 +307,7 @@ private void addCSS(UIViewRoot root, FacesContext context) { } @SuppressWarnings("unchecked") - List extCSSMap = (List) root.getViewMap().get(EXT_RESOURCE_KEY); + List extCSSMap = (List) getViewMap(getUniqueViewId(root)).get(EXT_RESOURCE_KEY); if (extCSSMap != null) { for (String file : extCSSMap) { String name = "css/" + file; @@ -310,7 +317,7 @@ private void addCSS(UIViewRoot root, FacesContext context) { } @SuppressWarnings("unchecked") - List themedCSSMap = (List) root.getViewMap().get(THEME_RESOURCE_KEY); + List themedCSSMap = (List) getViewMap(getUniqueViewId(root)).get(THEME_RESOURCE_KEY); if (themedCSSMap != null) { for (String file : themedCSSMap) { String name = "css/" + theme + "/" + file; @@ -456,7 +463,7 @@ private Map addMandatoryLibraries(UIViewRoot root, FacesContext createAndAddComponent(root, context, SCRIPT_RENDERER, "jq/jquery.js", C.BSF_LIBRARY); } - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); @SuppressWarnings("unchecked") Map basicResourceMap = (Map) viewMap.get(BASIC_JS_RESOURCE_KEY); @@ -565,7 +572,7 @@ public static void addResourceIfNecessary(String url) { public static void setFontAwesomeVersion(int version, Object uiComponent) { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); if (version != 4) { @SuppressWarnings("unchecked") @@ -598,7 +605,7 @@ public static void setFontAwesomeVersion(int version, Object uiComponent) { public static void setNeedsFontsAwesome(Object uiComponent) { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); ArrayList list = (ArrayList) viewMap.get(FONTAWESOME_USED); if (list == null) { list = new ArrayList(); @@ -610,7 +617,7 @@ public static void setNeedsFontsAwesome(Object uiComponent) { private boolean needsFontAwesome4() { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); - Map viewMap = root.getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(root)); @SuppressWarnings("unchecked") Map v5 = (Map) viewMap.get(FONTAWESOME_NEW_VERSION); @SuppressWarnings("unchecked") @@ -893,7 +900,7 @@ public static void addBasicJSResource(String library, String resource) { * @param resource The name of the resource file within the library folder. */ public static void addThemedCSSResource(String resource) { - Map viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot())); @SuppressWarnings("unchecked") List resourceList = (List) viewMap.get(THEME_RESOURCE_KEY); if (null == resourceList) { @@ -913,7 +920,7 @@ public static void addThemedCSSResource(String resource) { * @param resource The name of the resource file within the library folder. */ public static void addExtCSSResource(String resource) { - Map viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot())); @SuppressWarnings("unchecked") List resourceList = (List) viewMap.get(EXT_RESOURCE_KEY); if (null == resourceList) { @@ -927,7 +934,7 @@ public static void addExtCSSResource(String resource) { } private static void addResource(String resource, String library, String resourceKey, String resourceTypeKey) { - Map viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); + Map viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot())); @SuppressWarnings("unchecked") Map resourceMap = (Map) viewMap.get(resourceTypeKey); if (null == resourceMap) { @@ -1021,4 +1028,18 @@ public static void addDatatablesResourceIfNecessary(String defaultFilename, Stri addResourceIfNecessary(defaultFilename); } } + + private static String getUniqueViewId(UIViewRoot root) { + // Best idea for right now? + return String.valueOf(root.hashCode()); + } + + private static Map getViewMap(String uniqueViewId) { + Map viewMap = viewMaps.get(uniqueViewId); + if (viewMap == null) { + viewMap = new ConcurrentHashMap(); + viewMaps.put(uniqueViewId, viewMap); + } + return viewMap; + } }