myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r1154781 - in /myfaces/extensions/cdi/trunk/core: api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ impl/src/main/java/org/apache/myfaces/extensions/cdi...
Date Sun, 07 Aug 2011 22:18:44 GMT
Author: gpetracek
Date: Sun Aug  7 22:18:43 2011
New Revision: 1154781

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

Added:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/Bundle.java
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleKey.java
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundle.java
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/
    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/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ResourceBundleProducer.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/StringUtils.java
    myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/
    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/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/
    myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
Modified:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ConfigUtils.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/CodiUtils.java

Added: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/Bundle.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/Bundle.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/Bundle.java
(added)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/Bundle.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,57 @@
+/*
+ * 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.api.resource;
+
+import javax.enterprise.util.Nonbinding;
+import javax.inject.Qualifier;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Optional to specify a different bundle name
+ *
+ * @author Gerhard Petracek
+ */
+@Documented
+@Retention(RUNTIME)
+@Target({TYPE, CONSTRUCTOR, METHOD, FIELD})
+
+//cdi annotations
+@Qualifier
+public @interface Bundle
+{
+    /**
+     * Allows to specify the class which is mapped to the resource-bundle
+     * @return class which represents the resource-bundle
+     */
+    @Nonbinding
+    Class<?> value() default Class.class;
+
+    /**
+     * Esp. useful if the class which is mapped to the resource-bundle has a different name
+     * and can't be mapped to the bundle via convention. #name allows to explicitly specify
the name of the bundle.
+     * @return the overridden name which should be used to identify the resource-bundle
+     */
+    @Nonbinding
+    String name() default "";
+}

Added: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleKey.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleKey.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleKey.java
(added)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/BundleKey.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,31 @@
+/*
+ * 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.api.resource;
+
+import java.io.Serializable;
+
+/**
+ * Base interface which can be used for simple keys (if there is no need to inject the key
directly).
+ * The key can be used as type-safe argument for {@link ResourceBundle#getValue(Class)}
+ *
+ * @author Gerhard Petracek
+ */
+public interface BundleKey extends Serializable
+{
+}

Added: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundle.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundle.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundle.java
(added)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundle.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,67 @@
+/*
+ * 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.api.resource;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+/**
+ * @author Gerhard Petracek
+ */
+public interface ResourceBundle extends Serializable
+{
+    /**
+     * Allows to specify a bundle name
+     * @param bundleName name of the bundle which should be used
+     * @return the instance itself
+     */
+    ResourceBundle useBundle(String bundleName);
+
+    /**
+     * Allows to specify a class which is mapped to a bundle
+     * @param bundleClass class mapped to a bundle
+     * @return the instance itself
+     */
+    ResourceBundle useBundle(Class<?> bundleClass);
+
+    /**
+     * Allows to specify a locale
+     * @param locale locale which should be used
+     * @return the instance itself
+     */
+    ResourceBundle useLocale(Locale locale);
+
+    /**
+     * Returns the value for the given key (and the configured bundle and local).
+     * If the key extends a custom class and there is no specified bundle-name,
+     * the name of the super-class will be used as bundle-name.
+     *
+     * @param key current key
+     * @return the value for the given key
+     */
+    String getValue(Class<? extends BundleKey> key);
+
+    /**
+     * Returns the value for the given key (and the configured bundle and local).
+     *
+     * @param key current key
+     * @return the value for the given key
+     */
+    String getValue(String key);
+}

