Skip to content

Commit c275dea

Browse files
committed
Make use of new JSF 2.3 markResourceRendered/isResourceRendered
Added workaround to failing ViewScopedIT and CombinedResourceHandlerIT
1 parent 236e93b commit c275dea

File tree

8 files changed

+31
-104
lines changed

8 files changed

+31
-104
lines changed

src/main/java/org/omnifaces/cdi/viewscope/ViewScopeManager.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import static java.lang.Boolean.TRUE;
1616
import static java.lang.String.format;
1717
import static java.util.logging.Level.FINE;
18+
import static javax.faces.application.StateManager.IS_BUILDING_INITIAL_STATE;
19+
import static javax.faces.event.PhaseId.RENDER_RESPONSE;
1820
import static org.omnifaces.config.OmniFaces.OMNIFACES_LIBRARY_NAME;
1921
import static org.omnifaces.config.OmniFaces.OMNIFACES_SCRIPT_NAME;
2022
import static org.omnifaces.config.OmniFaces.OMNIFACES_UNLOAD_SCRIPT_NAME;
@@ -35,15 +37,11 @@
3537
import javax.enterprise.context.spi.CreationalContext;
3638
import javax.enterprise.inject.spi.Bean;
3739
import javax.enterprise.inject.spi.BeanManager;
38-
import javax.faces.application.StateManager;
3940
import javax.faces.context.FacesContext;
40-
import javax.faces.event.PhaseId;
4141
import javax.inject.Inject;
4242

4343
import org.omnifaces.cdi.BeanStorage;
4444
import org.omnifaces.cdi.ViewScoped;
45-
import org.omnifaces.resourcehandler.ResourceIdentifier;
46-
import org.omnifaces.util.Hacks;
4745

4846
/**
4947
* Manages view scoped bean creation and destroy. The creation is initiated by {@link ViewScopeContext} which is
@@ -84,7 +82,6 @@ public class ViewScopeManager {
8482

8583
private static final Logger logger = Logger.getLogger(ViewScopeManager.class.getName());
8684

87-
private static final ResourceIdentifier SCRIPT_ID = new ResourceIdentifier(OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME);
8885
private static final String SCRIPT_INIT = "OmniFaces.Unload.init('%s')";
8986
private static final int DEFAULT_BEANS_PER_VIEW_SCOPE = 3;
9087

@@ -143,7 +140,7 @@ public void preDestroyView() {
143140
}
144141
}
145142
else if (isAjaxRequestWithPartialRendering(context)) {
146-
Hacks.setScriptResourceRendered(context, SCRIPT_ID); // Otherwise MyFaces will load a new one during createViewScope() when still in same document (e.g. navigation).
143+
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).
147144
}
148145

149146
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) {
206203
FacesContext context = FacesContext.getCurrentInstance();
207204
boolean ajaxRequestWithPartialRendering = isAjaxRequestWithPartialRendering(context);
208205

209-
if (!Hacks.isScriptResourceRendered(context, SCRIPT_ID)) {
206+
if (!context.getApplication().getResourceHandler().isResourceRendered(context, OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME)) {
210207
if (ajaxRequestWithPartialRendering) {
211208
load(OMNIFACES_LIBRARY_NAME, OMNIFACES_UNLOAD_SCRIPT_NAME);
212209
}
213-
else if (context.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE || TRUE.equals(context.getAttributes().get(StateManager.IS_BUILDING_INITIAL_STATE))) {
210+
else if (context.getCurrentPhaseId() != RENDER_RESPONSE || TRUE.equals(context.getAttributes().get(IS_BUILDING_INITIAL_STATE))) {
214211
addScriptResourceToHead(OMNIFACES_LIBRARY_NAME, OMNIFACES_SCRIPT_NAME);
215212
}
216213
else {

src/main/java/org/omnifaces/component/script/DeferredScript.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
*/
1313
package org.omnifaces.component.script;
1414

