diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Name.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Name.java index 828edae..1bd68ff 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Name.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Name.java @@ -47,7 +47,9 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; +@Qualifier @Inherited @Retention(RUNTIME) @Target({ TYPE, FIELD, METHOD, PARAMETER }) diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomInjectionPoint.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomInjectionPoint.java new file mode 100644 index 0000000..8edc06d --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomInjectionPoint.java @@ -0,0 +1,136 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.bootstrap; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Member; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import javax.enterprise.inject.spi.Annotated; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.InjectionPoint; + +public class CustomInjectionPoint implements InjectionPoint { + + private final Bean bean; + + private final Type beanType; + + private final Set qualifiers; + + public CustomInjectionPoint(Bean bean, Type beanType, Annotation... qualifiers) { + this.bean = bean; + this.beanType = beanType; + this.qualifiers = new HashSet(Arrays.asList(qualifiers)); + } + + @Override + public Type getType() { + return this.beanType; + } + + @Override + public Set getQualifiers() { + return this.qualifiers; + } + + @Override + public Bean getBean() { + return this.bean; + } + + @Override + public Member getMember() { + return null; + } + + @Override + public boolean isDelegate() { + return false; + } + + @Override + public boolean isTransient() { + return false; + } + + @Override + public Annotated getAnnotated() { + return new Annotated() { + + @Override + public Type getBaseType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getTypeClosure() { + // TODO Auto-generated method stub + return null; + } + + @Override + @SuppressWarnings("unchecked") + public T getAnnotation(Class annotationType) { + T result = null; + + for (Annotation annotation : getAnnotations()) { + if (annotation.annotationType() == annotationType) { + result = (T) annotation; + break; + } + } + + return result; + } + + @Override + public Set getAnnotations() { + return qualifiers; + } + + @Override + public boolean isAnnotationPresent(Class annotationType) { + return qualifiers.contains(annotationType); + } + }; + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java index 3511a67..d15009a 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java @@ -73,26 +73,27 @@ public class ResourceBundleProducer implements Serializable { * baseName */ public static ResourceBundle create(String baseName, Locale locale) { - ResourceBundle bundle = null; - bundle = new ResourceBundle(baseName, locale); - return bundle; + return new ResourceBundle(baseName, locale); } /** * This method is the factory default for ResourceBundle. It creates the ResourceBundle based on a file called * messages.properties. */ - @Produces @Default - public ResourceBundle create(InjectionPoint ip) { - String baseName; - - if (ip != null && ip.getAnnotated().isAnnotationPresent(Name.class)) { - baseName = ip.getAnnotated().getAnnotation(Name.class).value(); - } else { - baseName = "messages"; - } + @Produces + public ResourceBundle createDefault(InjectionPoint ip) { + return create("messages", Beans.getReference(Locale.class)); + } + /** + * This method is the factory default for ResourceBundle. It creates the ResourceBundle based on a file called + * messages.properties. + */ + @Name("") + @Produces + public ResourceBundle createNamed(InjectionPoint ip) { + String baseName = ip.getAnnotated().getAnnotation(Name.class).value(); return create(baseName, Beans.getReference(Locale.class)); } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java index dea8b65..9064ab0 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java @@ -49,14 +49,18 @@ package br.gov.frameworkdemoiselle.util; import java.lang.annotation.Annotation; +import java.lang.reflect.Type; import java.util.Locale; import java.util.NoSuchElementException; import java.util.Set; +import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionPoint; import br.gov.frameworkdemoiselle.DemoiselleException; +import br.gov.frameworkdemoiselle.internal.bootstrap.CustomInjectionPoint; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; public final class Beans { @@ -78,7 +82,7 @@ public final class Beans { T instance; try { - instance = (T) getReference(manager.getBeans(beanClass, qualifiers), beanClass); + instance = (T) getReference(manager.getBeans(beanClass, qualifiers), beanClass, qualifiers); } catch (NoSuchElementException cause) { StringBuffer buffer = new StringBuffer(); @@ -126,10 +130,13 @@ public final class Beans { } @SuppressWarnings("unchecked") - private static T getReference(Set> beans, Class beanClass) { + private static T getReference(Set> beans, Class beanClass, Annotation... qualifiers) { Bean bean = beans.iterator().next(); - return (T) manager.getReference(bean, beanClass == null ? bean.getBeanClass() : beanClass, - manager.createCreationalContext(bean)); + CreationalContext context = manager.createCreationalContext(bean); + Type beanType = beanClass == null ? bean.getBeanClass() : beanClass; + InjectionPoint injectionPoint = new CustomInjectionPoint(bean, beanType, qualifiers); + + return (T) manager.getInjectableReference(injectionPoint, context); } private static T getReference(Set> beans) { @@ -139,4 +146,4 @@ public final class Beans { private static ResourceBundle getBundle() { return ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault()); } -} \ No newline at end of file +} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducerTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducerTest.java index 0705bb8..e26b501 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducerTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducerTest.java @@ -94,7 +94,7 @@ public class ResourceBundleProducerTest { @Test public void testCreateNullInjectionPoint() { ResourceBundleProducer factory = new ResourceBundleProducer(); - ResourceBundle resourceBundle = factory.create((InjectionPoint) null); + ResourceBundle resourceBundle = factory.createDefault((InjectionPoint) null); Assert.assertNotNull(resourceBundle); } @@ -114,7 +114,7 @@ public class ResourceBundleProducerTest { replay(ip); ResourceBundleProducer factory = new ResourceBundleProducer(); - Assert.assertNotNull(factory.create(ip)); + Assert.assertNotNull(factory.createDefault(ip)); } // @Test -- libgit2 0.21.2