Commit 8980d22f3445e1bd9d0f47782ee6d23ab18d8315

Authored by Emerson Oliveira
1 parent b485028f
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

Adição de métodos produtores, para possibilitar sua injeção
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java
  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 + */
1 37 package br.gov.frameworkdemoiselle.internal.producer;
2 38  
3 39 import java.io.Serializable;
... ... @@ -9,6 +45,9 @@ import java.util.Map;
9 45 import javax.annotation.PostConstruct;
10 46 import javax.annotation.PreDestroy;
11 47 import javax.enterprise.context.ApplicationScoped;
  48 +import javax.enterprise.inject.Default;
  49 +import javax.enterprise.inject.Produces;
  50 +import javax.enterprise.inject.spi.InjectionPoint;
12 51 import javax.inject.Inject;
13 52 import javax.persistence.EntityManagerFactory;
14 53 import javax.persistence.Persistence;
... ... @@ -23,6 +62,7 @@ import org.w3c.dom.NodeList;
23 62  
24 63 import br.gov.frameworkdemoiselle.DemoiselleException;
25 64 import br.gov.frameworkdemoiselle.annotation.Name;
  65 +import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
26 66 import br.gov.frameworkdemoiselle.util.ResourceBundle;
27 67  
28 68 @ApplicationScoped
... ... @@ -32,17 +72,39 @@ public class EntityManagerFactoryProducer implements Serializable {
32 72 private static final long serialVersionUID = 1L;
33 73  
34 74 private static final String ENTITY_MANAGER_RESOURCE = "META-INF/persistence.xml";
35   -
  75 +
36 76 @Inject
37   - private Logger logger;
  77 + protected Logger logger;
38 78  
39 79 @Inject
40 80 @Name("demoiselle-jpa-bundle")
41   - private ResourceBundle bundle;
  81 + protected ResourceBundle bundle;
  82 +
  83 + @Inject
  84 + private Persistences persistenceUnitReader;
42 85  
43 86 private final Map<ClassLoader, Map<String, EntityManagerFactory>> factoryCache = Collections
44 87 .synchronizedMap(new HashMap<ClassLoader, Map<String, EntityManagerFactory>>());
  88 +
  89 + @Default
  90 + @Produces
  91 + protected EntityManagerFactory createDefault(EntityManagerConfig config) {
  92 + String persistenceUnit = persistenceUnitReader.getFromProperties(config);
  93 +
  94 + if (persistenceUnit == null) {
  95 + persistenceUnit = persistenceUnitReader.getFromXML();
  96 + }
45 97  
  98 + return create(persistenceUnit);
  99 + }
  100 +
  101 + @Name("")
  102 + @Produces
  103 + protected EntityManagerFactory createNamed(InjectionPoint ip) {
  104 + String persistenceUnit = ip.getAnnotated().getAnnotation(Name.class).value();
  105 + return create(persistenceUnit);
  106 + }
  107 +
46 108 public EntityManagerFactory create(String persistenceUnit) {
47 109 EntityManagerFactory factory;
48 110  
... ...