deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject git commit: DELTASPIKE-487 removed unused logic (thanks netbeans for destroying my workspace)
Date Thu, 27 Feb 2014 22:51:31 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master e0acd5c76 -> 9f59afca6


DELTASPIKE-487 removed unused logic (thanks netbeans for destroying my workspace)

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

Branch: refs/heads/master
Commit: 9f59afca6df252229c1c7f1bee89f2f44e022750
Parents: e0acd5c
Author: tandraschko <tandraschko@apache.org>
Authored: Thu Feb 27 23:51:14 2014 +0100
Committer: tandraschko <tandraschko@apache.org>
Committed: Thu Feb 27 23:51:14 2014 +0100

----------------------------------------------------------------------
 .../core/util/ClassDeactivationUtils.java       | 207 ++++++++++---------
 .../request/DeltaSpikeLifecycleWrapper.java     |   2 +-
 2 files changed, 114 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9f59afca/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassDeactivationUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassDeactivationUtils.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassDeactivationUtils.java
index ef8ef57..a69f730 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassDeactivationUtils.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassDeactivationUtils.java
@@ -16,142 +16,161 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.deltaspike.jsf.impl.listener.request;
+package org.apache.deltaspike.core.util;
 
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-import org.apache.deltaspike.core.spi.scope.window.WindowContext;
-import org.apache.deltaspike.core.util.ClassDeactivationUtils;
-import org.apache.deltaspike.jsf.spi.scope.window.ClientWindow;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
 
-import javax.faces.context.FacesContext;
-import javax.faces.event.PhaseListener;
-import javax.faces.lifecycle.Lifecycle;
-import org.apache.deltaspike.core.impl.scope.DeltaSpikeContextExtension;
-import org.apache.deltaspike.core.impl.scope.viewaccess.ViewAccessContext;
+import javax.enterprise.inject.Typed;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
 
