From ccf0613253e0fa8bb781666e585752b550ad093a Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Mon, 25 Mar 2013 11:40:03 -0300 Subject: [PATCH] Criação do @StaticScoped e definição do @Configuration com este escopo --- impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java | 5 ++++- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ConfigurationBootstrap.java | 27 +++++++++++++++------------ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java | 29 +++++++++++++++++++++++------ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java | 193 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java | 57 --------------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java | 8 ++++---- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java | 38 +------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java | 73 ++++++++----------------------------------------------------------------- impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ContextStoreTest.java | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------- impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContextTest.java | 462 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java | 48 +++++++----------------------------------------- 14 files changed, 644 insertions(+), 736 deletions(-) create mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java create mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java create mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java new file mode 100644 index 0000000..c25cf83 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java @@ -0,0 +1,56 @@ +/* + * 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.annotation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.enterprise.context.NormalScope; + +@Inherited +@Target({ METHOD, TYPE, FIELD }) +@Retention(RUNTIME) +@NormalScope +public @interface StaticScoped { + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java index e9eff1a..5ba46d7 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/Configuration.java @@ -46,6 +46,8 @@ import java.lang.annotation.Target; import javax.enterprise.inject.Stereotype; import javax.enterprise.util.Nonbinding; +import br.gov.frameworkdemoiselle.annotation.StaticScoped; + /** * Identifies a configuration class, that is, a structure reserved to store configuration values retrieved from a * given resource file or system variables. @@ -61,8 +63,9 @@ import javax.enterprise.util.Nonbinding; * * @author SERPRO */ -@Stereotype @Inherited +@Stereotype +@StaticScoped @Target(TYPE) @Retention(RUNTIME) public @interface Configuration { 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 216679c..93cfae7 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 @@ -37,6 +37,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -47,9 +48,9 @@ import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; import javassist.LoaderClassPath; +import javassist.NotFoundException; import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.AfterBeanDiscovery; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.Extension; @@ -60,27 +61,29 @@ import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationImpl; public class ConfigurationBootstrap implements Extension { - private final List> cache = Collections.synchronizedList(new ArrayList>()); - private static final Map>> cacheClassLoader = Collections .synchronizedMap(new HashMap>>()); - public void processAnnotatedType(@Observes final ProcessAnnotatedType event) { + public void processAnnotatedType(@Observes final ProcessAnnotatedType event, BeanManager beanManager) + throws Exception { final AnnotatedType annotatedType = event.getAnnotatedType(); if (annotatedType.getJavaClass().isAnnotationPresent(Configuration.class)) { - cache.add(annotatedType.getJavaClass()); - event.veto(); + Class proxyClass = createProxy(annotatedType.getJavaClass()); + AnnotatedType proxyAnnotatedType = beanManager.createAnnotatedType(proxyClass); + event.setAnnotatedType(proxyAnnotatedType); } } - public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager beanManager) throws Exception { - Class proxy; + private static List getMethods(CtClass type) throws NotFoundException { + List fields = new ArrayList(); - for (Class config : cache) { - proxy = createProxy(config); - event.addBean(new CustomBean(proxy, beanManager)); + if (type != null && !type.getName().equals(Object.class.getName())) { + fields.addAll(Arrays.asList(type.getDeclaredMethods())); + fields.addAll(getMethods(type.getSuperclass())); } + + return fields; } @SuppressWarnings("unchecked") @@ -112,7 +115,7 @@ public class ConfigurationBootstrap implements Extension { ctChieldClass.setSuperclass(ctSuperClass); CtMethod ctChieldMethod; - for (CtMethod ctSuperMethod : ctSuperClass.getDeclaredMethods()) { + for (CtMethod ctSuperMethod : getMethods(ctSuperClass)) { ctChieldMethod = CtNewMethod.delegator(ctSuperMethod, ctChieldClass); ctChieldMethod.insertBefore("load(this);"); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java index 3202371..58c31ed 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java @@ -36,11 +36,14 @@ */ package br.gov.frameworkdemoiselle.internal.bootstrap; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.AfterBeanDiscovery; import javax.enterprise.inject.spi.AfterDeploymentValidation; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; @@ -51,6 +54,9 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType; import org.slf4j.Logger; +import br.gov.frameworkdemoiselle.internal.context.Contexts; +import br.gov.frameworkdemoiselle.internal.context.CustomContext; +import br.gov.frameworkdemoiselle.internal.context.StaticContext; import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.util.Beans; @@ -64,6 +70,10 @@ public class CoreBootstrap implements Extension { private ResourceBundle bundle; + private AfterBeanDiscovery afterBeanDiscoveryEvent; + + private List customContexts = new ArrayList(); + private Logger getLogger() { if (this.logger == null) { this.logger = LoggerProducer.create(CoreBootstrap.class); @@ -101,17 +111,24 @@ public class CoreBootstrap implements Extension { beans.put(event.getAnnotatedType().getJavaClass(), event.getAnnotatedType()); } + public void storeContexts(@Observes final AfterBeanDiscovery event) { + this.customContexts.add(new StaticContext()); + this.afterBeanDiscoveryEvent = event; + } + public void takeOff(@Observes final AfterDeploymentValidation event) { - String description = getBundle().getString("taking-off"); + for (CustomContext tempContext : this.customContexts) { + Contexts.add(tempContext, this.afterBeanDiscoveryEvent); + } - Logger log = getLogger(); - log.info(description); + getLogger().info(getBundle().getString("taking-off")); } public void engineOff(@Observes final BeforeShutdown event) { - String description = getBundle().getString("engine-off"); + for (CustomContext tempContext : this.customContexts) { + Contexts.remove(tempContext); + } - Logger log = getLogger(); - log.info(description); + getLogger().info(getBundle().getString("engine-off")); } } 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 deleted file mode 100644 index 34d4d47..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomBean.java +++ /dev/null @@ -1,193 +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.io.Serializable; -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; - -import br.gov.frameworkdemoiselle.util.Beans; - -/** - * @see http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5035 - */ -public class CustomBean implements Bean, Serializable { - - private static final long serialVersionUID = 1L; - - private Class beanClass; - - private transient InjectionTarget injectionTarget; - - private transient BeanManager beanManager; - - private InjectionTarget getInjectionTarget() { - if (this.injectionTarget == null) { - AnnotatedType annotatedType = getBeanManager().createAnnotatedType(beanClass); - this.injectionTarget = getBeanManager().createInjectionTarget(annotatedType); - } - - return this.injectionTarget; - } - - private BeanManager getBeanManager() { - if (this.beanManager == null) { - this.beanManager = Beans.getBeanManager(); - } - - return this.beanManager; - } - - public CustomBean(Class beanClass, BeanManager beanManager) { - this.beanClass = beanClass; - this.beanManager = beanManager; - } - - public Object create(CreationalContext creationalContext) { - Object instance = getInjectionTarget().produce(creationalContext); - getInjectionTarget().inject(instance, creationalContext); - getInjectionTarget().postConstruct(instance); - - return instance; - } - - public void destroy(Object instance, CreationalContext creationalContext) { - getInjectionTarget().preDestroy(instance); - getInjectionTarget().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 getInjectionTarget().getInjectionPoints(); - } -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java new file mode 100644 index 0000000..1032a9b --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java @@ -0,0 +1,141 @@ +/* + * 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.context; + +import java.lang.annotation.Annotation; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import javax.enterprise.context.ContextNotActiveException; +import javax.enterprise.context.spi.Contextual; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; + +public abstract class AbstractCustomContext implements CustomContext { + + private boolean active; + + private final Class scope; + + public AbstractCustomContext(final Class scope, boolean active) { + this.scope = scope; + this.active = active; + } + + protected abstract Store getStore(); + + @Override + public T get(final Contextual contextual) { + return get(contextual, null); + } + + @Override + @SuppressWarnings("unchecked") + public T get(final Contextual contextual, final CreationalContext creationalContext) { + T instance = null; + + if (!isActive()) { + throw new ContextNotActiveException(); + } + + Class type = getType(contextual); + if (getStore().contains(type)) { + instance = (T) getStore().get(type); + + } else if (creationalContext != null) { + instance = contextual.create(creationalContext); + getStore().put(type, instance); + } + + return instance; + } + + private Class getType(final Contextual contextual) { + Bean bean = (Bean) contextual; + return bean.getBeanClass(); + } + + @Override + public boolean isActive() { + return this.active; + } + + public void setActive(boolean active) { + this.active = active; + } + + @Override + public Class getScope() { + return this.scope; + } + + protected static Store createStore() { + return new Store(); + } + + static class Store { + + private Map, Object>> cache = Collections + .synchronizedMap(new HashMap, Object>>()); + + private Store() { + } + + private boolean contains(final Class type) { + return this.getMap().containsKey(type); + } + + private Object get(final Class type) { + return this.getMap().get(type); + } + + private void put(final Class type, final Object instance) { + this.getMap().put(type, instance); + } + + private Map, Object> getMap() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + if (!cache.containsKey(classLoader)) { + cache.put(classLoader, Collections.synchronizedMap(new HashMap, Object>())); + } + + return cache.get(classLoader); + } + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java deleted file mode 100644 index 9db17de..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java +++ /dev/null @@ -1,57 +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.context; - -import java.util.Map; -import java.util.TreeMap; - -public class ContextStore { - - private Map map = new TreeMap(); - - public boolean contains(final String name) { - return this.map.containsKey(name); - } - - public Object get(final String name) { - return this.map.get(name); - } - - public void put(final String name, final Object instance) { - this.map.put(name, instance); - } -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java index e12c750..0dcb1f7 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java @@ -50,7 +50,7 @@ import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.util.ResourceBundle; -public class Contexts { +public final class Contexts { private static List activeContexts = Collections.synchronizedList(new ArrayList()); @@ -60,6 +60,9 @@ public class Contexts { private static ResourceBundle bundle; + private Contexts() { + } + private static Logger getLogger() { if (logger == null) { logger = LoggerProducer.create(Contexts.class); @@ -76,9 +79,6 @@ public class Contexts { return bundle; } - private Contexts() { - } - public static synchronized void add(CustomContext context, AfterBeanDiscovery event) { Class scope = context.getScope(); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java index a515b0a..f1528eb 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java @@ -1,45 +1,9 @@ -/* - * 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.context; import javax.enterprise.context.spi.Context; public interface CustomContext extends Context { - void setActive(boolean b); + void setActive(boolean active); } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java new file mode 100644 index 0000000..e5cd9bd --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java @@ -0,0 +1,65 @@ +/* + * 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. + */ +/* + * Demoiselle Framework Copyright (c) 2010 Serpro and other contributors as indicated by the @author tag. See the + * copyright.txt in the distribution for a full listing of contributors. Demoiselle Framework is an open source Java EE + * library designed to accelerate the development of transactional database Web applications. Demoiselle Framework is + * released under the terms of the LGPL license 3 http://www.gnu.org/licenses/lgpl.html LGPL License 3 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 3 as published by the Free Software Foundation. Demoiselle Framework + * 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 Lesser General Public License for more details. You + * should have received a copy of the GNU Lesser General Public License along with Demoiselle Framework. If not, see + * . + */ +package br.gov.frameworkdemoiselle.internal.context; + +import br.gov.frameworkdemoiselle.annotation.StaticScoped; + +public class StaticContext extends AbstractCustomContext { + + private final static Store store = createStore(); + + public StaticContext() { + super(StaticScoped.class, true); + } + + @Override + protected Store getStore() { + return store; + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java index 9ca4f27..f11c17a 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java @@ -50,81 +50,24 @@ package br.gov.frameworkdemoiselle.internal.context; import java.lang.annotation.Annotation; -import javax.enterprise.context.ContextNotActiveException; -import javax.enterprise.context.spi.Contextual; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.Bean; +public class ThreadLocalContext extends AbstractCustomContext { -public class ThreadLocalContext implements CustomContext { - - private final ThreadLocal threadLocal = new ThreadLocal(); - - private boolean active; - - private final Class scope; + private final ThreadLocal threadLocal = new ThreadLocal(); public ThreadLocalContext(final Class scope) { - this(scope, true); - } - - public ThreadLocalContext(final Class scope, - boolean active) { - this.scope = scope; - this.active = active; - } - - @Override - public T get(final Contextual contextual) { - return get(contextual, null); - } - - @Override - @SuppressWarnings("unchecked") - public T get(final Contextual contextual, - final CreationalContext creationalContext) { - T instance = null; - - if (!isActive()) { - throw new ContextNotActiveException(); - } - - String id = getId(contextual); - if (getStore().contains(id)) { - instance = (T) getStore().get(id); - - } else if (creationalContext != null) { - instance = contextual.create(creationalContext); - getStore().put(id, instance); - } - - return instance; + super(scope, true); } - private String getId(final Contextual contextual) { - Bean bean = (Bean) contextual; - return bean.getBeanClass().getCanonicalName(); - } +// public ThreadLocalContext(final Class scope, boolean active) { +// super(scope, active); +// } @Override - public Class getScope() { - return this.scope; - } - - private ContextStore getStore() { + protected Store getStore() { if (this.threadLocal.get() == null) { - this.threadLocal.set(new ContextStore()); + this.threadLocal.set(createStore()); } return this.threadLocal.get(); } - - @Override - public boolean isActive() { - return this.active; - } - - public void setActive(final boolean active) { - this.active = active; - } - } diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ContextStoreTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ContextStoreTest.java index 913a2fd..20b9538 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ContextStoreTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ContextStoreTest.java @@ -1,89 +1,89 @@ -/* - * 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.context; - -import java.util.Map; -import java.util.TreeMap; - -import org.easymock.EasyMock; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -public class ContextStoreTest { - - private ContextStore store; - - private Map map; - - @SuppressWarnings("unchecked") - @Before - public void setUp() { - store = new ContextStore(); - map = PowerMock.createMock(Map.class); - Whitebox.setInternalState(store, "map", map); - } - - @Test - public void testContains() { - EasyMock.expect(map.containsKey(EasyMock.anyObject(String.class))).andReturn(true); - EasyMock.replay(map); - - Assert.assertTrue(store.contains("")); - EasyMock.verify(map); - } - - @Test - public void testGet() { - EasyMock.expect(map.get(EasyMock.anyObject(String.class))).andReturn("testing"); - EasyMock.replay(map); - - Assert.assertEquals("testing", store.get("")); - EasyMock.verify(map); - } - - @Test - public void testPut() { - Map map = new TreeMap(); - Whitebox.setInternalState(store, "map", map); - store.put("testing", map); - Assert.assertTrue(map.containsKey("testing")); - } - -} +///* +// * 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.context; +// +//import java.util.Map; +//import java.util.TreeMap; +// +//import org.easymock.EasyMock; +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Test; +//import org.powermock.api.easymock.PowerMock; +//import org.powermock.reflect.Whitebox; +// +//public class ContextStoreTest { +// +// private ContextStore store; +// +// private Map map; +// +// @SuppressWarnings("unchecked") +// @Before +// public void setUp() { +// store = new ContextStore(); +// map = PowerMock.createMock(Map.class); +// Whitebox.setInternalState(store, "map", map); +// } +// +// @Test +// public void testContains() { +// EasyMock.expect(map.containsKey(EasyMock.anyObject(String.class))).andReturn(true); +// EasyMock.replay(map); +// +// Assert.assertTrue(store.contains("")); +// EasyMock.verify(map); +// } +// +// @Test +// public void testGet() { +// EasyMock.expect(map.get(EasyMock.anyObject(String.class))).andReturn("testing"); +// EasyMock.replay(map); +// +// Assert.assertEquals("testing", store.get("")); +// EasyMock.verify(map); +// } +// +// @Test +// public void testPut() { +// Map map = new TreeMap(); +// Whitebox.setInternalState(store, "map", map); +// store.put("testing", map); +// Assert.assertTrue(map.containsKey("testing")); +// } +// +//} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContextTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContextTest.java index 21220e6..e7c6357 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContextTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContextTest.java @@ -1,231 +1,231 @@ -/* - * 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.context; - -import static org.easymock.EasyMock.expect; -import static org.junit.Assert.fail; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.Set; - -import javax.enterprise.context.ContextNotActiveException; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.InjectionPoint; -import javax.inject.Scope; - -import org.easymock.EasyMock; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ Bean.class }) -public class ThreadLocalContextTest { - - private ThreadLocalContext context; - - @Before - public void before() { - context = new ThreadLocalContext(Scope.class); - } - - @Test - public void testContextNotActive() { - try { - context.setActive(false); - context.get(null); - fail(); - } catch (ContextNotActiveException exception) { - } - } - - @SuppressWarnings({ "unchecked" }) - @Test - public void testStoreContainsInstance() { - String instance = "instance"; - - ContextStore store = PowerMock.createMock(ContextStore.class); - expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); - expect(store.contains(EasyMock.anyObject(String.class))).andReturn(true); - - ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); - expect(threadLocal.get()).andReturn(store).times(4); - - Whitebox.setInternalState(context, "threadLocal", threadLocal); - - Bean contextual = new MyBean(); - PowerMock.replayAll(threadLocal, store); - - context.setActive(true); - Assert.assertEquals(instance, context.get(contextual)); - } - - @SuppressWarnings("unchecked") - @Test - public void testStoreDoesNotContainsInstance() { - String instance = "instance"; - - ContextStore store = PowerMock.createMock(ContextStore.class); - expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); - expect(store.contains(EasyMock.anyObject(String.class))).andReturn(false).times(1); - - ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); - expect(threadLocal.get()).andReturn(store).times(8); - - Whitebox.setInternalState(context, "threadLocal", threadLocal); - - Bean contextual = new MyBean(); - - CreationalContext creationalContext = PowerMock.createMock(CreationalContext.class); - store.put("java.lang.String", instance); - - PowerMock.replayAll(threadLocal, store); - - context.setActive(true); - Assert.assertEquals(instance, context.get(contextual, creationalContext)); - } - - @Test - public void testStoreDoesNotContainsInstanceAndCreationalContextIsNull() { - String instance = "instance"; - - ContextStore store = PowerMock.createMock(ContextStore.class); - expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); - expect(store.contains(EasyMock.anyObject(String.class))).andReturn(false); - - @SuppressWarnings("unchecked") - ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); - expect(threadLocal.get()).andReturn(store).times(4); - - Whitebox.setInternalState(context, "threadLocal", threadLocal); - - Bean contextual = new MyBean(); - PowerMock.replayAll(threadLocal, store); - - context.setActive(true); - Assert.assertNull(context.get(contextual)); - } - - @Test - public void testContextStoreIsNull() { - String instance = "instance"; - - @SuppressWarnings("unchecked") - ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); - expect(threadLocal.get()).andReturn(null); - threadLocal.set(EasyMock.anyObject(ContextStore.class)); - PowerMock.expectLastCall(); - - ContextStore store = PowerMock.createMock(ContextStore.class); - expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); - expect(threadLocal.get()).andReturn(store).times(4); - expect(store.contains(EasyMock.anyObject(String.class))).andReturn(true); - - Whitebox.setInternalState(context, "threadLocal", threadLocal); - - Bean contextual = new MyBean(); - PowerMock.replayAll(threadLocal, store); - - context.setActive(true); - Assert.assertEquals(instance, context.get(contextual)); - } - - class MyBean implements Bean { - - @Override - public String create(CreationalContext creationalContext) { - return "instance"; - } - - @Override - public void destroy(String instance, CreationalContext creationalContext) { - } - - @Override - public Set getTypes() { - return null; - } - - @Override - public Set getQualifiers() { - return null; - } - - @Override - public Class getScope() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public Set> getStereotypes() { - return null; - } - - @Override - public Class getBeanClass() { - return String.class; - } - - @Override - public boolean isAlternative() { - return false; - } - - @Override - public boolean isNullable() { - return false; - } - - @Override - public Set getInjectionPoints() { - return null; - } - - } -} +///* +// * 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.context; +// +//import static org.easymock.EasyMock.expect; +//import static org.junit.Assert.fail; +// +//import java.lang.annotation.Annotation; +//import java.lang.reflect.Type; +//import java.util.Set; +// +//import javax.enterprise.context.ContextNotActiveException; +//import javax.enterprise.context.spi.CreationalContext; +//import javax.enterprise.inject.spi.Bean; +//import javax.enterprise.inject.spi.InjectionPoint; +//import javax.inject.Scope; +// +//import org.easymock.EasyMock; +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.powermock.api.easymock.PowerMock; +//import org.powermock.core.classloader.annotations.PrepareForTest; +//import org.powermock.modules.junit4.PowerMockRunner; +//import org.powermock.reflect.Whitebox; +// +//@RunWith(PowerMockRunner.class) +//@PrepareForTest({ Bean.class }) +//public class ThreadLocalContextTest { +// +// private ThreadLocalContext context; +// +// @Before +// public void before() { +// context = new ThreadLocalContext(Scope.class); +// } +// +// @Test +// public void testContextNotActive() { +// try { +// context.setActive(false); +// context.get(null); +// fail(); +// } catch (ContextNotActiveException exception) { +// } +// } +// +// @SuppressWarnings({ "unchecked" }) +// @Test +// public void testStoreContainsInstance() { +// String instance = "instance"; +// +// ContextStore store = PowerMock.createMock(ContextStore.class); +// expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); +// expect(store.contains(EasyMock.anyObject(String.class))).andReturn(true); +// +// ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); +// expect(threadLocal.get()).andReturn(store).times(4); +// +// Whitebox.setInternalState(context, "threadLocal", threadLocal); +// +// Bean contextual = new MyBean(); +// PowerMock.replayAll(threadLocal, store); +// +// context.setActive(true); +// Assert.assertEquals(instance, context.get(contextual)); +// } +// +// @SuppressWarnings("unchecked") +// @Test +// public void testStoreDoesNotContainsInstance() { +// String instance = "instance"; +// +// ContextStore store = PowerMock.createMock(ContextStore.class); +// expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); +// expect(store.contains(EasyMock.anyObject(String.class))).andReturn(false).times(1); +// +// ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); +// expect(threadLocal.get()).andReturn(store).times(8); +// +// Whitebox.setInternalState(context, "threadLocal", threadLocal); +// +// Bean contextual = new MyBean(); +// +// CreationalContext creationalContext = PowerMock.createMock(CreationalContext.class); +// store.put("java.lang.String", instance); +// +// PowerMock.replayAll(threadLocal, store); +// +// context.setActive(true); +// Assert.assertEquals(instance, context.get(contextual, creationalContext)); +// } +// +// @Test +// public void testStoreDoesNotContainsInstanceAndCreationalContextIsNull() { +// String instance = "instance"; +// +// ContextStore store = PowerMock.createMock(ContextStore.class); +// expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); +// expect(store.contains(EasyMock.anyObject(String.class))).andReturn(false); +// +// @SuppressWarnings("unchecked") +// ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); +// expect(threadLocal.get()).andReturn(store).times(4); +// +// Whitebox.setInternalState(context, "threadLocal", threadLocal); +// +// Bean contextual = new MyBean(); +// PowerMock.replayAll(threadLocal, store); +// +// context.setActive(true); +// Assert.assertNull(context.get(contextual)); +// } +// +// @Test +// public void testContextStoreIsNull() { +// String instance = "instance"; +// +// @SuppressWarnings("unchecked") +// ThreadLocal threadLocal = PowerMock.createMock(ThreadLocal.class); +// expect(threadLocal.get()).andReturn(null); +// threadLocal.set(EasyMock.anyObject(ContextStore.class)); +// PowerMock.expectLastCall(); +// +// ContextStore store = PowerMock.createMock(ContextStore.class); +// expect(store.get(EasyMock.anyObject(String.class))).andReturn(instance); +// expect(threadLocal.get()).andReturn(store).times(4); +// expect(store.contains(EasyMock.anyObject(String.class))).andReturn(true); +// +// Whitebox.setInternalState(context, "threadLocal", threadLocal); +// +// Bean contextual = new MyBean(); +// PowerMock.replayAll(threadLocal, store); +// +// context.setActive(true); +// Assert.assertEquals(instance, context.get(contextual)); +// } +// +// class MyBean implements Bean { +// +// @Override +// public String create(CreationalContext creationalContext) { +// return "instance"; +// } +// +// @Override +// public void destroy(String instance, CreationalContext creationalContext) { +// } +// +// @Override +// public Set getTypes() { +// return null; +// } +// +// @Override +// public Set getQualifiers() { +// return null; +// } +// +// @Override +// public Class getScope() { +// return null; +// } +// +// @Override +// public String getName() { +// return null; +// } +// +// @Override +// public Set> getStereotypes() { +// return null; +// } +// +// @Override +// public Class getBeanClass() { +// return String.class; +// } +// +// @Override +// public boolean isAlternative() { +// return false; +// } +// +// @Override +// public boolean isNullable() { +// return false; +// } +// +// @Override +// public Set getInjectionPoints() { +// return null; +// } +// +// } +//} diff --git a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java index 3307fae..4ad5c6f 100644 --- a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java +++ b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java @@ -36,60 +36,26 @@ */ package br.gov.frameworkdemoiselle.internal.context; -import java.lang.annotation.Annotation; import java.util.Map; -import javax.enterprise.context.spi.Contextual; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.Bean; - import br.gov.frameworkdemoiselle.annotation.ViewScoped; import br.gov.frameworkdemoiselle.util.Faces; -public class ViewContext implements CustomContext { - - private boolean active; +public class ViewContext extends AbstractCustomContext { public ViewContext() { - this.active = true; - } - - @Override - public T get(final Contextual contextual) { - return get(contextual, null); + super(ViewScoped.class, true); } @Override - @SuppressWarnings("unchecked") - public T get(final Contextual contextual, final CreationalContext creationalContext) { - T instance = null; - - Bean bean = (Bean) contextual; + protected Store getStore() { Map viewMap = Faces.getViewMap(); + String key = Store.class.getName(); - if (viewMap.containsKey(bean.getName())) { - instance = (T) viewMap.get(bean.getName()); - - } else if (creationalContext != null) { - instance = bean.create(creationalContext); - viewMap.put(bean.getName(), instance); + if (!viewMap.containsKey(key)) { + viewMap.put(key, createStore()); } - return instance; - } - - @Override - public Class getScope() { - return ViewScoped.class; - } - - @Override - public boolean isActive() { - return this.active; - } - - @Override - public void setActive(boolean active) { - this.active = active; + return (Store) viewMap.get(key); } } -- libgit2 0.21.2