Added: 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/ResourceBundleKey.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
(added)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/resource/ResourceBundleKey.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,75 @@
+/*
+ * 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.api.resource;
+
+import org.apache.myfaces.extensions.cdi.core.api.provider.BeanManagerProvider;
+import org.apache.myfaces.extensions.cdi.core.api.tools.DefaultAnnotation;
+
+/**
+ * Base class which has to be extended if a key should be injected.
+ *
+ * @author Gerhard Petracek
+ */
+public abstract class ResourceBundleKey implements BundleKey
+{
+    private transient ResourceBundle resourceBundle;
+
+    /**
+     * Returns the value of the resource-bundle represented by this key
+     *
+     * @return the value of the resource-bundle represented by this key
+     */
+    @Override
+    public String toString()
+    {
+        return getResourceBundle().getValue(getClass());
+    }
+
+    private ResourceBundle getResourceBundle()
+    {
+        if(this.resourceBundle == null)
+        {
+            Class bundleClass = getClass().getSuperclass();
+
+            if(!bundleClass.isAnnotationPresent(Bundle.class))
+            {
+                bundleClass = null;
+                for(Class interfaceClass : getClass().getInterfaces())
+                {
+                    if(interfaceClass.isAnnotationPresent(Bundle.class))
+                    {
+                        bundleClass = interfaceClass;
+                        break;
+                    }
+                }
+            }
+
+            if(bundleClass == null)
+            {
+                throw new IllegalStateException(getClass() + " has to extend a class or implement
an interface " +
+                        "which is annotated with @" + Bundle.class.getName());
+            }
+
+            this.resourceBundle = BeanManagerProvider.getInstance()
+                    .getContextualReference(ResourceBundle.class, DefaultAnnotation.of(Bundle.class));
+            this.resourceBundle.useBundle(bundleClass);
+        }
+        return resourceBundle;
+    }
+}

Modified: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ConfigUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ConfigUtils.java?rev=1154781&r1=1154780&r2=1154781&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ConfigUtils.java
(original)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/util/ConfigUtils.java
Sun Aug  7 22:18:43 2011
@@ -184,7 +184,7 @@ public abstract class ConfigUtils
      * @return Properties or <code>null</code> if the given property file doesn't
exist
      */
     //TODO
