You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2012/09/30 00:47:06 UTC

[1/2] git commit: DELTASPIKE-266 use AbstractContext for ViewScopedExtension

Updated Branches:
  refs/heads/master b29dab065 -> c9a8713e4


DELTASPIKE-266 use AbstractContext for  ViewScopedExtension


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/c9a8713e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/c9a8713e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/c9a8713e

Branch: refs/heads/master
Commit: c9a8713e4fe98675d874adde3b6ba73606a27112
Parents: 2385c87
Author: Mark Struberg <st...@apache.org>
Authored: Sun Sep 30 00:46:21 2012 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Sun Sep 30 00:46:21 2012 +0200

----------------------------------------------------------------------
 .../jsf/impl/scope/view/MockViewScopedContext.java |    6 +
 .../jsf/impl/scope/view/ViewScopedContext.java     |  147 ++++-----------
 .../jsf/impl/scope/view/ViewScopedExtension.java   |    7 +-
 3 files changed, 46 insertions(+), 114 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c9a8713e/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/MockViewScopedContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/MockViewScopedContext.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/MockViewScopedContext.java
index 75d60ea..6410cf1 100644
--- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/MockViewScopedContext.java
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/MockViewScopedContext.java
@@ -18,6 +18,7 @@
  */
 package org.apache.deltaspike.jsf.impl.scope.view;
 
+import javax.enterprise.inject.spi.BeanManager;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -41,6 +42,11 @@ public class MockViewScopedContext extends ViewScopedContext
 
     private static Map<String, Object> mockViewMap = new ConcurrentHashMap<String, Object>();
 
+    public MockViewScopedContext(BeanManager beanManager)
+    {
+        super(beanManager);
+    }
+
     /**
      * Simply clear the mock ViewMap.
      * This function should get called if the same unit test needs to test multiple

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c9a8713e/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedContext.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedContext.java
index 9d7f35a..052981d 100644
--- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedContext.java
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedContext.java
@@ -18,12 +18,9 @@
  */
 package org.apache.deltaspike.jsf.impl.scope.view;
 
-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.spi.BeanManager;
-import javax.enterprise.inject.spi.PassivationCapable;
 import javax.faces.bean.ViewScoped;
 import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
@@ -32,111 +29,61 @@ import javax.faces.event.SystemEvent;
 import javax.faces.event.SystemEventListener;
 import java.lang.annotation.Annotation;
 import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
 
-import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
+import org.apache.deltaspike.core.util.context.AbstractContext;
+import org.apache.deltaspike.core.util.context.ContextualStorage;
 
 /**
  * This class provides the contexts lifecycle for the
  * new JSF-2 &#064;ViewScoped Context.
  */
