Commit b485028f7b5c5b1270eefea6a72101d9b4164f28
1 parent
82f3bf45
Exists in
master
IN PROGRESS - issue FWK-166: Possibilidade do usuário injetar
EntityManagerFactory em sua aplicação https://demoiselle.atlassian.net/browse/FWK-166 Refatoração da classe, que agora utliza métodos da classe Persistences para extrair nomes de unidades de persistência, e exclusão de parâmetros desnecessários nos métodos produtores
Showing
1 changed file
with
17 additions
and
67 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerProducer.java
... | ... | @@ -38,7 +38,6 @@ package br.gov.frameworkdemoiselle.internal.producer; |
38 | 38 | |
39 | 39 | import java.io.Serializable; |
40 | 40 | import java.util.Map; |
41 | -import java.util.Set; | |
42 | 41 | |
43 | 42 | import javax.enterprise.context.ApplicationScoped; |
44 | 43 | import javax.enterprise.inject.Default; |
... | ... | @@ -48,14 +47,9 @@ import javax.enterprise.inject.spi.InjectionPoint; |
48 | 47 | import javax.inject.Inject; |
49 | 48 | import javax.persistence.EntityManager; |
50 | 49 | |
51 | -import org.slf4j.Logger; | |
52 | - | |
53 | -import br.gov.frameworkdemoiselle.DemoiselleException; | |
54 | 50 | import br.gov.frameworkdemoiselle.annotation.Name; |
55 | -import br.gov.frameworkdemoiselle.configuration.Configuration; | |
56 | 51 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; |
57 | 52 | import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; |
58 | -import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
59 | 53 | |
60 | 54 | /** |
61 | 55 | * <p> |
... | ... | @@ -64,26 +58,19 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; |
64 | 58 | * </p> |
65 | 59 | */ |
66 | 60 | @ApplicationScoped |
67 | -public class EntityManagerProducer implements Serializable{ | |
61 | +public class EntityManagerProducer implements Serializable { | |
68 | 62 | |
69 | 63 | private static final long serialVersionUID = 1L; |
70 | 64 | |
71 | 65 | @Inject |
72 | - private Logger logger; | |
73 | - | |
74 | - @Inject | |
75 | - @Name("demoiselle-jpa-bundle") | |
76 | - private ResourceBundle bundle; | |
77 | - | |
78 | - @Inject | |
79 | - private EntityManagerFactoryProducer factory; | |
80 | - | |
81 | - @Inject | |
82 | 66 | private Instance<EntityManagerStore> storeInstance; |
83 | - | |
67 | + | |
84 | 68 | @Inject |
85 | 69 | private EntityManagerConfig configuration; |
86 | - | |
70 | + | |
71 | + @Inject | |
72 | + private Persistences persistenceUnitReader; | |
73 | + | |
87 | 74 | /** |
88 | 75 | * <p> |
89 | 76 | * Default EntityManager factory. Tries two strategies to produces EntityManager instances. |
... | ... | @@ -98,80 +85,43 @@ public class EntityManagerProducer implements Serializable{ |
98 | 85 | */ |
99 | 86 | @Default |
100 | 87 | @Produces |
101 | - protected EntityManager createDefault(InjectionPoint ip, EntityManagerConfig config) { | |
102 | - String persistenceUnit = getFromProperties(config); | |
88 | + protected EntityManager createDefault(EntityManagerConfig config) { | |
89 | + String persistenceUnit = persistenceUnitReader.getFromProperties(config); | |
103 | 90 | |
104 | 91 | if (persistenceUnit == null) { |
105 | - persistenceUnit = getFromXML(); | |
92 | + persistenceUnit = persistenceUnitReader.getFromXML(); | |
106 | 93 | } |
107 | 94 | |
108 | 95 | return new EntityManagerProxy(persistenceUnit); |
109 | 96 | } |
110 | 97 | |
111 | 98 | /** |
112 | - * | |
113 | 99 | * <p> |
114 | - * Factory that reads the {@link AmbiguousQualifier} qualifier and creates an entity manager with | |
115 | - * a matching persistence unit name. | |
100 | + * Factory that reads the {@link AmbiguousQualifier} qualifier and creates an entity manager with a matching | |
101 | + * persistence unit name. | |
116 | 102 | * </p> |
117 | 103 | * |
118 | - * | |
119 | 104 | * @param config |
120 | 105 | * Suplies informations about EntityManager defined in properties file. |
121 | 106 | * @return Produced EntityManager. |
122 | 107 | */ |
123 | 108 | @Name("") |
124 | 109 | @Produces |
125 | - protected EntityManager createNamed(InjectionPoint ip, EntityManagerConfig config) { | |
110 | + protected EntityManager createNamed(InjectionPoint ip) { | |
126 | 111 | String persistenceUnit = ip.getAnnotated().getAnnotation(Name.class).value(); |
127 | 112 | return new EntityManagerProxy(persistenceUnit); |
128 | 113 | } |
129 | 114 | |
130 | - /** | |
131 | - * Tries to get persistence unit name from demoiselle.properties. | |
132 | - * | |
133 | - * @param config | |
134 | - * Configuration containing persistence unit name. | |
135 | - * @return Persistence unit name. | |
136 | - */ | |
137 | - private String getFromProperties(EntityManagerConfig config) { | |
138 | - String persistenceUnit = config.getDefaultPersistenceUnitName(); | |
139 | - | |
140 | - if (persistenceUnit != null) { | |
141 | - this.logger.debug(bundle.getString("getting-persistence-unit-from-properties", | |
142 | - Configuration.DEFAULT_RESOURCE)); | |
143 | - } | |
144 | - | |
145 | - return persistenceUnit; | |
146 | - } | |
147 | - | |
148 | - /** | |
149 | - * Uses persistence.xml to get informations about which persistence unit to use. Throws DemoiselleException if more | |
150 | - * than one Persistence Unit is defined. | |
151 | - * | |
152 | - * @return Persistence Unit AmbiguousQualifier | |
153 | - */ | |
154 | - private String getFromXML() { | |
155 | - Set<String> persistenceUnits = factory.getCache().keySet(); | |
156 | - | |
157 | - if (persistenceUnits.size() > 1) { | |
158 | - throw new DemoiselleException(bundle.getString("more-than-one-persistence-unit-defined", | |
159 | - Name.class.getSimpleName())); | |
160 | - } else { | |
161 | - return persistenceUnits.iterator().next(); | |
162 | - } | |
163 | - } | |
164 | - | |
165 | 115 | public EntityManager getEntityManager(String persistenceUnit) { |
166 | 116 | return getStore().getEntityManager(persistenceUnit); |
167 | 117 | } |
168 | - | |
118 | + | |
169 | 119 | public Map<String, EntityManager> getCache() { |
170 | 120 | return getStore().getCache(); |
171 | 121 | } |
172 | - | |
173 | - private EntityManagerStore getStore(){ | |
174 | - switch(configuration.getEntityManagerScope()){ | |
122 | + | |
123 | + private EntityManagerStore getStore() { | |
124 | + switch (configuration.getEntityManagerScope()) { | |
175 | 125 | case REQUEST: |
176 | 126 | return storeInstance.select(RequestEntityManagerStore.class).get(); |
177 | 127 | case APPLICATION: |
... | ... | @@ -188,5 +138,5 @@ public class EntityManagerProducer implements Serializable{ |
188 | 138 | return storeInstance.select(RequestEntityManagerStore.class).get(); |
189 | 139 | } |
190 | 140 | } |
191 | - | |
141 | + | |
192 | 142 | } | ... | ... |