Commit 3ef1b92b173e142c166c5e99e4f16928ec993ec9
1 parent
dc6f4fb1
Exists in
master
Implementando EntityManagerProducer com escopo configurável.
Showing
10 changed files
with
338 additions
and
63 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContextProducer.java
| @@ -52,10 +52,10 @@ import java.util.ArrayList; | @@ -52,10 +52,10 @@ import java.util.ArrayList; | ||
| 52 | import java.util.List; | 52 | import java.util.List; |
| 53 | import java.util.Locale; | 53 | import java.util.Locale; |
| 54 | 54 | ||
| 55 | -import javax.annotation.PostConstruct; | ||
| 56 | import javax.annotation.PreDestroy; | 55 | import javax.annotation.PreDestroy; |
| 57 | import javax.enterprise.context.ApplicationScoped; | 56 | import javax.enterprise.context.ApplicationScoped; |
| 58 | import javax.enterprise.inject.Produces; | 57 | import javax.enterprise.inject.Produces; |
| 58 | +import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
| 59 | import javax.enterprise.inject.spi.InjectionPoint; | 59 | import javax.enterprise.inject.spi.InjectionPoint; |
| 60 | 60 | ||
| 61 | import org.slf4j.Logger; | 61 | import org.slf4j.Logger; |
| @@ -86,20 +86,20 @@ public class CustomContextProducer { | @@ -86,20 +86,20 @@ public class CustomContextProducer { | ||
| 86 | 86 | ||
| 87 | private transient ResourceBundle bundle; | 87 | private transient ResourceBundle bundle; |
| 88 | 88 | ||
| 89 | - private List<CustomContext> contexts = new ArrayList<CustomContext>(); | ||
| 90 | - | ||
| 91 | - | ||
| 92 | /** | 89 | /** |
| 93 | - * Store a context into this producer. The context must have | ||
| 94 | - * been registered into CDI by a portable extension, this method will not do this. | 90 | + * <p>Store a context into this producer. The context must have |
| 91 | + * been registered into CDI (unsing {@link AfterBeanDiscovery#addContext(javax.enterprise.context.spi.Context context)}) by a portable extension, | ||
| 92 | + * this method will not do this.</p> | ||
| 93 | + * | ||
| 94 | + * <p>This producer can only produce contexts registered through this method.</p> | ||
| 95 | * | 95 | * |
| 96 | */ | 96 | */ |
| 97 | public void addRegisteredContext(CustomContext context){ | 97 | public void addRegisteredContext(CustomContext context){ |
| 98 | Logger logger = getLogger(); | 98 | Logger logger = getLogger(); |
| 99 | ResourceBundle bundle = getBundle(); | 99 | ResourceBundle bundle = getBundle(); |
| 100 | 100 | ||
| 101 | - if (!contexts.contains(context)){ | ||
| 102 | - contexts.add(context); | 101 | + if (!getContexts().contains(context)){ |
| 102 | + getContexts().add(context); | ||
| 103 | logger.debug( bundle.getString("bootstrap-context-added", context.getClass().getCanonicalName() , context.getScope().getSimpleName() ) ); | 103 | logger.debug( bundle.getString("bootstrap-context-added", context.getClass().getCanonicalName() , context.getScope().getSimpleName() ) ); |
| 104 | } | 104 | } |
| 105 | else{ | 105 | else{ |
| @@ -108,65 +108,59 @@ public class CustomContextProducer { | @@ -108,65 +108,59 @@ public class CustomContextProducer { | ||
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | /** | 110 | /** |
| 111 | - * Store a list of contexts into this producer. The contexts must have | ||
| 112 | - * been registered into CDI by a portable extension, this method will not do this. | ||
| 113 | - * | ||
| 114 | - */ | ||
| 115 | - @PostConstruct | ||
| 116 | - public void addRegisteredContexts(){ | ||
| 117 | - CustomContextBootstrap contextBootstrap = Beans.getReference(CustomContextBootstrap.class); | ||
| 118 | - | ||
| 119 | - List<CustomContext> contexts = contextBootstrap.getCustomContexts(); | ||
| 120 | - | ||
| 121 | - for (CustomContext context : contexts){ | ||
| 122 | - addRegisteredContext(context); | ||
| 123 | - } | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - /** | ||
| 127 | * Deactivates all registered contexts and clear the context collection | 111 | * Deactivates all registered contexts and clear the context collection |
| 128 | */ | 112 | */ |
| 129 | @PreDestroy | 113 | @PreDestroy |
| 130 | public void closeContexts(){ | 114 | public void closeContexts(){ |
| 131 | //Desativa todos os contextos registrados. | 115 | //Desativa todos os contextos registrados. |
| 132 | - for (CustomContext context : contexts){ | 116 | + for (CustomContext context : getContexts()){ |
| 133 | context.deactivate(); | 117 | context.deactivate(); |
| 134 | } | 118 | } |
| 135 | 119 | ||
| 136 | - contexts.clear(); | 120 | + getContexts().clear(); |
| 121 | + } | ||
| 122 | + | ||
| 123 | + private List<CustomContext> getContexts(){ | ||
| 124 | + /* The demoiselle-core CustomContextBootstrap class creates default contexts for the main | ||
| 125 | + * scopes of an application (request, session and conversation) and some custom contexts | ||
| 126 | + * (view and static). This method injects a reference to the CustomContextBootstrap to obtain those | ||
| 127 | + * contexts. Also any context registered after application start-up will be obtained by this method. */ | ||
| 128 | + | ||
| 129 | + CustomContextBootstrap contextBootstrap = Beans.getReference(CustomContextBootstrap.class); | ||
| 130 | + return contextBootstrap.getCustomContexts(); | ||
| 137 | } | 131 | } |
| 138 | 132 | ||
| 139 | /////////////PRODUCERS/////////////////// | 133 | /////////////PRODUCERS/////////////////// |
| 140 | 134 | ||
| 141 | @Produces | 135 | @Produces |
| 142 | - public RequestContext getRequestContext(InjectionPoint ip , CustomContextBootstrap extension){ | ||
| 143 | - return getContext(ip, extension); | 136 | + public RequestContext getRequestContext(InjectionPoint ip){ |
| 137 | + return getContext(ip); | ||
| 144 | } | 138 | } |
| 145 | 139 | ||
| 146 | @Produces | 140 | @Produces |
| 147 | - public SessionContext getSessionContext(InjectionPoint ip , CustomContextBootstrap extension){ | ||
| 148 | - return getContext(ip, extension); | 141 | + public SessionContext getSessionContext(InjectionPoint ip){ |
| 142 | + return getContext(ip); | ||
| 149 | } | 143 | } |
| 150 | 144 | ||
| 151 | @Produces | 145 | @Produces |
| 152 | - public ViewContext getViewContext(InjectionPoint ip , CustomContextBootstrap extension){ | ||
| 153 | - return getContext(ip, extension); | 146 | + public ViewContext getViewContext(InjectionPoint ip){ |
| 147 | + return getContext(ip); | ||
| 154 | } | 148 | } |
| 155 | 149 | ||
| 156 | @Produces | 150 | @Produces |
| 157 | - public StaticContext getStaticContext(InjectionPoint ip , CustomContextBootstrap extension){ | ||
| 158 | - return getContext(ip, extension); | 151 | + public StaticContext getStaticContext(InjectionPoint ip){ |
| 152 | + return getContext(ip); | ||
| 159 | } | 153 | } |
| 160 | 154 | ||
| 161 | @Produces | 155 | @Produces |
| 162 | - public ConversationContext getConversationContext(InjectionPoint ip , CustomContextBootstrap extension){ | ||
| 163 | - return getContext(ip, extension); | 156 | + public ConversationContext getConversationContext(InjectionPoint ip){ |
| 157 | + return getContext(ip); | ||
| 164 | } | 158 | } |
| 165 | 159 | ||
| 166 | /////////////END OF PRODUCERS/////////////////// | 160 | /////////////END OF PRODUCERS/////////////////// |
| 167 | 161 | ||
| 168 | @SuppressWarnings("unchecked") | 162 | @SuppressWarnings("unchecked") |
| 169 | - private <T extends CustomContext> T getContext(InjectionPoint ip , CustomContextBootstrap extension){ | 163 | + private <T extends CustomContext> T getContext(InjectionPoint ip){ |
| 170 | T producedContext = null; | 164 | T producedContext = null; |
| 171 | 165 | ||
| 172 | if (ip!=null){ | 166 | if (ip!=null){ |
| @@ -186,7 +180,7 @@ public class CustomContextProducer { | @@ -186,7 +180,7 @@ public class CustomContextProducer { | ||
| 186 | 180 | ||
| 187 | ArrayList<CustomContext> selectableContexts = new ArrayList<CustomContext>(); | 181 | ArrayList<CustomContext> selectableContexts = new ArrayList<CustomContext>(); |
| 188 | 182 | ||
| 189 | - for (CustomContext context : contexts){ | 183 | + for (CustomContext context : getContexts()){ |
| 190 | if ( contextClass.isAssignableFrom( context.getClass() ) ){ | 184 | if ( contextClass.isAssignableFrom( context.getClass() ) ){ |
| 191 | if (context.isActive()){ | 185 | if (context.isActive()){ |
| 192 | producedContext = context; | 186 | producedContext = context; |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationEnumValueExtractor.java
0 → 100644
| @@ -0,0 +1,82 @@ | @@ -0,0 +1,82 @@ | ||
| 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.implementation; | ||
| 38 | + | ||
| 39 | +import java.lang.reflect.Field; | ||
| 40 | +import java.util.Locale; | ||
| 41 | + | ||
| 42 | +import org.apache.commons.configuration.Configuration; | ||
| 43 | +import org.apache.commons.configuration.ConversionException; | ||
| 44 | + | ||
| 45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
| 46 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationValueExtractor; | ||
| 47 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 48 | + | ||
| 49 | +@Priority(Priority.L2_PRIORITY) | ||
| 50 | +public class ConfigurationEnumValueExtractor implements ConfigurationValueExtractor{ | ||
| 51 | + | ||
| 52 | + private transient ResourceBundle bundle; | ||
| 53 | + | ||
| 54 | + @Override | ||
| 55 | + public Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception { | ||
| 56 | + String value = configuration.getString(prefix + key); | ||
| 57 | + | ||
| 58 | + Object enums[] = field.getDeclaringClass().getEnumConstants(); | ||
| 59 | + | ||
| 60 | + for (int i=0; i<enums.length; i++){ | ||
| 61 | + if (enums[i].getClass().getSimpleName().equalsIgnoreCase(value)){ | ||
| 62 | + return enums[i]; | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + throw new ConversionException(getBundle().getString("configuration-not-conversion",value,field.getDeclaringClass().getCanonicalName())); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public boolean isSupported(Field field) { | ||
| 71 | + return field.getDeclaringClass().isEnum(); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + private ResourceBundle getBundle(){ | ||
| 75 | + if (bundle==null){ | ||
| 76 | + bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + return bundle; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/ConfigurationLoader.java
| @@ -100,7 +100,13 @@ public class ConfigurationLoader implements Serializable { | @@ -100,7 +100,13 @@ public class ConfigurationLoader implements Serializable { | ||
| 100 | private Collection<Field> fields; | 100 | private Collection<Field> fields; |
| 101 | 101 | ||
| 102 | public void load(Object object) throws ConfigurationException { | 102 | public void load(Object object) throws ConfigurationException { |
| 103 | - getLogger().debug(getBundle().getString("loading-configuration-class", object.getClass().getName())); | 103 | + load(object,true); |
| 104 | + } | ||
| 105 | + | ||
| 106 | + public void load(Object object,boolean logLoadingProcess) throws ConfigurationException { | ||
| 107 | + if (logLoadingProcess){ | ||
| 108 | + getLogger().debug(getBundle().getString("loading-configuration-class", object.getClass().getName())); | ||
| 109 | + } | ||
| 104 | 110 | ||
| 105 | this.object = object; | 111 | this.object = object; |
| 106 | 112 |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/Management.java
| @@ -55,6 +55,7 @@ import org.slf4j.Logger; | @@ -55,6 +55,7 @@ import org.slf4j.Logger; | ||
| 55 | 55 | ||
| 56 | import br.gov.frameworkdemoiselle.annotation.ManagedProperty; | 56 | import br.gov.frameworkdemoiselle.annotation.ManagedProperty; |
| 57 | import br.gov.frameworkdemoiselle.annotation.Name; | 57 | import br.gov.frameworkdemoiselle.annotation.Name; |
| 58 | +import br.gov.frameworkdemoiselle.context.ConversationContext; | ||
| 58 | import br.gov.frameworkdemoiselle.context.RequestContext; | 59 | import br.gov.frameworkdemoiselle.context.RequestContext; |
| 59 | import br.gov.frameworkdemoiselle.context.SessionContext; | 60 | import br.gov.frameworkdemoiselle.context.SessionContext; |
| 60 | import br.gov.frameworkdemoiselle.context.ViewContext; | 61 | import br.gov.frameworkdemoiselle.context.ViewContext; |
| @@ -306,6 +307,7 @@ public class Management implements Serializable { | @@ -306,6 +307,7 @@ public class Management implements Serializable { | ||
| 306 | RequestContext requestContext = Beans.getReference(RequestContext.class); | 307 | RequestContext requestContext = Beans.getReference(RequestContext.class); |
| 307 | ViewContext viewContext = Beans.getReference(ViewContext.class); | 308 | ViewContext viewContext = Beans.getReference(ViewContext.class); |
| 308 | SessionContext sessionContext = Beans.getReference(SessionContext.class); | 309 | SessionContext sessionContext = Beans.getReference(SessionContext.class); |
| 310 | + ConversationContext conversationContext = Beans.getReference(ConversationContext.class); | ||
| 309 | 311 | ||
| 310 | if (!requestContext.isActive()){ | 312 | if (!requestContext.isActive()){ |
| 311 | logger.debug(bundle.getString("management-debug-starting-custom-context", | 313 | logger.debug(bundle.getString("management-debug-starting-custom-context", |
| @@ -327,12 +329,20 @@ public class Management implements Serializable { | @@ -327,12 +329,20 @@ public class Management implements Serializable { | ||
| 327 | 329 | ||
| 328 | sessionContext.activate(); | 330 | sessionContext.activate(); |
| 329 | } | 331 | } |
| 332 | + | ||
| 333 | + if (!conversationContext.isActive()){ | ||
| 334 | + logger.debug(bundle.getString("management-debug-starting-custom-context", | ||
| 335 | + conversationContext.getClass().getCanonicalName(), managedType.getCanonicalName())); | ||
| 336 | + | ||
| 337 | + conversationContext.activate(); | ||
| 338 | + } | ||
| 330 | } | 339 | } |
| 331 | 340 | ||
| 332 | private void deactivateContexts(Class<?> managedType) { | 341 | private void deactivateContexts(Class<?> managedType) { |
| 333 | RequestContext requestContext = Beans.getReference(RequestContext.class); | 342 | RequestContext requestContext = Beans.getReference(RequestContext.class); |
| 334 | ViewContext viewContext = Beans.getReference(ViewContext.class); | 343 | ViewContext viewContext = Beans.getReference(ViewContext.class); |
| 335 | SessionContext sessionContext = Beans.getReference(SessionContext.class); | 344 | SessionContext sessionContext = Beans.getReference(SessionContext.class); |
| 345 | + ConversationContext conversationContext = Beans.getReference(ConversationContext.class); | ||
| 336 | 346 | ||
| 337 | if (requestContext.isActive()){ | 347 | if (requestContext.isActive()){ |
| 338 | logger.debug(bundle.getString("management-debug-stoping-custom-context", | 348 | logger.debug(bundle.getString("management-debug-stoping-custom-context", |
| @@ -354,6 +364,13 @@ public class Management implements Serializable { | @@ -354,6 +364,13 @@ public class Management implements Serializable { | ||
| 354 | 364 | ||
| 355 | sessionContext.deactivate(); | 365 | sessionContext.deactivate(); |
| 356 | } | 366 | } |
| 367 | + | ||
| 368 | + if (!conversationContext.isActive()){ | ||
| 369 | + logger.debug(bundle.getString("management-debug-starting-custom-context", | ||
| 370 | + conversationContext.getClass().getCanonicalName(), managedType.getCanonicalName())); | ||
| 371 | + | ||
| 372 | + conversationContext.activate(); | ||
| 373 | + } | ||
| 357 | } | 374 | } |
| 358 | 375 | ||
| 359 | public void shutdown(Collection<Class<? extends ManagementExtension>> monitoringExtensions) { | 376 | public void shutdown(Collection<Class<? extends ManagementExtension>> monitoringExtensions) { |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/EntityManagerBootstrap.java
0 → 100644
| @@ -0,0 +1,168 @@ | @@ -0,0 +1,168 @@ | ||
| 1 | +package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
| 2 | + | ||
| 3 | +import java.lang.annotation.Annotation; | ||
| 4 | +import java.lang.reflect.Type; | ||
| 5 | +import java.util.Set; | ||
| 6 | + | ||
| 7 | +import javax.enterprise.context.ApplicationScoped; | ||
| 8 | +import javax.enterprise.context.ConversationScoped; | ||
| 9 | +import javax.enterprise.context.RequestScoped; | ||
| 10 | +import javax.enterprise.context.SessionScoped; | ||
| 11 | +import javax.enterprise.event.Observes; | ||
| 12 | +import javax.enterprise.inject.spi.AnnotatedConstructor; | ||
| 13 | +import javax.enterprise.inject.spi.AnnotatedField; | ||
| 14 | +import javax.enterprise.inject.spi.AnnotatedMethod; | ||
| 15 | +import javax.enterprise.inject.spi.AnnotatedType; | ||
| 16 | +import javax.enterprise.inject.spi.BeanManager; | ||
| 17 | +import javax.enterprise.inject.spi.Extension; | ||
| 18 | +import javax.enterprise.inject.spi.ProcessAnnotatedType; | ||
| 19 | +import javax.enterprise.util.AnnotationLiteral; | ||
| 20 | + | ||
| 21 | +import br.gov.frameworkdemoiselle.annotation.ViewScoped; | ||
| 22 | +import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; | ||
| 23 | +import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; | ||
| 24 | +import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader; | ||
| 25 | +import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +public class EntityManagerBootstrap implements Extension { | ||
| 29 | + | ||
| 30 | + public void selectScopeForEntityManager(@Observes final ProcessAnnotatedType<EntityManagerProducer> event, BeanManager beanManager) { | ||
| 31 | + EntityManagerConfig config = new EntityManagerConfig(); | ||
| 32 | + new ConfigurationLoader().load(config,false); | ||
| 33 | + final EntityManagerScope entityManagerScope = config.getEntityManagerScope(); | ||
| 34 | + | ||
| 35 | + if (entityManagerScope != EntityManagerScope.NOSCOPE){ | ||
| 36 | + AnnotatedType<EntityManagerProducer> annotatedType = new AnnotatedType<EntityManagerProducer>() { | ||
| 37 | + | ||
| 38 | + private AnnotatedType<EntityManagerProducer> delegate = event.getAnnotatedType(); | ||
| 39 | + | ||
| 40 | + public Class<EntityManagerProducer> getJavaClass() { | ||
| 41 | + return delegate.getJavaClass(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public Type getBaseType() { | ||
| 45 | + return delegate.getBaseType(); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public Set<AnnotatedConstructor<EntityManagerProducer>> getConstructors() { | ||
| 49 | + return delegate.getConstructors(); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public Set<Type> getTypeClosure() { | ||
| 53 | + return delegate.getTypeClosure(); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + public Set<AnnotatedMethod<? super EntityManagerProducer>> getMethods() { | ||
| 57 | + return delegate.getMethods(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @SuppressWarnings("unchecked") | ||
| 61 | + public <T extends Annotation> T getAnnotation(Class<T> annotationType) { | ||
| 62 | + T returnedAnnotation = null; | ||
| 63 | + Class<?> expectedScope; | ||
| 64 | + | ||
| 65 | + switch(entityManagerScope){ | ||
| 66 | + case APPLICATION: | ||
| 67 | + expectedScope = ApplicationScoped.class; | ||
| 68 | + break; | ||
| 69 | + case CONVERSATION: | ||
| 70 | + expectedScope = ConversationScoped.class; | ||
| 71 | + break; | ||
| 72 | + case REQUEST: | ||
| 73 | + expectedScope = RequestScoped.class; | ||
| 74 | + break; | ||
| 75 | + case SESSION: | ||
| 76 | + expectedScope = SessionScoped.class; | ||
| 77 | + break; | ||
| 78 | + case VIEW: | ||
| 79 | + expectedScope = ViewScoped.class; | ||
| 80 | + break; | ||
| 81 | + default: | ||
| 82 | + expectedScope = null; | ||
| 83 | + break; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + if (annotationType.equals(expectedScope)){ | ||
| 87 | + switch(entityManagerScope){ | ||
| 88 | + case APPLICATION: | ||
| 89 | + returnedAnnotation = (T) new ApplicationScopedLiteral(); | ||
| 90 | + break; | ||
| 91 | + case CONVERSATION: | ||
| 92 | + returnedAnnotation = (T) new ConversationScopedLiteral(); | ||
| 93 | + break; | ||
| 94 | + case REQUEST: | ||
| 95 | + returnedAnnotation = (T) new ApplicationScopedLiteral(); | ||
| 96 | + break; | ||
| 97 | + case SESSION: | ||
| 98 | + returnedAnnotation = (T) new SessionScopedLiteral(); | ||
| 99 | + break; | ||
| 100 | + case VIEW: | ||
| 101 | + returnedAnnotation = (T) new ViewScopedLiteral(); | ||
| 102 | + break; | ||
| 103 | + default: | ||
| 104 | + returnedAnnotation = delegate.getAnnotation(annotationType); | ||
| 105 | + break; | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + else{ | ||
| 109 | + returnedAnnotation = delegate.getAnnotation(annotationType); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + return returnedAnnotation; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + public Set<AnnotatedField<? super EntityManagerProducer>> getFields() { | ||
| 116 | + return delegate.getFields(); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + public Set<Annotation> getAnnotations() { | ||
| 120 | + return delegate.getAnnotations(); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { | ||
| 124 | + return delegate.isAnnotationPresent(annotationType); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + }; | ||
| 129 | + | ||
| 130 | + event.setAnnotatedType(annotatedType); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + @SuppressWarnings("all") | ||
| 135 | + class ApplicationScopedLiteral extends AnnotationLiteral<ApplicationScoped> implements ApplicationScoped { | ||
| 136 | + private static final long serialVersionUID = 1L; | ||
| 137 | + | ||
| 138 | + private ApplicationScopedLiteral() {} | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + @SuppressWarnings("all") | ||
| 142 | + class RequestScopedLiteral extends AnnotationLiteral<RequestScoped> implements RequestScoped { | ||
| 143 | + private static final long serialVersionUID = 1L; | ||
| 144 | + | ||
| 145 | + private RequestScopedLiteral(){} | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + @SuppressWarnings("all") | ||
| 149 | + class SessionScopedLiteral extends AnnotationLiteral<SessionScoped> implements SessionScoped { | ||
| 150 | + private static final long serialVersionUID = 1L; | ||
| 151 | + | ||
| 152 | + private SessionScopedLiteral(){} | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + @SuppressWarnings("all") | ||
| 156 | + class ViewScopedLiteral extends AnnotationLiteral<ViewScoped> implements ViewScoped { | ||
| 157 | + private static final long serialVersionUID = 1L; | ||
| 158 | + | ||
| 159 | + private ViewScopedLiteral(){} | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + @SuppressWarnings("all") | ||
| 163 | + class ConversationScopedLiteral extends AnnotationLiteral<ConversationScoped> implements ConversationScoped { | ||
| 164 | + private static final long serialVersionUID = 1L; | ||
| 165 | + | ||
| 166 | + private ConversationScopedLiteral(){} | ||
| 167 | + } | ||
| 168 | +} |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfig.java
| @@ -38,6 +38,8 @@ package br.gov.frameworkdemoiselle.internal.configuration; | @@ -38,6 +38,8 @@ package br.gov.frameworkdemoiselle.internal.configuration; | ||
| 38 | 38 | ||
| 39 | import java.io.Serializable; | 39 | import java.io.Serializable; |
| 40 | 40 | ||
| 41 | +import javax.persistence.EntityManager; | ||
| 42 | + | ||
| 41 | import org.slf4j.Logger; | 43 | import org.slf4j.Logger; |
| 42 | 44 | ||
| 43 | import br.gov.frameworkdemoiselle.annotation.Name; | 45 | import br.gov.frameworkdemoiselle.annotation.Name; |
| @@ -58,14 +60,15 @@ public class EntityManagerConfig implements Serializable { | @@ -58,14 +60,15 @@ public class EntityManagerConfig implements Serializable { | ||
| 58 | */ | 60 | */ |
| 59 | // TODO Implementação apenas para manter a compatibilidade entre a versão 2.3 com a 2.4. | 61 | // TODO Implementação apenas para manter a compatibilidade entre a versão 2.3 com a 2.4. |
| 60 | @Name("unit.name") | 62 | @Name("unit.name") |
| 63 | + @Deprecated | ||
| 61 | private String persistenceUnitName; | 64 | private String persistenceUnitName; |
| 62 | 65 | ||
| 63 | @Name("default.unit.name") | 66 | @Name("default.unit.name") |
| 64 | private String defaultPersistenceUnitName; | 67 | private String defaultPersistenceUnitName; |
| 65 | 68 | ||
| 66 | @Name("entitymanager.scope") | 69 | @Name("entitymanager.scope") |
| 67 | - private String entityManagerScope = "request"; | ||
| 68 | - | 70 | + private EntityManagerScope entityManagerScope = EntityManagerScope.REQUEST; |
| 71 | + | ||
| 69 | /** | 72 | /** |
| 70 | * Getter for persistence unit name. | 73 | * Getter for persistence unit name. |
| 71 | */ | 74 | */ |
| @@ -74,6 +77,7 @@ public class EntityManagerConfig implements Serializable { | @@ -74,6 +77,7 @@ public class EntityManagerConfig implements Serializable { | ||
| 74 | * @deprecated | 77 | * @deprecated |
| 75 | * @return | 78 | * @return |
| 76 | */ | 79 | */ |
| 80 | + @Deprecated | ||
| 77 | public String getPersistenceUnitName() { | 81 | public String getPersistenceUnitName() { |
| 78 | return persistenceUnitName; | 82 | return persistenceUnitName; |
| 79 | } | 83 | } |
| @@ -96,15 +100,32 @@ public class EntityManagerConfig implements Serializable { | @@ -96,15 +100,32 @@ public class EntityManagerConfig implements Serializable { | ||
| 96 | return defaultPersistenceUnitName; | 100 | return defaultPersistenceUnitName; |
| 97 | } | 101 | } |
| 98 | 102 | ||
| 99 | - | ||
| 100 | - public String getEntityManagerScope() { | 103 | + /** |
| 104 | + * <p>Defines the scope of {@link EntityManager}'s produced by the internal producer.</p> | ||
| 105 | + * | ||
| 106 | + * <p>Valid values are NOSCOPE,REQUEST,SESSION,VIEW,CONVERSATION and APPLICATION.</p> | ||
| 107 | + * | ||
| 108 | + * <p>NOSCOPE means every injected entity manager will be a different instance.</p> | ||
| 109 | + * | ||
| 110 | + * <p>The default value is REQUEST, meaning the producer will create the same | ||
| 111 | + * entity manager for the duration of the request.</p> | ||
| 112 | + * | ||
| 113 | + */ | ||
| 114 | + public EntityManagerScope getEntityManagerScope() { | ||
| 101 | return entityManagerScope; | 115 | return entityManagerScope; |
| 102 | } | 116 | } |
| 103 | 117 | ||
| 104 | - | ||
| 105 | - public void setEntityManagerScope(String entityManagerScope) { | 118 | + public void setEntityManagerScope(EntityManagerScope entityManagerScope) { |
| 106 | this.entityManagerScope = entityManagerScope; | 119 | this.entityManagerScope = entityManagerScope; |
| 107 | } | 120 | } |
| 108 | - | ||
| 109 | - | 121 | + |
| 122 | + /** | ||
| 123 | + * Supported scopes for the entity manager | ||
| 124 | + * | ||
| 125 | + * @author serpro | ||
| 126 | + * | ||
| 127 | + */ | ||
| 128 | + public enum EntityManagerScope{ | ||
| 129 | + NOSCOPE,REQUEST,SESSION,VIEW,CONVERSATION,APPLICATION; | ||
| 130 | + } | ||
| 110 | } | 131 | } |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/context/PersistenceContext.java
| @@ -1,12 +0,0 @@ | @@ -1,12 +0,0 @@ | ||
| 1 | -package br.gov.frameworkdemoiselle.internal.context; | ||
| 2 | - | ||
| 3 | -import java.lang.annotation.Annotation; | ||
| 4 | - | ||
| 5 | - | ||
| 6 | -public class PersistenceContext extends AbstractThreadLocalContext { | ||
| 7 | - | ||
| 8 | - public PersistenceContext(Class<? extends Annotation> scope) { | ||
| 9 | - super(scope); | ||
| 10 | - } | ||
| 11 | - | ||
| 12 | -} |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerProducer.java
| @@ -44,7 +44,6 @@ import java.util.Set; | @@ -44,7 +44,6 @@ import java.util.Set; | ||
| 44 | 44 | ||
| 45 | import javax.annotation.PostConstruct; | 45 | import javax.annotation.PostConstruct; |
| 46 | import javax.annotation.PreDestroy; | 46 | import javax.annotation.PreDestroy; |
| 47 | -import javax.enterprise.context.RequestScoped; | ||
| 48 | import javax.enterprise.inject.Default; | 47 | import javax.enterprise.inject.Default; |
| 49 | import javax.enterprise.inject.Produces; | 48 | import javax.enterprise.inject.Produces; |
| 50 | import javax.enterprise.inject.spi.InjectionPoint; | 49 | import javax.enterprise.inject.spi.InjectionPoint; |
| @@ -66,9 +65,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | @@ -66,9 +65,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 66 | * Factory class responsible to produces instances of EntityManager. Produces instances based on informations defined in | 65 | * Factory class responsible to produces instances of EntityManager. Produces instances based on informations defined in |
| 67 | * persistence.xml, demoiselle.properties or @PersistenceUnit annotation. | 66 | * persistence.xml, demoiselle.properties or @PersistenceUnit annotation. |
| 68 | * </p> | 67 | * </p> |
| 69 | - * TODO allow users to define EntityManager's scope using demoiselle.properties | ||
| 70 | */ | 68 | */ |
| 71 | -@RequestScoped | ||
| 72 | public class EntityManagerProducer implements Serializable { | 69 | public class EntityManagerProducer implements Serializable { |
| 73 | 70 | ||
| 74 | private static final long serialVersionUID = 1L; | 71 | private static final long serialVersionUID = 1L; |
impl/extension/jpa/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
0 → 100644
impl/extension/jpa/src/main/resources/demoiselle-jpa-bundle.properties
| @@ -43,4 +43,5 @@ more-than-one-persistence-unit-defined=Existe mais de uma unidade de persist\u00 | @@ -43,4 +43,5 @@ more-than-one-persistence-unit-defined=Existe mais de uma unidade de persist\u00 | ||
| 43 | persistence-unit-name-found=Unidade de persist\u00EAncia "{0}" encontrada. | 43 | persistence-unit-name-found=Unidade de persist\u00EAncia "{0}" encontrada. |
| 44 | entity-manager-closed=O gerenciador de entidades foi fechado. | 44 | entity-manager-closed=O gerenciador de entidades foi fechado. |
| 45 | no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. | 45 | no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. |
| 46 | -malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados. | 46 | +malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados. |
| 47 | +invalid-scope-for-entity-manager=O escopo especificado para o Entity Manager \u00E9 inv\u00E1lido. Por favor informe um dos escopos v\u00E1lidos para a propriedade frameworkdemoiselle.persistence.entitymanager.scope\: request, session, view, conversation, application |