Sat, 17 Aug 2024 12:08:34 +0200
release version 1.3.0
151 | 1 | /* |
180 | 2 | * Copyright 2021 Mike Becker. All rights reserved. |
151 | 3 | * |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions are met: | |
6 | * | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
20 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
21 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
22 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | package de.uapcore.lightpit | |
27 | ||
254
55ca6cafc3dd
#233 migrate to Jakarta EE and update dependencies
Mike Becker <universe@uap-core.de>
parents:
247
diff
changeset
|
28 | import jakarta.servlet.ServletContextEvent |
55ca6cafc3dd
#233 migrate to Jakarta EE and update dependencies
Mike Becker <universe@uap-core.de>
parents:
247
diff
changeset
|
29 | import jakarta.servlet.ServletContextListener |
55ca6cafc3dd
#233 migrate to Jakarta EE and update dependencies
Mike Becker <universe@uap-core.de>
parents:
247
diff
changeset
|
30 | import jakarta.servlet.annotation.WebListener |
151 | 31 | import java.sql.SQLException |
32 | import javax.naming.Context | |
33 | import javax.naming.InitialContext | |
34 | import javax.naming.NamingException | |
35 | import javax.sql.DataSource | |
36 | ||
37 | /** | |
38 | * Provides access to the database. | |
39 | */ | |
40 | @WebListener | |
247 | 41 | class DataSourceProvider : ServletContextListener { |
42 | ||
43 | private val logger = MyLogger() | |
151 | 44 | |
158
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
45 | enum class Dialect { |
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
46 | Postgres |
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
47 | } |
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
48 | |
151 | 49 | /** |
50 | * The database dialect to use. | |
51 | * May be overridden by context parameter. | |
52 | * | |
53 | * @see Constants.CTX_ATTR_DB_DIALECT | |
54 | */ | |
158
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
55 | var dialect = Dialect.Postgres; private set |
151 | 56 | |
57 | /** | |
58 | * The data source, if available. | |
59 | */ | |
60 | var dataSource: DataSource? = null | |
61 | ||
62 | companion object { | |
63 | /** | |
64 | * The attribute name in the Servlet context under which an instance of this class can be found. | |
65 | */ | |
184
e8eecee6aadf
completes kotlin migration
Mike Becker <universe@uap-core.de>
parents:
180
diff
changeset
|
66 | const val SC_ATTR_NAME = "lightpit.service.DataSourceProvider" |
151 | 67 | |
68 | /** | |
69 | * Timeout in seconds for the validation test. | |
70 | */ | |
184
e8eecee6aadf
completes kotlin migration
Mike Becker <universe@uap-core.de>
parents:
180
diff
changeset
|
71 | private const val DB_TEST_TIMEOUT = 10 |
151 | 72 | |
73 | /** | |
74 | * The default schema to test against when validating the connection. | |
75 | * May be overridden by context parameter. | |
76 | * | |
77 | * @see Constants.CTX_ATTR_DB_SCHEMA | |
78 | */ | |
184
e8eecee6aadf
completes kotlin migration
Mike Becker <universe@uap-core.de>
parents:
180
diff
changeset
|
79 | private const val DB_DEFAULT_SCHEMA = "lightpit" |
151 | 80 | |
81 | /** | |
82 | * The JNDI resource name for the data source. | |
83 | */ | |
184
e8eecee6aadf
completes kotlin migration
Mike Becker <universe@uap-core.de>
parents:
180
diff
changeset
|
84 | private const val DS_JNDI_NAME = "jdbc/lightpit/app" |
151 | 85 | } |
86 | ||
87 | private fun checkConnection(ds: DataSource, testSchema: String) { | |
88 | try { | |
89 | ds.connection.use { conn -> | |
90 | if (!conn.isValid(DB_TEST_TIMEOUT)) { | |
91 | throw SQLException("Validation check failed.") | |
92 | } | |
93 | if (conn.isReadOnly) { | |
94 | throw SQLException("Connection is read-only and thus unusable.") | |
95 | } | |
96 | if (conn.schema != testSchema) { | |
97 | throw SQLException( | |
98 | String.format( | |
99 | "Connection is not configured to use the schema %s.", | |
100 | testSchema | |
101 | ) | |
102 | ) | |
103 | } | |
104 | val metaData = conn.metaData | |
247 | 105 | logger.info( |
310
bbf4eb9a71f8
fix regression after log string format change
Mike Becker <universe@uap-core.de>
parents:
254
diff
changeset
|
106 | "Connections as {0} to {1}/{2} ready to go.", |
151 | 107 | metaData.userName, |
108 | metaData.url, | |
109 | conn.schema | |
110 | ) | |
111 | } | |
112 | } catch (ex: SQLException) { | |
247 | 113 | logger.error("Checking database connection failed", ex) |
151 | 114 | } |
115 | } | |
116 | ||
117 | private fun retrieveDataSource(ctx: Context): DataSource? { | |
118 | return try { | |
119 | val ret = ctx.lookup(DS_JNDI_NAME) as DataSource | |
247 | 120 | logger.info("Data source retrieved.") |
151 | 121 | ret |
122 | } catch (ex: NamingException) { | |
247 | 123 | logger.error("Data source {0} not available.", DS_JNDI_NAME) |
124 | logger.error("Reason for the missing data source: ", ex) | |
151 | 125 | null |
126 | } | |
127 | } | |
128 | ||
129 | override fun contextInitialized(sce: ServletContextEvent?) { | |
130 | val sc = sce!!.servletContext | |
131 | ||
132 | val dbSchema = sc.getInitParameter(Constants.CTX_ATTR_DB_SCHEMA) ?: DB_DEFAULT_SCHEMA | |
167
3f30adba1c63
major refactoring of DAO architecture - also fixes #114
Mike Becker <universe@uap-core.de>
parents:
158
diff
changeset
|
133 | sc.getInitParameter(Constants.CTX_ATTR_DB_DIALECT)?.let { dbDialect -> |
151 | 134 | try { |
158
4f912cd42876
migrates constants and removes global functions
Mike Becker <universe@uap-core.de>
parents:
151
diff
changeset
|
135 | dialect = Dialect.valueOf(dbDialect) |
151 | 136 | } catch (ex: IllegalArgumentException) { |
247 | 137 | logger.error( |
138 | "Unknown or unsupported database dialect {0}. Defaulting to {1}.", | |
151 | 139 | dbDialect, |
140 | dialect | |
141 | ) | |
142 | } | |
143 | } | |
144 | ||
145 | dataSource = try { | |
247 | 146 | logger.debug("Trying to access JNDI context ...") |
151 | 147 | val initialCtx: Context = InitialContext() |
148 | val ctx = initialCtx.lookup("java:comp/env") as Context | |
149 | retrieveDataSource(ctx) | |
150 | } catch (ex: NamingException) { | |
247 | 151 | logger.error("Cannot access JNDI resources.", ex) |
151 | 152 | null |
153 | } catch (ex: ClassCastException) { | |
247 | 154 | logger.error("Cannot access JNDI resources.", ex) |
151 | 155 | null |
156 | } | |
157 | ||
158 | dataSource?.let { checkConnection(it, dbSchema) } | |
159 | ||
160 | sc.setAttribute(SC_ATTR_NAME, this) | |
247 | 161 | logger.info("Database facade injected into ServletContext.") |
151 | 162 | } |
163 | ||
164 | override fun contextDestroyed(sce: ServletContextEvent?) { | |
165 | dataSource = null | |
166 | } | |
167 | } |