Permalink
Browse files

Make use of new JSF 2.3 markResourceRendered/isResourceRendered

Added workaround to failing ViewScopedIT and CombinedResourceHandlerIT
  • Loading branch information...
BalusC committed May 21, 2017
1 parent 236e93b commit c275dead41048e38dfef8109f25143244b736058
@@ -15,6 +15,8 @@
import static java.lang.Boolean.TRUE;
import static java.lang.String.format;
import static java.util.logging.Level.FINE;
import static javax.faces.application.StateManager.IS_BUILDING_INITIAL_STATE;
import static javax.faces.event.PhaseId.RENDER_RESPONSE;
import static org.omnifaces.config.OmniFaces.OMNIFACES_LIBRARY_NAME;
import static org.omnifaces.config.OmniFaces.OMNIFACES_SCRIPT_NAME;
import static org.omnifaces.config.OmniFaces.OMNIFACES_UNLOAD_SCRIPT_NAME;
@@ -35,15 +37,11 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.faces.application.StateManager;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject;
import org.omnifaces.cdi.BeanStorage;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.resourcehandler.ResourceIdentifier;
import org.omnifaces.util.Hacks;
/**
* Manages view scoped bean creation and destroy. The creation is initiated by {@link ViewScopeContext} which is
@@ -84,7 +82,6 @@
private static final Logger logger = Logger.getLogger(ViewScopeManager.class.getName());
private static final ResourceIdentifier SCRIPT_ID = new ResourceIdentifier(OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME);
private static final String SCRIPT_INIT = "OmniFaces.Unload.init('%s')";
private static final int DEFAULT_BEANS_PER_VIEW_SCOPE = 3;
@@ -143,7 +140,7 @@ public void preDestroyView() {
}
}
else if (isAjaxRequestWithPartialRendering(context)) {
Hacks.setScriptResourceRendered(context, SCRIPT_ID); // Otherwise MyFaces will load a new one during createViewScope() when still in same document (e.g. navigation).
context.getApplication().getResourceHandler().markResourceRendered(context, OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME); // Otherwise MyFaces will load a new one during createViewScope() when still in same document (e.g. navigation).
}
if (getInstance(manager, ViewScopeStorageInSession.class, false) != null) { // Avoid unnecessary session creation when accessing storageInSession for nothing.
@@ -206,11 +203,11 @@ private static void registerUnloadScript(UUID beanStorageId) {
FacesContext context = FacesContext.getCurrentInstance();
boolean ajaxRequestWithPartialRendering = isAjaxRequestWithPartialRendering(context);
if (!Hacks.isScriptResourceRendered(context, SCRIPT_ID)) {
if (!context.getApplication().getResourceHandler().isResourceRendered(context, OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME)) {
if (ajaxRequestWithPartialRendering) {
load(OMNIFACES_LIBRARY_NAME, OMNIFACES_UNLOAD_SCRIPT_NAME);
}
else if (context.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE || TRUE.equals(context.getAttributes().get(StateManager.IS_BUILDING_INITIAL_STATE))) {
else if (context.getCurrentPhaseId() != RENDER_RESPONSE || TRUE.equals(context.getAttributes().get(IS_BUILDING_INITIAL_STATE))) {
addScriptResourceToHead(OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME);
}
else {
@@ -12,16 +12,19 @@
*/
package org.omnifaces.component.script;
import static org.omnifaces.config.OmniFaces.OMNIFACES_LIBRARY_NAME;
import static org.omnifaces.config.OmniFaces.OMNIFACES_SCRIPT_NAME;
import static org.omnifaces.util.Components.getAttribute;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.PostRestoreStateEvent;
import org.omnifaces.renderer.DeferredScriptRenderer;
import org.omnifaces.resourcehandler.ResourceIdentifier;
import org.omnifaces.util.Hacks;
/**
* <p>
@@ -51,7 +54,7 @@
* @see ScriptFamily
*/
@FacesComponent(DeferredScript.COMPONENT_TYPE)
@ResourceDependency(library="omnifaces", name="omnifaces.js", target="head")
@ResourceDependency(library=OMNIFACES_LIBRARY_NAME, name=OMNIFACES_SCRIPT_NAME, target="head")
@ListenerFor(systemEventClass=PostAddToViewEvent.class)
@ListenerFor(systemEventClass=PostRestoreStateEvent.class)
public class DeferredScript extends ScriptFamily {
@@ -80,7 +83,8 @@ public DeferredScript() {
@Override
public void processEvent(ComponentSystemEvent event) {
if (moveToBody(event)) {
Hacks.setScriptResourceRendered(getFacesContext(), new ResourceIdentifier(this));
FacesContext context = event.getFacesContext();
context.getApplication().getResourceHandler().markResourceRendered(context, getAttribute(this, "name"), getAttribute(this, "library"));
}
}
@@ -107,7 +107,7 @@ protected boolean moveToBody(ComponentSystemEvent event) {
return false;
}
FacesContext context = getFacesContext();
FacesContext context = event.getFacesContext();
UIViewRoot view = context.getViewRoot();
if (context.isPostback() ? !view.getComponentResources(context, "body").contains(this) : event instanceof PostAddToViewEvent) {
@@ -498,16 +498,18 @@ private void addCombined(FacesContext context, UIComponent component, String ren
private void addStylesheet(FacesContext context, UIComponent component, ResourceIdentifier id) {
if (stylesheets.add(component, id)) {
Hacks.setStylesheetResourceRendered(context, id); // Prevents future forced additions by libs.
context.getApplication().getResourceHandler().markResourceRendered(context, id.getName(), id.getLibrary()); // Prevents future forced additions by libs.
}
}
private void addScript(FacesContext context, UIComponent component, ResourceIdentifier id) {
if (Hacks.isScriptResourceRendered(context, id)) { // This is true when o:deferredScript is used.
ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
if (resourceHandler.isResourceRendered(context, id.getName(), id.getLibrary())) { // This is true when o:deferredScript is used.
componentResourcesToRemove.add(component);
}
else if (scripts.add(component, id)) {
Hacks.setScriptResourceRendered(context, id); // Prevents future forced additions by libs.
resourceHandler.markResourceRendered(context, id.getName(), id.getLibrary()); // Prevents future forced additions by libs.
}
}
@@ -17,10 +17,8 @@
import static java.lang.String.format;
import static org.omnifaces.util.Components.getClosestParent;
import static org.omnifaces.util.FacesLocal.getApplicationAttribute;
import static org.omnifaces.util.FacesLocal.getContextAttribute;
import static org.omnifaces.util.FacesLocal.getInitParameter;
import static org.omnifaces.util.FacesLocal.getSessionAttribute;
import static org.omnifaces.util.FacesLocal.setContextAttribute;
import static org.omnifaces.util.Reflection.accessField;
import static org.omnifaces.util.Reflection.instance;
import static org.omnifaces.util.Reflection.invokeMethod;
@@ -31,7 +29,6 @@
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
@@ -255,85 +252,6 @@ public static boolean isMyFacesUsed() {
// JSF resource handling related ----------------------------------------------------------------------------------
/**
* Set the given script resource as rendered.
* @param context The involved faces context.
* @param id The resource identifier.
* @since 1.8
*/
public static void setScriptResourceRendered(FacesContext context, ResourceIdentifier id) {
setMojarraResourceRendered(context, id);
if (isMyFacesUsed()) {
setMyFacesResourceRendered(context, MYFACES_RENDERED_SCRIPT_RESOURCES_KEY, id);
}
}
/**
* Returns whether the given script resource is rendered.
* @param context The involved faces context.
* @param id The resource identifier.
* @return Whether the given script resource is rendered.
* @since 1.8
*/
public static boolean isScriptResourceRendered(FacesContext context, ResourceIdentifier id) {
boolean rendered = isMojarraResourceRendered(context, id);
if (!rendered && isMyFacesUsed()) {
return isMyFacesResourceRendered(context, MYFACES_RENDERED_SCRIPT_RESOURCES_KEY, id);
}
else {
return rendered;
}
}
/**
* Set the given stylesheet resource as rendered.
* @param context The involved faces context.
* @param id The resource identifier.
* @since 1.8
*/
public static void setStylesheetResourceRendered(FacesContext context, ResourceIdentifier id) {
setMojarraResourceRendered(context, id);
if (isMyFacesUsed()) {
setMyFacesResourceRendered(context, MYFACES_RENDERED_STYLESHEET_RESOURCES_KEY, id);
}
}
private static void setMojarraResourceRendered(FacesContext context, ResourceIdentifier id) {
context.getAttributes().put(id.getName() + id.getLibrary(), true);
}
private static boolean isMojarraResourceRendered(FacesContext context, ResourceIdentifier id) {
return context.getAttributes().containsKey(id.getName() + id.getLibrary());
}
private static void setMyFacesResourceRendered(FacesContext context, String key, ResourceIdentifier id) {
getMyFacesResourceMap(context, key).put(getMyFacesResourceKey(id), true);
}
private static boolean isMyFacesResourceRendered(FacesContext context, String key, ResourceIdentifier id) {
return getMyFacesResourceMap(context, key).containsKey(getMyFacesResourceKey(id));
}
private static Map<String, Boolean> getMyFacesResourceMap(FacesContext context, String key) {
Map<String, Boolean> map = getContextAttribute(context, key);
if (map == null) {
map = new HashMap<>();
setContextAttribute(context, key, map);
}
return map;
}
private static String getMyFacesResourceKey(ResourceIdentifier id) {
String library = id.getLibrary();
String name = id.getName();
return (library != null) ? (library + '/' + name) : name;
}
/**
* Returns the default resource maximum age in milliseconds.
* @return The default resource maximum age in milliseconds.
@@ -12,9 +12,9 @@
*/
package org.omnifaces.test.cdi.viewscoped;
import static javax.faces.application.ResourceHandler.RESOURCE_IDENTIFIER;
import static org.omnifaces.cdi.viewscope.ViewScopeManager.isUnloadRequest;
import static org.omnifaces.util.Faces.getContext;
import static org.omnifaces.util.Faces.getViewId;
import static org.omnifaces.util.Faces.hasContext;
import static org.omnifaces.util.Messages.addGlobalInfo;
@@ -25,6 +25,7 @@
import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.util.Faces;
@Named
@ViewScoped
@@ -57,9 +58,11 @@ public void submit() {
addGlobalInfo("submit ");
}
public String navigate() {
public void navigate() {
Object renderedResources = Faces.getContextAttribute(RESOURCE_IDENTIFIER);
Faces.setViewRoot(Faces.getViewId()); // TODO: replace by return getViewId() once Mojarra 2.3.1 is released. See #4249
Faces.setContextAttribute(RESOURCE_IDENTIFIER, renderedResources); // TODO: remove once Mojarra 2.3.1 is released. See #4249
addGlobalInfo("navigate ");
return getViewId();
}
@PreDestroy
@@ -22,7 +22,6 @@
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Ignore;
import org.junit.Test;
import org.omnifaces.test.OmniFacesIT;
import org.openqa.selenium.WebElement;
@@ -85,7 +84,7 @@ public void nonAjax() {
verifyElements();
}
@Test @Ignore // TODO: Fails after ajaxRebuild. Make use of JSF 2.3 markResourceRendered() / isResourceRendered() in CombinedResourceHandler.
@Test
public void ajax() {
verifyElements();
guardAjax(ajaxSubmit).click();
@@ -94,7 +93,7 @@ public void ajax() {
verifyElements();
}
@Test @Ignore // TODO: Fails after ajaxRebuild. Make use of JSF 2.3 markResourceRendered() / isResourceRendered() in CombinedResourceHandler.
@Test
public void mixed() {
verifyElements();
guardHttp(nonAjaxSubmit).click();
@@ -12,6 +12,8 @@
*/
package org.omnifaces.test.resourcehandler.combinedresourcehandler;
import static javax.faces.application.ResourceHandler.RESOURCE_IDENTIFIER;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@@ -22,7 +24,9 @@
public class CombinedResourceHandlerITBean {
public void rebuild() {
Object renderedResources = Faces.getContextAttribute(RESOURCE_IDENTIFIER);
Faces.setViewRoot(Faces.getViewId());
Faces.setContextAttribute(RESOURCE_IDENTIFIER, renderedResources); // TODO: remove once Mojarra 2.3.1 is released. See #4249
}
}

0 comments on commit c275dea

Please sign in to comment.