Commit 82f3bf451951266b15377cd78f9352cd57b0d6c2

Authored by Emerson Oliveira
1 parent b39acb64
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 classe utilitáiria que fornece métodos para extrair o nome do
Persistence Unit
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java 0 → 100644
... ... @@ -0,0 +1,99 @@
  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.producer;
  38 +
  39 +import java.util.Set;
  40 +
  41 +import javax.inject.Inject;
  42 +import javax.inject.Singleton;
  43 +
  44 +import org.slf4j.Logger;
  45 +
  46 +import br.gov.frameworkdemoiselle.DemoiselleException;
  47 +import br.gov.frameworkdemoiselle.annotation.Name;
  48 +import br.gov.frameworkdemoiselle.configuration.Configuration;
  49 +import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
  50 +import br.gov.frameworkdemoiselle.util.ResourceBundle;
  51 +
  52 +@Singleton
  53 +public class Persistences {
  54 +
  55 + @Inject
  56 + protected Logger logger;
  57 +
  58 + @Inject
  59 + @Name("demoiselle-jpa-bundle")
  60 + protected ResourceBundle bundle;
  61 +
  62 + @Inject
  63 + private EntityManagerFactoryProducer factory;
  64 +
  65 + /**
  66 + * Tries to get persistence unit name from demoiselle.properties.
  67 + *
  68 + * @param config
  69 + * Configuration containing persistence unit name.
  70 + * @return Persistence unit name.
  71 + */
  72 + protected String getFromProperties(EntityManagerConfig config) {
  73 + String persistenceUnit = config.getDefaultPersistenceUnitName();
  74 +
  75 + if (persistenceUnit != null) {
  76 + this.logger.debug(bundle.getString("getting-persistence-unit-from-properties",
  77 + Configuration.DEFAULT_RESOURCE));
  78 + }
  79 +
  80 + return persistenceUnit;
  81 + }
  82 +
  83 + /**
  84 + * Uses persistence.xml to get informations about which persistence unit to use. Throws DemoiselleException if more
  85 + * than one Persistence Unit is defined.
  86 + *
  87 + * @return Persistence Unit AmbiguousQualifier
  88 + */
  89 + protected String getFromXML() {
  90 + Set<String> persistenceUnits = factory.getCache().keySet();
  91 +
  92 + if (persistenceUnits.size() > 1) {
  93 + throw new DemoiselleException(bundle.getString("more-than-one-persistence-unit-defined",
  94 + Name.class.getSimpleName()));
  95 + } else {
  96 + return persistenceUnits.iterator().next();
  97 + }
  98 + }
  99 +}
... ...