You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2018/10/30 17:49:17 UTC
[myfaces] branch master updated: refactored + fixed PreDestroy on
ViewScoped and FlowScoped
This is an automated email from the ASF dual-hosted git repository.
tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git
The following commit(s) were added to refs/heads/master by this push:
new 45e1ed9 refactored + fixed PreDestroy on ViewScoped and FlowScoped
45e1ed9 is described below
commit 45e1ed9e9631e435ea8779f82f3c2984bc51a14b
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Tue Oct 30 18:49:10 2018 +0100
refactored + fixed PreDestroy on ViewScoped and FlowScoped
---
...Provider.java => CDIInjectionProviderImpl.java} | 6 +-
.../CDIViewScopeProviderImpl.java} | 37 ++------
.../flow/cdi/DefaultCDIFacesFlowProvider.java | 34 +++++++
.../flow/impl/DefaultFacesFlowProvider.java | 6 ++
.../org/apache/myfaces/spi/FacesFlowProvider.java | 2 +
.../CDIAnnotationDelegateInjectionProvider.java | 3 +-
.../spi/impl/DefaultInjectionProviderFactory.java | 18 ----
.../spi/impl/DefaultViewScopeProviderFactory.java | 5 +-
.../myfaces/webapp/AbstractFacesInitializer.java | 23 ++++-
.../webapp/MyFacesContainerInitializer.java | 9 ++
.../myfaces/webapp/MyFacesHttpSessionListener.java | 105 +++++++++++++++++++++
11 files changed, 189 insertions(+), 59 deletions(-)
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/impl/CDIAnnotationInjectionProvider.java b/impl/src/main/java/org/apache/myfaces/cdi/CDIInjectionProviderImpl.java
similarity index 93%
rename from impl/src/main/java/org/apache/myfaces/cdi/impl/CDIAnnotationInjectionProvider.java
rename to impl/src/main/java/org/apache/myfaces/cdi/CDIInjectionProviderImpl.java
index f02335c..3f2730c 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/impl/CDIAnnotationInjectionProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/CDIInjectionProviderImpl.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.cdi.impl;
+package org.apache.myfaces.cdi;
import org.apache.myfaces.cdi.util.CDIUtils;
import org.apache.myfaces.spi.InjectionProvider;
@@ -32,11 +32,11 @@ import javax.faces.context.ExternalContext;
*
* @author Leonardo Uribe
*/
-public class CDIAnnotationInjectionProvider extends InjectionProvider
+public class CDIInjectionProviderImpl extends InjectionProvider
{
private BeanManager beanManager;
- public CDIAnnotationInjectionProvider(ExternalContext externalContext)
+ public CDIInjectionProviderImpl(ExternalContext externalContext)
{
beanManager = CDIUtils.getBeanManager(externalContext);
}
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/impl/CDIManagedBeanHandlerImpl.java b/impl/src/main/java/org/apache/myfaces/cdi/view/CDIViewScopeProviderImpl.java
similarity index 79%
rename from impl/src/main/java/org/apache/myfaces/cdi/impl/CDIManagedBeanHandlerImpl.java
rename to impl/src/main/java/org/apache/myfaces/cdi/view/CDIViewScopeProviderImpl.java
index 66d7cf6..025a92f 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/impl/CDIManagedBeanHandlerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/CDIViewScopeProviderImpl.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.cdi.impl;
+package org.apache.myfaces.cdi.view;
import java.util.Map;
import javax.enterprise.inject.spi.BeanManager;
@@ -26,25 +26,21 @@ import javax.servlet.ServletContext;
import org.apache.myfaces.cdi.util.BeanProvider;
import org.apache.myfaces.cdi.util.CDIUtils;
import org.apache.myfaces.cdi.JsfApplicationArtifactHolder;
-import org.apache.myfaces.cdi.view.ViewScopeBeanHolder;
-import org.apache.myfaces.cdi.view.ViewScopeCDIMap;
-import org.apache.myfaces.flow.cdi.FlowScopeBeanHolder;
import org.apache.myfaces.spi.ViewScopeProvider;
/**
*
* @author Leonardo Uribe
*/
-public class CDIManagedBeanHandlerImpl extends ViewScopeProvider
+public class CDIViewScopeProviderImpl extends ViewScopeProvider
{
private BeanManager beanManager;
private ViewScopeBeanHolder viewScopeBeanHolder;
- private FlowScopeBeanHolder flowScopeBeanHolder;
- public CDIManagedBeanHandlerImpl()
+ public CDIViewScopeProviderImpl()
{
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
beanManager = CDIUtils.getBeanManager(externalContext);
@@ -66,27 +62,20 @@ public class CDIManagedBeanHandlerImpl extends ViewScopeProvider
}
return viewScopeBeanHolder;
}
-
- private FlowScopeBeanHolder getFlowScopeBeanHolder()
- {
- if (flowScopeBeanHolder == null)
- {
- flowScopeBeanHolder = BeanProvider.getContextualReference(
- beanManager, FlowScopeBeanHolder.class, false);
- }
- return flowScopeBeanHolder;
- }
-
+
+ @Override
public Map<String, Object> createViewScopeMap(FacesContext facesContext, String viewScopeId)
{
return new ViewScopeCDIMap(facesContext, viewScopeId);
}
+ @Override
public Map<String, Object> restoreViewScopeMap(FacesContext facesContext, String viewScopeId)
{
return new ViewScopeCDIMap(facesContext, viewScopeId);
}
+ @Override
public String generateViewScopeId(FacesContext facesContext)
{
return getViewScopeBeanHolder().generateUniqueViewScopeId();
@@ -95,6 +84,7 @@ public class CDIManagedBeanHandlerImpl extends ViewScopeProvider
/**
*
*/
+ @Override
public void onSessionDestroyed()
{
// In CDI case, the best way to deal with this is use a method
@@ -112,10 +102,6 @@ public class CDIManagedBeanHandlerImpl extends ViewScopeProvider
{
getViewScopeBeanHolder().destroyBeans();
}
- if (isFlowScopeBeanHolderCreated(facesContext))
- {
- getFlowScopeBeanHolder().destroyBeans();
- }
}
}
}
@@ -125,13 +111,6 @@ public class CDIManagedBeanHandlerImpl extends ViewScopeProvider
return facesContext.getExternalContext().
getSessionMap().containsKey(ViewScopeBeanHolder.VIEW_SCOPE_PREFIX_KEY);
}
-
-
- private boolean isFlowScopeBeanHolderCreated(FacesContext facesContext)
- {
- return facesContext.getExternalContext().
- getSessionMap().containsKey(FlowScopeBeanHolder.FLOW_SCOPE_PREFIX_KEY);
- }
@Override
public void destroyViewScopeMap(FacesContext facesContext, String viewScopeId)
diff --git a/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java b/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java
index 3a7603b..7ced9ef 100644
--- a/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java
@@ -26,6 +26,7 @@ import java.util.logging.Logger;
import javax.enterprise.inject.spi.BeanManager;
import javax.faces.context.FacesContext;
import javax.faces.flow.Flow;
+import org.apache.myfaces.cdi.util.BeanProvider;
import org.apache.myfaces.cdi.util.CDIUtils;
import org.apache.myfaces.flow.FlowUtils;
import org.apache.myfaces.spi.FacesFlowProvider;
@@ -43,6 +44,39 @@ public class DefaultCDIFacesFlowProvider extends FacesFlowProvider
static final char SEPARATOR_CHAR = '.';
+ private boolean isFlowScopeBeanHolderCreated(FacesContext facesContext)
+ {
+ return facesContext.getExternalContext().
+ getSessionMap().containsKey(FlowScopeBeanHolder.FLOW_SCOPE_PREFIX_KEY);
+ }
+
+ @Override
+ public void onSessionDestroyed()
+ {
+ // In CDI case, the best way to deal with this is use a method
+ // with @PreDestroy annotation on a session scope bean
+ // ( ViewScopeBeanHolder.destroyBeans() ). There is no need
+ // to do anything else in this location, but it is advised
+ // in CDI the beans are destroyed at the end of the request,
+ // not when invalidateSession() is called.
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ if (facesContext != null)
+ {
+ if (facesContext.getExternalContext().getSession(false) != null)
+ {
+ if (isFlowScopeBeanHolderCreated(facesContext))
+ {
+ FlowScopeBeanHolder flowScopeBeanHolder = BeanProvider.getContextualReference(
+ _beanManager, FlowScopeBeanHolder.class, false);
+ if (flowScopeBeanHolder != null)
+ {
+ flowScopeBeanHolder.destroyBeans();
+ }
+ }
+ }
+ }
+ }
+
@Override
public Iterator<Flow> getAnnotatedFlows(FacesContext facesContext)
{
diff --git a/impl/src/main/java/org/apache/myfaces/flow/impl/DefaultFacesFlowProvider.java b/impl/src/main/java/org/apache/myfaces/flow/impl/DefaultFacesFlowProvider.java
index daaf11e..8b20413 100644
--- a/impl/src/main/java/org/apache/myfaces/flow/impl/DefaultFacesFlowProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/flow/impl/DefaultFacesFlowProvider.java
@@ -133,4 +133,10 @@ public class DefaultFacesFlowProvider extends FacesFlowProvider
}
return map;
}
+
+ @Override
+ public void onSessionDestroyed()
+ {
+
+ }
}
diff --git a/impl/src/main/java/org/apache/myfaces/spi/FacesFlowProvider.java b/impl/src/main/java/org/apache/myfaces/spi/FacesFlowProvider.java
index 0884027..34abe0c 100644
--- a/impl/src/main/java/org/apache/myfaces/spi/FacesFlowProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/spi/FacesFlowProvider.java
@@ -29,6 +29,8 @@ import javax.faces.flow.Flow;
*/
public abstract class FacesFlowProvider
{
+ public abstract void onSessionDestroyed();
+
public abstract Iterator<Flow> getAnnotatedFlows(FacesContext facesContext);
public abstract void doAfterEnterFlow(FacesContext context, Flow flow);
diff --git a/impl/src/main/java/org/apache/myfaces/spi/impl/CDIAnnotationDelegateInjectionProvider.java b/impl/src/main/java/org/apache/myfaces/spi/impl/CDIAnnotationDelegateInjectionProvider.java
index 0ae237a..6563ef8 100644
--- a/impl/src/main/java/org/apache/myfaces/spi/impl/CDIAnnotationDelegateInjectionProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/spi/impl/CDIAnnotationDelegateInjectionProvider.java
@@ -19,6 +19,7 @@
package org.apache.myfaces.spi.impl;
import javax.faces.context.ExternalContext;
+import org.apache.myfaces.cdi.CDIInjectionProviderImpl;
import org.apache.myfaces.shared.util.ClassUtils;
import org.apache.myfaces.spi.InjectionProvider;
import org.apache.myfaces.spi.InjectionProviderException;
@@ -39,7 +40,7 @@ public class CDIAnnotationDelegateInjectionProvider extends InjectionProvider
try
{
Class clazz = ClassUtils.simpleClassForName(
- "org.apache.myfaces.cdi.impl.CDIAnnotationInjectionProvider");
+ CDIInjectionProviderImpl.class.getName());
delegate = (InjectionProvider) clazz.getConstructor(
ExternalContext.class).newInstance(externalContext);
}
diff --git a/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultInjectionProviderFactory.java b/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultInjectionProviderFactory.java
index b1eac57..daaf6b0 100644
--- a/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultInjectionProviderFactory.java
+++ b/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultInjectionProviderFactory.java
@@ -267,24 +267,6 @@ public class DefaultInjectionProviderFactory extends InjectionProviderFactory
private InjectionProvider resolveFallbackInjectionProvider(ExternalContext externalContext)
{
- /* Added entry in META-INF/services/org.apache.myfaces.spi.InjectionProvider to
- * give precedence to CDI integration instead server integration.
- if (ExternalSpecifications.isCDIAvailable(externalContext))
- {
- try
- {
- Class clazz = ClassUtils.simpleClassForName(
- "org.apache.myfaces.cdi.impl.CDIAnnotationInjectionProvider");
- return (InjectionProvider) clazz.getConstructor(
- ExternalContext.class).newInstance(externalContext);
- }
- catch(Exception e)
- {
- //Ignore
- }
- }
- */
-
try
{
ClassUtils.classForName("javax.annotation.PreDestroy");
diff --git a/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultViewScopeProviderFactory.java b/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultViewScopeProviderFactory.java
index 5347915..f64572b 100644
--- a/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultViewScopeProviderFactory.java
+++ b/impl/src/main/java/org/apache/myfaces/spi/impl/DefaultViewScopeProviderFactory.java
@@ -19,6 +19,7 @@
package org.apache.myfaces.spi.impl;
import javax.faces.context.ExternalContext;
+import org.apache.myfaces.cdi.view.CDIViewScopeProviderImpl;
import org.apache.myfaces.shared.util.ClassUtils;
import org.apache.myfaces.spi.ViewScopeProvider;
import org.apache.myfaces.spi.ViewScopeProviderFactory;
@@ -45,9 +46,7 @@ public class DefaultViewScopeProviderFactory extends ViewScopeProviderFactory
{
if (ExternalSpecifications.isCDIAvailable(externalContext))
{
- returnValue = (ViewScopeProvider) ClassUtils.newInstance(
- "org.apache.myfaces.cdi.impl.CDIManagedBeanHandlerImpl");
- //CDIManagedBeanHandler.getInstance(externalContext);
+ returnValue = (ViewScopeProvider) ClassUtils.newInstance(CDIViewScopeProviderImpl.class.getName());
}
// cache the result on the ApplicationMap
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java b/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
index 2284ad7..c0d7752 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
@@ -68,6 +68,8 @@ import org.apache.myfaces.push.EndpointImpl;
import org.apache.myfaces.push.WebsocketConfigurator;
import org.apache.myfaces.push.WebsocketFacesInit;
import org.apache.myfaces.shared.util.ClassUtils;
+import org.apache.myfaces.spi.FacesFlowProvider;
+import org.apache.myfaces.spi.FacesFlowProviderFactory;
import org.apache.myfaces.spi.ServiceProviderFinder;
import org.apache.myfaces.spi.ServiceProviderFinderFactory;
import org.apache.myfaces.view.facelets.ViewPoolProcessor;
@@ -185,12 +187,23 @@ public abstract class AbstractFacesInitializer implements FacesInitializer
initContainerIntegration(servletContext, externalContext);
- ViewScopeProviderFactory factory = ViewScopeProviderFactory.getViewScopeHandlerFactory(
- externalContext);
-
- ViewScopeProvider viewScopeHandler = factory.getViewScopeHandler(
- externalContext);
+ ViewScopeProviderFactory viewScopeProviderFactory =
+ ViewScopeProviderFactory.getViewScopeHandlerFactory(externalContext);
+ ViewScopeProvider viewScopeProvider = viewScopeProviderFactory.getViewScopeHandler(externalContext);
+ FacesFlowProviderFactory facesFlowProviderFactory =
+ FacesFlowProviderFactory.getFacesFlowProviderFactory(externalContext);
+ FacesFlowProvider facesFlowProvider = facesFlowProviderFactory.getFacesFlowProvider(externalContext);
+
+ MyFacesHttpSessionListener listener = (MyFacesHttpSessionListener)
+ externalContext.getApplicationMap().get(
+ MyFacesHttpSessionListener.APPLICATION_MAP_KEY);
+ if (listener != null)
+ {
+ listener.setViewScopeProvider(viewScopeProvider);
+ listener.setFacesFlowProvider(facesFlowProvider);
+ }
+
String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION);
if ("false".equals(useEncryption))
{
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
index 2a31869..9675f94 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
@@ -127,6 +127,15 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
{
log.log(Level.INFO, "Using " + MyFacesContainerInitializer.class.getName());
+
+ MyFacesHttpSessionListener httpSessionListener = new MyFacesHttpSessionListener();
+ servletContext.addListener(httpSessionListener);
+ // Publishes the MyFacesHttpSessionListener instance into the servletContext.
+ // This allows the FacesConfigurator to access the instance and to set the
+ // correct ManagedBeanDestroyer instance on it.
+ servletContext.setAttribute(MyFacesHttpSessionListener.APPLICATION_MAP_KEY, httpSessionListener);
+
+
boolean startDireclty = shouldStartupRegardless(servletContext);
if (startDireclty)
{
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesHttpSessionListener.java b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesHttpSessionListener.java
new file mode 100644
index 0000000..d81a054
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesHttpSessionListener.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.webapp;
+
+import javax.faces.context.ExceptionHandler;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+import org.apache.myfaces.context.ExceptionHandlerImpl;
+import org.apache.myfaces.context.ReleaseableExternalContext;
+import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
+import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
+import org.apache.myfaces.spi.FacesFlowProvider;
+import org.apache.myfaces.spi.ViewScopeProvider;
+
+public class MyFacesHttpSessionListener implements HttpSessionListener
+{
+ public static final String APPLICATION_MAP_KEY = MyFacesHttpSessionListener.class.getName();
+
+ private ViewScopeProvider viewScopeProvider = null;
+ private FacesFlowProvider facesFlowProvider = null;
+
+ public void setViewScopeProvider(ViewScopeProvider viewScopeProvider)
+ {
+ this.viewScopeProvider = viewScopeProvider;
+ }
+
+ public void setFacesFlowProvider(FacesFlowProvider facesFlowProvider)
+ {
+ this.facesFlowProvider = facesFlowProvider;
+ }
+
+ @Override
+ public void sessionCreated(HttpSessionEvent event)
+ {
+ }
+
+ @Override
+ public void sessionDestroyed(HttpSessionEvent event)
+ {
+ // If we don't propagate this event, CDI will do for us but outside JSF control
+ // so when @PreDestroy methods are called there will not be an active FacesContext.
+ // The trick here is ensure clean the affected scopes to avoid duplicates.
+ // Remember cdi session scope is different from jsf session scope, because in
+ // jsf case the beans are stored under a session attribute, so it has the problem
+ // with attributeRemoved, but on cdi a wrapper is used instead, avoiding the problem.
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ if (facesContext != null)
+ {
+ if (viewScopeProvider != null)
+ {
+ viewScopeProvider.onSessionDestroyed();
+ }
+ if (facesFlowProvider != null)
+ {
+ facesFlowProvider.onSessionDestroyed();
+ }
+ }
+ else
+ {
+ // In case no FacesContext is available, we are on session invalidation
+ // through timeout. In that case, create a dummy FacesContext for this one
+ // like the one used in startup or shutdown and invoke the destroy method.
+ try
+ {
+ ServletContext servletContext = event.getSession().getServletContext();
+ ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, false);
+ ExceptionHandler exceptionHandler = new ExceptionHandlerImpl();
+ facesContext = new StartupFacesContextImpl(externalContext,
+ (ReleaseableExternalContext) externalContext, exceptionHandler, false);
+
+ if (viewScopeProvider != null)
+ {
+ viewScopeProvider.onSessionDestroyed();
+ }
+ if (facesFlowProvider != null)
+ {
+ facesFlowProvider.onSessionDestroyed();
+ }
+ }
+ finally
+ {
+ facesContext.release();
+ }
+ }
+ }
+}