Repository: deltaspike
Updated Branches:
  refs/heads/master 44d188449 -> 925174755


DELTASPIKE-955 Added a default deactivator to the code base to help simplify 
usage.


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

Branch: refs/heads/master
Commit: 925174755f4135174c608a5e08de3d0ee40b8e69
Parents: 44d1884
Author: John D. Ament <johndam...@apache.org>
Authored: Sun Aug 16 22:52:39 2015 -0400
Committer: John D. Ament <johndam...@apache.org>
Committed: Sun Aug 16 22:52:39 2015 -0400

----------------------------------------------------------------------
 .../activation/DefaultClassDeactivator.java     | 60 ++++++++++++++++
 .../activation/DefaultClassDeactivatorTest.java | 76 ++++++++++++++++++++
 .../META-INF/apache-deltaspike.properties       |  4 +-
 documentation/src/main/asciidoc/spi.adoc        | 13 ++++
 4 files changed, 152 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92517475/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/activation/DefaultClassDeactivator.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/activation/DefaultClassDeactivator.java
 
b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/activation/DefaultClassDeactivator.java
new file mode 100644
index 0000000..a0a6212
--- /dev/null
+++ 
b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/activation/DefaultClassDeactivator.java
@@ -0,0 +1,60 @@
+/*
+ * 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.impl.activation;
+
+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 java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * This is a default implementation of ClassDeactivator which uses {@see 
ConfigSource} to resolve configuration options
+ * for whether a class is active or not.
+ *
+ * By design, this is not a well performant implementation.  It is a useful 
utility to avoid implementing the interface
+ * manually and may be an easy way to spin up test archives with some classes 
purposefully disabled.
+ */
+public class DefaultClassDeactivator implements ClassDeactivator
+{
+    public static final String KEY_PREFIX = "deactivate.";
+
+    private static final Logger LOG = 
Logger.getLogger(DefaultClassDeactivator.class.getName());
+
+    @Override
+    public Boolean isActivated(Class<? extends Deactivatable> targetClass)
+    {
+        final String key = KEY_PREFIX + targetClass.getName();
+        final String value = ConfigResolver.getPropertyValue(key);
+        if (value == null)
+        {
+            return null;
+        }
+        else
+        {
+            if (LOG.isLoggable(Level.FINE))
+            {
+                LOG.log(Level.FINE, "Deactivation setting for {0} found to be 
{1} based on configuration.",
+                        new Object[]{key, value});
+            }
+            return !Boolean.valueOf(value);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92517475/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/DefaultClassDeactivatorTest.java
----------------------------------------------------------------------
diff --git 
a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/DefaultClassDeactivatorTest.java
 
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/DefaultClassDeactivatorTest.java
new file mode 100644
index 0000000..e2b664c
--- /dev/null
+++ 
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/DefaultClassDeactivatorTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.test.core.impl.activation;
+
+import org.apache.deltaspike.core.impl.activation.DefaultClassDeactivator;
+import org.apache.deltaspike.core.spi.activation.ClassDeactivator;
+import org.apache.deltaspike.test.category.DeltaSpikeTest;
+import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+
+@RunWith(Arquillian.class)
+public class DefaultClassDeactivatorTest extends ClassDeactivationTest
+{
+    @Deployment
+    public static WebArchive deploy()
+    {
+        String simpleName = DefaultClassDeactivatorTest.class.getSimpleName();
+        String archiveName = simpleName.substring(0, 1).toLowerCase() + 
simpleName.substring(1);
+
+        StringBuilder dsPropsBuilder = new StringBuilder();
+        dsPropsBuilder.append(ClassDeactivator.class.getName()).append("=")
+                .append(DefaultClassDeactivator.class.getName()).append("\n")
+                // this gets picked up on app servers, not when using an 
embedded impl
+                
.append(DefaultClassDeactivator.KEY_PREFIX).append(DeactivatedClass.class.getName()).append("=true").append("\n");
+
+
+        JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, 
"testClassDeactivationTest.jar")
+                .addPackage(ClassDeactivationWarFileTest.class.getPackage())
+                .addClass(DefaultClassDeactivator.class)
+                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+
+        return ShrinkWrap.create(WebArchive.class, archiveName + ".war")
+                .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive())
+                .addAsLibraries(testJar)
+                .addAsResource(new StringAsset(dsPropsBuilder.toString()), 
DeltaSpikeTest.DELTASPIKE_PROPERTIES)
+                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @Test
+    public void testViaInstantiatedCopyTheyAreDeactivated()
+    {
+        DefaultClassDeactivator defaultClassDeactivator = new 
DefaultClassDeactivator();
+        Boolean activated = 
defaultClassDeactivator.isActivated(ActivatedClass.class);
+        assertNull(activated);
+
+        Boolean deactivated = 
defaultClassDeactivator.isActivated(DeactivatedClass.class);
+        assertFalse(deactivated);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92517475/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
----------------------------------------------------------------------
diff --git 
a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties 
b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
index e79bc48..2187530 100644
--- 
a/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
+++ 
b/deltaspike/core/impl/src/test/resources/META-INF/apache-deltaspike.properties
@@ -41,4 +41,6 @@ configPropertyTrue8=1
 propertyFloat=123.45
 
 
-my.very.secret=onlyIDoKnowIt
\ No newline at end of file
+my.very.secret=onlyIDoKnowIt
+
+deactivate.org.apache.deltaspike.test.core.impl.activation.DeactivatedClass=true
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92517475/documentation/src/main/asciidoc/spi.adoc
----------------------------------------------------------------------
diff --git a/documentation/src/main/asciidoc/spi.adoc 
b/documentation/src/main/asciidoc/spi.adoc
index fda2ded..2847117 100644
--- a/documentation/src/main/asciidoc/spi.adoc
+++ b/documentation/src/main/asciidoc/spi.adoc
@@ -44,6 +44,19 @@ public class CustomClassDeactivator implements 
ClassDeactivator
 
 A class-deactivator will be resolved from the environment via the default 
resolvers or via a custom resolver which allows to use any type of 
configuration-format. (see 
`org.apache.deltaspike.core.api.config.ConfigResolver`). The key is the fully 
qualified name of the interface 
(`org.apache.deltaspike.core.spi.activation.ClassDeactivator`).
 
+Starting with (TBD v1.5.1), Apache DeltaSpike ships a default Class 
Deactivator.  It is designed mostly for testing purposes, but is meant to 
reduce code overhead
+and allow configuration to drive classes to deactivate.  It is built upon the 
`ConfigSource` paradigm, which allows for configuration based keys to 
deactivate your
+classes.  If you're not using any other ConfigSource, you can simply add 
entries to `META-INF/apache-deltaspike.properties` to disable classes at 
runtime.  Here's an
+example configuration
+
+[source]
+----------------------------------------------------------------------------
+org.apache.deltaspike.core.spi.activation.ClassDeactivator=org.apache.deltaspike.core.impl.activation.DefaultClassDeactivator
+deactivate.org.apache.deltaspike.test.core.impl.activation.DeactivatedClass=true
+----------------------------------------------------------------------------
+
+By listing the class in the properties file and setting the value to `true`, 
the class will be deactivated.  This is valid for anything where 
`Boolean.valueOf` returns true.
+
 == ConfigSource
 
 [TODO]

Reply via email to