Mon, 21 Dec 2020 18:29:34 +0100
major refactoring of DAO architecture - also fixes #114
/* * Copyright 2020 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package de.uapcore.lightpit import java.sql.SQLException import javax.naming.Context import javax.naming.InitialContext import javax.naming.NamingException import javax.servlet.ServletContextEvent import javax.servlet.ServletContextListener import javax.servlet.annotation.WebListener import javax.sql.DataSource /** * Provides access to the database. */ @WebListener class DataSourceProvider : ServletContextListener, LoggingTrait { enum class Dialect { Postgres } /** * The database dialect to use. * May be overridden by context parameter. * * @see Constants.CTX_ATTR_DB_DIALECT */ var dialect = Dialect.Postgres; private set /** * The data source, if available. */ var dataSource: DataSource? = null companion object { /** * The attribute name in the Servlet context under which an instance of this class can be found. */ val SC_ATTR_NAME = "lightpit.service.DataSourceProvider" /** * Timeout in seconds for the validation test. */ private val DB_TEST_TIMEOUT = 10 /** * The default schema to test against when validating the connection. * May be overridden by context parameter. * * @see Constants.CTX_ATTR_DB_SCHEMA */ private val DB_DEFAULT_SCHEMA = "lightpit" /** * The JNDI resource name for the data source. */ private val DS_JNDI_NAME = "jdbc/lightpit/app" } private fun checkConnection(ds: DataSource, testSchema: String) { try { ds.connection.use { conn -> if (!conn.isValid(DB_TEST_TIMEOUT)) { throw SQLException("Validation check failed.") } if (conn.isReadOnly) { throw SQLException("Connection is read-only and thus unusable.") } if (conn.schema != testSchema) { throw SQLException( String.format( "Connection is not configured to use the schema %s.", testSchema ) ) } val metaData = conn.metaData logger().info( "Connections as {} to {}/{} ready to go.", metaData.userName, metaData.url, conn.schema ) } } catch (ex: SQLException) { logger().error("Checking database connection failed", ex) } } private fun retrieveDataSource(ctx: Context): DataSource? { return try { val ret = ctx.lookup(DS_JNDI_NAME) as DataSource logger().info("Data source retrieved.") ret } catch (ex: NamingException) { logger().error("Data source {} not available.", DS_JNDI_NAME) logger().error("Reason for the missing data source: ", ex) null } } override fun contextInitialized(sce: ServletContextEvent?) { val sc = sce!!.servletContext val dbSchema = sc.getInitParameter(Constants.CTX_ATTR_DB_SCHEMA) ?: DB_DEFAULT_SCHEMA sc.getInitParameter(Constants.CTX_ATTR_DB_DIALECT)?.let { dbDialect -> try { dialect = Dialect.valueOf(dbDialect) } catch (ex: IllegalArgumentException) { logger().error( "Unknown or unsupported database dialect {}. Defaulting to {}.", dbDialect, dialect ) } } dataSource = try { logger().debug("Trying to access JNDI context ...") val initialCtx: Context = InitialContext() val ctx = initialCtx.lookup("java:comp/env") as Context retrieveDataSource(ctx) } catch (ex: NamingException) { logger().error("Cannot access JNDI resources.", ex) null } catch (ex: ClassCastException) { logger().error("Cannot access JNDI resources.", ex) null } dataSource?.let { checkConnection(it, dbSchema) } sc.setAttribute(SC_ATTR_NAME, this) logger().info("Database facade injected into ServletContext.") } override fun contextDestroyed(sce: ServletContextEvent?) { dataSource = null } }