You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2015/10/13 21:34:01 UTC

[1/2] deltaspike git commit: DELTASPIKE-1001 revert of revision 40cc44418ae128317d66359e2f7cf4d3aacb1512

Repository: deltaspike
Updated Branches:
  refs/heads/master 02bc71397 -> 737d53063


DELTASPIKE-1001 revert of revision 40cc44418ae128317d66359e2f7cf4d3aacb1512


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

Branch: refs/heads/master
Commit: eeee4641303ee7ea0080cb3bddf154f54c3de26f
Parents: 02bc713
Author: gpetracek <gp...@apache.org>
Authored: Tue Oct 13 20:18:43 2015 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Tue Oct 13 20:18:43 2015 +0200

----------------------------------------------------------------------
 .../core/util/ClassDeactivationUtils.java       | 153 ++++++++++++++---
 .../BaseClassDeactivationController.java        | 168 -------------------
 .../CachingClassDeactivationController.java     |  61 -------
 .../NonCachingClassDeactivationController.java  |  27 ---
 .../CachingClassDeactivationControllerTest.java |  43 -----
 .../util/activation/GenericDeactivatable.java   |  26 ---
 .../util/activation/GenericDeactivator.java     |  33 ----
 ...nCachingClassDeactivationControllerTest.java |  44 -----
 .../test/api/config/TestConfigSource.java       |   1 -
 9 files changed, 134 insertions(+), 422 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/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 28d9290..6fcd715 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
@@ -18,15 +18,17 @@
  */
 package org.apache.deltaspike.core.util;
 
-import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.base.CoreBaseConfig;
+import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
 import org.apache.deltaspike.core.spi.activation.Deactivatable;
-import org.apache.deltaspike.core.util.activation.BaseClassDeactivationController;
-import org.apache.deltaspike.core.util.activation.CachingClassDeactivationController;
-import org.apache.deltaspike.core.util.activation.NonCachingClassDeactivationController;
 
 import javax.enterprise.inject.Typed;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
 
 /**
  * Helper methods for {@link ClassDeactivator}
@@ -34,11 +36,24 @@ import java.util.List;
 @Typed()
 public abstract class ClassDeactivationUtils
 {
-    private static final List<ProjectStage> NON_CACHING_PROJECT_STAGES =
-        Arrays.asList(ProjectStage.Development,ProjectStage.UnitTest);
+    private static final Logger LOG = Logger.getLogger(ClassDeactivationUtils.class.getName());
 
-    private static BaseClassDeactivationController classDeactivationController = null;
+    /**
+     * 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>>();
 
+    /**
+     * 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()
     {
         // prevent instantiation
@@ -52,30 +67,130 @@ public abstract class ClassDeactivationUtils
      */
     public static boolean isActivated(Class<? extends Deactivatable> targetClass)
     {
-        return getController().isActivated(targetClass);
+        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
+
+        if (activatedClassCacheEntry == null)
+        {
+            initDeactivatableCacheFor(targetClass);
+            activatedClassCacheEntry = activationStatusCache.get(targetClass);
+        }
+        return activatedClassCacheEntry;
     }
 
-    private static BaseClassDeactivationController getController()
+    private static synchronized void initDeactivatableCacheFor(Class<? extends Deactivatable> targetClass)
     {
-        if (classDeactivationController == null)
+        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
+
+        if (activatedClassCacheEntry != null) //double-check
         {
-            classDeactivationController = calculateControllerToUse();
+            return;
         }
 
-        return classDeactivationController;
+        List<ClassDeactivator> classDeactivators = getClassDeactivators();
+
+        Boolean isActivated = Boolean.TRUE;
+        Class<? extends ClassDeactivator> deactivatedBy = null;
+
+        LOG.fine("start evaluation if " + targetClass.getName() + " is de-/activated");
+
+        // we get the classActivators ordered by it's ordinal
+        // thus the last one which returns != null 'wins' ;)
+        for (ClassDeactivator classDeactivator : classDeactivators)
+        {
+            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());
+                }
+            }
+        }
+
+        cacheResult(targetClass, isActivated);
+    }
+
+    private static void cacheResult(Class<? extends Deactivatable> targetClass, Boolean activated)
+    {
+        activationStatusCache.put(targetClass, activated);
+        LOG.info("class: " + targetClass.getName() + " activated=" + activated);
     }
 
