Skip to content

Commit

Permalink
#463 Skip registering the unload script on stateless views and log warn
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed May 15, 2018
1 parent fb9fb9e commit 2cfa566
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/omnifaces/cdi/viewscope/ViewScopeManager.java
Expand Up @@ -24,10 +24,13 @@
import static org.omnifaces.util.Components.addScriptResourceToBody;
import static org.omnifaces.util.Components.addScriptResourceToHead;
import static org.omnifaces.util.Components.addScriptToBody;
import static org.omnifaces.util.Faces.getViewId;
import static org.omnifaces.util.Faces.getViewRoot;
import static org.omnifaces.util.FacesLocal.getRequestParameter;
import static org.omnifaces.util.FacesLocal.isAjaxRequestWithPartialRendering;

import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -88,8 +91,13 @@ public class ViewScopeManager {
private static final String SCRIPT_INIT = "OmniFaces.Unload.init('%s')";
private static final int DEFAULT_BEANS_PER_VIEW_SCOPE = 3;

private static final String WARNING_UNSUPPORTED_STATE_SAVING = "@ViewScoped %s"
+ " requires non-stateless views in order to be able to properly destroy the bean."
+ " The current view %s is stateless and this may cause memory leaks."
+ " Consider subclassing the bean with @javax.faces.view.ViewScoped annotation.";

private static final String ERROR_INVALID_STATE_SAVING = "@ViewScoped(saveInViewState=true) %s"
+ " requires web.xml context parameter 'javax.faces.STATE_SAVING_METHOD' being set to 'client'.";
+ " requires web.xml context parameter 'javax.faces.STATE_SAVING_METHOD' being set to 'client'.";

// Variables ------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -177,7 +185,12 @@ private <T> BeanStorage getBeanStorage(Contextual<T> type) {
beanStorageId = UUID.randomUUID();

if (storage instanceof ViewScopeStorageInSession) {
registerUnloadScript(beanStorageId);
if (getViewRoot().isTransient()) {
logger.log(Level.WARNING, format(WARNING_UNSUPPORTED_STATE_SAVING, beanClass.getName(), getViewId()));
}
else {
registerUnloadScript(beanStorageId);
}
}
}

Expand Down

0 comments on commit 2cfa566

Please sign in to comment.