myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r1154862 - in /myfaces/extensions/cdi/trunk/core: api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ impl/src/test/java/org/apache/myfaces/extensio...
Date Mon, 08 Aug 2011 07:49:11 GMT
Author: gpetracek
Date: Mon Aug  8 07:49:10 2011
New Revision: 1154862

URL: http://svn.apache.org/viewvc?rev=1154862&view=rev
Log:
EXTCDI-204 and EXTCDI-211 injectable resource-bundle and resource-bundle-value

Added:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleValue.java
      - copied, changed from r1154790, myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
Removed:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
Modified:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
    myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
    myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties

Copied: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleValue.java
(from r1154790, myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleValue.java?p2=myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleValue.java&p1=myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java&r1=1154790&r2=1154862&rev=1154862&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
(original)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleValue.java
Mon Aug  8 07:49:10 2011
@@ -24,11 +24,13 @@ import org.apache.myfaces.extensions.cdi
 import java.lang.annotation.Annotation;
 
 /**
- * Base class which has to be extended if a key should be injected.
+ * Base class which has to be extended if a key resolves directly to the value.
+ * Compared to {@link BundleKey} it isn't needed to use the {@link ResourceBundle} manually
+ * for resolving the value. It's possible to call #toString in-/directly.
  *
  * @author Gerhard Petracek
  */
-public abstract class ResourceBundleKey implements BundleKey
+public abstract class BundleValue implements BundleKey
 {
     private transient ResourceBundle resourceBundle;
 
@@ -58,7 +60,8 @@ public abstract class ResourceBundleKey 
         return getResourceBundle().getValue(getClass());
     }
 
-    private ResourceBundle getResourceBundle()
+    //override it e.g. for test-cases
+    protected ResourceBundle getResourceBundle()
     {
         if(this.resourceBundle == null)
         {

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java?rev=1154862&r1=1154861&r2=1154862&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
Mon Aug  8 07:49:10 2011
@@ -21,6 +21,7 @@ package org.apache.myfaces.extensions.cd
 import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
 import org.apache.myfaces.extensions.cdi.core.api.resource.Bundle;
 import org.apache.myfaces.extensions.cdi.core.api.resource.BundleKey;
+import org.apache.myfaces.extensions.cdi.core.api.resource.BundleValue;
 import org.apache.myfaces.extensions.cdi.core.api.resource.ResourceBundle;
 import org.apache.myfaces.extensions.cdi.core.api.util.ConfigUtils;
 import org.apache.myfaces.extensions.cdi.core.impl.projectstage.ProjectStageProducer;
@@ -28,6 +29,8 @@ import org.apache.myfaces.extensions.cdi
 
 import javax.enterprise.inject.Typed;
 import javax.inject.Named;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.Properties;
@@ -89,6 +92,12 @@ class DefaultResourceBundle implements R
      */
     public String getValue(Class<? extends BundleKey> key)
     {
+        //in case of an inner class
+        if("".equals(key.getSimpleName()) && BundleKey.class.isAssignableFrom(key.getSuperclass()))
+        {
+            key = (Class<? extends BundleKey>) key.getSuperclass();
+        }
+
         Named named = key.getAnnotation(Named.class);
 
         String resourceKey = null;
@@ -105,10 +114,39 @@ class DefaultResourceBundle implements R
         }
 
         Class<?> bundleClass = key.getSuperclass();
-        if(this.bundleName == null && !Object.class.getName().equals(bundleClass.getName()))
+        if(this.bundleName == null &&
+                !Object.class.getName().equals(bundleClass.getName()) &&
+                !BundleValue.class.isAssignableFrom(bundleClass))
         {
             useBundle(bundleClass);
         }
+        else
+        {
+            List<Class> bundleClassCandidates = new ArrayList<Class>();
+
+            for(Class interfaceClass : key.getInterfaces())
+            {
+                if(interfaceClass.isAnnotationPresent(Bundle.class))
+                {
+                    useBundle(interfaceClass);
+                    break;
+                }
+                if(!BundleKey.class.isAssignableFrom(interfaceClass))
+                {
+                    bundleClassCandidates.add(interfaceClass);
+                }
+            }
+
+            if(this.bundleName == null && bundleClassCandidates.size() == 1)
+            {
+                useBundle(bundleClassCandidates.iterator().next());
+            }
+            else if(this.bundleName == null && bundleClassCandidates.size() >
1)
+            {
+                throw new IllegalStateException(key.getName() + " implements multiple custom
interfaces and " +
+                        "non of them is annotated with @" + Bundle.class);
+            }
+        }
         return getValue(resourceKey);
     }
 

Modified: myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java?rev=1154862&r1=1154861&r2=1154862&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
Mon Aug  8 07:49:10 2011
@@ -30,19 +30,38 @@ public class ResourceBundleTest
     @Test
     public void testNonTypesafeBundleKey() throws Exception
     {
-        ResourceBundle resourceBundle = getResourceBundle();
+        ResourceBundle resourceBundle = getTestResourceBundle();
         Assert.assertEquals(resourceBundle.useBundle(getClass().getPackage().getName() +
".testBundle").getValue("value1"), "1");
     }
 
     @Test
     public void testTypesafeBundleKey() throws Exception
     {
-        ResourceBundle resourceBundle = getResourceBundle();
-        Assert.assertEquals(resourceBundle.getValue(Testbundle.MyValue.class), "2");
-        Assert.assertEquals(resourceBundle.getValue(Testbundle.MyValue1.class), "3");
+        ResourceBundle resourceBundle = getTestResourceBundle();
+        Assert.assertEquals(resourceBundle.getValue(Testbundle.MyValue.class), "2.1");
+        Assert.assertEquals(resourceBundle.getValue(Testbundle.MyValue1.class), "2.2");
     }
 
-    private ResourceBundle getResourceBundle()
+    @Test
+    public void testTypesafeBundleValue() throws Exception
+    {
+        Assert.assertEquals(new Testbundle.MyValue2(){
+            @Override
+            protected ResourceBundle getResourceBundle()
+            {
+                return getTestResourceBundle();
+            }
+        }.toString(), "3.1");
+        Assert.assertEquals(new Testbundle.MyValue3(){
+            @Override
+            protected ResourceBundle getResourceBundle()
+            {
+                return getTestResourceBundle();
+            }
+        }.toString(), "3.2");
+    }
+
+    private ResourceBundle getTestResourceBundle()
     {
         return new ResourceBundleProducer()
         {

Modified: myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java?rev=1154862&r1=1154861&r2=1154862&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
Mon Aug  8 07:49:10 2011
@@ -19,17 +19,21 @@
 package org.apache.myfaces.extensions.cdi.core.test.impl.resource;
 
 import org.apache.myfaces.extensions.cdi.core.api.resource.BundleKey;
+import org.apache.myfaces.extensions.cdi.core.api.resource.BundleValue;
 
 import javax.enterprise.inject.Typed;
 import javax.inject.Named;
 
 @Typed()
-public class Testbundle
+public interface Testbundle
 {
-    public static class MyValue extends Testbundle implements BundleKey
-    {}
+    public static class MyValue implements Testbundle, BundleKey {}
 
     @Named("my.value")
-    public static class MyValue1 extends Testbundle implements BundleKey
-    {}
+    public static class MyValue1 implements Testbundle, BundleKey {}
+
+    public static class MyValue2 extends BundleValue implements Testbundle {}
+
+    @Named("my.value2")
+    public static class MyValue3 extends BundleValue implements Testbundle {}
 }

Modified: myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties?rev=1154862&r1=1154861&r2=1154862&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
Mon Aug  8 07:49:10 2011
@@ -16,5 +16,7 @@
 # under the License.
 
 value1=1
-my_value=2
-my.value=3
\ No newline at end of file
+my_value=2.1
+my.value=2.2
+my_value2=3.1
+my.value2=3.2



Mime
View raw message