-    private static Properties getProperties(String resourceName)
+    public static Properties getProperties(String resourceName)
     {
         Properties properties = null;
         ClassLoader classLoader = ClassUtils.getClassLoader(resourceName);

Added: 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=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/DefaultResourceBundle.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,167 @@
+/*
+ * 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.resource;
+
+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.ResourceBundle;
+import org.apache.myfaces.extensions.cdi.core.api.util.ConfigUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.projectstage.ProjectStageProducer;
+import org.apache.myfaces.extensions.cdi.core.impl.util.StringUtils;
+
+import javax.enterprise.inject.Typed;
+import javax.inject.Named;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+class DefaultResourceBundle implements ResourceBundle
+{
+    private static final long serialVersionUID = 117890966460274247L;
+
+    private String bundleName;
+    private Locale locale;
+
+    /**
+     * {@inheritDoc}
+     */
+    public ResourceBundle useBundle(String name)
+    {
+        this.bundleName = name;
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public ResourceBundle useBundle(Class<?> bundleClass)
+    {
+        Bundle bundleName = bundleClass.getAnnotation(Bundle.class);
+        if(bundleName != null)
+        {
+            this.bundleName = bundleName.name();
+        }
+
+        if(this.bundleName == null || "".equals(this.bundleName))
+        {
+            String className = bundleClass.getSimpleName();
+            className = className.substring(0, 1).toLowerCase() + className.substring(1);
+            className = StringUtils.replaceUpperCaseCharactersWithUnderscores(className);
+            this.bundleName = bundleClass.getPackage().getName() + "." + className;
+        }
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public ResourceBundle useLocale(Locale locale)
+    {
+        this.locale = locale;
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getValue(Class<? extends BundleKey> key)
+    {
+        Named named = key.getAnnotation(Named.class);
+
+        String resourceKey = null;
+
+        if(named != null)
+        {
+            resourceKey = named.value();
+        }
+
+        if(resourceKey == null)
+        {
+            resourceKey = StringUtils.replaceUpperCaseCharactersWithUnderscores(
+                    key.getSimpleName().substring(0, 1).toLowerCase() + key.getSimpleName().substring(1));
+        }
+
+        Class<?> bundleClass = key.getSuperclass();
+        if(this.bundleName == null && !Object.class.getName().equals(bundleClass.getName()))
+        {
+            useBundle(bundleClass);
+        }
+        return getValue(resourceKey);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getValue(String key)
+    {
+        if(key == null)
+        {
+            return null;
+        }
+
+        if(this.locale == null)
+        {
+            this.locale = Locale.getDefault();
+        }
+
+        if(this.bundleName == null)
+        {
+            if(ProjectStageProducer.getInstance().getProjectStage() == ProjectStage.Development)
+            {
+                Logger logger = Logger.getLogger(DefaultResourceBundle.class.getName());
+                if(logger.isLoggable(Level.WARNING))
+                {
+                    logger.warning("no custom bundle name provided - the codi properties
file " +
+                            "META-INF/myfaces-extcdi.properties is used as fallback");
+                }
+            }
+            this.bundleName = "META-INF/myfaces-extcdi.properties";
+        }
+
+        if(this.bundleName.contains("/"))
+        {
+            Properties properties = ConfigUtils.getProperties(this.bundleName);
+
+            if(properties == null)
+            {
+                return null;
+            }
+            return properties.getProperty(key);
+        }
+        try
+        {
+            if(this.locale == null)
+            {
+                return java.util.ResourceBundle.getBundle(this.bundleName).getString(key);
+            }
+            return java.util.ResourceBundle.getBundle(this.bundleName, this.locale).getString(key);
+        }
+        catch (MissingResourceException e)
+        {
+            return null;
+        }
+    }
+}

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ResourceBundleProducer.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ResourceBundleProducer.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ResourceBundleProducer.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/resource/ResourceBundleProducer.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,78 @@
+/*
+ * 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.resource;
+
+import org.apache.myfaces.extensions.cdi.core.api.resource.Bundle;
+import org.apache.myfaces.extensions.cdi.core.api.resource.ResourceBundle;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+/**
+ * @author Gerhard Petracek
+ */
+@ApplicationScoped
+public class ResourceBundleProducer
+{
+    @Produces
+    @Bundle
+    @Dependent
+    protected ResourceBundle injectableResourceBundle(InjectionPoint injectionPoint)
+    {
+        Bundle bundle = getBundleClass(injectionPoint.getQualifiers());
+
+        if(bundle != null)
+        {
+            Class bundleClass = bundle.value();
+
+            if(bundleClass != null && !Class.class.getName().equals(bundleClass.getName()))
+            {
+                return createDefaultResourceBundle().useBundle(bundleClass);
+            }
+
+            if(!"".equals(bundle.name()))
+            {
+                return createDefaultResourceBundle().useBundle(bundle.name());
+            }
+        }
+        return createDefaultResourceBundle();
+    }
+
+    private static Bundle getBundleClass(Set<Annotation> qualifiers)
+    {
+        for(Annotation qualifier : qualifiers)
+        {
+            if(Bundle.class.isAssignableFrom(qualifier.annotationType()))
+            {
+                return ((Bundle)qualifier);
+            }
+        }
+
+        return null;
+    }
+
+    protected ResourceBundle createDefaultResourceBundle()
+    {
+        return new DefaultResourceBundle();
+    }
+}

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/CodiUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/CodiUtils.java?rev=1154781&r1=1154780&r2=1154781&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/CodiUtils.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/CodiUtils.java
Sun Aug  7 22:18:43 2011
@@ -562,23 +562,6 @@ public abstract class CodiUtils
 
             baseKey = baseKey.substring(0, 1).toLowerCase() + baseKey.substring(1);
 
-            StringBuilder dynamicKey = new StringBuilder(baseKey.length());
-
-            Character current;
-            for(int i = 0; i < baseKey.length(); i++)
-            {
-                current = baseKey.charAt(i);
-                if(Character.isUpperCase(current))
-                {
-                    dynamicKey.append("_");
-                    dynamicKey.append(Character.toLowerCase(current));
-                }
-                else
-                {
-                    dynamicKey.append(current);
-                }
-            }
-
             String className = runtimeException.getStackTrace()[1].getClassName();
 
             Class configClass = ClassUtils.tryToLoadClassForName(className);
@@ -593,7 +576,8 @@ public abstract class CodiUtils
                 className = className.substring(className.lastIndexOf(".") + 1);
             }
 
-            key = className + "." + dynamicKey.toString();
+            String convertedKey = StringUtils.replaceUpperCaseCharactersWithUnderscores(baseKey);
+            key = className + "." + convertedKey;
         }
 
         String result = lookupFromEnvironment(key, String.class, null, null);

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/StringUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/StringUtils.java?rev=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/StringUtils.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/util/StringUtils.java
Sun Aug  7 22:18:43 2011
@@ -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.myfaces.extensions.cdi.core.impl.util;
+
+import javax.enterprise.inject.Typed;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Typed()
+public class StringUtils
+{
+    private StringUtils()
+    {
+        // prevent instantiation
+    }
+
+    /**
+     * Replaces all upper-case characters of the given key with an underscore and
+     * the lower-case version of the character
+     * @param baseKey current key
+     * @return the transformed version of the given key
+     */
+    public static String replaceUpperCaseCharactersWithUnderscores(String baseKey)
+    {
+        StringBuilder dynamicKey = new StringBuilder(baseKey.length());
+
+        Character current;
+        for(int i = 0; i < baseKey.length(); i++)
+        {
+            current = baseKey.charAt(i);
+            if(Character.isUpperCase(current))
+            {
+                dynamicKey.append("_");
+                dynamicKey.append(Character.toLowerCase(current));
+            }
+            else
+            {
+                dynamicKey.append(current);
+            }
+        }
+        return dynamicKey.toString();
+    }
+}

Added: 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=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/ResourceBundleTest.java
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,56 @@
+/*
+ * 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.test.impl.resource;
+
+import org.apache.myfaces.extensions.cdi.core.api.resource.ResourceBundle;
+import org.apache.myfaces.extensions.cdi.core.impl.resource.ResourceBundleProducer;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import javax.enterprise.inject.spi.InjectionPoint;
+
+public class ResourceBundleTest
+{
+    @Test
+    public void testNonTypesafeBundleKey() throws Exception
+    {
+        ResourceBundle resourceBundle = getResourceBundle();
+        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");
+    }
+
+    private ResourceBundle getResourceBundle()
+    {
+        return new ResourceBundleProducer()
+        {
+            @Override
+            public ResourceBundle injectableResourceBundle(InjectionPoint injectionPoint)
+            {
+                return createDefaultResourceBundle();
+            }
+        }.injectableResourceBundle(null);
+    }
+}

Added: 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=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/java/org/apache/myfaces/extensions/cdi/core/test/impl/resource/Testbundle.java
Sun Aug  7 22:18:43 2011
@@ -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.myfaces.extensions.cdi.core.test.impl.resource;
+
+import org.apache.myfaces.extensions.cdi.core.api.resource.BundleKey;
+
+import javax.enterprise.inject.Typed;
+import javax.inject.Named;
+
+@Typed()
+public class Testbundle
+{
+    public static class MyValue extends Testbundle implements BundleKey
+    {}
+
+    @Named("my.value")
+    public static class MyValue1 extends Testbundle implements BundleKey
+    {}
+}

Added: 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=1154781&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
(added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/test/resources/org/apache/myfaces/extensions/cdi/core/test/impl/resource/testbundle.properties
Sun Aug  7 22:18:43 2011
@@ -0,0 +1,20 @@
+# 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.
+
+value1=1
+my_value=2
+my.value=3
\ No newline at end of file



Mime
View raw message