-public class ViewScopedContext implements Context, SystemEventListener
+public class ViewScopedContext extends AbstractContext implements SystemEventListener
 {
-    private static final String COMPONENT_ID_MAP_NAME = "deltaspike.componentIdMap";
-    private static final String CREATIONAL_MAP_NAME = "deltaspike.creationalInstanceMap";
-    private static final String UNCHECKED = "unchecked";
+    private static final String CONTEXTUAL_MAP_NAME = "deltaspike.contextualInstanceMap";
 
     private boolean isJsfSubscribed = false;
 
-    /**
-     * {@inheritDoc}
-     */
+    private BeanManager beanManager;
+
+    public ViewScopedContext(BeanManager beanManager)
+    {
+        super(beanManager);
+        this.beanManager = beanManager;
+    }
+
     @Override
-    public <T> T get(Contextual<T> component)
+    protected ContextualStorage getContextualStorage(boolean createIfNotExists)
     {
-        checkActive();
+        Map<String, Object> viewMap = getViewMap();
+        ContextualStorage storage = (ContextualStorage) viewMap.get(CONTEXTUAL_MAP_NAME);
 
-        if (!isJsfSubscribed)
+        if (storage == null && createIfNotExists)
         {
-            FacesContext.getCurrentInstance().getApplication().subscribeToEvent(PreDestroyViewMapEvent.class, this);
-
-            isJsfSubscribed = true;
+            storage = new ContextualStorage(beanManager, false);
+            viewMap.put(CONTEXTUAL_MAP_NAME, storage);
         }
 
-        Map<String, Object> viewMap = getViewMap();
-
-        @SuppressWarnings(UNCHECKED)
-        Map<String, Object> componentIdMap = (Map<String, Object>) viewMap.get(COMPONENT_ID_MAP_NAME);
+        return storage;
+    }
 
-        if (componentIdMap == null)
-        {
-            return null;
-        }
+    @Override
+    public <T> T get(Contextual<T> bean)
+    {
+        subscribeToJsf();
 
-        @SuppressWarnings(UNCHECKED)
-        T instance = (T)componentIdMap.get(((PassivationCapable)component).getId());
-        return instance;
+        return super.get(bean);
     }
 
+
     /**
      * {@inheritDoc}
      */
     @Override
-    public <T> T get(Contextual<T> component, CreationalContext<T> creationalContext)
+    public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
     {
-        if (!(component instanceof PassivationCapable))
-        {
-            throw new IllegalStateException(component.toString() +
-                    " doesn't implement " + PassivationCapable.class.getName());
-        }
-
-        checkActive();
-
-        Map<String, Object> viewMap = getViewMap();
+        subscribeToJsf();
 
-        @SuppressWarnings(UNCHECKED)
-        Map<String, Object> componentIdMap = (Map<String, Object>) viewMap.get(COMPONENT_ID_MAP_NAME);
-
-        if (componentIdMap == null)
-        {
-            // TODO we now need to start being carefull with reentrancy...
-            componentIdMap = new ConcurrentHashMap<String, Object>();
-            viewMap.put(COMPONENT_ID_MAP_NAME, componentIdMap);
-        }
-
-        @SuppressWarnings(UNCHECKED)
-        T instance = (T) componentIdMap.get(((PassivationCapable)component).getId());
-        if (instance != null)
-        {
-            return instance;
-        }
-
-        if (creationalContext == null)
-        {
-            return null;
-        }
-
-        instance = component.create(creationalContext);
-
-        if (instance == null)
-        {
-            return null;
-        }
-
-        Map<String, CreationalContext<?>> creationalContextMap
-            = (Map<String, CreationalContext<?>>) viewMap.get(CREATIONAL_MAP_NAME);
-
-        if (creationalContextMap == null)
-        {
-            creationalContextMap = new ConcurrentHashMap<String, CreationalContext<?>>();
-            viewMap.put(CREATIONAL_MAP_NAME, creationalContextMap);
-        }
-
-        componentIdMap.put(((PassivationCapable)component).getId(), instance);
-        creationalContextMap.put(((PassivationCapable)component).getId(), creationalContext);
-
-        return  instance;
+        return super.get(bean, creationalContext);
     }
 
     /**
@@ -157,15 +104,17 @@ public class ViewScopedContext implements Context, SystemEventListener
         return getViewRoot() != null;
     }
 
-    private void checkActive()
+    private void subscribeToJsf()
     {
-        if (!isActive())
+        if (!isJsfSubscribed)
         {
-            throw new ContextNotActiveException("WebBeans context with scope annotation " +
-                                                "@ViewScoped is not active with respect to the current thread");
+            FacesContext.getCurrentInstance().getApplication().subscribeToEvent(PreDestroyViewMapEvent.class, this);
+
+            isJsfSubscribed = true;
         }
     }
 
+
     /**
      * {@inheritDoc}
      */
@@ -192,34 +141,10 @@ public class ViewScopedContext implements Context, SystemEventListener
     {
         if (event instanceof PreDestroyViewMapEvent)
         {
-            // better use the viewmap we get from the event to prevent concurrent modification problems
-            Map<String, Object> viewMap = ((UIViewRoot) event.getSource()).getViewMap();
-
-            Map<String, Object> componentIdMap
-                = (Map<String, Object>) viewMap.get(COMPONENT_ID_MAP_NAME);
-
-            Map<String, CreationalContext<?>> creationalContextMap
-                = (Map<String, CreationalContext<?>>) viewMap.get(CREATIONAL_MAP_NAME);
-
-            if (componentIdMap != null)
-            {
-                BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
-                for ( Entry<String, Object> componentEntry : componentIdMap.entrySet())
-                {
-                    String beanId = componentEntry.getKey();
-                    // there is no nice way to explain the Java Compiler that we are handling the same type T,
-                    // therefore we need completely drop the type information :(
-                    Contextual contextual = beanManager.getPassivationCapableBean(beanId);
-                    Object instance = componentEntry.getValue();
-                    CreationalContext creational = creationalContextMap.get(beanId);
-
-                    contextual.destroy(instance, creational);
-                }
-            }
+            destroyAllActive();
         }
     }
 
-
     protected UIViewRoot getViewRoot()
     {
         FacesContext context = FacesContext.getCurrentInstance();

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c9a8713e/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedExtension.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedExtension.java
index fdee10f..c001da4 100644
--- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedExtension.java
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/view/ViewScopedExtension.java
@@ -21,6 +21,7 @@ package org.apache.deltaspike.jsf.impl.scope.view;
 
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
 import javax.faces.bean.ViewScoped;
@@ -67,7 +68,7 @@ public class ViewScopedExtension implements Extension, Deactivatable
         beforeBeanDiscovery.addScope(ViewScoped.class, true, true);
     }
 
-    public void registerViewContext(@Observes AfterBeanDiscovery afterBeanDiscovery)
+    public void registerViewContext(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
     {
         if (!isActivated)
         {
@@ -85,12 +86,12 @@ public class ViewScopedExtension implements Extension, Deactivatable
         if (projectStage == ProjectStage.UnitTest)
         {
             // for unit tests, we use the mock context
-            afterBeanDiscovery.addContext(new MockViewScopedContext());
+            afterBeanDiscovery.addContext(new MockViewScopedContext(beanManager));
         }
         else
         {
             // otherwise we use the real JSF ViewMap context
-            afterBeanDiscovery.addContext(new ViewScopedContext());
+            afterBeanDiscovery.addContext(new ViewScopedContext(beanManager));
         }
     }