You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by st...@apache.org on 2010/05/19 21:59:10 UTC

svn commit: r946360 - in /myfaces/extensions/cdi/trunk/core/impl/src: main/java/org/apache/myfaces/extensions/cdi/core/impl/projectstage/ main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ test/java/org/apache/myfaces/extensions/cdi/core/test...

Author: struberg
Date: Wed May 19 19:59:09 2010
New Revision: 946360

URL: http://svn.apache.org/viewvc?rev=946360&view=rev
Log:
EXTCDI-22 make ProjectStageProducer ClassLoader aware

Added:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.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/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/projectstage/testbeans/MyMailServiceMockImpl.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=946360&r1=946359&r2=946360&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 Wed May 19 19:59:09 2010
@@ -29,10 +29,9 @@ 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.io.InputStream;
-import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -79,7 +78,6 @@ public class ProjectStageProducer
      */
     protected ProjectStageProducer()
     {
-
     }
 
     /**
@@ -119,20 +117,12 @@ public class ProjectStageProducer
     {
         if (psp == null)
         {
-            //X TODO I'm sure there is a common way to get the current classloader in MyFaces...
-            ClassLoader cl = Thread.currentThread().getContextClassLoader();
 
-            InputStream is = cl.getResourceAsStream("/META-INF/extcdi/extcdi.properties");
-            if (is != null)
+            String pspClassName = CodiUtils.getCodiProperty(PROJECTSTAGE_PRODUCER_PROPERTY_KEY);
+            if (pspClassName != null && pspClassName.length() > 0)
             {
-                Properties props = new Properties();
-                props.load(is);
-                String pspClassName = props.getProperty(PROJECTSTAGE_PRODUCER_PROPERTY_KEY);
-                if (pspClassName != null && pspClassName.length() > 0)
-                {
-                    Class<ProjectStageProducer> pspClass = (Class<ProjectStageProducer>) Class.forName(pspClassName);
-                    psp = pspClass.newInstance();
-                }
+                Class<ProjectStageProducer> pspClass = (Class<ProjectStageProducer>) Class.forName(pspClassName);
+                psp = pspClass.newInstance();
             }
 
             if (psp == null)

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java?rev=946360&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java (added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java Wed May 19 19:59:09 2010
@@ -0,0 +1,120 @@
+/*
+ * 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 java.io.IOException;
+import java.io.InputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Properties;
+
+/**
+ * This is a collection of a few useful static helper functions.
+ *
+ * <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
+ */
+public class CodiUtils
+{
+
+    public static final String CODI_PROPERTIES = "/META-INF/extcdi/extcdi.properties";
+
+    /**
+     * Detect the right ClassLoader.
+     * The lookup order is determined by:
+     * <ol>
+     *  <li>ContextClassLoader of the current Thread</li>
+     *  <li>ClassLoader of the given Object 'o'</li>
+     *  <li>ClassLoader of this very CodiUtils class</li>
+     * </ol>
+     *
+     * @param o if not <code>null</code> it may get used to detect the classloader.
+     * @return The {@link ClassLoader} which should get used to create new instances
+     */
+    public static ClassLoader getClassLoader(Object o)
+    {
+        ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+            {
+                public ClassLoader run()
+                {
+                    try
+                    {
+                        return Thread.currentThread().getContextClassLoader();
+                    }
+                    catch (Exception e)
+                    {
+                        return null;
+                    }
+                }
+            }
+        );
+
+        if (loader == null && o != null)
+        {
+            loader = o.getClass().getClassLoader();
+        }
+
+        if (loader == null)
+        {
+            loader = CodiUtils.class.getClassLoader();
+        }
+
+        return loader;
+    }
+
+    /**
+     * Load Properties from a configuration file with the given resourceName.
+     *
+     * @param resourceName
+     * @return Properties or <code>null</code> if the given property file doesn't exist
+     * @throws IOException on underlying IO problems
+     */
+    public static Properties getProperties(String resourceName) throws IOException
+    {
+        Properties props = null;
+        ClassLoader cl = getClassLoader(resourceName);
+        InputStream is = cl.getResourceAsStream(resourceName);
+        if (is != null)
+        {
+            props = new Properties();
+            props.load(is);
+        }
+
+        return props;
+    }
+
+    /**
+     * Lookup the given property from the default CODI properties file.
+     * @param propertyName
+     * @return the value of the property or <code>null</code> it it doesn't exist.
+     * @throws IOException
+     * @throws IllegalArgumentException if the standard CODI properties file couldn't get found
+     */
+    public static String getCodiProperty(String propertyName) throws IOException
+    {
+        String value = null;
+        Properties props = getProperties(CODI_PROPERTIES);
+
+        if (props != null)
+        {
+            value = props.getProperty(propertyName);
+        }
+
+        return value;
+    }
+}

Modified: myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/projectstage/testbeans/MyMailServiceMockImpl.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/projectstage/testbeans/MyMailServiceMockImpl.java?rev=946360&r1=946359&r2=946360&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/projectstage/testbeans/MyMailServiceMockImpl.java (original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/projectstage/testbeans/MyMailServiceMockImpl.java Wed May 19 19:59:09 2010
@@ -27,7 +27,7 @@ import javax.enterprise.inject.Alternati
  * <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
  */
 @Alternative
-@ProjectStageActivated({ProjectStage.CUnitTest.class})
+@ProjectStageActivated({ProjectStage.CUnitTest.class, ProjectStage.CDevelopment.class})
 public class MyMailServiceMockImpl implements MyMailService
 {