From 38a50bbb67bd3e5762b4bd8fa05eb2c406765740 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Wed, 19 Sep 2012 09:38:18 -0300 Subject: [PATCH] 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 | 25 +++---------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java | 171 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java | 43 +------------------------------------------ 4 files changed, 175 insertions(+), 235 deletions(-) create mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java index 6b41095..a99003f 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java @@ -74,7 +74,7 @@ public class ConfigurationBootstrap implements Extension { for (Class config : cache) { proxy = createProxy(config); - event.addBean(new ProxyBean(proxy, beanManager)); + event.addBean(new CustomBean(proxy, beanManager)); } } @@ -85,40 +85,21 @@ public class ConfigurationBootstrap implements Extension { ClassPool pool = ClassPool.getDefault(); ClassPool.doPruning = true; - + CtClass ctChieldClass = pool.getOrNull(chieldClassName); ClassLoader classLoader = type.getClassLoader(); if (ctChieldClass == null) { - pool.appendClassPath(new LoaderClassPath(classLoader)); - // classLoader = Thread.currentThread().getContextClassLoader(); - // pool.appendClassPath(new LoaderClassPath(classLoader)); - // classLoader = ConfigurationLoader.getClassLoaderForClass(superClassName); - // pool.appendClassPath(new LoaderClassPath(classLoader)); - CtClass ctSuperClass = pool.get(superClassName); - // ctChieldClass = pool.makeClass(chieldClassName, ctSuperClass); ctChieldClass = pool.getAndRename(ConfigurationImpl.class.getCanonicalName(), chieldClassName); ctChieldClass.setSuperclass(ctSuperClass); - // for (CtField ctFieldImpl : ctClassImpl.getDeclaredFields()) { - // ctChieldClass.addField(new CtField(ctFieldImpl, ctChieldClass)); - // System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFF-----------" + ctFieldImpl.toString()); - // } - - // for (CtMethod ctMethodImpl : ctClassImpl.getDeclaredMethods()) { - // ctChieldClass.addMethod(new CtMethod(ctMethodImpl, ctChieldClass, null)); - // System.out.println("MMMMMMMMMMMMMMMMMMMMMMMMMM-----------" + ctMethodImpl.toString()); - // } - CtMethod ctChieldMethod; for (CtMethod ctSuperMethod : ctSuperClass.getDeclaredMethods()) { ctChieldMethod = CtNewMethod.delegator(ctSuperMethod, ctChieldClass); - ctChieldMethod.insertBefore("loadProxyConfigurarion(this);"); - // ctChieldMethod - // .insertBefore("new br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader().load(this);"); + ctChieldMethod.insertBefore("load(this);"); ctChieldClass.addMethod(ctChieldMethod); } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java new file mode 100644 index 0000000..daf7e46 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java @@ -0,0 +1,171 @@ +/* + * 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.Type; +import java.util.HashSet; +import java.util.Set; + +import javax.enterprise.context.Dependent; +import javax.enterprise.context.NormalScope; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.Alternative; +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Default; +import javax.enterprise.inject.Stereotype; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.InjectionTarget; +import javax.enterprise.util.AnnotationLiteral; +import javax.inject.Named; +import javax.inject.Qualifier; +import javax.inject.Scope; + +/** + * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 + */ +public class CustomBean implements Bean { + + private Class beanClass; + + private InjectionTarget injectionTarget; + + public CustomBean(Class beanClass, BeanManager beanManager) { + AnnotatedType annotatedType = beanManager.createAnnotatedType(beanClass); + + this.injectionTarget = beanManager.createInjectionTarget(annotatedType); + this.beanClass = beanClass; + } + + public Object create(CreationalContext creationalContext) { + Object instance = injectionTarget.produce(creationalContext); + injectionTarget.inject(instance, creationalContext); + injectionTarget.postConstruct(instance); + + return instance; + } + + public void destroy(Object instance, CreationalContext creationalContext) { + injectionTarget.preDestroy(instance); + injectionTarget.dispose(instance); + creationalContext.release(); + } + + public Set getTypes() { + Set types = new HashSet(); + types.add(beanClass.getSuperclass()); + types.add(Object.class); + + return types; + } + + @SuppressWarnings("serial") + public Set getQualifiers() { + Set result = new HashSet(); + + result.add(new AnnotationLiteral() { + }); + result.add(new AnnotationLiteral() { + }); + + for (Annotation annotation : beanClass.getAnnotations()) { + if (annotation.getClass().isAnnotationPresent(Qualifier.class)) { + result.add(annotation); + } + } + + return result; + } + + public Class getScope() { + Class result = Dependent.class; + + Class annotationClass; + for (Annotation annotation : beanClass.getAnnotations()) { + annotationClass = annotation.getClass(); + + if (annotationClass.isAnnotationPresent(Scope.class) + || annotationClass.isAnnotationPresent(NormalScope.class)) { + result = annotationClass; + break; + } + } + + return result; + } + + public String getName() { + String result = null; + + if (beanClass.isAnnotationPresent(Named.class)) { + result = beanClass.getAnnotation(Named.class).value(); + } + + return result; + } + + public Set> getStereotypes() { + Set> result = new HashSet>(); + + for (Annotation annotation : beanClass.getAnnotations()) { + if (annotation.getClass().isAnnotationPresent(Stereotype.class)) { + result.add(annotation.getClass()); + } + } + + return result; + } + + public Class getBeanClass() { + return beanClass; + } + + public boolean isAlternative() { + return beanClass.isAnnotationPresent(Alternative.class); + } + + public boolean isNullable() { + return false; + } + + public Set getInjectionPoints() { + return injectionTarget.getInjectionPoints(); + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java deleted file mode 100644 index b89d5f3..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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.Type; -import java.util.HashSet; -import java.util.Set; - -import javax.enterprise.context.Dependent; -import javax.enterprise.context.NormalScope; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.Alternative; -import javax.enterprise.inject.Any; -import javax.enterprise.inject.Default; -import javax.enterprise.inject.Stereotype; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.InjectionPoint; -import javax.enterprise.inject.spi.InjectionTarget; -import javax.enterprise.util.AnnotationLiteral; -import javax.inject.Named; -import javax.inject.Qualifier; -import javax.inject.Scope; - -/** - * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 - */ -public class ProxyBean implements Bean { - - private Class beanClass; - - private InjectionTarget injectionTarget; - - public ProxyBean(Class beanClass, BeanManager beanManager) { - AnnotatedType annotatedType = beanManager.createAnnotatedType(beanClass); - - this.injectionTarget = beanManager.createInjectionTarget(annotatedType); - this.beanClass = beanClass; - } - - public Object create(CreationalContext creationalContext) { - Object instance = injectionTarget.produce(creationalContext); - injectionTarget.inject(instance, creationalContext); - injectionTarget.postConstruct(instance); - - return instance; - } - - public void destroy(Object instance, CreationalContext creationalContext) { - injectionTarget.preDestroy(instance); - injectionTarget.dispose(instance); - creationalContext.release(); - } - - public Set getTypes() { - Set types = new HashSet(); - types.add(beanClass.getSuperclass()); - types.add(Object.class); - - return types; - } - - @SuppressWarnings("serial") - public Set getQualifiers() { - Set result = new HashSet(); - - result.add(new AnnotationLiteral() { - }); - result.add(new AnnotationLiteral() { - }); - - for (Annotation annotation : beanClass.getAnnotations()) { - if (annotation.getClass().isAnnotationPresent(Qualifier.class)) { - result.add(annotation); - } - } - - return result; - } - - public Class getScope() { - Class result = Dependent.class; - - Class annotationClass; - for (Annotation annotation : beanClass.getAnnotations()) { - annotationClass = annotation.getClass(); - - if (annotationClass.isAnnotationPresent(Scope.class) - || annotationClass.isAnnotationPresent(NormalScope.class)) { - result = annotationClass; - break; - } - } - - return result; - } - - public String getName() { - String result = null; - - if (beanClass.isAnnotationPresent(Named.class)) { - result = beanClass.getAnnotation(Named.class).value(); - } - - return result; - } - - public Set> getStereotypes() { - Set> result = new HashSet>(); - - for (Annotation annotation : beanClass.getAnnotations()) { - if (annotation.getClass().isAnnotationPresent(Stereotype.class)) { - result.add(annotation.getClass()); - } - } - - return result; - } - - public Class getBeanClass() { - return beanClass; - } - - public boolean isAlternative() { - return beanClass.isAnnotationPresent(Alternative.class); - } - - public boolean isNullable() { - return false; - } - - public Set getInjectionPoints() { - return injectionTarget.getInjectionPoints(); - } -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java index 891aab1..9486c56 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationImpl.java @@ -1,58 +1,17 @@ package br.gov.frameworkdemoiselle.internal.implementation; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; import br.gov.frameworkdemoiselle.util.Beans; public class ConfigurationImpl { - private static Map>> cache; - private boolean loaded = false; - private synchronized static Map>> getCache() { - if (cache == null) { - cache = Collections.synchronizedMap(new HashMap>>()); - } - - return cache; - } - @SuppressWarnings("unused") - private synchronized void loadProxyConfigurarion(Object instance) { + private synchronized void load(Object instance) { if (!loaded) { Beans.getReference(ConfigurationLoader.class).load(instance); loaded = true; } } - - @SuppressWarnings("unused") - private synchronized static void loadProxyConfigurarionX(Object instance) { - Class clazz = instance.getClass().getSuperclass(); - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - - boolean loaded = true; - - if (getCache().containsKey(classLoader)) { - if (!getCache().get(classLoader).contains(clazz)) { - loaded = false; - } - - } else { - List> classes = Collections.synchronizedList(new ArrayList>()); - getCache().put(classLoader, classes); - loaded = false; - } - - if (!loaded) { -// new br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader().load(instance); - Beans.getReference(ConfigurationLoader.class).load(instance); - getCache().get(classLoader).add(clazz); - } - } } -- libgit2 0.21.2