Commit d9e0312ea7e5eb8f1e1933d6088a39b906c2fddb
Exists in
master
Merge pull request #19 from zyc/2.3
Ajuste no bug com @Configuration em EAR
Showing
8 changed files
with
245 additions
and
21 deletions
Show diff stats
impl/core/pom.xml
| ... | ... | @@ -94,6 +94,12 @@ |
| 94 | 94 | <artifactId>slf4j-api</artifactId> |
| 95 | 95 | </dependency> |
| 96 | 96 | <dependency> |
| 97 | + <groupId>org.javassist</groupId> | |
| 98 | + <artifactId>javassist</artifactId> | |
| 99 | + <version>3.14.0-GA</version> | |
| 100 | + </dependency> | |
| 101 | + | |
| 102 | + <dependency> | |
| 97 | 103 | <groupId>commons-configuration</groupId> |
| 98 | 104 | <artifactId>commons-configuration</artifactId> |
| 99 | 105 | <exclusions> | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java
| ... | ... | @@ -43,11 +43,8 @@ import java.lang.annotation.Inherited; |
| 43 | 43 | import java.lang.annotation.Retention; |
| 44 | 44 | import java.lang.annotation.Target; |
| 45 | 45 | |
| 46 | -import javax.enterprise.context.ApplicationScoped; | |
| 47 | 46 | import javax.enterprise.inject.Stereotype; |
| 48 | 47 | import javax.enterprise.util.Nonbinding; |
| 49 | -import javax.inject.Singleton; | |
| 50 | -import javax.interceptor.InterceptorBinding; | |
| 51 | 48 | |
| 52 | 49 | /** |
| 53 | 50 | * Identifies a <b>configuration class</b>, that is, a structure reserved to store configuration values retrieved from a |
| ... | ... | @@ -64,7 +61,7 @@ import javax.interceptor.InterceptorBinding; |
| 64 | 61 | * |
| 65 | 62 | * @author SERPRO |
| 66 | 63 | */ |
| 67 | -@Singleton | |
| 64 | +// @Singleton | |
| 68 | 65 | @Stereotype |
| 69 | 66 | @Inherited |
| 70 | 67 | @Target(TYPE) | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java
| ... | ... | @@ -40,26 +40,69 @@ import java.util.ArrayList; |
| 40 | 40 | import java.util.Collections; |
| 41 | 41 | import java.util.List; |
| 42 | 42 | |
| 43 | +import javassist.ClassPool; | |
| 44 | +import javassist.CtClass; | |
| 45 | +import javassist.CtMethod; | |
| 46 | +import javassist.CtNewMethod; | |
| 47 | +import javassist.LoaderClassPath; | |
| 48 | + | |
| 43 | 49 | import javax.enterprise.event.Observes; |
| 50 | +import javax.enterprise.inject.spi.AfterBeanDiscovery; | |
| 44 | 51 | import javax.enterprise.inject.spi.AnnotatedType; |
| 52 | +import javax.enterprise.inject.spi.BeanManager; | |
| 45 | 53 | import javax.enterprise.inject.spi.Extension; |
| 46 | 54 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 47 | 55 | |
| 48 | 56 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
| 57 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationImpl; | |
| 49 | 58 | |
| 50 | 59 | public class ConfigurationBootstrap implements Extension { |
| 51 | 60 | |
| 52 | - private final List<Class<?>> cache = Collections.synchronizedList(new ArrayList<Class<?>>()); | |
| 61 | + private final List<Class<Object>> cache = Collections.synchronizedList(new ArrayList<Class<Object>>()); | |
| 53 | 62 | |
| 54 | - public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event) { | |
| 55 | - final AnnotatedType<T> annotatedType = event.getAnnotatedType(); | |
| 63 | + public void processAnnotatedType(@Observes final ProcessAnnotatedType<Object> event) { | |
| 64 | + final AnnotatedType<Object> annotatedType = event.getAnnotatedType(); | |
| 56 | 65 | |
| 57 | 66 | if (annotatedType.getJavaClass().isAnnotationPresent(Configuration.class)) { |
| 58 | - getCache().add(annotatedType.getJavaClass()); | |
| 67 | + cache.add(annotatedType.getJavaClass()); | |
| 68 | + event.veto(); | |
| 69 | + } | |
| 70 | + } | |
| 71 | + | |
| 72 | + public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager beanManager) throws Exception { | |
| 73 | + Class<Object> proxy; | |
| 74 | + | |
| 75 | + for (Class<Object> config : cache) { | |
| 76 | + proxy = createProxy(config); | |
| 77 | + event.addBean(new CustomBean(proxy, beanManager)); | |
| 59 | 78 | } |
| 60 | 79 | } |
| 61 | 80 | |
| 62 | - public List<Class<?>> getCache() { | |
| 63 | - return cache; | |
| 81 | + @SuppressWarnings("unchecked") | |
| 82 | + private Class<Object> createProxy(Class<Object> type) throws Exception { | |
| 83 | + String superClassName = type.getCanonicalName(); | |
| 84 | + String chieldClassName = superClassName + "__DemoiselleProxy"; | |
| 85 | + | |
| 86 | + ClassPool pool = ClassPool.getDefault(); | |
| 87 | + CtClass ctChieldClass = pool.getOrNull(chieldClassName); | |
| 88 | + | |
| 89 | + ClassLoader classLoader = type.getClassLoader(); | |
| 90 | + if (ctChieldClass == null) { | |
| 91 | + pool.appendClassPath(new LoaderClassPath(classLoader)); | |
| 92 | + CtClass ctSuperClass = pool.get(superClassName); | |
| 93 | + | |
| 94 | + ctChieldClass = pool.getAndRename(ConfigurationImpl.class.getCanonicalName(), chieldClassName); | |
| 95 | + ctChieldClass.setSuperclass(ctSuperClass); | |
| 96 | + | |
| 97 | + CtMethod ctChieldMethod; | |
| 98 | + for (CtMethod ctSuperMethod : ctSuperClass.getDeclaredMethods()) { | |
| 99 | + ctChieldMethod = CtNewMethod.delegator(ctSuperMethod, ctChieldClass); | |
| 100 | + ctChieldMethod.insertBefore("load(this);"); | |
| 101 | + | |
| 102 | + ctChieldClass.addMethod(ctChieldMethod); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + return ctChieldClass.toClass(classLoader, type.getProtectionDomain()); | |
| 64 | 107 | } |
| 65 | 108 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java
0 → 100644
| ... | ... | @@ -0,0 +1,171 @@ |
| 1 | +/* | |
| 2 | + * Demoiselle Framework | |
| 3 | + * Copyright (C) 2010 SERPRO | |
| 4 | + * ---------------------------------------------------------------------------- | |
| 5 | + * This file is part of Demoiselle Framework. | |
| 6 | + * | |
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | + * as published by the Free Software Foundation. | |
| 10 | + * | |
| 11 | + * This program is distributed in the hope that it will be useful, | |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + * GNU General Public License for more details. | |
| 15 | + * | |
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | + * ---------------------------------------------------------------------------- | |
| 21 | + * Este arquivo é parte do Framework Demoiselle. | |
| 22 | + * | |
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | + * do Software Livre (FSF). | |
| 26 | + * | |
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | + * para maiores detalhes. | |
| 31 | + * | |
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | + */ | |
| 37 | +package br.gov.frameworkdemoiselle.internal.bootstrap; | |
| 38 | + | |
| 39 | +import java.lang.annotation.Annotation; | |
| 40 | +import java.lang.reflect.Type; | |
| 41 | +import java.util.HashSet; | |
| 42 | +import java.util.Set; | |
| 43 | + | |
| 44 | +import javax.enterprise.context.Dependent; | |
| 45 | +import javax.enterprise.context.NormalScope; | |
| 46 | +import javax.enterprise.context.spi.CreationalContext; | |
| 47 | +import javax.enterprise.inject.Alternative; | |
| 48 | +import javax.enterprise.inject.Any; | |
| 49 | +import javax.enterprise.inject.Default; | |
| 50 | +import javax.enterprise.inject.Stereotype; | |
| 51 | +import javax.enterprise.inject.spi.AnnotatedType; | |
| 52 | +import javax.enterprise.inject.spi.Bean; | |
| 53 | +import javax.enterprise.inject.spi.BeanManager; | |
| 54 | +import javax.enterprise.inject.spi.InjectionPoint; | |
| 55 | +import javax.enterprise.inject.spi.InjectionTarget; | |
| 56 | +import javax.enterprise.util.AnnotationLiteral; | |
| 57 | +import javax.inject.Named; | |
| 58 | +import javax.inject.Qualifier; | |
| 59 | +import javax.inject.Scope; | |
| 60 | + | |
| 61 | +/** | |
| 62 | + * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 | |
| 63 | + */ | |
| 64 | +public class CustomBean implements Bean<Object> { | |
| 65 | + | |
| 66 | + private Class<Object> beanClass; | |
| 67 | + | |
| 68 | + private InjectionTarget<Object> injectionTarget; | |
| 69 | + | |
| 70 | + public CustomBean(Class<Object> beanClass, BeanManager beanManager) { | |
| 71 | + AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(beanClass); | |
| 72 | + | |
| 73 | + this.injectionTarget = beanManager.createInjectionTarget(annotatedType); | |
| 74 | + this.beanClass = beanClass; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public Object create(CreationalContext<Object> creationalContext) { | |
| 78 | + Object instance = injectionTarget.produce(creationalContext); | |
| 79 | + injectionTarget.inject(instance, creationalContext); | |
| 80 | + injectionTarget.postConstruct(instance); | |
| 81 | + | |
| 82 | + return instance; | |
| 83 | + } | |
| 84 | + | |
| 85 | + public void destroy(Object instance, CreationalContext<Object> creationalContext) { | |
| 86 | + injectionTarget.preDestroy(instance); | |
| 87 | + injectionTarget.dispose(instance); | |
| 88 | + creationalContext.release(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public Set<Type> getTypes() { | |
| 92 | + Set<Type> types = new HashSet<Type>(); | |
| 93 | + types.add(beanClass.getSuperclass()); | |
| 94 | + types.add(Object.class); | |
| 95 | + | |
| 96 | + return types; | |
| 97 | + } | |
| 98 | + | |
| 99 | + @SuppressWarnings("serial") | |
| 100 | + public Set<Annotation> getQualifiers() { | |
| 101 | + Set<Annotation> result = new HashSet<Annotation>(); | |
| 102 | + | |
| 103 | + result.add(new AnnotationLiteral<Default>() { | |
| 104 | + }); | |
| 105 | + result.add(new AnnotationLiteral<Any>() { | |
| 106 | + }); | |
| 107 | + | |
| 108 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 109 | + if (annotation.getClass().isAnnotationPresent(Qualifier.class)) { | |
| 110 | + result.add(annotation); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + return result; | |
| 115 | + } | |
| 116 | + | |
| 117 | + public Class<? extends Annotation> getScope() { | |
| 118 | + Class<? extends Annotation> result = Dependent.class; | |
| 119 | + | |
| 120 | + Class<? extends Annotation> annotationClass; | |
| 121 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 122 | + annotationClass = annotation.getClass(); | |
| 123 | + | |
| 124 | + if (annotationClass.isAnnotationPresent(Scope.class) | |
| 125 | + || annotationClass.isAnnotationPresent(NormalScope.class)) { | |
| 126 | + result = annotationClass; | |
| 127 | + break; | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + return result; | |
| 132 | + } | |
| 133 | + | |
| 134 | + public String getName() { | |
| 135 | + String result = null; | |
| 136 | + | |
| 137 | + if (beanClass.isAnnotationPresent(Named.class)) { | |
| 138 | + result = beanClass.getAnnotation(Named.class).value(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + return result; | |
| 142 | + } | |
| 143 | + | |
| 144 | + public Set<Class<? extends Annotation>> getStereotypes() { | |
| 145 | + Set<Class<? extends Annotation>> result = new HashSet<Class<? extends Annotation>>(); | |
| 146 | + | |
| 147 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 148 | + if (annotation.getClass().isAnnotationPresent(Stereotype.class)) { | |
| 149 | + result.add(annotation.getClass()); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + return result; | |
| 154 | + } | |
| 155 | + | |
| 156 | + public Class<Object> getBeanClass() { | |
| 157 | + return beanClass; | |
| 158 | + } | |
| 159 | + | |
| 160 | + public boolean isAlternative() { | |
| 161 | + return beanClass.isAnnotationPresent(Alternative.class); | |
| 162 | + } | |
| 163 | + | |
| 164 | + public boolean isNullable() { | |
| 165 | + return false; | |
| 166 | + } | |
| 167 | + | |
| 168 | + public Set<InjectionPoint> getInjectionPoints() { | |
| 169 | + return injectionTarget.getInjectionPoints(); | |
| 170 | + } | |
| 171 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java
| ... | ... | @@ -40,11 +40,9 @@ import javax.enterprise.event.Observes; |
| 40 | 40 | |
| 41 | 41 | import org.slf4j.Logger; |
| 42 | 42 | |
| 43 | -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | |
| 44 | 43 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; |
| 45 | 44 | import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; |
| 46 | 45 | import br.gov.frameworkdemoiselle.lifecycle.Startup; |
| 47 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 48 | 46 | |
| 49 | 47 | /** |
| 50 | 48 | * This class is the bootstrap to execute the processes at load time. |
| ... | ... | @@ -63,12 +61,6 @@ public class StartupBootstrap extends AbstractLifecycleBootstrap<Startup> { |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | public void startup(@Observes AfterStartupProccess event) { |
| 66 | - ConfigurationLoader loader = Beans.getReference(ConfigurationLoader.class); | |
| 67 | - ConfigurationBootstrap bootstrap = Beans.getReference(ConfigurationBootstrap.class); | |
| 68 | - for (Class<?> clazz: bootstrap.getCache()) { | |
| 69 | - Object object = Beans.getReference(clazz); | |
| 70 | - loader.load(object); | |
| 71 | - } | |
| 72 | 64 | proccessEvent(); |
| 73 | 65 | } |
| 74 | 66 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java
0 → 100644
| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | +package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | + | |
| 3 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | |
| 4 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 5 | + | |
| 6 | +public class ConfigurationImpl { | |
| 7 | + | |
| 8 | + private boolean loaded = false; | |
| 9 | + | |
| 10 | + @SuppressWarnings("unused") | |
| 11 | + private synchronized void load(Object instance) { | |
| 12 | + if (!loaded) { | |
| 13 | + Beans.getReference(ConfigurationLoader.class).load(instance); | |
| 14 | + loaded = true; | |
| 15 | + } | |
| 16 | + } | |
| 17 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java
| ... | ... | @@ -38,13 +38,11 @@ package br.gov.frameworkdemoiselle.internal.producer; |
| 38 | 38 | |
| 39 | 39 | import java.io.Serializable; |
| 40 | 40 | import java.util.Locale; |
| 41 | -import java.util.MissingResourceException; | |
| 42 | 41 | |
| 43 | 42 | import javax.enterprise.inject.Default; |
| 44 | 43 | import javax.enterprise.inject.Produces; |
| 45 | 44 | import javax.enterprise.inject.spi.InjectionPoint; |
| 46 | 45 | |
| 47 | -import br.gov.frameworkdemoiselle.DemoiselleException; | |
| 48 | 46 | import br.gov.frameworkdemoiselle.annotation.Name; |
| 49 | 47 | import br.gov.frameworkdemoiselle.util.Beans; |
| 50 | 48 | import br.gov.frameworkdemoiselle.util.ResourceBundle; | ... | ... |
parent/minimal/pom.xml