Commit 782fd5e310564b778ed457787d318fba08e491e4
1 parent
e5a94074
Exists in
master
Refatoração dos contextos customizados
Showing
12 changed files
with
193 additions
and
317 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java
| ... | ... | @@ -59,7 +59,7 @@ import br.gov.frameworkdemoiselle.DemoiselleException; |
| 59 | 59 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 60 | 60 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; |
| 61 | 61 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 62 | -import br.gov.frameworkdemoiselle.internal.context.CustomContext; | |
| 62 | +import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | |
| 63 | 63 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
| 64 | 64 | import br.gov.frameworkdemoiselle.internal.implementation.AnnotatedMethodProcessor; |
| 65 | 65 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| ... | ... | @@ -74,7 +74,7 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 74 | 74 | private List<AnnotatedMethodProcessor> processors = Collections |
| 75 | 75 | .synchronizedList(new ArrayList<AnnotatedMethodProcessor>()); |
| 76 | 76 | |
| 77 | - private List<CustomContext> tempContexts = new ArrayList<CustomContext>(); | |
| 77 | + private List<AbstractCustomContext> tempContexts = new ArrayList<AbstractCustomContext>(); | |
| 78 | 78 | |
| 79 | 79 | private AfterBeanDiscovery afterBeanDiscoveryEvent; |
| 80 | 80 | |
| ... | ... | @@ -134,7 +134,7 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 134 | 134 | Exception failure = null; |
| 135 | 135 | |
| 136 | 136 | if (!registered) { |
| 137 | - for (CustomContext tempContext : tempContexts) { | |
| 137 | + for (AbstractCustomContext tempContext : tempContexts) { | |
| 138 | 138 | Contexts.add(tempContext, afterBeanDiscoveryEvent); |
| 139 | 139 | } |
| 140 | 140 | |
| ... | ... | @@ -168,7 +168,7 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | private void unloadTempContexts() { |
| 171 | - for (CustomContext tempContext : tempContexts) { | |
| 171 | + for (AbstractCustomContext tempContext : tempContexts) { | |
| 172 | 172 | Contexts.remove(tempContext); |
| 173 | 173 | } |
| 174 | 174 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java
0 → 100644
| ... | ... | @@ -0,0 +1,135 @@ |
| 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.context; | |
| 38 | + | |
| 39 | +import java.lang.annotation.Annotation; | |
| 40 | +import java.util.Collections; | |
| 41 | +import java.util.HashMap; | |
| 42 | +import java.util.Map; | |
| 43 | + | |
| 44 | +import javax.enterprise.context.ContextNotActiveException; | |
| 45 | +import javax.enterprise.context.spi.Context; | |
| 46 | +import javax.enterprise.context.spi.Contextual; | |
| 47 | +import javax.enterprise.context.spi.CreationalContext; | |
| 48 | +import javax.enterprise.inject.spi.Bean; | |
| 49 | + | |
| 50 | +public abstract class AbstractCustomContext implements Context { | |
| 51 | + | |
| 52 | + private boolean active; | |
| 53 | + | |
| 54 | + private final Class<? extends Annotation> scope; | |
| 55 | + | |
| 56 | + public AbstractCustomContext(final Class<? extends Annotation> scope, boolean active) { | |
| 57 | + this.scope = scope; | |
| 58 | + this.active = active; | |
| 59 | + } | |
| 60 | + | |
| 61 | + protected abstract Store getStore(); | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public <T> T get(final Contextual<T> contextual) { | |
| 65 | + return get(contextual, null); | |
| 66 | + } | |
| 67 | + | |
| 68 | + @Override | |
| 69 | + @SuppressWarnings("unchecked") | |
| 70 | + public <T> T get(final Contextual<T> contextual, final CreationalContext<T> creationalContext) { | |
| 71 | + T instance = null; | |
| 72 | + | |
| 73 | + if (!isActive()) { | |
| 74 | + throw new ContextNotActiveException(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + Class<?> type = getType(contextual); | |
| 78 | + if (getStore().contains(type)) { | |
| 79 | + instance = (T) getStore().get(type); | |
| 80 | + | |
| 81 | + } else if (creationalContext != null) { | |
| 82 | + instance = contextual.create(creationalContext); | |
| 83 | + getStore().put(type, instance); | |
| 84 | + } | |
| 85 | + | |
| 86 | + return instance; | |
| 87 | + } | |
| 88 | + | |
| 89 | + private <T> Class<?> getType(final Contextual<T> contextual) { | |
| 90 | + Bean<T> bean = (Bean<T>) contextual; | |
| 91 | + return bean.getBeanClass(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + @Override | |
| 95 | + public boolean isActive() { | |
| 96 | + return this.active; | |
| 97 | + } | |
| 98 | + | |
| 99 | + public void setActive(final boolean active) { | |
| 100 | + this.active = active; | |
| 101 | + } | |
| 102 | + | |
| 103 | + @Override | |
| 104 | + public Class<? extends Annotation> getScope() { | |
| 105 | + return this.scope; | |
| 106 | + } | |
| 107 | + | |
| 108 | + static class Store { | |
| 109 | + | |
| 110 | + private Map<ClassLoader, Map<Class<?>, Object>> cache = Collections | |
| 111 | + .synchronizedMap(new HashMap<ClassLoader, Map<Class<?>, Object>>()); | |
| 112 | + | |
| 113 | + public boolean contains(final Class<?> type) { | |
| 114 | + return this.getMap().containsKey(type); | |
| 115 | + } | |
| 116 | + | |
| 117 | + public Object get(final Class<?> type) { | |
| 118 | + return this.getMap().get(type); | |
| 119 | + } | |
| 120 | + | |
| 121 | + public void put(final Class<?> type, final Object instance) { | |
| 122 | + this.getMap().put(type, instance); | |
| 123 | + } | |
| 124 | + | |
| 125 | + private Map<Class<?>, Object> getMap() { | |
| 126 | + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
| 127 | + | |
| 128 | + if (!cache.containsKey(classLoader)) { | |
| 129 | + cache.put(classLoader, Collections.synchronizedMap(new HashMap<Class<?>, Object>())); | |
| 130 | + } | |
| 131 | + | |
| 132 | + return cache.get(classLoader); | |
| 133 | + } | |
| 134 | + } | |
| 135 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java
| ... | ... | @@ -1,57 +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.context; | |
| 38 | - | |
| 39 | -import java.util.Map; | |
| 40 | -import java.util.TreeMap; | |
| 41 | - | |
| 42 | -public class ContextStore { | |
| 43 | - | |
| 44 | - private Map<String, Object> map = new TreeMap<String, Object>(); | |
| 45 | - | |
| 46 | - public boolean contains(final String name) { | |
| 47 | - return this.map.containsKey(name); | |
| 48 | - } | |
| 49 | - | |
| 50 | - public Object get(final String name) { | |
| 51 | - return this.map.get(name); | |
| 52 | - } | |
| 53 | - | |
| 54 | - public void put(final String name, final Object instance) { | |
| 55 | - this.map.put(name, instance); | |
| 56 | - } | |
| 57 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java
| ... | ... | @@ -52,9 +52,9 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; |
| 52 | 52 | |
| 53 | 53 | public final class Contexts { |
| 54 | 54 | |
| 55 | - private static List<CustomContext> activeContexts = Collections.synchronizedList(new ArrayList<CustomContext>()); | |
| 55 | + private static List<AbstractCustomContext> activeContexts = Collections.synchronizedList(new ArrayList<AbstractCustomContext>()); | |
| 56 | 56 | |
| 57 | - private static List<CustomContext> inactiveContexts = Collections.synchronizedList(new ArrayList<CustomContext>()); | |
| 57 | + private static List<AbstractCustomContext> inactiveContexts = Collections.synchronizedList(new ArrayList<AbstractCustomContext>()); | |
| 58 | 58 | |
| 59 | 59 | private static Logger logger; |
| 60 | 60 | |
| ... | ... | @@ -79,7 +79,7 @@ public final class Contexts { |
| 79 | 79 | return bundle; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - public static synchronized void add(CustomContext context, AfterBeanDiscovery event) { | |
| 82 | + public static synchronized void add(AbstractCustomContext context, AfterBeanDiscovery event) { | |
| 83 | 83 | Class<? extends Annotation> scope = context.getScope(); |
| 84 | 84 | |
| 85 | 85 | getLogger() |
| ... | ... | @@ -99,10 +99,10 @@ public final class Contexts { |
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - private static CustomContext get(Class<? extends Annotation> scope, List<CustomContext> contexts) { | |
| 103 | - CustomContext result = null; | |
| 102 | + private static AbstractCustomContext get(Class<? extends Annotation> scope, List<AbstractCustomContext> contexts) { | |
| 103 | + AbstractCustomContext result = null; | |
| 104 | 104 | |
| 105 | - for (CustomContext context : contexts) { | |
| 105 | + for (AbstractCustomContext context : contexts) { | |
| 106 | 106 | if (scope.equals(context.getScope())) { |
| 107 | 107 | result = context; |
| 108 | 108 | break; |
| ... | ... | @@ -112,7 +112,7 @@ public final class Contexts { |
| 112 | 112 | return result; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - public static synchronized void remove(CustomContext context) { | |
| 115 | + public static synchronized void remove(AbstractCustomContext context) { | |
| 116 | 116 | getLogger().trace( |
| 117 | 117 | getBundle().getString("custom-context-was-unregistered", context.getScope().getCanonicalName())); |
| 118 | 118 | |
| ... | ... | @@ -120,7 +120,7 @@ public final class Contexts { |
| 120 | 120 | activeContexts.remove(context); |
| 121 | 121 | context.setActive(false); |
| 122 | 122 | |
| 123 | - CustomContext inactive = get(context.getScope(), inactiveContexts); | |
| 123 | + AbstractCustomContext inactive = get(context.getScope(), inactiveContexts); | |
| 124 | 124 | if (inactive != null) { |
| 125 | 125 | activeContexts.add(inactive); |
| 126 | 126 | inactive.setActive(true); |
| ... | ... | @@ -133,8 +133,8 @@ public final class Contexts { |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | public static synchronized void clear() { |
| 136 | - CustomContext context; | |
| 137 | - for (Iterator<CustomContext> iter = activeContexts.iterator(); iter.hasNext();) { | |
| 136 | + AbstractCustomContext context; | |
| 137 | + for (Iterator<AbstractCustomContext> iter = activeContexts.iterator(); iter.hasNext();) { | |
| 138 | 138 | context = iter.next(); |
| 139 | 139 | context.setActive(false); |
| 140 | 140 | iter.remove(); |
| ... | ... | @@ -144,11 +144,11 @@ public final class Contexts { |
| 144 | 144 | inactiveContexts.clear(); |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - public static synchronized List<CustomContext> getActiveContexts() { | |
| 147 | + public static synchronized List<AbstractCustomContext> getActiveContexts() { | |
| 148 | 148 | return activeContexts; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - public static synchronized List<CustomContext> getInactiveContexts() { | |
| 151 | + public static synchronized List<AbstractCustomContext> getInactiveContexts() { | |
| 152 | 152 | return inactiveContexts; |
| 153 | 153 | } |
| 154 | 154 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContext.java
| ... | ... | @@ -1,45 +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.context; | |
| 38 | - | |
| 39 | -import javax.enterprise.context.spi.Context; | |
| 40 | - | |
| 41 | -public interface CustomContext extends Context { | |
| 42 | - | |
| 43 | - void setActive(boolean b); | |
| 44 | - | |
| 45 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java
| ... | ... | @@ -48,84 +48,18 @@ |
| 48 | 48 | */ |
| 49 | 49 | package br.gov.frameworkdemoiselle.internal.context; |
| 50 | 50 | |
| 51 | -import java.lang.annotation.Annotation; | |
| 52 | - | |
| 53 | -import javax.enterprise.context.ContextNotActiveException; | |
| 54 | -import javax.enterprise.context.spi.Contextual; | |
| 55 | -import javax.enterprise.context.spi.CreationalContext; | |
| 56 | -import javax.enterprise.inject.spi.Bean; | |
| 57 | - | |
| 58 | 51 | import br.gov.frameworkdemoiselle.annotation.StaticScoped; |
| 59 | 52 | |
| 60 | -public class StaticContext implements CustomContext { | |
| 61 | - | |
| 62 | - private final static ContextStore store = new ContextStore(); | |
| 63 | - | |
| 64 | - // private final Map<Class<?>, Object> cache = Collections.synchronizedMap(new HashMap<Class<?>, Object>()); | |
| 53 | +public class StaticContext extends AbstractCustomContext { | |
| 65 | 54 | |
| 66 | - private boolean active; | |
| 67 | - | |
| 68 | - private final Class<? extends Annotation> scope; | |
| 69 | - | |
| 70 | - public StaticContext(final Class<? extends Annotation> scope) { | |
| 71 | - this(scope, true); | |
| 72 | - } | |
| 55 | + private final static Store store = new Store(); | |
| 73 | 56 | |
| 74 | 57 | public StaticContext() { |
| 75 | - this(StaticScoped.class, true); | |
| 76 | - } | |
| 77 | - | |
| 78 | - public StaticContext(final Class<? extends Annotation> scope, boolean active) { | |
| 79 | - this.scope = scope; | |
| 80 | - this.active = active; | |
| 81 | - } | |
| 82 | - | |
| 83 | - @Override | |
| 84 | - public <T> T get(final Contextual<T> contextual) { | |
| 85 | - return get(contextual, null); | |
| 86 | - } | |
| 87 | - | |
| 88 | - @Override | |
| 89 | - @SuppressWarnings("unchecked") | |
| 90 | - public <T> T get(final Contextual<T> contextual, final CreationalContext<T> creationalContext) { | |
| 91 | - T instance = null; | |
| 92 | - | |
| 93 | - if (!isActive()) { | |
| 94 | - throw new ContextNotActiveException(); | |
| 95 | - } | |
| 96 | - | |
| 97 | - String id = getId(contextual); | |
| 98 | - if (getStore().contains(id)) { | |
| 99 | - instance = (T) getStore().get(id); | |
| 100 | - | |
| 101 | - } else if (creationalContext != null) { | |
| 102 | - instance = contextual.create(creationalContext); | |
| 103 | - getStore().put(id, instance); | |
| 104 | - } | |
| 105 | - | |
| 106 | - return instance; | |
| 107 | - } | |
| 108 | - | |
| 109 | - private <T> String getId(final Contextual<T> contextual) { | |
| 110 | - Bean<T> bean = (Bean<T>) contextual; | |
| 111 | - return bean.getBeanClass().getCanonicalName(); | |
| 58 | + super(StaticScoped.class, true); | |
| 112 | 59 | } |
| 113 | 60 | |
| 114 | 61 | @Override |
| 115 | - public Class<? extends Annotation> getScope() { | |
| 116 | - return this.scope; | |
| 117 | - } | |
| 118 | - | |
| 119 | - private ContextStore getStore() { | |
| 62 | + protected Store getStore() { | |
| 120 | 63 | return store; |
| 121 | 64 | } |
| 122 | - | |
| 123 | - @Override | |
| 124 | - public boolean isActive() { | |
| 125 | - return this.active; | |
| 126 | - } | |
| 127 | - | |
| 128 | - public void setActive(final boolean active) { | |
| 129 | - this.active = active; | |
| 130 | - } | |
| 131 | 65 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ThreadLocalContext.java
| ... | ... | @@ -50,81 +50,24 @@ package br.gov.frameworkdemoiselle.internal.context; |
| 50 | 50 | |
| 51 | 51 | import java.lang.annotation.Annotation; |
| 52 | 52 | |
| 53 | -import javax.enterprise.context.ContextNotActiveException; | |
| 54 | -import javax.enterprise.context.spi.Contextual; | |
| 55 | -import javax.enterprise.context.spi.CreationalContext; | |
| 56 | -import javax.enterprise.inject.spi.Bean; | |
| 53 | +public class ThreadLocalContext extends AbstractCustomContext { | |
| 57 | 54 | |
| 58 | -public class ThreadLocalContext implements CustomContext { | |
| 59 | - | |
| 60 | - private final ThreadLocal<ContextStore> threadLocal = new ThreadLocal<ContextStore>(); | |
| 61 | - | |
| 62 | - private boolean active; | |
| 63 | - | |
| 64 | - private final Class<? extends Annotation> scope; | |
| 55 | + private final ThreadLocal<Store> threadLocal = new ThreadLocal<Store>(); | |
| 65 | 56 | |
| 66 | 57 | public ThreadLocalContext(final Class<? extends Annotation> scope) { |
| 67 | - this(scope, true); | |
| 68 | - } | |
| 69 | - | |
| 70 | - public ThreadLocalContext(final Class<? extends Annotation> scope, | |
| 71 | - boolean active) { | |
| 72 | - this.scope = scope; | |
| 73 | - this.active = active; | |
| 74 | - } | |
| 75 | - | |
| 76 | - @Override | |
| 77 | - public <T> T get(final Contextual<T> contextual) { | |
| 78 | - return get(contextual, null); | |
| 79 | - } | |
| 80 | - | |
| 81 | - @Override | |
| 82 | - @SuppressWarnings("unchecked") | |
| 83 | - public <T> T get(final Contextual<T> contextual, | |
| 84 | - final CreationalContext<T> creationalContext) { | |
| 85 | - T instance = null; | |
| 86 | - | |
| 87 | - if (!isActive()) { | |
| 88 | - throw new ContextNotActiveException(); | |
| 89 | - } | |
| 90 | - | |
| 91 | - String id = getId(contextual); | |
| 92 | - if (getStore().contains(id)) { | |
| 93 | - instance = (T) getStore().get(id); | |
| 94 | - | |
| 95 | - } else if (creationalContext != null) { | |
| 96 | - instance = contextual.create(creationalContext); | |
| 97 | - getStore().put(id, instance); | |
| 98 | - } | |
| 99 | - | |
| 100 | - return instance; | |
| 58 | + super(scope, true); | |
| 101 | 59 | } |
| 102 | 60 | |
| 103 | - private <T> String getId(final Contextual<T> contextual) { | |
| 104 | - Bean<T> bean = (Bean<T>) contextual; | |
| 105 | - return bean.getBeanClass().getCanonicalName(); | |
| 106 | - } | |
| 61 | +// public ThreadLocalContext(final Class<? extends Annotation> scope, boolean active) { | |
| 62 | +// super(scope, active); | |
| 63 | +// } | |
| 107 | 64 | |
| 108 | 65 | @Override |
| 109 | - public Class<? extends Annotation> getScope() { | |
| 110 | - return this.scope; | |
| 111 | - } | |
| 112 | - | |
| 113 | - private ContextStore getStore() { | |
| 66 | + protected Store getStore() { | |
| 114 | 67 | if (this.threadLocal.get() == null) { |
| 115 | - this.threadLocal.set(new ContextStore()); | |
| 68 | + this.threadLocal.set(new Store()); | |
| 116 | 69 | } |
| 117 | 70 | |
| 118 | 71 | return this.threadLocal.get(); |
| 119 | 72 | } |
| 120 | - | |
| 121 | - @Override | |
| 122 | - public boolean isActive() { | |
| 123 | - return this.active; | |
| 124 | - } | |
| 125 | - | |
| 126 | - public void setActive(final boolean active) { | |
| 127 | - this.active = active; | |
| 128 | - } | |
| 129 | - | |
| 130 | 73 | } | ... | ... |
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/JsfBootstrap.java
| ... | ... | @@ -44,14 +44,14 @@ import javax.enterprise.inject.spi.AfterBeanDiscovery; |
| 44 | 44 | import javax.enterprise.inject.spi.AfterDeploymentValidation; |
| 45 | 45 | import javax.enterprise.inject.spi.Extension; |
| 46 | 46 | |
| 47 | +import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | |
| 47 | 48 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 48 | -import br.gov.frameworkdemoiselle.internal.context.CustomContext; | |
| 49 | 49 | import br.gov.frameworkdemoiselle.internal.context.ViewContext; |
| 50 | 50 | import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; |
| 51 | 51 | |
| 52 | 52 | public class JsfBootstrap implements Extension { |
| 53 | 53 | |
| 54 | - private List<CustomContext> tempContexts = new ArrayList<CustomContext>(); | |
| 54 | + private List<AbstractCustomContext> tempContexts = new ArrayList<AbstractCustomContext>(); | |
| 55 | 55 | |
| 56 | 56 | private AfterBeanDiscovery afterBeanDiscoveryEvent; |
| 57 | 57 | |
| ... | ... | @@ -61,13 +61,13 @@ public class JsfBootstrap implements Extension { |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | public void addContexts(@Observes final AfterDeploymentValidation event) { |
| 64 | - for (CustomContext tempContext : this.tempContexts) { | |
| 64 | + for (AbstractCustomContext tempContext : this.tempContexts) { | |
| 65 | 65 | Contexts.add(tempContext, this.afterBeanDiscoveryEvent); |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | public void removeContexts(@Observes AfterShutdownProccess event) { |
| 70 | - for (CustomContext tempContext : this.tempContexts) { | |
| 70 | + for (AbstractCustomContext tempContext : this.tempContexts) { | |
| 71 | 71 | Contexts.remove(tempContext); |
| 72 | 72 | } |
| 73 | 73 | } | ... | ... |
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/ViewContext.java
| ... | ... | @@ -36,60 +36,26 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.internal.context; |
| 38 | 38 | |
| 39 | -import java.lang.annotation.Annotation; | |
| 40 | 39 | import java.util.Map; |
| 41 | 40 | |
| 42 | -import javax.enterprise.context.spi.Contextual; | |
| 43 | -import javax.enterprise.context.spi.CreationalContext; | |
| 44 | -import javax.enterprise.inject.spi.Bean; | |
| 45 | - | |
| 46 | 41 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 47 | 42 | import br.gov.frameworkdemoiselle.util.Faces; |
| 48 | 43 | |
| 49 | -public class ViewContext implements CustomContext { | |
| 50 | - | |
| 51 | - private boolean active; | |
| 44 | +public class ViewContext extends AbstractCustomContext { | |
| 52 | 45 | |
| 53 | 46 | public ViewContext() { |
| 54 | - this.active = true; | |
| 55 | - } | |
| 56 | - | |
| 57 | - @Override | |
| 58 | - public <T> T get(final Contextual<T> contextual) { | |
| 59 | - return get(contextual, null); | |
| 47 | + super(ViewScoped.class, true); | |
| 60 | 48 | } |
| 61 | 49 | |
| 62 | 50 | @Override |
| 63 | - @SuppressWarnings("unchecked") | |
| 64 | - public <T> T get(final Contextual<T> contextual, final CreationalContext<T> creationalContext) { | |
| 65 | - T instance = null; | |
| 66 | - | |
| 67 | - Bean<T> bean = (Bean<T>) contextual; | |
| 51 | + protected Store getStore() { | |
| 68 | 52 | Map<String, Object> viewMap = Faces.getViewMap(); |
| 53 | + String key = Store.class.getName(); | |
| 69 | 54 | |
| 70 | - if (viewMap.containsKey(bean.getName())) { | |
| 71 | - instance = (T) viewMap.get(bean.getName()); | |
| 72 | - | |
| 73 | - } else if (creationalContext != null) { | |
| 74 | - instance = bean.create(creationalContext); | |
| 75 | - viewMap.put(bean.getName(), instance); | |
| 55 | + if (!viewMap.containsKey(key)) { | |
| 56 | + viewMap.put(key, new Store()); | |
| 76 | 57 | } |
| 77 | 58 | |
| 78 | - return instance; | |
| 79 | - } | |
| 80 | - | |
| 81 | - @Override | |
| 82 | - public Class<? extends Annotation> getScope() { | |
| 83 | - return ViewScoped.class; | |
| 84 | - } | |
| 85 | - | |
| 86 | - @Override | |
| 87 | - public boolean isActive() { | |
| 88 | - return this.active; | |
| 89 | - } | |
| 90 | - | |
| 91 | - @Override | |
| 92 | - public void setActive(boolean active) { | |
| 93 | - this.active = active; | |
| 59 | + return (Store) viewMap.get(key); | |
| 94 | 60 | } |
| 95 | 61 | } | ... | ... |
impl/extension/jsf/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/JsfBootstrapTest.java
| ... | ... | @@ -58,8 +58,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; |
| 58 | 58 | import org.powermock.modules.junit4.PowerMockRunner; |
| 59 | 59 | import org.powermock.reflect.Whitebox; |
| 60 | 60 | |
| 61 | +import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | |
| 61 | 62 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 62 | -import br.gov.frameworkdemoiselle.internal.context.CustomContext; | |
| 63 | 63 | import br.gov.frameworkdemoiselle.internal.context.ViewContext; |
| 64 | 64 | import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; |
| 65 | 65 | import br.gov.frameworkdemoiselle.util.Beans; |
| ... | ... | @@ -85,17 +85,17 @@ public class JsfBootstrapTest { |
| 85 | 85 | public void testStoreContexts() { |
| 86 | 86 | bootstrap.storeContexts(event); |
| 87 | 87 | replay(event); |
| 88 | - | |
| 88 | + | |
| 89 | 89 | Assert.assertEquals(event, Whitebox.getInternalState(bootstrap, "afterBeanDiscoveryEvent")); |
| 90 | - List<CustomContext> context = Whitebox.getInternalState(bootstrap, "tempContexts"); | |
| 90 | + List<AbstractCustomContext> context = Whitebox.getInternalState(bootstrap, "tempContexts"); | |
| 91 | 91 | Assert.assertEquals(1, context.size()); |
| 92 | 92 | verifyAll(); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | @Test |
| 96 | 96 | public void testAddContexts() { |
| 97 | - List<CustomContext> tempContexts = new ArrayList<CustomContext>(); | |
| 98 | - CustomContext tempContext = new ViewContext(); | |
| 97 | + List<AbstractCustomContext> tempContexts = new ArrayList<AbstractCustomContext>(); | |
| 98 | + AbstractCustomContext tempContext = new ViewContext(); | |
| 99 | 99 | tempContexts.add(tempContext); |
| 100 | 100 | Whitebox.setInternalState(bootstrap, "tempContexts", tempContexts); |
| 101 | 101 | Whitebox.setInternalState(bootstrap, "afterBeanDiscoveryEvent", event); |
| ... | ... | @@ -113,7 +113,7 @@ public class JsfBootstrapTest { |
| 113 | 113 | @Test |
| 114 | 114 | public void testRemoveContexts() { |
| 115 | 115 | bootstrap.storeContexts(event); |
| 116 | - | |
| 116 | + | |
| 117 | 117 | AfterShutdownProccess afterShutdownProccess = createMock(AfterShutdownProccess.class); |
| 118 | 118 | replay(event, afterShutdownProccess); |
| 119 | 119 | bootstrap.removeContexts(afterShutdownProccess); | ... | ... |
impl/extension/se/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/SeBootstrap.java
| ... | ... | @@ -49,13 +49,13 @@ import javax.enterprise.inject.spi.Extension; |
| 49 | 49 | |
| 50 | 50 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 51 | 51 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 52 | -import br.gov.frameworkdemoiselle.internal.context.CustomContext; | |
| 52 | +import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | |
| 53 | 53 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
| 54 | 54 | import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; |
| 55 | 55 | |
| 56 | 56 | public class SeBootstrap implements Extension { |
| 57 | 57 | |
| 58 | - private List<CustomContext> tempContexts = new ArrayList<CustomContext>(); | |
| 58 | + private List<AbstractCustomContext> tempContexts = new ArrayList<AbstractCustomContext>(); | |
| 59 | 59 | |
| 60 | 60 | private AfterBeanDiscovery afterBeanDiscoveryEvent; |
| 61 | 61 | |
| ... | ... | @@ -69,13 +69,13 @@ public class SeBootstrap implements Extension { |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | public void addContexts(@Observes final AfterDeploymentValidation event) { |
| 72 | - for (CustomContext tempContext : this.tempContexts) { | |
| 72 | + for (AbstractCustomContext tempContext : this.tempContexts) { | |
| 73 | 73 | Contexts.add(tempContext, this.afterBeanDiscoveryEvent); |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | public void removeContexts(@Observes AfterShutdownProccess event) { |
| 78 | - for (CustomContext tempContext : this.tempContexts) { | |
| 78 | + for (AbstractCustomContext tempContext : this.tempContexts) { | |
| 79 | 79 | Contexts.remove(tempContext); |
| 80 | 80 | } |
| 81 | 81 | } | ... | ... |
impl/extension/se/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/SeBootstrapTest.java
| ... | ... | @@ -56,19 +56,19 @@ import org.powermock.core.classloader.annotations.PrepareForTest; |
| 56 | 56 | import org.powermock.modules.junit4.PowerMockRunner; |
| 57 | 57 | import org.powermock.reflect.Whitebox; |
| 58 | 58 | |
| 59 | +import br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext; | |
| 59 | 60 | import br.gov.frameworkdemoiselle.internal.context.Contexts; |
| 60 | -import br.gov.frameworkdemoiselle.internal.context.CustomContext; | |
| 61 | 61 | import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; |
| 62 | 62 | import br.gov.frameworkdemoiselle.util.Beans; |
| 63 | 63 | |
| 64 | 64 | @RunWith(PowerMockRunner.class) |
| 65 | 65 | @PrepareForTest({ Beans.class, Contexts.class }) |
| 66 | 66 | public class SeBootstrapTest { |
| 67 | - | |
| 67 | + | |
| 68 | 68 | private SeBootstrap seBootstrap; |
| 69 | - | |
| 69 | + | |
| 70 | 70 | private AfterBeanDiscovery event; |
| 71 | - | |
| 71 | + | |
| 72 | 72 | @Before |
| 73 | 73 | public void before() { |
| 74 | 74 | event = createMock(AfterBeanDiscovery.class); |
| ... | ... | @@ -77,22 +77,22 @@ public class SeBootstrapTest { |
| 77 | 77 | replay(Beans.class); |
| 78 | 78 | seBootstrap = new SeBootstrap(); |
| 79 | 79 | } |
| 80 | - | |
| 80 | + | |
| 81 | 81 | @Test |
| 82 | 82 | public void testStoreContext() { |
| 83 | 83 | seBootstrap.storeContexts(event); |
| 84 | 84 | replay(event); |
| 85 | - | |
| 85 | + | |
| 86 | 86 | Assert.assertEquals(event, Whitebox.getInternalState(seBootstrap, "afterBeanDiscoveryEvent")); |
| 87 | - List<CustomContext> context = Whitebox.getInternalState(seBootstrap, "tempContexts"); | |
| 87 | + List<AbstractCustomContext> context = Whitebox.getInternalState(seBootstrap, "tempContexts"); | |
| 88 | 88 | Assert.assertEquals(4, context.size()); |
| 89 | 89 | verifyAll(); |
| 90 | 90 | } |
| 91 | - | |
| 91 | + | |
| 92 | 92 | @Test |
| 93 | 93 | public void testRemoveContexts() { |
| 94 | 94 | seBootstrap.storeContexts(event); |
| 95 | - | |
| 95 | + | |
| 96 | 96 | AfterShutdownProccess afterShutdownProccess = createMock(AfterShutdownProccess.class); |
| 97 | 97 | replay(event, afterShutdownProccess); |
| 98 | 98 | seBootstrap.removeContexts(afterShutdownProccess); | ... | ... |