Thu, 22 Oct 2020 13:06:46 +0200
fixes issue list not using the project's node
universe@16 | 1 | /* |
universe@16 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@34 | 3 | * |
universe@24 | 4 | * Copyright 2018 Mike Becker. All rights reserved. |
universe@34 | 5 | * |
universe@16 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@16 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@16 | 8 | * |
universe@16 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@16 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@16 | 11 | * |
universe@16 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@16 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@16 | 14 | * documentation and/or other materials provided with the distribution. |
universe@16 | 15 | * |
universe@16 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@16 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@16 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@16 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@16 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@16 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@16 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@16 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@16 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@16 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@16 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@34 | 27 | * |
universe@16 | 28 | */ |
universe@16 | 29 | package de.uapcore.lightpit; |
universe@16 | 30 | |
universe@33 | 31 | import org.slf4j.Logger; |
universe@33 | 32 | import org.slf4j.LoggerFactory; |
universe@33 | 33 | |
universe@16 | 34 | import javax.naming.Context; |
universe@16 | 35 | import javax.naming.InitialContext; |
universe@16 | 36 | import javax.naming.NamingException; |
universe@16 | 37 | import javax.servlet.ServletContext; |
universe@16 | 38 | import javax.servlet.ServletContextEvent; |
universe@16 | 39 | import javax.servlet.ServletContextListener; |
universe@16 | 40 | import javax.servlet.annotation.WebListener; |
universe@16 | 41 | import javax.sql.DataSource; |
universe@33 | 42 | import java.sql.Connection; |
universe@33 | 43 | import java.sql.DatabaseMetaData; |
universe@33 | 44 | import java.sql.SQLException; |
universe@33 | 45 | import java.util.Optional; |
universe@16 | 46 | |
universe@16 | 47 | /** |
universe@16 | 48 | * Provides access to different privilege layers within the database. |
universe@16 | 49 | */ |
universe@16 | 50 | @WebListener |
universe@16 | 51 | public final class DatabaseFacade implements ServletContextListener { |
universe@33 | 52 | |
universe@16 | 53 | private static final Logger LOG = LoggerFactory.getLogger(DatabaseFacade.class); |
universe@33 | 54 | |
universe@16 | 55 | /** |
universe@16 | 56 | * Timeout in seconds for the validation test. |
universe@16 | 57 | */ |
universe@16 | 58 | private static final int DB_TEST_TIMEOUT = 10; |
universe@33 | 59 | |
universe@34 | 60 | /** |
universe@34 | 61 | * Specifies the database dialect. |
universe@34 | 62 | */ |
universe@33 | 63 | public enum Dialect { |
universe@33 | 64 | Postgres |
universe@19 | 65 | } |
universe@33 | 66 | |
universe@19 | 67 | /** |
universe@19 | 68 | * The database dialect to use. |
universe@33 | 69 | * <p> |
universe@34 | 70 | * May be overridden by context parameter. |
universe@33 | 71 | * |
universe@19 | 72 | * @see Constants#CTX_ATTR_DB_DIALECT |
universe@19 | 73 | */ |
universe@19 | 74 | private Dialect dialect = Dialect.Postgres; |
universe@34 | 75 | |
universe@16 | 76 | /** |
universe@16 | 77 | * The default schema to test against when validating the connection. |
universe@34 | 78 | * <p> |
universe@16 | 79 | * May be overridden by context parameter. |
universe@34 | 80 | * |
universe@19 | 81 | * @see Constants#CTX_ATTR_DB_SCHEMA |
universe@16 | 82 | */ |
universe@16 | 83 | private static final String DB_DEFAULT_SCHEMA = "lightpit"; |
universe@34 | 84 | |
universe@16 | 85 | /** |
universe@19 | 86 | * The attribute name in the Servlet context under which an instance of this class can be found. |
universe@16 | 87 | */ |
universe@16 | 88 | public static final String SC_ATTR_NAME = DatabaseFacade.class.getName(); |
universe@33 | 89 | |
universe@19 | 90 | private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; |
universe@33 | 91 | private DataSource dataSource; |
universe@34 | 92 | |
universe@16 | 93 | /** |
universe@19 | 94 | * Returns the data source. |
universe@34 | 95 | * |
universe@19 | 96 | * @return a data source |
universe@16 | 97 | */ |
universe@38 | 98 | public DataSource getDataSource() { |
universe@38 | 99 | return dataSource; |
universe@34 | 100 | } |
universe@34 | 101 | |
universe@19 | 102 | public Dialect getSQLDialect() { |
universe@19 | 103 | return dialect; |
universe@16 | 104 | } |
universe@33 | 105 | |
universe@33 | 106 | private static void checkConnection(DataSource ds, String testSchema) { |
universe@16 | 107 | try (Connection conn = ds.getConnection()) { |
universe@16 | 108 | if (!conn.isValid(DB_TEST_TIMEOUT)) { |
universe@16 | 109 | throw new SQLException("Validation check failed."); |
universe@16 | 110 | } |
universe@16 | 111 | if (conn.isReadOnly()) { |
universe@16 | 112 | throw new SQLException("Connection is read-only and thus unusable."); |
universe@16 | 113 | } |
universe@16 | 114 | if (!conn.getSchema().equals(testSchema)) { |
universe@16 | 115 | throw new SQLException(String.format("Connection is not configured to use the schema %s.", testSchema)); |
universe@16 | 116 | } |
universe@16 | 117 | DatabaseMetaData metaData = conn.getMetaData(); |
universe@16 | 118 | LOG.info("Connections as {} to {}/{} ready to go.", metaData.getUserName(), metaData.getURL(), conn.getSchema()); |
universe@16 | 119 | } catch (SQLException ex) { |
universe@33 | 120 | LOG.error("Checking database connection failed", ex); |
universe@16 | 121 | } |
universe@16 | 122 | } |
universe@33 | 123 | |
universe@33 | 124 | private static DataSource retrieveDataSource(Context ctx) { |
universe@16 | 125 | DataSource ret = null; |
universe@16 | 126 | try { |
universe@33 | 127 | ret = (DataSource) ctx.lookup(DS_JNDI_NAME); |
universe@19 | 128 | LOG.info("Data source retrieved."); |
universe@16 | 129 | } catch (NamingException ex) { |
universe@19 | 130 | LOG.error("Data source {} not available.", DS_JNDI_NAME); |
universe@16 | 131 | LOG.error("Reason for the missing data source: ", ex); |
universe@16 | 132 | } |
universe@33 | 133 | return ret; |
universe@16 | 134 | } |
universe@16 | 135 | |
universe@16 | 136 | @Override |
universe@16 | 137 | public void contextInitialized(ServletContextEvent sce) { |
universe@33 | 138 | ServletContext sc = sce.getServletContext(); |
universe@34 | 139 | |
universe@19 | 140 | dataSource = null; |
universe@34 | 141 | |
universe@16 | 142 | final String dbSchema = Optional |
universe@16 | 143 | .ofNullable(sc.getInitParameter(Constants.CTX_ATTR_DB_SCHEMA)) |
universe@16 | 144 | .orElse(DB_DEFAULT_SCHEMA); |
universe@19 | 145 | final String dbDialect = sc.getInitParameter(Constants.CTX_ATTR_DB_DIALECT); |
universe@19 | 146 | if (dbDialect != null) { |
universe@19 | 147 | try { |
universe@19 | 148 | dialect = Dialect.valueOf(dbDialect); |
universe@19 | 149 | } catch (IllegalArgumentException ex) { |
universe@20 | 150 | LOG.error("Unknown or unsupported database dialect {}. Defaulting to {}.", dbDialect, dialect); |
universe@19 | 151 | } |
universe@19 | 152 | } |
universe@16 | 153 | |
universe@16 | 154 | try { |
universe@50 | 155 | LOG.debug("Trying to access JNDI context ..."); |
universe@16 | 156 | Context initialCtx = new InitialContext(); |
universe@50 | 157 | Context ctx = (Context) initialCtx.lookup("java:comp/env"); |
universe@33 | 158 | |
universe@19 | 159 | dataSource = retrieveDataSource(ctx); |
universe@33 | 160 | |
universe@33 | 161 | if (dataSource != null) { |
universe@33 | 162 | checkConnection(dataSource, dbSchema); |
universe@33 | 163 | } |
universe@16 | 164 | } catch (NamingException | ClassCastException ex) { |
universe@16 | 165 | LOG.error("Cannot access JNDI resources.", ex); |
universe@16 | 166 | } |
universe@34 | 167 | |
universe@16 | 168 | sc.setAttribute(SC_ATTR_NAME, this); |
universe@16 | 169 | LOG.info("Database facade injected into ServletContext."); |
universe@16 | 170 | } |
universe@16 | 171 | |
universe@16 | 172 | @Override |
universe@16 | 173 | public void contextDestroyed(ServletContextEvent sce) { |
universe@19 | 174 | dataSource = null; |
universe@34 | 175 | } |
universe@16 | 176 | } |