-    private static BaseClassDeactivationController calculateControllerToUse()
+    /**
+     * @return the List of configured @{link ClassDeactivator}s for the current context ClassLoader.
+     */
+    private static List<ClassDeactivator> getClassDeactivators()
     {
-        ProjectStage currentProjectStage = ProjectStageProducer.getInstance().getProjectStage();
+        ClassLoader classLoader = ClassUtils.getClassLoader(null);
+        List<ClassDeactivator> classDeactivators = classDeactivatorMap.get(classLoader);
 
-        if (NON_CACHING_PROJECT_STAGES.contains(currentProjectStage))
+        if (classDeactivators == null)
         {
-            return new NonCachingClassDeactivationController();
+            return initConfiguredClassDeactivators(classLoader);
         }
-        else
+
+        return classDeactivators;
+    }
+
+    //synchronized isn't needed - #initDeactivatableCacheFor is already synchronized
+    private static List<ClassDeactivator> initConfiguredClassDeactivators(ClassLoader classLoader)
+    {
+        if (!ServiceUtils.loadServiceImplementations(ClassDeactivator.class).isEmpty())
         {
-            return new CachingClassDeactivationController();
+            CoreBaseConfig.Validation.ViolationMode violationMode = CoreBaseConfig.Validation.VIOLATION_MODE;
+
+            String message = "It isn't supported to configure " + ClassDeactivator.class.getName() +
+                    " via the std. service-loader config. " +
+                    "Please remove all META-INF/services/" + ClassDeactivator.class.getName() + " files. " +
+                    "Please configure it via the DeltaSpike-Config (e.g. META-INF/apache-deltaspike.properties).";
+
+            if (violationMode == CoreBaseConfig.Validation.ViolationMode.FAIL)
+            {
+                throw new IllegalStateException(message);
+            }
+            else if (violationMode == CoreBaseConfig.Validation.ViolationMode.WARN)
+            {
+                LOG.warning(message);
+            }
+        }
+
+        List<String> classDeactivatorClassNames = ConfigResolver.getAllPropertyValues(ClassDeactivator.class.getName());
+
+        List<ClassDeactivator> classDeactivators = new ArrayList<ClassDeactivator>();
+
+        for (String classDeactivatorClassName : classDeactivatorClassNames)
+        {
+            LOG.fine("processing ClassDeactivator: " + classDeactivatorClassName);
+
+            try
+            {
+                ClassDeactivator currentClassDeactivator =
+                        (ClassDeactivator) ClassUtils.instantiateClassForName(classDeactivatorClassName);
+                classDeactivators.add(currentClassDeactivator);
+            }
+            catch (Exception e)
+            {
+                LOG.warning(classDeactivatorClassName + " can't be instantiated");
+                throw new IllegalStateException(e);
+            }
         }
+
+        classDeactivatorMap.put(classLoader, classDeactivators);
+        return classDeactivators;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/BaseClassDeactivationController.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/BaseClassDeactivationController.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/BaseClassDeactivationController.java
deleted file mode 100644
index 74c3b34..0000000
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/BaseClassDeactivationController.java
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.apache.deltaspike.core.api.config.ConfigResolver;
-import org.apache.deltaspike.core.api.config.base.CoreBaseConfig;
-import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
-import org.apache.deltaspike.core.spi.activation.Deactivatable;
-import org.apache.deltaspike.core.util.ClassUtils;
-import org.apache.deltaspike.core.util.ServiceUtils;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.logging.Logger;
-
-public abstract class BaseClassDeactivationController
-{
-    private static final Logger LOG = Logger.getLogger(BaseClassDeactivationController.class.getName());
-
-    /**
-     * 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>>();
-
-    /**
-     * 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 boolean isActivated(Class<? extends Deactivatable> targetClass)
-    {
-        return calculateDeactivationStatusFor(targetClass);
-    }
-
-    /**
-     * Determines if the given class is activated or not.  ClassDeactivators are cached at a class loader level.
-     *
-     * @param targetClass the Deactivatable class to search on
-     * @return true if this class is activated, false if its not activated
-     */
-    protected static synchronized boolean calculateDeactivationStatusFor(Class<? extends Deactivatable> targetClass)
-    {
-        List<ClassDeactivator> classDeactivators = getClassDeactivators();
-
-        Boolean isActivated = Boolean.TRUE;
-        Class<? extends ClassDeactivator> deactivatedBy = null;
-
-        LOG.fine("start evaluation if " + targetClass.getName() + " is de-/activated");
-
-        // we get the classActivators ordered by it's ordinal
-        // thus the last one which returns != null 'wins' ;)
-        for (ClassDeactivator classDeactivator : classDeactivators)
-        {
-            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());
-                }
-            }
-        }
-
-        return isActivated;
-    }
-
-    /**
-     * @return the List of configured @{link ClassDeactivator}s for the current context ClassLoader.
-     */
-    private static List<ClassDeactivator> getClassDeactivators()
-    {
-        ClassLoader classLoader = ClassUtils.getClassLoader(null);
-        List<ClassDeactivator> classDeactivators = classDeactivatorMap.get(classLoader);
-
-        if (classDeactivators == null)
-        {
-            return initConfiguredClassDeactivators(classLoader);
-        }
-
-        return classDeactivators;
-    }
-
-    //synchronized isn't needed - #initDeactivatableCacheFor is already synchronized
-    private static List<ClassDeactivator> initConfiguredClassDeactivators(ClassLoader classLoader)
-    {
-        if (!ServiceUtils.loadServiceImplementations(ClassDeactivator.class).isEmpty())
-        {
-            CoreBaseConfig.Validation.ViolationMode violationMode = CoreBaseConfig.Validation.VIOLATION_MODE;
-
-            String message = "It isn't supported to configure " + ClassDeactivator.class.getName() +
-                    " via the std. service-loader config. " +
-                    "Please remove all META-INF/services/" + ClassDeactivator.class.getName() + " files. " +
-                    "Please configure it via the DeltaSpike-Config (e.g. META-INF/apache-deltaspike.properties).";
-
-            if (violationMode == CoreBaseConfig.Validation.ViolationMode.FAIL)
-            {
-                throw new IllegalStateException(message);
-            }
-            else if (violationMode == CoreBaseConfig.Validation.ViolationMode.WARN)
-            {
-                LOG.warning(message);
-            }
-        }
-
-        List<String> classDeactivatorClassNames = ConfigResolver.getAllPropertyValues(ClassDeactivator.class.getName());
-
-        List<ClassDeactivator> classDeactivators = new ArrayList<ClassDeactivator>();
-
-        for (String classDeactivatorClassName : classDeactivatorClassNames)
-        {
-            LOG.fine("processing ClassDeactivator: " + classDeactivatorClassName);
-
-            try
-            {
-                ClassDeactivator currentClassDeactivator =
-                        (ClassDeactivator) ClassUtils.instantiateClassForName(classDeactivatorClassName);
-                classDeactivators.add(currentClassDeactivator);
-            }
-            catch (Exception e)
-            {
-                LOG.warning(classDeactivatorClassName + " can't be instantiated");
-                throw new IllegalStateException(e);
-            }
-        }
-
-        classDeactivatorMap.put(classLoader, classDeactivators);
-        return classDeactivators;
-    }
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationController.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationController.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationController.java
deleted file mode 100644
index 729a384..0000000
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationController.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.apache.deltaspike.core.spi.activation.Deactivatable;
-
-import javax.enterprise.inject.Typed;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-@Typed()
-public class CachingClassDeactivationController extends BaseClassDeactivationController
-{
-    /**
-     * 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 Map<Class<? extends Deactivatable>, Boolean> activationStatusCache
-        = new ConcurrentHashMap<Class<? extends Deactivatable>, Boolean>();
-
-    /**
-     * 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 boolean isActivated(Class<? extends Deactivatable> targetClass)
-    {
-        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
-
-        if (activatedClassCacheEntry == null)
-        {
-            activatedClassCacheEntry = loadAndCacheResult(targetClass);
-        }
-        return activatedClassCacheEntry;
-    }
-
-    private synchronized boolean loadAndCacheResult(Class<? extends Deactivatable> targetClass)
-    {
-        boolean activatedClassCacheEntry = BaseClassDeactivationController.calculateDeactivationStatusFor(targetClass);
-        activationStatusCache.put(targetClass, activatedClassCacheEntry);
-        return activatedClassCacheEntry;
-    }
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationController.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationController.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationController.java
deleted file mode 100644
index 1020d38..0000000
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationController.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import javax.enterprise.inject.Typed;
-
-@Typed()
-public class NonCachingClassDeactivationController extends BaseClassDeactivationController
-{
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationControllerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationControllerTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationControllerTest.java
deleted file mode 100644
index 4c8cdd3..0000000
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/CachingClassDeactivationControllerTest.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class CachingClassDeactivationControllerTest {
-    @Before
-    public void initGenericDeactivator()
-    {
-        GenericDeactivator.ACTIVATED = null;
-    }
-
-    @Test
-    public void shouldIgnoreChangesToModifiedStatus()
-    {
-        CachingClassDeactivationController controller = new CachingClassDeactivationController();
-        boolean activated = controller.isActivated(GenericDeactivatable.class);
-        Assert.assertTrue(activated);
-        GenericDeactivator.ACTIVATED = false;
-        activated = controller.isActivated(GenericDeactivatable.class);
-        Assert.assertTrue(activated);
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivatable.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivatable.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivatable.java
deleted file mode 100644
index ba6841e..0000000
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivatable.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.apache.deltaspike.core.spi.activation.Deactivatable;
-
-public class GenericDeactivatable implements Deactivatable
-{
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivator.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivator.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivator.java
deleted file mode 100644
index a137a37..0000000
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/GenericDeactivator.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
-import org.apache.deltaspike.core.spi.activation.Deactivatable;
-
-public class GenericDeactivator implements ClassDeactivator
-{
-    public static Boolean ACTIVATED = null;
-    @Override
-    public Boolean isActivated(Class<? extends Deactivatable> targetClass)
-    {
-        return ACTIVATED;
-    }
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationControllerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationControllerTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationControllerTest.java
deleted file mode 100644
index 82ddcd3..0000000
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/NonCachingClassDeactivationControllerTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.deltaspike.core.util.activation;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class NonCachingClassDeactivationControllerTest
-{
-    @Before
-    public void initGenericDeactivator()
-    {
-        GenericDeactivator.ACTIVATED = null;
-    }
-    @Test
-    public void shouldNotCacheResults()
-    {
-
-        NonCachingClassDeactivationController controller = new NonCachingClassDeactivationController();
-        boolean activated = controller.isActivated(GenericDeactivatable.class);
-        Assert.assertTrue(activated);
-        GenericDeactivator.ACTIVATED = false;
-        activated = controller.isActivated(GenericDeactivatable.class);
-        Assert.assertFalse(activated);
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/eeee4641/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
index 5cebd4c..7f0897c 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
@@ -77,7 +77,6 @@ public class TestConfigSource implements ConfigSource
         props.put("deltaspike.test.class-value", "org.apache.deltaspike.test.api.config.TestConfigSource");
         props.put("deltaspike.test.date-value", "2014-12-24");
         props.put("deltaspike.test.invalid-value", "wrong");
-        props.put("org.apache.deltaspike.core.spi.activation.ClassDeactivator","org.apache.deltaspike.core.util.activation.GenericDeactivator");
     }
 
     @Override


[2/2] deltaspike git commit: DELTASPIKE-1001 cleanup caches in case of project-stage unit-test/development

Posted by gp...@apache.org.
DELTASPIKE-1001 cleanup caches in case of project-stage unit-test/development


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

Branch: refs/heads/master
Commit: 737d53063bf45789f8d09277d2b079cd33d89614
Parents: eeee464
Author: gpetracek <gp...@apache.org>
Authored: Tue Oct 13 20:30:55 2015 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Tue Oct 13 21:26:25 2015 +0200

----------------------------------------------------------------------
 .../core/util/ClassDeactivationUtils.java       |  48 ++++++--
 .../activation/EditableTestDeactivator.java     |  47 ++++++++
 ...jectStageDependentClassDeactivationTest.java | 111 +++++++++++++++++++
 .../test/api/config/TestConfigSource.java       |   1 +
 4 files changed, 197 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/737d5306/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 6fcd715..2379ee9 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
@@ -20,6 +20,7 @@ package org.apache.deltaspike.core.util;
 
 import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.apache.deltaspike.core.api.config.base.CoreBaseConfig;
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
 import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
 import org.apache.deltaspike.core.spi.activation.Deactivatable;
 
@@ -41,7 +42,7 @@ public abstract class ClassDeactivationUtils
     /**
      * 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
@@ -53,7 +54,9 @@ public abstract class ClassDeactivationUtils
      */
     private static Map<Class<? extends Deactivatable>, Boolean> activationStatusCache
         = new ConcurrentHashMap<Class<? extends Deactivatable>, Boolean>();
-    
+
+    private static ProjectStage previouslyDetectedProjectStage;
+
     private ClassDeactivationUtils()
     {
         // prevent instantiation
@@ -67,19 +70,43 @@ public abstract class ClassDeactivationUtils
      */
     public static boolean isActivated(Class<? extends Deactivatable> targetClass)
     {
-        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
+        performProjectStageDependentCleanup();
+
+        //just to support parallel access to #isActivated (+ reset) in project-stage unit-test and development
+        Map<Class<? extends Deactivatable>, Boolean> activeCache = activationStatusCache;
+        Boolean activatedClassCacheEntry = activeCache.get(targetClass);
 
         if (activatedClassCacheEntry == null)
         {
-            initDeactivatableCacheFor(targetClass);
-            activatedClassCacheEntry = activationStatusCache.get(targetClass);
+            initDeactivatableCacheFor(targetClass, activeCache);
+            activatedClassCacheEntry = activeCache.get(targetClass);
         }
         return activatedClassCacheEntry;
     }
 
-    private static synchronized void initDeactivatableCacheFor(Class<? extends Deactivatable> targetClass)
+    private static void performProjectStageDependentCleanup()
+    {
+        ProjectStage currentProjectStage = ProjectStageProducer.getInstance().getProjectStage();
+
+        if (previouslyDetectedProjectStage != currentProjectStage)
+        {
+            previouslyDetectedProjectStage = currentProjectStage;
+            //don't use #clear to support parallel access to #isActivated (+ reset) without synchronization
+            activationStatusCache = new ConcurrentHashMap<Class<? extends Deactivatable>, Boolean>();
+
+            //#clear is ok here due to the handling in the synchronized method #initDeactivatableCacheFor
+            classDeactivatorMap.clear();
+        }
+        else if (currentProjectStage == ProjectStage.UnitTest || currentProjectStage == ProjectStage.Development)
+        {
+            activationStatusCache = new ConcurrentHashMap<Class<? extends Deactivatable>, Boolean>();
+        }
+    }
+
+    private static synchronized void initDeactivatableCacheFor(Class<? extends Deactivatable> targetClass,
+                                                               Map<Class<? extends Deactivatable>, Boolean> activeCache)
     {
-        Boolean activatedClassCacheEntry = activationStatusCache.get(targetClass);
+        Boolean activatedClassCacheEntry = activeCache.get(targetClass);
 
         if (activatedClassCacheEntry != null) //double-check
         {
@@ -122,12 +149,13 @@ public abstract class ClassDeactivationUtils
             }
         }
 
-        cacheResult(targetClass, isActivated);
+        cacheResult(targetClass, isActivated, activeCache);
     }
 
-    private static void cacheResult(Class<? extends Deactivatable> targetClass, Boolean activated)
+    private static void cacheResult(Class<? extends Deactivatable> targetClass, Boolean activated,
+                                    Map<Class<? extends Deactivatable>, Boolean> activeCache)
     {
-        activationStatusCache.put(targetClass, activated);
+        activeCache.put(targetClass, activated);
         LOG.info("class: " + targetClass.getName() + " activated=" + activated);
     }
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/737d5306/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/EditableTestDeactivator.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/EditableTestDeactivator.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/EditableTestDeactivator.java
new file mode 100644
index 0000000..cd06855
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/EditableTestDeactivator.java
@@ -0,0 +1,47 @@
+/*
+ * 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.deltaspike.core.util.activation;
+
+import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EditableTestDeactivator implements ClassDeactivator
+{
+    private static Map<Class<? extends Deactivatable>, Boolean> result = new HashMap<Class<? extends Deactivatable>, Boolean>();
+
+    @Override
+    public Boolean isActivated(Class<? extends Deactivatable> targetClass)
+    {
+        return result.get(targetClass);
+    }
+
+    public static void activate(Class<? extends Deactivatable> classToActivate)
+    {
+        result.put(classToActivate, true);
+    }
+
+    public static void deactivate(Class<? extends Deactivatable> classToActivate)
+    {
+        result.put(classToActivate, false);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/737d5306/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/ProjectStageDependentClassDeactivationTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/ProjectStageDependentClassDeactivationTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/ProjectStageDependentClassDeactivationTest.java
new file mode 100644
index 0000000..6ca2a93
--- /dev/null
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/activation/ProjectStageDependentClassDeactivationTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.deltaspike.core.util.activation;
+
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+import org.apache.deltaspike.core.util.ClassDeactivationUtils;
+import org.apache.deltaspike.core.util.ProjectStageProducer;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ProjectStageDependentClassDeactivationTest
+{
+    @Test
+    public void deactivationResultInProjectStageUnitTest()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.UnitTest);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(false, ClassDeactivationUtils.isActivated(classToCheck));
+    }
+
+    @Test
+    public void deactivationResultInProjectStageDevelopment()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.Development);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(false, ClassDeactivationUtils.isActivated(classToCheck));
+    }
+
+    @Test
+    public void deactivationResultInProjectStageProduction()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.Production);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck)); //due to the cached result
+    }
+
+    @Test
+    public void deactivationResultInProjectStageIntegrationTest()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.IntegrationTest);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck)); //due to the cached result
+    }
+
+    @Test
+    public void deactivationResultInProjectStageStaging()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.Staging);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck)); //due to the cached result
+    }
+
+    @Test
+    public void deactivationResultInProjectStageSystemTest()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.SystemTest);
+
+        final Class<? extends Deactivatable> classToCheck = TestDeactivatable.class;
+        EditableTestDeactivator.activate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck));
+
+        EditableTestDeactivator.deactivate(classToCheck);
+        Assert.assertEquals(true, ClassDeactivationUtils.isActivated(classToCheck)); //due to the cached result
+    }
+
+    private static class TestDeactivatable implements Deactivatable
+    {
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/737d5306/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
index 7f0897c..709c1ee 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
@@ -77,6 +77,7 @@ public class TestConfigSource implements ConfigSource
         props.put("deltaspike.test.class-value", "org.apache.deltaspike.test.api.config.TestConfigSource");
         props.put("deltaspike.test.date-value", "2014-12-24");
         props.put("deltaspike.test.invalid-value", "wrong");
+        props.put("org.apache.deltaspike.core.spi.activation.ClassDeactivator","org.apache.deltaspike.core.util.activation.EditableTestDeactivator");
     }
 
     @Override