Commit 408a4ab3a96b9de59b77ebf122b74bee76f5d47c
1 parent
3567cb60
Exists in
master
Carregamento das classes de configuração via proxy utilizando Javassist
Showing
3 changed files
with
180 additions
and
11 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java
... | ... | @@ -40,12 +40,20 @@ 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 | + | |
43 | 48 | import javax.enterprise.event.Observes; |
49 | +import javax.enterprise.inject.spi.AfterBeanDiscovery; | |
44 | 50 | import javax.enterprise.inject.spi.AnnotatedType; |
51 | +import javax.enterprise.inject.spi.BeanManager; | |
45 | 52 | import javax.enterprise.inject.spi.Extension; |
46 | 53 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
47 | 54 | |
48 | 55 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
56 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | |
49 | 57 | |
50 | 58 | public class ConfigurationBootstrap implements Extension { |
51 | 59 | |
... | ... | @@ -55,11 +63,50 @@ public class ConfigurationBootstrap implements Extension { |
55 | 63 | final AnnotatedType<T> annotatedType = event.getAnnotatedType(); |
56 | 64 | |
57 | 65 | if (annotatedType.getJavaClass().isAnnotationPresent(Configuration.class)) { |
58 | - getCache().add(annotatedType.getJavaClass()); | |
66 | + cache.add(annotatedType.getJavaClass()); | |
67 | + event.veto(); | |
68 | + } | |
69 | + } | |
70 | + | |
71 | + public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception { | |
72 | + Class<?> proxy; | |
73 | + | |
74 | + for (Class<?> config : cache) { | |
75 | + proxy = createProxy(config); | |
76 | + abd.addBean(new ProxyBean((Class<Object>) proxy, beanManager)); | |
59 | 77 | } |
60 | 78 | } |
61 | 79 | |
62 | - public List<Class<?>> getCache() { | |
63 | - return cache; | |
80 | + @SuppressWarnings("unchecked") | |
81 | + private Class<Object> createProxy(Class<?> type) throws Exception { | |
82 | + String superClassName = type.getCanonicalName(); | |
83 | + String chieldClassName = superClassName + "__DemoiselleProxy"; | |
84 | + ClassPool pool = ClassPool.getDefault(); | |
85 | + | |
86 | + CtClass ctChieldClass = pool.makeClass(chieldClassName); | |
87 | + CtClass ctSuperClass = pool.get(superClassName); | |
88 | + ctChieldClass.setSuperclass(ctSuperClass); | |
89 | + | |
90 | + StringBuffer buffer = new StringBuffer(); | |
91 | + buffer.append("new "); | |
92 | + buffer.append(ConfigurationLoader.class.getCanonicalName()); | |
93 | + buffer.append("().load(this);"); | |
94 | + | |
95 | + CtMethod ctChieldMethod; | |
96 | + for (CtMethod ctSuperMethod : ctSuperClass.getDeclaredMethods()) { | |
97 | + ctChieldMethod = CtNewMethod.delegator(ctSuperMethod, ctChieldClass); | |
98 | + ctChieldMethod.insertBefore(buffer.toString()); | |
99 | + | |
100 | + ctChieldClass.addMethod(ctChieldMethod); | |
101 | + } | |
102 | + | |
103 | + // CtConstructor ctChieldDefaultConstructor = CtNewConstructor.defaultConstructor(ctChieldClass); | |
104 | + // ctChieldClass.addConstructor(ctChieldDefaultConstructor); | |
105 | + // | |
106 | + // for (CtConstructor ctConstructor : ctChieldClass.getConstructors()) { | |
107 | + // ctConstructor.insertBefore(buffer.toString()); | |
108 | + // } | |
109 | + | |
110 | + return ctChieldClass.toClass(); | |
64 | 111 | } |
65 | 112 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java
0 → 100644
... | ... | @@ -0,0 +1,130 @@ |
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.Collections; | |
42 | +import java.util.HashSet; | |
43 | +import java.util.Set; | |
44 | + | |
45 | +import javax.enterprise.context.spi.CreationalContext; | |
46 | +import javax.enterprise.inject.Any; | |
47 | +import javax.enterprise.inject.Default; | |
48 | +import javax.enterprise.inject.spi.AnnotatedType; | |
49 | +import javax.enterprise.inject.spi.Bean; | |
50 | +import javax.enterprise.inject.spi.BeanManager; | |
51 | +import javax.enterprise.inject.spi.InjectionPoint; | |
52 | +import javax.enterprise.inject.spi.InjectionTarget; | |
53 | +import javax.enterprise.util.AnnotationLiteral; | |
54 | +import javax.inject.Singleton; | |
55 | + | |
56 | +/** | |
57 | + * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 | |
58 | + */ | |
59 | +public class ProxyBean implements Bean<Object> { | |
60 | + | |
61 | + private Class<Object> proxy; | |
62 | + | |
63 | + private InjectionTarget<Object> injectionTarget; | |
64 | + | |
65 | + public ProxyBean(Class<Object> proxy, BeanManager beanManager) { | |
66 | + AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(proxy); | |
67 | + | |
68 | + this.injectionTarget = beanManager.createInjectionTarget(annotatedType); | |
69 | + this.proxy = proxy; | |
70 | + } | |
71 | + | |
72 | + public Object create(CreationalContext<Object> creationalContext) { | |
73 | + Object instance = injectionTarget.produce(creationalContext); | |
74 | + injectionTarget.inject(instance, creationalContext); | |
75 | + injectionTarget.postConstruct(instance); | |
76 | + return instance; | |
77 | + } | |
78 | + | |
79 | + public void destroy(Object instance, CreationalContext<Object> creationalContext) { | |
80 | + injectionTarget.preDestroy(instance); | |
81 | + injectionTarget.dispose(instance); | |
82 | + creationalContext.release(); | |
83 | + } | |
84 | + | |
85 | + public Set<Type> getTypes() { | |
86 | + Set<Type> types = new HashSet<Type>(); | |
87 | + types.add(proxy.getSuperclass()); | |
88 | + types.add(Object.class); | |
89 | + return types; | |
90 | + } | |
91 | + | |
92 | + @SuppressWarnings("serial") | |
93 | + public Set<Annotation> getQualifiers() { | |
94 | + Set<Annotation> qualifiers = new HashSet<Annotation>(); | |
95 | + qualifiers.add(new AnnotationLiteral<Default>() { | |
96 | + }); | |
97 | + qualifiers.add(new AnnotationLiteral<Any>() { | |
98 | + }); | |
99 | + | |
100 | + return qualifiers; | |
101 | + } | |
102 | + | |
103 | + public Class<? extends Annotation> getScope() { | |
104 | + return Singleton.class; | |
105 | + } | |
106 | + | |
107 | + public String getName() { | |
108 | + return null; | |
109 | + } | |
110 | + | |
111 | + public Set<Class<? extends Annotation>> getStereotypes() { | |
112 | + return Collections.emptySet(); | |
113 | + } | |
114 | + | |
115 | + public Class<Object> getBeanClass() { | |
116 | + return proxy; | |
117 | + } | |
118 | + | |
119 | + public boolean isAlternative() { | |
120 | + return false; | |
121 | + } | |
122 | + | |
123 | + public boolean isNullable() { | |
124 | + return false; | |
125 | + } | |
126 | + | |
127 | + public Set<InjectionPoint> getInjectionPoints() { | |
128 | + return injectionTarget.getInjectionPoints(); | |
129 | + } | |
130 | +} | ... | ... |
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 | } | ... | ... |