myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jak...@apache.org
Subject svn commit: r1004812 - 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:57:32 GMT
Author: jakobk
Date: Tue Oct  5 20:57:31 2010
New Revision: 1004812

URL: http://svn.apache.org/viewvc?rev=1004812&view=rev
Log:
use JndiUtils for JNDI lookup in ProjectStageProducer and cleanup ClassDeactivation

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
    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
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=1004812&r1=1004811&r2=1004812&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:57:31 2010
@@ -21,17 +21,14 @@ package org.apache.myfaces.extensions.cd
 
 import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
 import org.apache.myfaces.extensions.cdi.core.impl.utils.CodiUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.utils.JndiUtils;
 
 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 java.io.IOException;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
@@ -200,30 +197,14 @@ public class ProjectStageProducer
 
     protected ProjectStage getProjectStageFromJNDI()
     {
-        // TODO use JndiUtils
-
         ProjectStage ps = null;
         String stageName = null;
+
         try
         {
-            Context ctx = new InitialContext();
-            Object temp = ctx.lookup(PROJECT_STAGE_JNDI_NAME);
-            if (temp != null)
-            {
-                if (temp instanceof String)
-                {
-                    stageName = (String) temp;
-                }
-                else
-                {
-                    log.log(Level.SEVERE,
-                            "JNDI lookup for key " + PROJECT_STAGE_JNDI_NAME
-                            + " should return a java.lang.String value");
-                    return null;
-                }
-            }
+            stageName = JndiUtils.lookup(PROJECT_STAGE_JNDI_NAME, String.class);
         }
-        catch (NamingException e)
+        catch (RuntimeException jndiException)
         {
             // no-op
         }

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=1004812&r1=1004811&r2=1004812&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:57:31 2010
@@ -60,27 +60,12 @@ public class ClassDeactivation
         ClassDeactivator classDeactivator = null;
 
         // check system property
-        String classDeactivatorName = System.getProperty(CLASS_DEACTIVATOR_PROPERTY_NAME);
-        if (classDeactivatorName != null)
-        {
-            classDeactivator = tryToInstantiateClassForName(classDeactivatorName, ClassDeactivator.class);
-        }
+        classDeactivator = getClassDeactivatorFromEnvironment();
 
         // 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
-            }
+            classDeactivator = getClassDeactivatorFromJNDI();
         }
 
         // use default deactivator
@@ -104,6 +89,38 @@ public class ClassDeactivation
         return classDeactivator;
     }
 
+    private static ClassDeactivator getClassDeactivatorFromEnvironment()
+    {
+        String classDeactivatorName = System.getProperty(CLASS_DEACTIVATOR_PROPERTY_NAME);
+        if (classDeactivatorName != null)
+        {
+            return tryToInstantiateClassForName(classDeactivatorName, ClassDeactivator.class);
+        }
+
+        return null;
+    }
+
+    private static ClassDeactivator getClassDeactivatorFromJNDI()
+    {
+        String classDeactivatorName = null;
+
+        try
+        {
+            classDeactivatorName = JndiUtils.lookup(CLASS_DEACTIVATOR_JNDI_NAME, String.class);
+        }
+        catch (RuntimeException jndiException)
+        {
+            // noop - lookup did not work
+        }
+
+        if (classDeactivatorName != null)
+        {
+            return tryToInstantiateClassForName(classDeactivatorName, ClassDeactivator.class);
+        }
+
+        return null;
+    }
+
     private static AbstractClassDeactivator createClassDeactivatorPlaceholder()
     {
         return new AbstractClassDeactivator()

Modified: 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=1004812&r1=1004811&r2=1004812&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/JndiUtils.java
Tue Oct  5 20:57:31 2010
@@ -22,6 +22,8 @@ import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * This is the internal helper class for low level access to JNDI
@@ -29,6 +31,8 @@ import javax.naming.NamingException;
 public final class JndiUtils
 {
 
+    private final static Logger log = Logger.getLogger(JndiUtils.class.getName());
+
     private static InitialContext initialContext = null;
 
     static
@@ -99,7 +103,25 @@ public final class JndiUtils
     {
         try
         {
-            return (T) initialContext.lookup(name);
+            Object lookedUp = initialContext.lookup(name);
+
+            if (lookedUp != null)
+            {
+                if (expectedClass.isAssignableFrom(lookedUp.getClass()))
+                {
+                    // we have a value and the type fits
+                    return (T) lookedUp;
+                }
+                else
+                {
+                    // we have a value, but the value does not fit
+                    log.log(Level.SEVERE,
+                            "JNDI lookup for key " + name
+                            + " should return a value of " + expectedClass);
+                }
+            }
+
+            return null;
         }
         catch (NamingException e)
         {



Mime
View raw message