15+
import static org.omnifaces.config.OmniFaces.OMNIFACES_LIBRARY_NAME;
16+
import static org.omnifaces.config.OmniFaces.OMNIFACES_SCRIPT_NAME;
17+
import static org.omnifaces.util.Components.getAttribute;
18+
1519
import javax.faces.application.ResourceDependency;
1620
import javax.faces.component.FacesComponent;
21+
import javax.faces.context.FacesContext;
1722
import javax.faces.event.ComponentSystemEvent;
1823
import javax.faces.event.ListenerFor;
1924
import javax.faces.event.PostAddToViewEvent;
2025
import javax.faces.event.PostRestoreStateEvent;
2126

2227
import org.omnifaces.renderer.DeferredScriptRenderer;
23-
import org.omnifaces.resourcehandler.ResourceIdentifier;
24-
import org.omnifaces.util.Hacks;
2528

2629
/**
2730
* <p>
@@ -51,7 +54,7 @@
5154
* @see ScriptFamily
5255
*/
5356
@FacesComponent(DeferredScript.COMPONENT_TYPE)
54-
@ResourceDependency(library="omnifaces", name="omnifaces.js", target="head")
57+
@ResourceDependency(library=OMNIFACES_LIBRARY_NAME, name=OMNIFACES_SCRIPT_NAME, target="head")
5558
@ListenerFor(systemEventClass=PostAddToViewEvent.class)
5659
@ListenerFor(systemEventClass=PostRestoreStateEvent.class)
5760
public class DeferredScript extends ScriptFamily {
@@ -80,7 +83,8 @@ public DeferredScript() {
8083
@Override
8184
public void processEvent(ComponentSystemEvent event) {
8285
if (moveToBody(event)) {
83-
Hacks.setScriptResourceRendered(getFacesContext(), new ResourceIdentifier(this));
86+
FacesContext context = event.getFacesContext();
87+
context.getApplication().getResourceHandler().markResourceRendered(context, getAttribute(this, "name"), getAttribute(this, "library"));
8488
}
8589
}
8690

src/main/java/org/omnifaces/component/script/ScriptFamily.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected boolean moveToBody(ComponentSystemEvent event) {
107107
return false;
108108
}
109109

110-
FacesContext context = getFacesContext();
110+
FacesContext context = event.getFacesContext();
111111
UIViewRoot view = context.getViewRoot();
112112

113113
if (context.isPostback() ? !view.getComponentResources(context, "body").contains(this) : event instanceof PostAddToViewEvent) {

src/main/java/org/omnifaces/resourcehandler/CombinedResourceHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,16 +498,18 @@ private void addCombined(FacesContext context, UIComponent component, String ren
498498

499499
private void addStylesheet(FacesContext context, UIComponent component, ResourceIdentifier id) {
500500
if (stylesheets.add(component, id)) {
501-
Hacks.setStylesheetResourceRendered(context, id); // Prevents future forced additions by libs.
501+
context.getApplication().getResourceHandler().markResourceRendered(context, id.getName(), id.getLibrary()); // Prevents future forced additions by libs.
502502
}
503503
}
504504

505505
private void addScript(FacesContext context, UIComponent component, ResourceIdentifier id) {
506-
if (Hacks.isScriptResourceRendered(context, id)) { // This is true when o:deferredScript is used.
506+
ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
507+
508+
if (resourceHandler.isResourceRendered(context, id.getName(), id.getLibrary())) { // This is true when o:deferredScript is used.
507509
componentResourcesToRemove.add(component);
508510
}
509511
else if (scripts.add(component, id)) {
510-
Hacks.setScriptResourceRendered(context, id); // Prevents future forced additions by libs.
512+
resourceHandler.markResourceRendered(context, id.getName(), id.getLibrary()); // Prevents future forced additions by libs.
511513
}
512514
}
513515

src/main/java/org/omnifaces/util/Hacks.java

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
import static java.lang.String.format;
1818
import static org.omnifaces.util.Components.getClosestParent;
1919
import static org.omnifaces.util.FacesLocal.getApplicationAttribute;
20-
import static org.omnifaces.util.FacesLocal.getContextAttribute;
2120
import static org.omnifaces.util.FacesLocal.getInitParameter;
2221
import static org.omnifaces.util.FacesLocal.getSessionAttribute;
23-
import static org.omnifaces.util.FacesLocal.setContextAttribute;
2422
import static org.omnifaces.util.Reflection.accessField;
2523
import static org.omnifaces.util.Reflection.instance;
2624
import static org.omnifaces.util.Reflection.invokeMethod;
@@ -31,7 +29,6 @@
3129
import java.io.Serializable;
3230
import java.util.Collection;
3331
import java.util.Collections;
34-
import java.util.HashMap;
3532
import java.util.HashSet;
3633
import java.util.LinkedHashSet;
3734
import java.util.List;
@@ -255,85 +252,6 @@ public static boolean isMyFacesUsed() {
255252

256253
// JSF resource handling related ----------------------------------------------------------------------------------
257254

258-
/**
259-
* Set the given script resource as rendered.
260-
* @param context The involved faces context.
261-
* @param id The resource identifier.
262-
* @since 1.8
263-
*/
264-
public static void setScriptResourceRendered(FacesContext context, ResourceIdentifier id) {
265-
setMojarraResourceRendered(context, id);
266-
267-
if (isMyFacesUsed()) {
268-
setMyFacesResourceRendered(context, MYFACES_RENDERED_SCRIPT_RESOURCES_KEY, id);
269-
}
270-
}
271-
272-
/**
273-
* Returns whether the given script resource is rendered.
274-
* @param context The involved faces context.
275-
* @param id The resource identifier.
276-
* @return Whether the given script resource is rendered.
277-
* @since 1.8
278-
*/
279-
public static boolean isScriptResourceRendered(FacesContext context, ResourceIdentifier id) {
280-
boolean rendered = isMojarraResourceRendered(context, id);
281-
282-
if (!rendered && isMyFacesUsed()) {
283-
return isMyFacesResourceRendered(context, MYFACES_RENDERED_SCRIPT_RESOURCES_KEY, id);
284-
}
285-
else {
286-
return rendered;
287-
}
288-
}
289-
290-
/**
291-
* Set the given stylesheet resource as rendered.
292-
* @param context The involved faces context.
293-
* @param id The resource identifier.
294-
* @since 1.8
295-
*/
296-
public static void setStylesheetResourceRendered(FacesContext context, ResourceIdentifier id) {
297-
setMojarraResourceRendered(context, id);
298-
299-
if (isMyFacesUsed()) {
300-
setMyFacesResourceRendered(context, MYFACES_RENDERED_STYLESHEET_RESOURCES_KEY, id);
301-
}
302-
}
303-
304-
private static void setMojarraResourceRendered(FacesContext context, ResourceIdentifier id) {
305-
context.getAttributes().put(id.getName() + id.getLibrary(), true);
306-
}
307-
308-
private static boolean isMojarraResourceRendered(FacesContext context, ResourceIdentifier id) {
309-
return context.getAttributes().containsKey(id.getName() + id.getLibrary());
310-
}
311-
312-
private static void setMyFacesResourceRendered(FacesContext context, String key, ResourceIdentifier id) {
313-
getMyFacesResourceMap(context, key).put(getMyFacesResourceKey(id), true);
314-
}
315-
316-
private static boolean isMyFacesResourceRendered(FacesContext context, String key, ResourceIdentifier id) {
317-
return getMyFacesResourceMap(context, key).containsKey(getMyFacesResourceKey(id));
318-
}
319-
320-
private static Map<String, Boolean> getMyFacesResourceMap(FacesContext context, String key) {
321-
Map<String, Boolean> map = getContextAttribute(context, key);
322-
323-
if (map == null) {
324-
map = new HashMap<>();
325-
setContextAttribute(context, key, map);
326-
}
327-
328-
return map;
329-
}
330-
331-
private static String getMyFacesResourceKey(ResourceIdentifier id) {
332-
String library = id.getLibrary();
333-
String name = id.getName();
334-
return (library != null) ? (library + '/' + name) : name;
335-
}
336-
337255
/**
338256
* Returns the default resource maximum age in milliseconds.
339257
* @return The default resource maximum age in milliseconds.

src/test/java/org/omnifaces/test/cdi/viewscoped/ViewScopedITBean.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313
package org.omnifaces.test.cdi.viewscoped;
1414

15+
import static javax.faces.application.ResourceHandler.RESOURCE_IDENTIFIER;
1516
import static org.omnifaces.cdi.viewscope.ViewScopeManager.isUnloadRequest;
1617
import static org.omnifaces.util.Faces.getContext;
17-
import static org.omnifaces.util.Faces.getViewId;
1818
import static org.omnifaces.util.Faces.hasContext;
1919
import static org.omnifaces.util.Messages.addGlobalInfo;
2020

@@ -25,6 +25,7 @@
2525
import javax.inject.Named;
2626

2727
import org.omnifaces.cdi.ViewScoped;
28+
import org.omnifaces.util.Faces;
2829

2930
@Named
3031
@ViewScoped
@@ -57,9 +58,11 @@ public void submit() {
5758
addGlobalInfo("submit ");
5859
}
5960

60-
public String navigate() {
61+
public void navigate() {
62+
Object renderedResources = Faces.getContextAttribute(RESOURCE_IDENTIFIER);
63+
Faces.setViewRoot(Faces.getViewId()); // TODO: replace by return getViewId() once Mojarra 2.3.1 is released. See #4249
64+
Faces.setContextAttribute(RESOURCE_IDENTIFIER, renderedResources); // TODO: remove once Mojarra 2.3.1 is released. See #4249
6165
addGlobalInfo("navigate ");
62-
return getViewId();
6366
}
6467

6568
@PreDestroy

src/test/java/org/omnifaces/test/resourcehandler/combinedresourcehandler/CombinedResourceHandlerIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import org.jboss.arquillian.container.test.api.Deployment;
2424
import org.jboss.shrinkwrap.api.spec.WebArchive;
25-
import org.junit.Ignore;
2625
import org.junit.Test;
2726
import org.omnifaces.test.OmniFacesIT;
2827
import org.openqa.selenium.WebElement;
@@ -85,7 +84,7 @@ public void nonAjax() {
8584
verifyElements();
8685
}
8786

88-
@Test @Ignore // TODO: Fails after ajaxRebuild. Make use of JSF 2.3 markResourceRendered() / isResourceRendered() in CombinedResourceHandler.
87+
@Test
8988
public void ajax() {
9089
verifyElements();
9190
guardAjax(ajaxSubmit).click();
@@ -94,7 +93,7 @@ public void ajax() {
9493
verifyElements();
9594
}
9695

97-
@Test @Ignore // TODO: Fails after ajaxRebuild. Make use of JSF 2.3 markResourceRendered() / isResourceRendered() in CombinedResourceHandler.
96+
@Test
9897
public void mixed() {
9998
verifyElements();
10099
guardHttp(nonAjaxSubmit).click();

src/test/java/org/omnifaces/test/resourcehandler/combinedresourcehandler/CombinedResourceHandlerITBean.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.omnifaces.test.resourcehandler.combinedresourcehandler;
1414

15+
import static javax.faces.application.ResourceHandler.RESOURCE_IDENTIFIER;
16+
1517
import javax.enterprise.context.RequestScoped;
1618
import javax.inject.Named;
1719

@@ -22,7 +24,9 @@
2224
public class CombinedResourceHandlerITBean {
2325

2426
public void rebuild() {
27+
Object renderedResources = Faces.getContextAttribute(RESOURCE_IDENTIFIER);
2528
Faces.setViewRoot(Faces.getViewId());
29+
Faces.setContextAttribute(RESOURCE_IDENTIFIER, renderedResources); // TODO: remove once Mojarra 2.3.1 is released. See #4249
2630
}
2731

2832
}

0 commit comments

Comments
 (0)