Commit fd4ec7ad2029f7ced930181e4da1b88f42facab4
1 parent
f0b5d303
Exists in
master
Implementando escopo de entity manager configuravel
Showing
1 changed file
with
127 additions
and
93 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/EntityManagerBootstrap.java
| @@ -2,6 +2,8 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | @@ -2,6 +2,8 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
| 2 | 2 | ||
| 3 | import java.lang.annotation.Annotation; | 3 | import java.lang.annotation.Annotation; |
| 4 | import java.lang.reflect.Type; | 4 | import java.lang.reflect.Type; |
| 5 | +import java.net.URL; | ||
| 6 | +import java.util.Locale; | ||
| 5 | import java.util.Set; | 7 | import java.util.Set; |
| 6 | 8 | ||
| 7 | import javax.enterprise.context.ApplicationScoped; | 9 | import javax.enterprise.context.ApplicationScoped; |
| @@ -16,153 +18,185 @@ import javax.enterprise.inject.spi.AnnotatedType; | @@ -16,153 +18,185 @@ import javax.enterprise.inject.spi.AnnotatedType; | ||
| 16 | import javax.enterprise.inject.spi.BeanManager; | 18 | import javax.enterprise.inject.spi.BeanManager; |
| 17 | import javax.enterprise.inject.spi.Extension; | 19 | import javax.enterprise.inject.spi.Extension; |
| 18 | import javax.enterprise.inject.spi.ProcessAnnotatedType; | 20 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 21 | +import javax.enterprise.inject.spi.ProcessBean; | ||
| 19 | import javax.enterprise.util.AnnotationLiteral; | 22 | import javax.enterprise.util.AnnotationLiteral; |
| 20 | 23 | ||
| 24 | +import org.apache.commons.configuration.PropertiesConfiguration; | ||
| 25 | +import org.slf4j.Logger; | ||
| 26 | + | ||
| 27 | +import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 21 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 28 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 29 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | ||
| 22 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; | 30 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; |
| 23 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; | 31 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; |
| 24 | -import br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader; | ||
| 25 | import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; | 32 | import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; |
| 26 | - | 33 | +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; |
| 34 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 27 | 35 | ||
| 28 | public class EntityManagerBootstrap implements Extension { | 36 | public class EntityManagerBootstrap implements Extension { |
| 37 | + | ||
| 38 | + private Logger logger; | ||
| 39 | + | ||
| 40 | + private transient ResourceBundle bundle; | ||
| 29 | 41 | ||
| 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(); | 42 | + private EntityManagerScope selectedScope; |
| 39 | 43 | ||
| 44 | + public void replaceAnnotatedType(@Observes final ProcessAnnotatedType<EntityManagerProducer> event , BeanManager manager){ | ||
| 45 | + if (event.getAnnotatedType().getJavaClass().equals(EntityManagerProducer.class)){ | ||
| 46 | + AnnotatedType<EntityManagerProducer> wrapper = new AnnotatedType<EntityManagerProducer>() { | ||
| 47 | + | ||
| 48 | + private final AnnotatedType<EntityManagerProducer> delegate = event.getAnnotatedType(); | ||
| 49 | + | ||
| 40 | public Class<EntityManagerProducer> getJavaClass() { | 50 | public Class<EntityManagerProducer> getJavaClass() { |
| 41 | return delegate.getJavaClass(); | 51 | return delegate.getJavaClass(); |
| 42 | } | 52 | } |
| 43 | - | 53 | + |
| 44 | public Type getBaseType() { | 54 | public Type getBaseType() { |
| 45 | return delegate.getBaseType(); | 55 | return delegate.getBaseType(); |
| 46 | } | 56 | } |
| 47 | - | 57 | + |
| 48 | public Set<AnnotatedConstructor<EntityManagerProducer>> getConstructors() { | 58 | public Set<AnnotatedConstructor<EntityManagerProducer>> getConstructors() { |
| 49 | return delegate.getConstructors(); | 59 | return delegate.getConstructors(); |
| 50 | } | 60 | } |
| 51 | - | 61 | + |
| 52 | public Set<Type> getTypeClosure() { | 62 | public Set<Type> getTypeClosure() { |
| 53 | return delegate.getTypeClosure(); | 63 | return delegate.getTypeClosure(); |
| 54 | } | 64 | } |
| 55 | - | 65 | + |
| 56 | public Set<AnnotatedMethod<? super EntityManagerProducer>> getMethods() { | 66 | public Set<AnnotatedMethod<? super EntityManagerProducer>> getMethods() { |
| 57 | return delegate.getMethods(); | 67 | return delegate.getMethods(); |
| 58 | } | 68 | } |
| 59 | - | 69 | + |
| 60 | @SuppressWarnings("unchecked") | 70 | @SuppressWarnings("unchecked") |
| 61 | public <T extends Annotation> T getAnnotation(Class<T> annotationType) { | 71 | public <T extends Annotation> T getAnnotation(Class<T> annotationType) { |
| 62 | - T returnedAnnotation = null; | ||
| 63 | - Class<?> expectedScope; | ||
| 64 | - | ||
| 65 | - switch(entityManagerScope){ | 72 | + T returnedAnnotation; |
| 73 | + switch(getConfiguredEntityManagerScope()){ | ||
| 66 | case APPLICATION: | 74 | case APPLICATION: |
| 67 | - expectedScope = ApplicationScoped.class; | ||
| 68 | - break; | 75 | + returnedAnnotation = (T) (annotationType.equals(ApplicationScoped.class) ? new AnnotationLiteral<ApplicationScoped>() { |
| 76 | + private static final long serialVersionUID = 1L; | ||
| 77 | + } : delegate.getAnnotation(annotationType)); | ||
| 69 | case CONVERSATION: | 78 | case CONVERSATION: |
| 70 | - expectedScope = ConversationScoped.class; | ||
| 71 | - break; | 79 | + returnedAnnotation = (T) (annotationType.equals(ConversationScoped.class) ? new AnnotationLiteral<ApplicationScoped>() { |
| 80 | + private static final long serialVersionUID = 1L; | ||
| 81 | + } : delegate.getAnnotation(annotationType)); | ||
| 82 | + case NOSCOPE: | ||
| 83 | + returnedAnnotation = delegate.getAnnotation(annotationType); | ||
| 72 | case REQUEST: | 84 | case REQUEST: |
| 73 | - expectedScope = RequestScoped.class; | ||
| 74 | - break; | 85 | + returnedAnnotation = (T) (annotationType.equals(RequestScoped.class) ? new AnnotationLiteral<ApplicationScoped>() { |
| 86 | + private static final long serialVersionUID = 1L; | ||
| 87 | + } : delegate.getAnnotation(annotationType)); | ||
| 75 | case SESSION: | 88 | case SESSION: |
| 76 | - expectedScope = SessionScoped.class; | ||
| 77 | - break; | 89 | + returnedAnnotation = (T) (annotationType.equals(SessionScoped.class) ? new AnnotationLiteral<ApplicationScoped>() { |
| 90 | + private static final long serialVersionUID = 1L; | ||
| 91 | + } : delegate.getAnnotation(annotationType)); | ||
| 78 | case VIEW: | 92 | case VIEW: |
| 79 | - expectedScope = ViewScoped.class; | ||
| 80 | - break; | 93 | + returnedAnnotation = (T) (annotationType.equals(ViewScoped.class) ? new AnnotationLiteral<ApplicationScoped>() { |
| 94 | + private static final long serialVersionUID = 1L; | ||
| 95 | + } : delegate.getAnnotation(annotationType)); | ||
| 81 | default: | 96 | 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); | 97 | + returnedAnnotation = delegate.getAnnotation(annotationType); |
| 110 | } | 98 | } |
| 111 | 99 | ||
| 112 | return returnedAnnotation; | 100 | return returnedAnnotation; |
| 113 | } | 101 | } |
| 114 | - | 102 | + |
| 115 | public Set<AnnotatedField<? super EntityManagerProducer>> getFields() { | 103 | public Set<AnnotatedField<? super EntityManagerProducer>> getFields() { |
| 116 | return delegate.getFields(); | 104 | return delegate.getFields(); |
| 117 | } | 105 | } |
| 118 | - | 106 | + |
| 119 | public Set<Annotation> getAnnotations() { | 107 | public Set<Annotation> getAnnotations() { |
| 120 | return delegate.getAnnotations(); | 108 | return delegate.getAnnotations(); |
| 121 | } | 109 | } |
| 122 | - | 110 | + |
| 123 | public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { | 111 | public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { |
| 124 | - return delegate.isAnnotationPresent(annotationType); | 112 | + switch(getConfiguredEntityManagerScope()){ |
| 113 | + case APPLICATION: | ||
| 114 | + return annotationType.equals(ApplicationScoped.class) ? true : delegate.isAnnotationPresent(annotationType); | ||
| 115 | + case CONVERSATION: | ||
| 116 | + return annotationType.equals(ConversationScoped.class) ? true : delegate.isAnnotationPresent(annotationType); | ||
| 117 | + case NOSCOPE: | ||
| 118 | + return delegate.isAnnotationPresent(annotationType); | ||
| 119 | + case REQUEST: | ||
| 120 | + return annotationType.equals(RequestScoped.class) ? true : delegate.isAnnotationPresent(annotationType); | ||
| 121 | + case SESSION: | ||
| 122 | + return annotationType.equals(SessionScoped.class) ? true : delegate.isAnnotationPresent(annotationType); | ||
| 123 | + case VIEW: | ||
| 124 | + return annotationType.equals(ViewScoped.class) ? true : delegate.isAnnotationPresent(annotationType); | ||
| 125 | + default: | ||
| 126 | + return delegate.isAnnotationPresent(annotationType); | ||
| 127 | + } | ||
| 125 | } | 128 | } |
| 126 | - | ||
| 127 | - | ||
| 128 | }; | 129 | }; |
| 129 | 130 | ||
| 130 | - event.setAnnotatedType(annotatedType); | 131 | + event.setAnnotatedType(wrapper); |
| 131 | } | 132 | } |
| 132 | } | 133 | } |
| 133 | 134 | ||
| 134 | - @SuppressWarnings("all") | ||
| 135 | - class ApplicationScopedLiteral extends AnnotationLiteral<ApplicationScoped> implements ApplicationScoped { | ||
| 136 | - private static final long serialVersionUID = 1L; | ||
| 137 | - | ||
| 138 | - private ApplicationScopedLiteral() {} | 135 | + public void configureBean(@Observes ProcessBean<EntityManagerProducer> event , BeanManager manager){ |
| 136 | + Class<? extends Annotation> beanScope = event.getBean().getScope(); | ||
| 137 | + System.out.println(beanScope.toString()); | ||
| 139 | } | 138 | } |
| 140 | 139 | ||
| 141 | - @SuppressWarnings("all") | ||
| 142 | - class RequestScopedLiteral extends AnnotationLiteral<RequestScoped> implements RequestScoped { | ||
| 143 | - private static final long serialVersionUID = 1L; | ||
| 144 | - | ||
| 145 | - private RequestScopedLiteral(){} | ||
| 146 | - } | 140 | + private EntityManagerScope getConfiguredEntityManagerScope() { |
| 141 | + if (selectedScope==null){ | ||
| 142 | + EntityManagerScope entityManagerScope = null; | ||
| 143 | + URL configURL = getClass().getResource("demoiselle.properties"); | ||
| 147 | 144 | ||
| 148 | - @SuppressWarnings("all") | ||
| 149 | - class SessionScopedLiteral extends AnnotationLiteral<SessionScoped> implements SessionScoped { | ||
| 150 | - private static final long serialVersionUID = 1L; | ||
| 151 | - | ||
| 152 | - private SessionScopedLiteral(){} | ||
| 153 | - } | 145 | + if (configURL != null) { |
| 146 | + try { | ||
| 147 | + org.apache.commons.configuration.Configuration config = new PropertiesConfiguration( | ||
| 148 | + configURL); | ||
| 149 | + Configuration configAnnotation = EntityManagerConfig.class | ||
| 150 | + .getAnnotation(Configuration.class); | ||
| 151 | + Name nameAnnotation = EntityManagerConfig.class.getDeclaredField("entityManagerScope") | ||
| 152 | + .getAnnotation(Name.class); | ||
| 154 | 153 | ||
| 155 | - @SuppressWarnings("all") | ||
| 156 | - class ViewScopedLiteral extends AnnotationLiteral<ViewScoped> implements ViewScoped { | ||
| 157 | - private static final long serialVersionUID = 1L; | ||
| 158 | - | ||
| 159 | - private ViewScopedLiteral(){} | ||
| 160 | - } | 154 | + String prefix = configAnnotation.prefix(); |
| 155 | + String sufix = nameAnnotation.value(); | ||
| 156 | + | ||
| 157 | + String property = prefix.endsWith(".") ? prefix + sufix : prefix + "." + sufix; | ||
| 158 | + | ||
| 159 | + String scopeValue = config.getString(property, EntityManagerScope.REQUEST.name()) | ||
| 160 | + .toUpperCase(); | ||
| 161 | + | ||
| 162 | + for (EntityManagerScope currentScope : EntityManagerScope.values()) { | ||
| 163 | + if (currentScope.name().equals(scopeValue)) { | ||
| 164 | + entityManagerScope = currentScope; | ||
| 165 | + break; | ||
| 166 | + } | ||
| 167 | + } | ||
| 161 | 168 | ||
| 162 | - @SuppressWarnings("all") | ||
| 163 | - class ConversationScopedLiteral extends AnnotationLiteral<ConversationScoped> implements ConversationScoped { | ||
| 164 | - private static final long serialVersionUID = 1L; | ||
| 165 | - | ||
| 166 | - private ConversationScopedLiteral(){} | 169 | + if (entityManagerScope == null) { |
| 170 | + entityManagerScope = EntityManagerScope.REQUEST; | ||
| 171 | + } | ||
| 172 | + } catch (Exception e) { | ||
| 173 | + getLogger().debug(getBundle().getString("")); | ||
| 174 | + entityManagerScope = EntityManagerScope.REQUEST; | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + else{ | ||
| 178 | + entityManagerScope = EntityManagerScope.REQUEST; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + this.selectedScope = entityManagerScope; | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + return selectedScope; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + private Logger getLogger() { | ||
| 188 | + if (logger == null) { | ||
| 189 | + logger = LoggerProducer.create(EntityManagerBootstrap.class); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + return logger; | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + private ResourceBundle getBundle() { | ||
| 196 | + if (bundle == null) { | ||
| 197 | + bundle = new ResourceBundle("demoiselle-jpa-bundle.properties", Locale.getDefault()); | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + return bundle; | ||
| 167 | } | 201 | } |
| 168 | } | 202 | } |