Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/main/java/net/bootsfaces/listeners/AddResourcesListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,7 @@
* @author Stephan Rauh
*/
public class AddResourcesListener implements SystemEventListener {
private static final Map<String, Map<String, Object>> viewMaps = new ConcurrentHashMap<>();

private static final Logger LOGGER = Logger.getLogger("net.bootsfaces.listeners.AddResourcesListener");

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -154,7 +161,7 @@ public void processEvent(SystemEvent event) throws AbortProcessingException {
* @return
*/
private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext context) {
Map<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));

// check explicit js request
if (viewMap.get(RESOURCE_KEY) != null) {
Expand Down Expand Up @@ -284,7 +291,7 @@ private void addCSS(UIViewRoot root, FacesContext context) {
// 2) Font Awesome
if (useCDNImportForFontAwesome) {
InternalFALink output = new InternalFALink();
Map<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));
if (viewMap.containsKey(FONTAWESOME_USED)) {
String version = (String) viewMap.get(FONTAWESOME_VERSION);
if (version != null) {
Expand All @@ -300,7 +307,7 @@ private void addCSS(UIViewRoot root, FacesContext context) {
}

@SuppressWarnings("unchecked")
List<String> extCSSMap = (List<String>) root.getViewMap().get(EXT_RESOURCE_KEY);
List<String> extCSSMap = (List<String>) getViewMap(getUniqueViewId(root)).get(EXT_RESOURCE_KEY);
if (extCSSMap != null) {
for (String file : extCSSMap) {
String name = "css/" + file;
Expand All @@ -310,7 +317,7 @@ private void addCSS(UIViewRoot root, FacesContext context) {
}

@SuppressWarnings("unchecked")
List<String> themedCSSMap = (List<String>) root.getViewMap().get(THEME_RESOURCE_KEY);
List<String> themedCSSMap = (List<String>) getViewMap(getUniqueViewId(root)).get(THEME_RESOURCE_KEY);
if (themedCSSMap != null) {
for (String file : themedCSSMap) {
String name = "css/" + theme + "/" + file;
Expand Down Expand Up @@ -456,7 +463,7 @@ private Map<String, Object> addMandatoryLibraries(UIViewRoot root, FacesContext
createAndAddComponent(root, context, SCRIPT_RENDERER, "jq/jquery.js", C.BSF_LIBRARY);
}

Map<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));
@SuppressWarnings("unchecked")
Map<String, String> basicResourceMap = (Map<String, String>) viewMap.get(BASIC_JS_RESOURCE_KEY);

Expand Down Expand Up @@ -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<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));

if (version != 4) {
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -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<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));
ArrayList<Object> list = (ArrayList<Object>) viewMap.get(FONTAWESOME_USED);
if (list == null) {
list = new ArrayList<Object>();
Expand All @@ -610,7 +617,7 @@ public static void setNeedsFontsAwesome(Object uiComponent) {
private boolean needsFontAwesome4() {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getViewRoot();
Map<String, Object> viewMap = root.getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(root));
@SuppressWarnings("unchecked")
Map<Object, Boolean> v5 = (Map<Object, Boolean>) viewMap.get(FONTAWESOME_NEW_VERSION);
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -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<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot()));
@SuppressWarnings("unchecked")
List<String> resourceList = (List<String>) viewMap.get(THEME_RESOURCE_KEY);
if (null == resourceList) {
Expand All @@ -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<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot()));
@SuppressWarnings("unchecked")
List<String> resourceList = (List<String>) viewMap.get(EXT_RESOURCE_KEY);
if (null == resourceList) {
Expand All @@ -927,7 +934,7 @@ public static void addExtCSSResource(String resource) {
}

private static void addResource(String resource, String library, String resourceKey, String resourceTypeKey) {
Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Map<String, Object> viewMap = getViewMap(getUniqueViewId(FacesContext.getCurrentInstance().getViewRoot()));
@SuppressWarnings("unchecked")
Map<String, String> resourceMap = (Map<String, String>) viewMap.get(resourceTypeKey);
if (null == resourceMap) {
Expand Down Expand Up @@ -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<String, Object> getViewMap(String uniqueViewId) {
Map<String, Object> viewMap = viewMaps.get(uniqueViewId);
if (viewMap == null) {
viewMap = new ConcurrentHashMap<String, Object>();
viewMaps.put(uniqueViewId, viewMap);
}
return viewMap;
}
}