myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jak...@apache.org
Subject svn commit: r1004801 - in /myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl: projectstage/ProjectStageProducer.java utils/ClassDeactivation.java utils/JndiUtils.java
Date Tue, 05 Oct 2010 20:08:49 GMT
Author: jakobk
Date: Tue Oct  5 20:08:49 2010
New Revision: 1004801

URL: http://svn.apache.org/viewvc?rev=1004801&view=rev
Log:
EXTCDI-71 ClassDeactivator configuration via JNDI

Added:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
Modified:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ProjectStageProducer.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ClassDeactivation.java

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ProjectStageProducer.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ProjectStageProducer.java?rev=1004801&r1=1004800&r2=1004801&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ProjectStageProducer.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ProjectStageProducer.java
Tue Oct  5 20:08:49 2010
@@ -19,18 +19,17 @@
 package org.apache.myfaces.extensions.cdi.core.impl.projectstage;
 
 
-import javax.enterprise.context.Dependent;
+import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
+import org.apache.myfaces.extensions.cdi.core.impl.utils.CodiUtils;
+
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.Default;
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
-
-import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
-import org.apache.myfaces.extensions.cdi.core.impl.utils.CodiUtils;
-
 import java.io.IOException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -201,6 +200,8 @@ public class ProjectStageProducer
 
     protected ProjectStage getProjectStageFromJNDI()
     {
+        // TODO use JndiUtils
+
         ProjectStage ps = null;
         String stageName = null;
         try

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ClassDeactivation.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ClassDeactivation.java?rev=1004801&r1=1004800&r2=1004801&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ClassDeactivation.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ClassDeactivation.java
Tue Oct  5 20:08:49 2010
@@ -18,10 +18,12 @@
  */
 package org.apache.myfaces.extensions.cdi.core.impl.utils;
 
+import org.apache.myfaces.extensions.cdi.core.api.AbstractClassDeactivator;
+import org.apache.myfaces.extensions.cdi.core.api.ClassDeactivator;
+
 import java.util.logging.Logger;
+
 import static org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils.tryToInstantiateClassForName;
-import org.apache.myfaces.extensions.cdi.core.api.ClassDeactivator;
-import org.apache.myfaces.extensions.cdi.core.api.AbstractClassDeactivator;
 
 /**
  * @author Gerhard Petracek
@@ -30,7 +32,9 @@ public class ClassDeactivation
 {
     private static Logger logger = Logger.getLogger(ClassDeactivation.class.getName());
 
-    private static final String DEFAULT_CLASS_DEACTIVATOR = "org.apache.myfaces.extensions.cdi.ClassDeactivator";
+    private static final String CLASS_DEACTIVATOR_PROPERTY_NAME = "org.apache.myfaces.extensions.cdi.ClassDeactivator";
+
+    private static final String CLASS_DEACTIVATOR_JNDI_NAME = "java:comp/env/myfaces-codi/ClassDeactivator";
 
     public static boolean isClassActivated(Class targetClass)
     {
@@ -38,16 +42,11 @@ public class ClassDeactivation
 
         if(classDeactivator == null)
         {
-            classDeactivator = addClassDeactivator();
+            classDeactivator = getClassDeactivator();
         }
 
         boolean classDeactivated = classDeactivator.getDeactivatedClasses().contains(targetClass);
 
-        if(classDeactivated)
-        {
-            logger.info("deactivate: " + targetClass);
-        }
-
         return !classDeactivated;
     }
 
@@ -56,26 +55,49 @@ public class ClassDeactivation
         ClassDeactivatorStorage.setClassDeactivator(classDeactivator);
     }
 
-    private static ClassDeactivator addClassDeactivator()
+    private static ClassDeactivator getClassDeactivator()
     {
-        ClassDeactivator classDeactivator = tryToInstantiateClassForName(
-                DEFAULT_CLASS_DEACTIVATOR , ClassDeactivator.class);
-
-        //overrule with vm param
-        String classDeactivatorName = System.getProperty(DEFAULT_CLASS_DEACTIVATOR);
+        ClassDeactivator classDeactivator = null;
 
+        // check system property
+        String classDeactivatorName = System.getProperty(CLASS_DEACTIVATOR_PROPERTY_NAME);
         if (classDeactivatorName != null)
         {
             classDeactivator = tryToInstantiateClassForName(classDeactivatorName, ClassDeactivator.class);
         }
 
-        if(classDeactivator == null)
+        // check JNDI param
+        if (classDeactivator == null)
+        {
+            try
+            {
+                classDeactivatorName = JndiUtils.lookup(CLASS_DEACTIVATOR_JNDI_NAME, String.class);
+                if (classDeactivatorName != null)
+                {
+                    classDeactivator = tryToInstantiateClassForName(classDeactivatorName,
ClassDeactivator.class);
+                }
+            }
+            catch (RuntimeException re)
+            {
+                // noop - lookup did not work
+            }
+        }
+
+        // use default deactivator
+        if (classDeactivator == null)
         {
             classDeactivator = createClassDeactivatorPlaceholder();
         }
         else
         {
             logger.info("used class deactivator: " + classDeactivator.getClass().getName());
+
+            // 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);

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java?rev=1004801&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
Tue Oct  5 20:08:49 2010
@@ -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.myfaces.extensions.cdi.core.impl.utils;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+/**
+ * This is the internal helper class for low level access to JNDI
+ */
+public final class JndiUtils
+{
+
+    private static InitialContext initialContext = null;
+
+    static
+    {
+        try
+        {
+            initialContext = new InitialContext();
+        }
+        catch (Exception e)
+        {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
+    private JndiUtils()
+    {
+        // prevent instantiation
+    }
+
+    public static InitialContext getInitialContext()
+    {
+        return initialContext;
+    }
+
+    public static void bind(String name, Object object)
+    {
+        try
+        {
+            Context context = initialContext;
+
+            String[] parts = name.split("/");
+
+            for(int i = 0; i < parts.length - 1; i++)
+            {
+                try
+                {
+                    context = (Context)initialContext.lookup(parts[i]);
+                }
+                catch(NameNotFoundException e)
+                {
+                    context = initialContext.createSubcontext(parts[i]);
+                }
+            }
+
+            context.bind(parts[parts.length - 1], object);
+        }
+        catch (NamingException e)
+        {
+            throw new RuntimeException("Could not bind " + name + " to JNDI", e);
+        }
+    }
+
+    public static void unbind(String name)
+    {
+        try
+        {
+            initialContext.unbind(name);
+
+        }
+        catch (NamingException e)
+        {
+            throw new RuntimeException("Could not unbind " + name + " from JNDI", e);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T lookup(String name, Class<? extends T> expectedClass)
+    {
+        try
+        {
+            return (T) initialContext.lookup(name);
+        }
+        catch (NamingException e)
+        {
+            throw new RuntimeException("Could not get " + name + " from JNDI", e);
+        }
+    }
+
+}



Mime
View raw message