You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2016/12/22 01:35:13 UTC
svn commit: r1775568 [2/2] - in /myfaces/core/branches/2.3.x:
api/src/main/java/javax/faces/annotation/
api/src/main/java/javax/faces/component/
impl/src/main/java/org/apache/myfaces/cdi/component/
impl/src/main/java/org/apache/myfaces/cdi/faces/ impl/...
Copied: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeBeanHolder.java (from r1773270, myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeBeanHolder.java)
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeBeanHolder.java?p2=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeBeanHolder.java&p1=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeBeanHolder.java&r1=1773270&r2=1775568&rev=1775568&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeBeanHolder.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeBeanHolder.java Thu Dec 22 01:35:13 2016
@@ -16,136 +16,67 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.flow.cdi;
+package org.apache.myfaces.cdi.viewTransient;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
-import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.spi.BeanManager;
-import javax.faces.context.ExceptionHandler;
-import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
-import javax.faces.flow.Flow;
-import javax.faces.flow.FlowHandler;
-import javax.faces.lifecycle.ClientWindow;
-import javax.inject.Inject;
-import javax.servlet.ServletContext;
import org.apache.myfaces.cdi.util.ContextualInstanceInfo;
import org.apache.myfaces.cdi.util.ContextualStorage;
-import org.apache.myfaces.cdi.view.ApplicationContextBean;
-import org.apache.myfaces.context.ReleaseableExternalContext;
-import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
-import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
-import org.apache.myfaces.flow.util.FlowUtils;
-import org.apache.myfaces.shared.config.MyfacesConfig;
-import org.apache.myfaces.shared.context.ExceptionHandlerImpl;
/**
- *
- * This holder will store the flow scope active ids and it's beans for the current
- * HTTP Session. We use standard SessionScoped bean to not need
- * to treat async-supported and similar headache.
- *
- * @author lu4242
+ * Stateless class to deal with ViewTransientScope. This scope depends on the current FacesContext.
*/
-@SessionScoped
-public class FlowScopeBeanHolder implements Serializable
+public class ViewTransientScopeBeanHolder
{
- /**
- * key: client window id + flow id
- * value: the {@link ContextualStorage} which holds all the
- * {@link javax.enterprise.inject.spi.Bean}s.
- */
- private Map<String, ContextualStorage> storageMap;
-
- private Map<String, List<String>> activeFlowMapKeys;
-
- private FacesFlowClientWindowCollection windowCollection;
-
- public static final String CURRENT_FLOW_SCOPE_MAP = "oam.CURRENT_FLOW_SCOPE_MAP";
- private static final String FLOW_SCOPE_PREFIX = "oam.flow.SCOPE";
+ public static final String VIEW_TRANSIENT_SCOPE_MAP = "oam.VIEW_TRANSIENT_SCOPE_MAP";
- public static final String FLOW_SCOPE_PREFIX_KEY = FLOW_SCOPE_PREFIX+".KEY";
+ public static final String VIEW_TRANSIENT_SCOPE_MAP_INFO = "oam.VIEW_TRANSIENT_SCOPE_MAP_INFO";
- @Inject
- ApplicationContextBean applicationContextBean;
-
- public FlowScopeBeanHolder()
+ public ViewTransientScopeBeanHolder()
{
}
- @PostConstruct
public void init()
{
- storageMap = new ConcurrentHashMap<String, ContextualStorage>();
- activeFlowMapKeys = new ConcurrentHashMap<String, List<String>>();
- windowCollection = null;
-
- FacesContext facesContext = FacesContext.getCurrentInstance();
- this.refreshClientWindow(facesContext);
- facesContext.getExternalContext().getSessionMap().put(FLOW_SCOPE_PREFIX_KEY,
- 1);
}
- /**
- * This method will return the ContextualStorage or create a new one
- * if no one is yet assigned to the current flowClientWindowId.
- * @param beanManager we need the CDI {@link BeanManager} for serialisation.
- * @param flowClientWindowId the flowClientWindowId for the current flow.
- */
- public ContextualStorage getContextualStorage(BeanManager beanManager, String flowClientWindowId)
+ public static ContextualStorage getContextualStorage(BeanManager beanManager, FacesContext facesContext)
{
- ContextualStorage contextualStorage = storageMap.get(flowClientWindowId);
+ ContextualStorage contextualStorage = (ContextualStorage)
+ facesContext.getViewRoot().getTransientStateHelper().getTransient(VIEW_TRANSIENT_SCOPE_MAP);
if (contextualStorage == null)
{
- synchronized (this)
- {
- contextualStorage = storageMap.get(flowClientWindowId);
- if (contextualStorage == null)
- {
- contextualStorage = new ContextualStorage(beanManager, true, true);
- storageMap.put(flowClientWindowId, contextualStorage);
- }
- }
+ contextualStorage = new ContextualStorage(beanManager, false, false);
+ facesContext.getViewRoot().getTransientStateHelper()
+ .putTransient(VIEW_TRANSIENT_SCOPE_MAP, contextualStorage);
}
return contextualStorage;
}
- public ContextualStorage getContextualStorageNoCreate(BeanManager beanManager, String flowClientWindowId)
+ public ContextualStorage getContextualStorageNoCreate(BeanManager beanManager, FacesContext facesContext)
{
- return storageMap.get(flowClientWindowId);
+ return (ContextualStorage) facesContext.getViewRoot()
+ .getTransientStateHelper().getTransient(VIEW_TRANSIENT_SCOPE_MAP);
}
- public Map<String, ContextualStorage> getStorageMap()
- {
- return storageMap;
- }
-
- public Map<Object, Object> getFlowScopeMap(
- BeanManager beanManager, String flowClientWindowId, boolean create)
+ public Map<Object, Object> getFacesScopeMap(
+ BeanManager beanManager, FacesContext facesContext, boolean create)
{
Map<Object, Object> map = null;
if (create)
{
ContextualStorage contextualStorage = getContextualStorage(
- beanManager, flowClientWindowId);
- ContextualInstanceInfo info = contextualStorage.getStorage().get(CURRENT_FLOW_SCOPE_MAP);
+ beanManager, facesContext);
+ ContextualInstanceInfo info = contextualStorage.getStorage().get(VIEW_TRANSIENT_SCOPE_MAP_INFO);
if (info == null)
{
info = new ContextualInstanceInfo<Object>();
- contextualStorage.getStorage().put(CURRENT_FLOW_SCOPE_MAP, info);
+ contextualStorage.getStorage().put(VIEW_TRANSIENT_SCOPE_MAP_INFO, info);
}
map = (Map<Object, Object>) info.getContextualInstance();
if (map == null)
@@ -157,10 +88,10 @@ public class FlowScopeBeanHolder impleme
else
{
ContextualStorage contextualStorage = getContextualStorageNoCreate(
- beanManager, flowClientWindowId);
+ beanManager, facesContext);
if (contextualStorage != null)
{
- ContextualInstanceInfo info = contextualStorage.getStorage().get(CURRENT_FLOW_SCOPE_MAP);
+ ContextualInstanceInfo info = contextualStorage.getStorage().get(VIEW_TRANSIENT_SCOPE_MAP_INFO);
if (info != null)
{
map = (Map<Object, Object>) info.getContextualInstance();
@@ -174,179 +105,21 @@ public class FlowScopeBeanHolder impleme
*
* This method will replace the storageMap and with
* a new empty one.
- * This method can be used to properly destroy the WindowBeanHolder beans
- * without having to sync heavily. Any
- * {@link javax.enterprise.inject.spi.Bean#destroy(Object, javax.enterprise.context.spi.CreationalContext)}
- * should be performed on the returned old storage map.
* @return the old storageMap.
*/
- public Map<String, ContextualStorage> forceNewStorage()
+ public ContextualStorage forceNewStorage(FacesContext facesContext)
{
- Map<String, ContextualStorage> oldStorageMap = storageMap;
- storageMap = new ConcurrentHashMap<String, ContextualStorage>();
- return oldStorageMap;
+ return (ContextualStorage) facesContext.getViewRoot()
+ .getTransientStateHelper().putTransient(VIEW_TRANSIENT_SCOPE_MAP, null);
}
- /**
- * This method properly destroys all current @WindowScoped beans
- * of the active session and also prepares the storage for new beans.
- * It will automatically get called when the session context closes
- * but can also get invoked manually, e.g. if a user likes to get rid
- * of all it's @WindowScoped beans.
- */
- //@PreDestroy
- public void destroyBeans()
- {
- // we replace the old windowBeanHolder beans with a new storage Map
- // an afterwards destroy the old Beans without having to care about any syncs.
- Map<String, ContextualStorage> oldWindowContextStorages = forceNewStorage();
-
- for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
- {
- FlowScopedContextImpl.destroyAllActive(contextualStorage);
- }
- }
-
- /**
- * See description on ViewScopeBeanHolder for details about how this works
- */
- @PreDestroy
- public void destroyBeansOnPreDestroy()
- {
- Map<String, ContextualStorage> oldWindowContextStorages = forceNewStorage();
- if (!oldWindowContextStorages.isEmpty())
- {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- ServletContext servletContext = null;
- if (facesContext == null)
- {
- try
- {
- servletContext = applicationContextBean.getServletContext();
- }
- catch (Throwable e)
- {
- Logger.getLogger(FlowScopeBeanHolder.class.getName()).log(Level.WARNING,
- "Cannot locate servletContext to create FacesContext on @PreDestroy flow scope beans. "
- + "The beans will be destroyed without active FacesContext instance.");
- servletContext = null;
- }
- }
- if (facesContext == null &&
- servletContext != null)
- {
- try
- {
- ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, false);
- ExceptionHandler exceptionHandler = new ExceptionHandlerImpl();
- facesContext = new StartupFacesContextImpl(externalContext,
- (ReleaseableExternalContext) externalContext, exceptionHandler, false);
- for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
- {
- FlowScopedContextImpl.destroyAllActive(contextualStorage);
- }
- }
- finally
- {
- facesContext.release();
- }
- }
- else
- {
- for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
- {
- FlowScopedContextImpl.destroyAllActive(contextualStorage);
- }
- }
- }
- }
-
- public void refreshClientWindow(FacesContext facesContext)
- {
- if (windowCollection == null)
- {
- Integer ft = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).
- getNumberOfFacesFlowClientWindowIdsInSession();
- windowCollection = new FacesFlowClientWindowCollection(new ClientWindowFacesFlowLRUMap(ft));
- }
- ClientWindow cw = facesContext.getExternalContext().getClientWindow();
- if (cw != null && cw.getId() != null)
- {
- windowCollection.setFlowScopeBeanHolder(this);
- windowCollection.put(cw.getId(), "");
- }
- }
-
- public void clearFlowMap(String clientWindowId)
+ public void destroyBeans(FacesContext facesContext)
{
- List<String> activeFlowKeys = activeFlowMapKeys.remove(clientWindowId);
- if (activeFlowKeys != null && !activeFlowKeys.isEmpty())
+ ContextualStorage oldWindowContextStorages = forceNewStorage(facesContext);
+ if (oldWindowContextStorages != null)
{
- for (String flowMapKey : activeFlowKeys)
- {
- ContextualStorage contextualStorage = storageMap.remove(flowMapKey);
- if (contextualStorage != null)
- {
- FlowScopedContextImpl.destroyAllActive(contextualStorage);
- }
- }
+ ViewTransientScopedContextImpl.destroyAllActive(oldWindowContextStorages);
}
}
-
- public List<String> getActiveFlowMapKeys(FacesContext facesContext)
- {
- ClientWindow cw = facesContext.getExternalContext().getClientWindow();
- String baseKey = cw.getId();
- List<String> activeFlowKeys = activeFlowMapKeys.get(baseKey);
- if (activeFlowKeys == null)
- {
- return Collections.emptyList();
- }
- else
- {
- return activeFlowKeys;
- }
- }
-
- public void createCurrentFlowScope(FacesContext facesContext)
- {
- ClientWindow cw = facesContext.getExternalContext().getClientWindow();
- String baseKey = cw.getId();
-
- FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
- Flow flow = flowHandler.getCurrentFlow(facesContext);
- String flowMapKey = FlowUtils.getFlowMapKey(facesContext, flow);
- List<String> activeFlowKeys = activeFlowMapKeys.get(baseKey);
- if (activeFlowKeys == null)
- {
- activeFlowKeys = new ArrayList<String>();
-
- }
- activeFlowKeys.add(0, flowMapKey);
- activeFlowMapKeys.put(baseKey, activeFlowKeys);
- refreshClientWindow(facesContext);
- }
-
- public void destroyCurrentFlowScope(FacesContext facesContext)
- {
- ClientWindow cw = facesContext.getExternalContext().getClientWindow();
- String baseKey = cw.getId();
-
- FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
- Flow flow = flowHandler.getCurrentFlow(facesContext);
- String flowMapKey = FlowUtils.getFlowMapKey(facesContext, flow);
-
- ContextualStorage contextualStorage = storageMap.remove(flowMapKey);
- if (contextualStorage != null)
- {
- FlowScopedContextImpl.destroyAllActive(contextualStorage);
- }
-
- List<String> activeFlowKeys = activeFlowMapKeys.get(baseKey);
- if (activeFlowKeys != null && !activeFlowKeys.isEmpty())
- {
- activeFlowKeys.remove(flowMapKey);
- }
- }
}
Copied: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeContextExtension.java (from r1773270, myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextExtension.java)
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeContextExtension.java?p2=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeContextExtension.java&p1=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextExtension.java&r1=1773270&r2=1775568&rev=1775568&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextExtension.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeContextExtension.java Thu Dec 22 01:35:13 2016
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.cdi.view;
+package org.apache.myfaces.cdi.viewTransient;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
@@ -24,32 +24,29 @@ import javax.enterprise.inject.spi.Annot
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
-import javax.faces.view.ViewScoped;
/**
* Handle ViewScope related features.
- *
- * @author Leonardo Uribe
*/
-public class ViewScopeContextExtension implements Extension
+public class ViewTransientScopeContextExtension implements Extension
{
- private ViewScopeContextImpl viewScopeContext;
+ private ViewTransientScopedContextImpl facesScopeContext;
void beforeBeanDiscovery(
@Observes final BeforeBeanDiscovery event, BeanManager beanManager)
{
- event.addScope(ViewScoped.class, true, true);
- // Register ViewScopeBeanHolder as a bean with CDI annotations, so the system
- // can take it into account, and use it later when necessary.
- AnnotatedType appContext = beanManager.createAnnotatedType(ApplicationContextBean.class);
- event.addAnnotatedType(appContext);
- AnnotatedType bean = beanManager.createAnnotatedType(ViewScopeBeanHolder.class);
- event.addAnnotatedType(bean);
+ event.addScope(ViewTransientScoped.class, true, false);
+
+ AnnotatedType<ViewTransientScopeObjectProducer> viewTransientScopeObjectProducer =
+ beanManager.createAnnotatedType(ViewTransientScopeObjectProducer.class);
+ event.addAnnotatedType(viewTransientScopeObjectProducer);
}
void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
{
- viewScopeContext = new ViewScopeContextImpl(beanManager);
- afterBeanDiscovery.addContext(viewScopeContext);
+ facesScopeContext = new ViewTransientScopedContextImpl(beanManager);
+ afterBeanDiscovery.addContext(facesScopeContext);
+
+ // afterBeanDiscovery.addBean(new DynamicComponentProducer());
}
}
Added: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeObjectProducer.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeObjectProducer.java?rev=1775568&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeObjectProducer.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopeObjectProducer.java Thu Dec 22 01:35:13 2016
@@ -0,0 +1,43 @@
+/*
+ * 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.cdi.viewTransient;
+
+import java.util.Map;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Produces;
+import javax.faces.annotation.ViewMap;
+import javax.faces.context.FacesContext;
+
+/**
+ * This class produces all objects that are bound or related to FacesContext
+ */
+@ApplicationScoped
+public class ViewTransientScopeObjectProducer
+{
+
+ @Produces
+ @ViewMap
+ @ViewTransientScoped
+ public Map<String, Object> getViewMap()
+ {
+ return FacesContext.getCurrentInstance().getViewRoot().getViewMap();
+ }
+
+}
Added: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScoped.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScoped.java?rev=1775568&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScoped.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScoped.java Thu Dec 22 01:35:13 2016
@@ -0,0 +1,40 @@
+/*
+ * 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.cdi.viewTransient;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.enterprise.context.NormalScope;
+
+/**
+ *
+ */
+@NormalScope
+@Inherited
+@Documented
+@Target(value={ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
+@Retention(value= RetentionPolicy.RUNTIME)
+public @interface ViewTransientScoped
+{
+
+}
Copied: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopedContextImpl.java (from r1773270, myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopedContextImpl.java)
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopedContextImpl.java?p2=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopedContextImpl.java&p1=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopedContextImpl.java&r1=1773270&r2=1775568&rev=1775568&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopedContextImpl.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/viewTransient/ViewTransientScopedContextImpl.java Thu Dec 22 01:35:13 2016
@@ -16,134 +16,72 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.flow.cdi;
+package org.apache.myfaces.cdi.viewTransient;
import java.lang.annotation.Annotation;
-import java.util.List;
import java.util.Map;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Typed;
-import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.faces.context.FacesContext;
-import javax.faces.flow.Flow;
-import javax.faces.flow.FlowHandler;
-import javax.faces.flow.FlowScoped;
-import org.apache.myfaces.cdi.util.BeanProvider;
import org.apache.myfaces.cdi.util.ContextualInstanceInfo;
import org.apache.myfaces.cdi.util.ContextualStorage;
-import org.apache.myfaces.flow.FlowReference;
-import org.apache.myfaces.flow.util.FlowUtils;
/**
- * Minimal implementation of FlowScope.
- *
- * @TODO: We need something better for this part. The problem is the beans
- * are just put under the session map using the old know SubKeyMap hack used
- * in Flash object, but CDI implementations like OWB solves the passivation
- * problem better. The ideal is provide a myfaces specific SPI interface, to
- * allow provide custom implementation of this detail.
- *
- * @TODO: FlowHandler.transition() method should call this object when the
- * user enter or exit a flow.
- *
- * @author Leonardo Uribe
+ * Minimal implementation of FacesScope.
*/
@Typed()
-public class FlowScopedContextImpl implements Context
+public class ViewTransientScopedContextImpl implements Context
{
- /**
- * Whether the Context is for a passivating scope.
- */
- private final boolean passivatingScope;
- /**
- * needed for serialisation and passivationId
- */
private BeanManager beanManager;
- private Map<Class, FlowReference> flowBeanReferences;
-
- //private FlowScopeBeanHolder flowScopeBeanHolder;
-
- public FlowScopedContextImpl(BeanManager beanManager,
- Map<Class, FlowReference> flowBeanReferences)
+ public ViewTransientScopedContextImpl(BeanManager beanManager)
{
this.beanManager = beanManager;
- this.flowBeanReferences = flowBeanReferences;
- passivatingScope = beanManager.isPassivatingScope(getScope());
}
- /*
- public void initFlowContext(FlowScopeBeanHolder flowScopeBeanHolder)
+ protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder()
{
- this.flowScopeBeanHolder = flowScopeBeanHolder;
- }*/
-
- protected FlowScopeBeanHolder getFlowScopeBeanHolder()
- {
- return getFlowScopeBeanHolder(FacesContext.getCurrentInstance());
+ return getViewTransientScopeBeanHolder(FacesContext.getCurrentInstance());
}
- protected FlowScopeBeanHolder getFlowScopeBeanHolder(FacesContext facesContext)
+ protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder(FacesContext facesContext)
{
- FlowScopeBeanHolder flowScopeBeanHolder = (FlowScopeBeanHolder)
- facesContext.getExternalContext().getApplicationMap().get(
- "oam.flow.FlowScopeBeanHolder");
- if (flowScopeBeanHolder == null)
- {
- flowScopeBeanHolder = BeanProvider.getContextualReference(
- beanManager, FlowScopeBeanHolder.class, false);
- facesContext.getExternalContext().getApplicationMap().put(
- "oam.flow.FlowScopeBeanHolder", flowScopeBeanHolder);
- }
- return flowScopeBeanHolder;
+ return new ViewTransientScopeBeanHolder();
}
- public String getCurrentClientWindowFlowId(FacesContext facesContext)
- {
- String flowMapKey = null;
- FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
- Flow flow = flowHandler.getCurrentFlow(facesContext);
- if (flow != null)
- {
- flowMapKey = FlowUtils.getFlowMapKey(facesContext, flow);
- }
- return flowMapKey;
- }
-
/**
* An implementation has to return the underlying storage which
* contains the items held in the Context.
* @param createIfNotExist whether a ContextualStorage shall get created if it doesn't yet exist.
* @return the underlying storage
*/
- protected ContextualStorage getContextualStorage(boolean createIfNotExist, String clientWindowFlowId)
+ protected ContextualStorage getContextualStorage(boolean createIfNotExist, FacesContext facesContext)
{
- //FacesContext facesContext = FacesContext.getCurrentInstance();
- //String clientWindowFlowId = getCurrentClientWindowFlowId(facesContext);
- if (clientWindowFlowId == null)
+ if (facesContext == null)
{
- throw new ContextNotActiveException("FlowScopedContextImpl: no current active flow");
+ throw new ContextNotActiveException("FacesScopedContextImpl: no current active facesContext");
}
if (createIfNotExist)
{
- return getFlowScopeBeanHolder().getContextualStorage(beanManager, clientWindowFlowId);
+ return getViewTransientScopeBeanHolder(facesContext).getContextualStorage(beanManager, facesContext);
}
else
{
- return getFlowScopeBeanHolder().getContextualStorageNoCreate(beanManager, clientWindowFlowId);
+ return getViewTransientScopeBeanHolder(facesContext).getContextualStorageNoCreate(
+ beanManager, facesContext);
}
}
public Class<? extends Annotation> getScope()
{
- return FlowScoped.class;
+ return ViewTransientScoped.class;
}
public boolean isActive()
@@ -157,18 +95,11 @@ public class FlowScopedContextImpl imple
{
return false;
}
- Flow flow = facesContext.getApplication().
- getFlowHandler().getCurrentFlow(facesContext);
-
- return flow != null;
- }
-
- /**
- * @return whether the served scope is a passivating scope
- */
- public boolean isPassivatingScope()
- {
- return passivatingScope;
+ else if (facesContext.getViewRoot() == null)
+ {
+ return false;
+ }
+ return true;
}
@Override
@@ -178,149 +109,41 @@ public class FlowScopedContextImpl imple
checkActive(facesContext);
-
- FlowReference reference = flowBeanReferences.get(((Bean)bean).getBeanClass());
- if (reference != null)
+ if (facesContext != null)
{
- String flowMapKey = FlowUtils.getFlowMapKey(facesContext, reference);
- if (flowMapKey != null)
+ ContextualStorage storage = getContextualStorage(false, facesContext);
+ if (storage != null)
{
- ContextualStorage storage = getContextualStorage(false, flowMapKey);
- if (storage != null)
- {
- Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
- ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
+ Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
+ ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
- if (contextualInstanceInfo != null)
- {
- return (T) contextualInstanceInfo.getContextualInstance();
- }
+ if (contextualInstanceInfo != null)
+ {
+ return (T) contextualInstanceInfo.getContextualInstance();
}
}
- else
- {
- throw new IllegalStateException("Flow "+ reference.getId()+
- " cannot be found when resolving bean " +bean.toString());
- }
}
-
- List<String> activeFlowMapKeys = getFlowScopeBeanHolder().getActiveFlowMapKeys(facesContext);
- for (String flowMapKey : activeFlowMapKeys)
+ else
{
- ContextualStorage storage = getContextualStorage(false, flowMapKey);
- if (storage == null)
- {
- //return null;
- continue;
- }
-
- Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
- ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
- if (contextualInstanceInfo == null)
- {
- //return null;
- continue;
- }
-
- return (T) contextualInstanceInfo.getContextualInstance();
+ throw new IllegalStateException("FacesContext cannot be found when resolving bean " +bean.toString());
}
return null;
}
- @Override
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
checkActive(facesContext);
- if (passivatingScope && !(bean instanceof PassivationCapable))
+ if (!(bean instanceof PassivationCapable))
{
throw new IllegalStateException(bean.toString() +
" doesn't implement " + PassivationCapable.class.getName());
}
-
- FlowReference reference = flowBeanReferences.get(((Bean)bean).getBeanClass());
- if (reference != null)
- {
- String flowMapKey = FlowUtils.getFlowMapKey(facesContext, reference);
- if (flowMapKey != null)
- {
- ContextualStorage storage = getContextualStorage(false, flowMapKey);
- if (storage != null)
- {
- Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
- ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
- if (contextualInstanceInfo != null)
- {
- return (T) contextualInstanceInfo.getContextualInstance();
- }
- }
- }
- else
- {
- throw new IllegalStateException("Flow "+ reference.getId()+
- " cannot be found when resolving bean " + bean.toString());
- }
-
- FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
- // Since it is possible to have only the flow id without documentId, the best
- // is first get the flow using flowHandler.getFlow and then check if the flow is
- // active or not, but using the documentId and id of the retrieved flow.
- Flow flow = flowHandler.getFlow(facesContext,
- reference.getDocumentId() == null ? "" : reference.getDocumentId(), reference.getId());
- if (flow == null)
- {
- throw new IllegalStateException(bean.toString() + "cannot be created because flow "+ reference.getId()+
- " is not registered");
- }
- if (!flowHandler.isActive(facesContext, flow.getDefiningDocumentId(), flow.getId()))
- {
- throw new IllegalStateException(bean.toString() + "cannot be created if flow "+ reference.getId()+
- " is not active");
- }
-
- ContextualStorage storage = getContextualStorage(true, flowMapKey);
- Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
- ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
-
- if (contextualInstanceInfo != null)
- {
- @SuppressWarnings("unchecked")
- final T instance = (T) contextualInstanceInfo.getContextualInstance();
-
- if (instance != null)
- {
- return instance;
- }
- }
-
- return storage.createContextualInstance(bean, creationalContext);
- }
-
- List<String> activeFlowMapKeys = getFlowScopeBeanHolder().getActiveFlowMapKeys(facesContext);
- for (String flowMapKey : activeFlowMapKeys)
- {
- ContextualStorage storage = getContextualStorage(false, flowMapKey);
-
- Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
- ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
-
- if (contextualInstanceInfo != null)
- {
- @SuppressWarnings("unchecked")
- final T instance = (T) contextualInstanceInfo.getContextualInstance();
-
- if (instance != null)
- {
- return instance;
- }
- }
+ ContextualStorage storage = getContextualStorage(true, facesContext);
- }
-
- ContextualStorage storage = getContextualStorage(true, getCurrentClientWindowFlowId(facesContext));
Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
@@ -334,7 +157,7 @@ public class FlowScopedContextImpl imple
return instance;
}
}
-
+
return storage.createContextualInstance(bean, creationalContext);
}
@@ -346,44 +169,21 @@ public class FlowScopedContextImpl imple
public boolean destroy(Contextual bean)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
- List<String> activeFlowMapKeys = getFlowScopeBeanHolder().getActiveFlowMapKeys(facesContext);
- for (String flowMapKey : activeFlowMapKeys)
+ ContextualStorage storage = getContextualStorage(false, facesContext);
+ if (storage == null)
{
- ContextualStorage storage = getContextualStorage(false, flowMapKey);
- if (storage == null)
- {
- //return false;
- continue;
- }
- ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
-
- if (contextualInstanceInfo == null)
- {
- //return false;
- continue;
- }
-
- bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
- return true;
+ return false;
}
- return false;
- }
+ ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
- /**
- * destroys all the Contextual Instances in the Storage returned by
- * {@link #getContextualStorage(boolean)}.
- */
- /*
- public void destroyAllActive()
- {
- ContextualStorage storage = getContextualStorage(false);
- if (storage == null)
+ if (contextualInstanceInfo == null)
{
- return;
+ return false;
}
- destroyAllActive(storage);
- }*/
+ bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
+ return true;
+ }
/**
* Destroys all the Contextual Instances in the specified ContextualStorage.
@@ -395,7 +195,7 @@ public class FlowScopedContextImpl imple
Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
{
- if (!FlowScopeBeanHolder.CURRENT_FLOW_SCOPE_MAP.equals(entry.getKey()))
+ if (!ViewTransientScopeBeanHolder.VIEW_TRANSIENT_SCOPE_MAP_INFO.equals(entry.getKey()))
{
Contextual bean = storage.getBean(entry.getKey());
Modified: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java?rev=1775568&r1=1775567&r2=1775568&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java Thu Dec 22 01:35:13 2016
@@ -40,10 +40,12 @@ import javax.faces.render.RenderKitFacto
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import org.apache.myfaces.cdi.faces.FacesScopeProvider;
import org.apache.myfaces.context.ReleaseableExternalContext;
import org.apache.myfaces.context.ReleaseableFacesContextFactory;
import org.apache.myfaces.shared.util.NullIterator;
+import org.apache.myfaces.util.ExternalSpecifications;
/**
* @author Manfred Geiler (latest modification by $Author$)
@@ -132,6 +134,10 @@ public class FacesContextImpl extends Fa
public final void release()
{
assertNotReleased();
+ if (ExternalSpecifications.isCDIAvailable(getExternalContext()))
+ {
+ FacesScopeProvider.destroyBeans(this);
+ }
_messages = null;
_orderedMessages = null;
Modified: myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1775568&r1=1775567&r2=1775568&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension Thu Dec 22 01:35:13 2016
@@ -4,3 +4,6 @@ org.apache.myfaces.flow.cdi.FlowScopeCDI
org.apache.myfaces.cdi.dependent.DependentBeanExtension
org.apache.myfaces.config.annotation.CdiAnnotationProviderExtension
org.apache.myfaces.push.cdi.PushContextCDIExtension
+org.apache.myfaces.cdi.faces.FacesScopeContextExtension
+org.apache.myfaces.cdi.viewTransient.ViewTransientScopeContextExtension
+org.apache.myfaces.cdi.component.ComponentBindingExtension