-class DeltaSpikeLifecycleWrapper extends Lifecycle
+/**
+ * Helper methods for {@link ClassDeactivator}
+ */
+@Typed()
+public abstract class ClassDeactivationUtils
 {
-    private final Lifecycle wrapped;
-
-    private JsfRequestBroadcaster jsfRequestBroadcaster;
-
-    private ClientWindow clientWindow;
-    private WindowContext windowContext;
-    private DeltaSpikeContextExtension contextExtension;
+    private static final Logger LOG = Logger.getLogger(ClassDeactivationUtils.class.getName());
 
-    private volatile Boolean initialized;
+    /**
+     * This Map holds the ClassLoader as first level to make it possible to have different
configurations per 
+     * WebApplication in an EAR or other Multi-ClassLoader scenario.
+     * 
+     * The Map then contains a List of {@link ClassDeactivator}s in order of their configured
ordinal.
+     */
+    private static Map<ClassLoader, List<ClassDeactivator>> classDeactivatorMap
+        = new ConcurrentHashMap<ClassLoader, List<ClassDeactivator>>();
 
-    DeltaSpikeLifecycleWrapper(Lifecycle wrapped)
+    /**
+     * Cache for the result. It won't contain many classes but it might be accessed frequently.
+     * Valid entries are only true or false. If an entry isn't available or null, it gets
calculated.
+     */
+    private static Map<Class<? extends Deactivatable>, Boolean> activationStatusCache
+        = new ConcurrentHashMap<Class<? extends Deactivatable>, Boolean>();
+    
+    private ClassDeactivationUtils()
     {
-        this.wrapped = wrapped;
+        // prevent instantiation
     }
 
-    Lifecycle getWrapped()
+    /**
+     * Evaluates if the given {@link Deactivatable} is active.
+     *
+     * @param targetClass {@link Deactivatable} under test.
+     * @return <code>true</code> if it is active, <code>false</code>
otherwise
+     */
+    public static boolean isActivated(Class<? extends Deactivatable> targetClass)
     {
-        return wrapped;
-    }
+        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
 
-    @Override
-    public void addPhaseListener(PhaseListener phaseListener)
-    {
-        this.wrapped.addPhaseListener(phaseListener);
+        if (activatedClassCacheEntry == null)
+        {
+            initDeactivatableCacheFor(targetClass);
+            activatedClassCacheEntry = activationStatusCache.get(targetClass);
+        }
+        return activatedClassCacheEntry;
     }
 
-    /**
-     * Broadcasts
-     * {@link org.apache.deltaspike.core.api.lifecycle.Initialized} and
-     * {@link org.apache.deltaspike.core.api.lifecycle.Destroyed}
-     * //TODO StartupEvent
-     */
-    @Override
-    public void execute(FacesContext facesContext)
+    private static synchronized void initDeactivatableCacheFor(Class<? extends Deactivatable>
targetClass)
     {
-        //can happen due to the window-handling of deltaspike
-        if (facesContext.getResponseComplete())
+        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
+
+        if (activatedClassCacheEntry != null) //double-check
         {
             return;
         }
 
-        lazyInit();
+        List<ClassDeactivator> classDeactivators = getClassDeactivators();
 
-        //TODO broadcastApplicationStartupBroadcaster();
-        broadcastInitializedJsfRequestEvent(facesContext);
+        Boolean isActivated = Boolean.TRUE;
+        Class<? extends ClassDeactivator> deactivatedBy = null;
 
-        // ClientWindow handling
-        String windowId = clientWindow.getWindowId(facesContext);
-        if (windowId != null)
-        {
-            windowContext.activateWindow(windowId);
-        }
+        LOG.fine("start evaluation if " + targetClass.getName() + " is de-/activated");
 
-        if (!FacesContext.getCurrentInstance().getResponseComplete())
+        // we get the classActivators ordered by it's ordinal
+        // thus the last one which returns != null 'wins' ;)
+        for (ClassDeactivator classDeactivator : classDeactivators)
         {
-            this.wrapped.execute(facesContext);
+            Boolean isLocallyActivated = classDeactivator.isActivated(targetClass);
+
+            if (isLocallyActivated != null)
+            {
+                isActivated = isLocallyActivated;
+
+                /*
+                * Check and log the details across class-deactivators
+                */
+                if (!isActivated)
+                {
+                    deactivatedBy = classDeactivator.getClass();
+                    LOG.fine("Deactivating class " + targetClass);
+                }
+                else if (deactivatedBy != null)
+                {
+                    LOG.fine("Reactivation of: " + targetClass.getName() + " by " +
+                            classDeactivator.getClass().getName() +
+                            " - original deactivated by: " + deactivatedBy.getName() + ".\n"
+
+                            "If that isn't the intended behaviour, you have to use a higher
ordinal for " +
+                            deactivatedBy.getName());
+                }
+            }
         }
-    }
 
-    @Override
-    public PhaseListener[] getPhaseListeners()
-    {
-        return this.wrapped.getPhaseListeners();
+        cacheResult(targetClass, isActivated);
     }
 
-    @Override
-    public void removePhaseListener(PhaseListener phaseListener)
+    private static void cacheResult(Class<? extends Deactivatable> targetClass, Boolean
activated)
     {
-        this.wrapped.removePhaseListener(phaseListener);
+        activationStatusCache.put(targetClass, activated);
+        LOG.info("class: " + targetClass.getName() + " activated=" + activated);
     }
 
     /**
-     * Performs cleanup tasks after the rendering process
+     * @return the List of configured @{link ClassDeactivator}s for the current context ClassLoader.
      */
-    @Override
-    public void render(FacesContext facesContext)
+    private static List<ClassDeactivator> getClassDeactivators()
     {
-        this.wrapped.render(facesContext);
-        
-        if (facesContext.getViewRoot() != null)
-        {
-            ViewAccessContext viewAccessContext = contextExtension.getViewAccessScopedContext();
-            if (viewAccessContext != null)
-            {
-                viewAccessContext.onRenderingFinished(facesContext.getViewRoot().getViewId());
-            }
-        }
-    }
+        ClassLoader classLoader = ClassUtils.getClassLoader(null);
+        List<ClassDeactivator> classDeactivators = classDeactivatorMap.get(classLoader);
 
-    private void broadcastInitializedJsfRequestEvent(FacesContext facesContext)
-    {
-        if (this.jsfRequestBroadcaster != null)
+        if (classDeactivators == null)
         {
-            this.jsfRequestBroadcaster.broadcastInitializedJsfRequestEvent(facesContext);
+            return initConfiguredClassDeactivators(classLoader);
         }
-    }
 
-    private void lazyInit()
-    {
-        if (this.initialized == null)
-        {
-            init();
-        }
+        return classDeactivators;
     }
 
-    private synchronized void init()
+    //synchronized isn't needed - #initDeactivatableCacheFor is already synchronized
+    private static List<ClassDeactivator> initConfiguredClassDeactivators(ClassLoader
classLoader)
     {
-        // switch into paranoia mode
-        if (this.initialized == null)
+        List<String> classDeactivatorClassNames = ConfigResolver.getAllPropertyValues(ClassDeactivator.class.getName());
+
+        List<ClassDeactivator> classDeactivators = new ArrayList<ClassDeactivator>();
+
+        for (String classDeactivatorClassName : classDeactivatorClassNames)
         {
-            if (ClassDeactivationUtils.isActivated(JsfRequestBroadcaster.class))
+            LOG.fine("processing ClassDeactivator: " + classDeactivatorClassName);
+
+            try
             {
-                this.jsfRequestBroadcaster =
-                        BeanProvider.getContextualReference(JsfRequestBroadcaster.class,
true);
+                ClassDeactivator currentClassDeactivator =
+                        (ClassDeactivator) ClassUtils.instantiateClassForName(classDeactivatorClassName);
+                classDeactivators.add(currentClassDeactivator);
+            }
+            catch (Exception e)
+            {
+                LOG.warning(classDeactivatorClassName + " can't be instantiated");
+                throw new IllegalStateException(e);
             }
-
-            clientWindow = BeanProvider.getContextualReference(ClientWindow.class, true);
-            windowContext = BeanProvider.getContextualReference(WindowContext.class, true);
-            contextExtension = BeanProvider.getContextualReference(DeltaSpikeContextExtension.class,
true);
-            
-            this.initialized = true;
         }
+
+        classDeactivatorMap.put(classLoader, classDeactivators);
+        return classDeactivators;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9f59afca/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/listener/request/DeltaSpikeLifecycleWrapper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/listener/request/DeltaSpikeLifecycleWrapper.java
b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/listener/request/DeltaSpikeLifecycleWrapper.java
index 7e610f1..ef8ef57 100644
--- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/listener/request/DeltaSpikeLifecycleWrapper.java
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/listener/request/DeltaSpikeLifecycleWrapper.java
@@ -110,7 +110,7 @@ class DeltaSpikeLifecycleWrapper extends Lifecycle
     {
         this.wrapped.render(facesContext);
         
-        if (!facesContext.isPostback() && facesContext.getViewRoot() != null)
+        if (facesContext.getViewRoot() != null)
         {
             ViewAccessContext viewAccessContext = contextExtension.getViewAccessScopedContext();
             if (viewAccessContext != null)


Mime
View raw message