Commit 2e92007aeb43686bc9dfe14f8bcb5c145a4d9968

Authored by Cleverson Sacramento
1 parent 6b7e32cb
Exists in master

Criação dos produtores de DataSource e Connection

impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/DataSourceConfig.java 0 → 100644
... ... @@ -0,0 +1,55 @@
  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.configuration;
  38 +
  39 +import java.io.Serializable;
  40 +
  41 +import br.gov.frameworkdemoiselle.annotation.Name;
  42 +import br.gov.frameworkdemoiselle.configuration.Configuration;
  43 +
  44 +@Configuration(prefix = "frameworkdemoiselle.jdbc.")
  45 +public class DataSourceConfig implements Serializable {
  46 +
  47 + private static final long serialVersionUID = 1L;
  48 +
  49 + @Name("jndi.name")
  50 + private String jndiName;
  51 +
  52 + public String getJndiName() {
  53 + return jndiName;
  54 + }
  55 +}
... ...
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ConnectionProducer.java 0 → 100644
... ... @@ -0,0 +1,66 @@
  1 +package br.gov.frameworkdemoiselle.internal.producer;
  2 +
  3 +import java.io.Serializable;
  4 +import java.sql.Connection;
  5 +
  6 +import javax.annotation.PreDestroy;
  7 +import javax.enterprise.context.RequestScoped;
  8 +import javax.enterprise.inject.Produces;
  9 +import javax.sql.DataSource;
  10 +
  11 +import br.gov.frameworkdemoiselle.configuration.ConfigurationException;
  12 +import br.gov.frameworkdemoiselle.util.Beans;
  13 +
  14 +@RequestScoped
  15 +public class ConnectionProducer implements Serializable {
  16 +
  17 + private static final long serialVersionUID = 1L;
  18 +
  19 + private transient Connection connection;
  20 +
  21 + @Produces
  22 + public Connection create() {
  23 + if (this.connection == null) {
  24 + this.connection = init();
  25 + }
  26 +
  27 + return this.connection;
  28 + }
  29 +
  30 + private Connection init() {
  31 + Connection result;
  32 +
  33 + try {
  34 + DataSource dataSource = Beans.getReference(DataSource.class);
  35 + result = dataSource.getConnection();
  36 +
  37 + } catch (Exception cause) {
  38 + // TODO Colocar uma mensagem amigável
  39 +
  40 + throw new ConfigurationException("", cause);
  41 + }
  42 +
  43 + return result;
  44 + }
  45 +
  46 + @PreDestroy
  47 + public void close() {
  48 + if (this.connection != null) {
  49 +
  50 + try {
  51 + if (this.connection.isClosed()) {
  52 + // TODO Logar um warning informando que a conexão já havia sido finalizada.
  53 +
  54 + } else {
  55 + this.connection.close();
  56 + // TODO Logar um info informando que a conexão foi finalizada.
  57 + }
  58 +
  59 + } catch (Exception cause) {
  60 + // TODO Colocar uma mensagem amigável
  61 +
  62 + throw new ConfigurationException("", cause);
  63 + }
  64 + }
  65 + }
  66 +}
... ...
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/producer/DataSourceProducer.java 0 → 100644
... ... @@ -0,0 +1,51 @@
  1 +package br.gov.frameworkdemoiselle.internal.producer;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +import javax.enterprise.context.ApplicationScoped;
  6 +import javax.enterprise.inject.Produces;
  7 +import javax.naming.Context;
  8 +import javax.naming.InitialContext;
  9 +import javax.sql.DataSource;
  10 +
  11 +import br.gov.frameworkdemoiselle.configuration.ConfigurationException;
  12 +import br.gov.frameworkdemoiselle.internal.configuration.DataSourceConfig;
  13 +import br.gov.frameworkdemoiselle.util.Beans;
  14 +
  15 +@ApplicationScoped
  16 +public class DataSourceProducer implements Serializable {
  17 +
  18 + private static final long serialVersionUID = 1L;
  19 +
  20 + private transient DataSource dataSource;
  21 +
  22 + @Produces
  23 + public DataSource create() {
  24 + if (this.dataSource == null) {
  25 + this.dataSource = init();
  26 + }
  27 +
  28 + return this.dataSource;
  29 + }
  30 +
  31 + private DataSource init() {
  32 + DataSource result;
  33 +
  34 + try {
  35 + DataSourceConfig config = Beans.getReference(DataSourceConfig.class);
  36 + String jndi = config.getJndiName();
  37 +
  38 + // TODO Lançar exceção caso o JNDI esteja vazio ou nulo.
  39 +
  40 + Context context = new InitialContext();
  41 + result = (DataSource) context.lookup(jndi);
  42 +
  43 + } catch (Exception cause) {
  44 + // TODO Colocar uma mensagem amigável
  45 +
  46 + throw new ConfigurationException("", cause);
  47 + }
  48 +
  49 + return result;
  50 + }
  51 +}
... ...