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 2011/12/28 14:59:20 UTC

git commit: DELTASPIKE-24 initial import of Deactivatable - without ClassDeactivator lookup

Updated Branches:
  refs/heads/master 70d3cf1ed -> 1f9fb71d7


DELTASPIKE-24 initial import of Deactivatable - without ClassDeactivator lookup


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

Branch: refs/heads/master
Commit: 1f9fb71d7cbaedf23e191a837ac9a6daf8f446d5
Parents: 70d3cf1
Author: gpetracek <gp...@apache.org>
Authored: Wed Dec 28 14:57:53 2011 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Wed Dec 28 14:57:53 2011 +0100

----------------------------------------------------------------------
 .../api/activation/AbstractClassDeactivator.java   |   60 ++++++++
 .../core/api/activation/ClassDeactivator.java      |   40 ++++++
 .../core/api/activation/Deactivatable.java         |   35 +++++
 .../core/impl/util/ClassDeactivation.java          |  110 +++++++++++++++
 .../core/impl/util/ClassDeactivatorStorage.java    |   62 ++++++++
 5 files changed, 307 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/1f9fb71d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/AbstractClassDeactivator.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/AbstractClassDeactivator.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/AbstractClassDeactivator.java
new file mode 100644
index 0000000..f7a2a13
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/AbstractClassDeactivator.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.api.activation;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Base implementation which allows an easier class-deactivator implementation
+ */
+public abstract class AbstractClassDeactivator implements ClassDeactivator
+{
+    //HashSet due to Serializable warning in checkstyle rules
+    private HashSet<Class> deactivatedClasses = null;
+
+    /**
+     * {@inheritDoc}
+     */
+    public final Set<Class> getDeactivatedClasses()
+    {
+        if (this.deactivatedClasses == null)
+        {
+            this.deactivatedClasses = new HashSet<Class>();
+            deactivateClasses();
+        }
+        return this.deactivatedClasses;
+    }
+
+    /**
+     * Can be used by sub-classes to add deactivated classes easily.
+     *
+     * @param deactivatedClass class to deactivate
+     */
+    protected final void addDeactivatedClass(Class deactivatedClass)
+    {
+        this.deactivatedClasses.add(deactivatedClass);
+    }
+
+    /**
+     * An implementation has to add classes which shouldn't be used by CODI.
+     * (use {@link #addDeactivatedClass(Class)} for adding classes)
+     */
+    protected abstract void deactivateClasses();
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/1f9fb71d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/ClassDeactivator.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/ClassDeactivator.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/ClassDeactivator.java
new file mode 100644
index 0000000..4c71c2c
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/ClassDeactivator.java
@@ -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.deltaspike.core.api.activation;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * A class-deactivator allows to specify deactivated classes which can't be deactivated via std. CDI mechanisms.
+ * 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. An easy way to configure it permanently is e.g.
+ * to use the service-loader approach. Furthermore, {@link AbstractClassDeactivator} is a convenience class which
+ * allows an easier implementation. All classes which implement {@link Deactivatable} in-/directly, can be deactivated
+ * with this mechanism. For all other classes/beans, you can use the veto mechanism provided by CDI.
+ */
+public interface ClassDeactivator extends Serializable
+{
+    /**
+     * Provides classes which should be deactivated.
+     *
+     * @return classes which should be deactivated
+     */
+    Set<Class> getDeactivatedClasses();
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/1f9fb71d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/Deactivatable.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/Deactivatable.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/Deactivatable.java
new file mode 100644
index 0000000..2144367
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/activation/Deactivatable.java
@@ -0,0 +1,35 @@
+/*
+ * 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.api.activation;
+
+/**
+ * Interface to allow easier detection of deactivatable classes.
+ * These classes are activated by default (e.g. via CDI config).
+ * Since CDI, JSF,... currently don't allow to deactivate default implementations,
+ * CODI has to introduce a proprietary mechanism.
+ */
+public interface Deactivatable
+{
+    /**
+     * Returns if the current instance is active or not.
+     *
+     * @return true if the current instance is active, false otherwise
+     */
+    boolean isActivated();
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/1f9fb71d/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivation.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivation.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivation.java
new file mode 100644
index 0000000..c75fa96
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivation.java
@@ -0,0 +1,110 @@
+/*
+ * 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.util;
+
+import org.apache.deltaspike.core.api.activation.AbstractClassDeactivator;
+import org.apache.deltaspike.core.api.activation.ClassDeactivator;
+
+import javax.enterprise.inject.Typed;
+import java.util.logging.Logger;
+
+/**
+ * Helper methods for {@link ClassDeactivator}
+ */
+@Typed()
+public class ClassDeactivation
+{
+    private static final Logger LOGGER = Logger.getLogger(ClassDeactivation.class.getName());
+
+    private ClassDeactivation()
+    {
+    }
+
+    /**
+     * Evaluates if the given class is active
+     *
+     * @param targetClass current class
+     * @return true if it is active, false otherwise
+     */
+    public static boolean isClassActivated(Class targetClass)
+    {
+        ClassDeactivator classDeactivator = ClassDeactivatorStorage.getClassDeactivator();
+
+        if (classDeactivator == null)
+        {
+            classDeactivator = getClassDeactivator();
+        }
+
+        boolean classDeactivated = classDeactivator.getDeactivatedClasses().contains(targetClass);
+
+        return !classDeactivated;
+    }
+
+    /**
+     * Allows to provide a custom {@link ClassDeactivator}
+     *
+     * @param classDeactivator class-deactivator which should be used
+     */
+    public static void setClassDeactivator(ClassDeactivator classDeactivator)
+    {
+        ClassDeactivatorStorage.setClassDeactivator(classDeactivator);
+    }
+
+    private static ClassDeactivator getClassDeactivator()
+    {
+        ClassDeactivator classDeactivator = null;
+
+        //X TODO lookup of ClassDeactivator
+        //classDeactivator =
+        //    CodiUtils.lookupFromEnvironment(ClassDeactivator.class, new ClassDeactivatorAggregator(), null);
+
+        // use default deactivator
+        if (classDeactivator == null)
+        {
+            classDeactivator = createClassDeactivatorPlaceholder();
+        }
+        else
+        {
+            LOGGER.info("used class deactivator: " + classDeactivator.toString());
+
+            // display deactivated classes here once
+            // NOTE that isClassActivated() will be called many times for the same class
+            for (Class<?> deactivatedClass : classDeactivator.getDeactivatedClasses())
+            {
+                LOGGER.info("deactivate: " + deactivatedClass);
+            }
+        }
+
+        ClassDeactivatorStorage.setClassDeactivator(classDeactivator);
+        return classDeactivator;
+    }
+
+    private static AbstractClassDeactivator createClassDeactivatorPlaceholder()
+    {
+        return new AbstractClassDeactivator()
+        {
+            private static final long serialVersionUID = 3365575383802245760L;
+
+            protected void deactivateClasses()
+            {
+                //do nothing
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/1f9fb71d/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivatorStorage.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivatorStorage.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivatorStorage.java
new file mode 100644
index 0000000..0b3661a
--- /dev/null
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/util/ClassDeactivatorStorage.java
@@ -0,0 +1,62 @@
+/*
+ * 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.util;
+
+import org.apache.deltaspike.core.api.activation.ClassDeactivator;
+import org.apache.deltaspike.core.api.util.ClassUtils;
+
+import javax.enterprise.inject.Typed;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Cache for {@link ClassDeactivator} implementations
+ */
+@Typed()
+class ClassDeactivatorStorage
+{
+    private static Map<ClassLoader, ClassDeactivator> classDeactivatorMap
+            = new ConcurrentHashMap<ClassLoader, ClassDeactivator>();
+
+    static void setClassDeactivator(ClassDeactivator classDeactivator)
+    {
+        if (classDeactivator != null)
+        {
+            classDeactivatorMap.put(getClassLoader(), classDeactivator);
+        }
+        else
+        {
+            classDeactivatorMap.remove(getClassLoader());
+        }
+    }
+
+    static ClassDeactivator getClassDeactivator()
+    {
+        if (!classDeactivatorMap.containsKey(getClassLoader()))
+        {
+            return null;
+        }
+        return classDeactivatorMap.get(getClassLoader());
+    }
+
+    private static ClassLoader getClassLoader()
+    {
+        return ClassUtils.getClassLoader(null);
+    }
+}