Commit 38a50bbb67bd3e5762b4bd8fa05eb2c406765740

Authored by Cleverson Sacramento
1 parent 6e1afc34
Exists in master

Limpeza e organização do código-fonte referente ao proxy javassist para

classes de configuração
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java
... ... @@ -74,7 +74,7 @@ public class ConfigurationBootstrap implements Extension {
74 74  
75 75 for (Class<Object> config : cache) {
76 76 proxy = createProxy(config);
77   - event.addBean(new ProxyBean(proxy, beanManager));
  77 + event.addBean(new CustomBean(proxy, beanManager));
78 78 }
79 79 }
80 80  
... ... @@ -85,40 +85,21 @@ public class ConfigurationBootstrap implements Extension {
85 85  
86 86 ClassPool pool = ClassPool.getDefault();
87 87 ClassPool.doPruning = true;
88   -
  88 +
89 89 CtClass ctChieldClass = pool.getOrNull(chieldClassName);
90 90  
91 91 ClassLoader classLoader = type.getClassLoader();
92 92 if (ctChieldClass == null) {
93   -
94 93 pool.appendClassPath(new LoaderClassPath(classLoader));
95   - // classLoader = Thread.currentThread().getContextClassLoader();
96   - // pool.appendClassPath(new LoaderClassPath(classLoader));
97   - // classLoader = ConfigurationLoader.getClassLoaderForClass(superClassName);
98   - // pool.appendClassPath(new LoaderClassPath(classLoader));
99   -
100 94 CtClass ctSuperClass = pool.get(superClassName);
101 95  
102   - // ctChieldClass = pool.makeClass(chieldClassName, ctSuperClass);
103 96 ctChieldClass = pool.getAndRename(ConfigurationImpl.class.getCanonicalName(), chieldClassName);
104 97 ctChieldClass.setSuperclass(ctSuperClass);
105 98  
106   - // for (CtField ctFieldImpl : ctClassImpl.getDeclaredFields()) {
107   - // ctChieldClass.addField(new CtField(ctFieldImpl, ctChieldClass));
108   - // System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFF-----------" + ctFieldImpl.toString());
109   - // }
110   -
111   - // for (CtMethod ctMethodImpl : ctClassImpl.getDeclaredMethods()) {
112   - // ctChieldClass.addMethod(new CtMethod(ctMethodImpl, ctChieldClass, null));
113   - // System.out.println("MMMMMMMMMMMMMMMMMMMMMMMMMM-----------" + ctMethodImpl.toString());
114   - // }
115   -
116 99 CtMethod ctChieldMethod;
117 100 for (CtMethod ctSuperMethod : ctSuperClass.getDeclaredMethods()) {
118 101 ctChieldMethod = CtNewMethod.delegator(ctSuperMethod, ctChieldClass);
119   - ctChieldMethod.insertBefore("loadProxyConfigurarion(this);");
120   - // ctChieldMethod
121   - // .insertBefore("new br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader().load(this);");
  102 + ctChieldMethod.insertBefore("load(this);");
122 103  
123 104 ctChieldClass.addMethod(ctChieldMethod);
124 105 }
... ...
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/ProxyBean.java
... ... @@ -1,171 +0,0 @@
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 ProxyBean implements Bean<Object> {
65   -
66   - private Class<Object> beanClass;
67   -
68   - private InjectionTarget<Object> injectionTarget;
69   -
70   - public ProxyBean(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/implementation/ConfigurationImpl.java
1 1 package br.gov.frameworkdemoiselle.internal.implementation;
2 2  
3   -import java.util.ArrayList;
4   -import java.util.Collections;
5   -import java.util.HashMap;
6   -import java.util.List;
7   -import java.util.Map;
8   -
9 3 import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;
10 4 import br.gov.frameworkdemoiselle.util.Beans;
11 5  
12 6 public class ConfigurationImpl {
13 7  
14   - private static Map<ClassLoader, List<Class<?>>> cache;
15   -
16 8 private boolean loaded = false;
17 9  
18   - private synchronized static Map<ClassLoader, List<Class<?>>> getCache() {
19   - if (cache == null) {
20   - cache = Collections.synchronizedMap(new HashMap<ClassLoader, List<Class<?>>>());
21   - }
22   -
23   - return cache;
24   - }
25   -
26 10 @SuppressWarnings("unused")
27   - private synchronized void loadProxyConfigurarion(Object instance) {
  11 + private synchronized void load(Object instance) {
28 12 if (!loaded) {
29 13 Beans.getReference(ConfigurationLoader.class).load(instance);
30 14 loaded = true;
31 15 }
32 16 }
33   -
34   - @SuppressWarnings("unused")
35   - private synchronized static void loadProxyConfigurarionX(Object instance) {
36   - Class<?> clazz = instance.getClass().getSuperclass();
37   - ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
38   -
39   - boolean loaded = true;
40   -
41   - if (getCache().containsKey(classLoader)) {
42   - if (!getCache().get(classLoader).contains(clazz)) {
43   - loaded = false;
44   - }
45   -
46   - } else {
47   - List<Class<?>> classes = Collections.synchronizedList(new ArrayList<Class<?>>());
48   - getCache().put(classLoader, classes);
49   - loaded = false;
50   - }
51   -
52   - if (!loaded) {
53   -// new br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader().load(instance);
54   - Beans.getReference(ConfigurationLoader.class).load(instance);
55   - getCache().get(classLoader).add(clazz);
56   - }
57   - }
58 17 }
... ...