src/main/kotlin/de/uapcore/lightpit/DataSourceProvider.kt

changeset 247
e71ae69c68c0
parent 184
e8eecee6aadf
child 254
55ca6cafc3dd
equal deleted inserted replaced
246:9a81a11be70e 247:e71ae69c68c0
36 36
37 /** 37 /**
38 * Provides access to the database. 38 * Provides access to the database.
39 */ 39 */
40 @WebListener 40 @WebListener
41 class DataSourceProvider : ServletContextListener, LoggingTrait { 41 class DataSourceProvider : ServletContextListener {
42
43 private val logger = MyLogger()
42 44
43 enum class Dialect { 45 enum class Dialect {
44 Postgres 46 Postgres
45 } 47 }
46 48
98 testSchema 100 testSchema
99 ) 101 )
100 ) 102 )
101 } 103 }
102 val metaData = conn.metaData 104 val metaData = conn.metaData
103 logger().info( 105 logger.info(
104 "Connections as {} to {}/{} ready to go.", 106 "Connections as {} to {}/{} ready to go.",
105 metaData.userName, 107 metaData.userName,
106 metaData.url, 108 metaData.url,
107 conn.schema 109 conn.schema
108 ) 110 )
109 } 111 }
110 } catch (ex: SQLException) { 112 } catch (ex: SQLException) {
111 logger().error("Checking database connection failed", ex) 113 logger.error("Checking database connection failed", ex)
112 } 114 }
113 } 115 }
114 116
115 private fun retrieveDataSource(ctx: Context): DataSource? { 117 private fun retrieveDataSource(ctx: Context): DataSource? {
116 return try { 118 return try {
117 val ret = ctx.lookup(DS_JNDI_NAME) as DataSource 119 val ret = ctx.lookup(DS_JNDI_NAME) as DataSource
118 logger().info("Data source retrieved.") 120 logger.info("Data source retrieved.")
119 ret 121 ret
120 } catch (ex: NamingException) { 122 } catch (ex: NamingException) {
121 logger().error("Data source {} not available.", DS_JNDI_NAME) 123 logger.error("Data source {0} not available.", DS_JNDI_NAME)
122 logger().error("Reason for the missing data source: ", ex) 124 logger.error("Reason for the missing data source: ", ex)
123 null 125 null
124 } 126 }
125 } 127 }
126 128
127 override fun contextInitialized(sce: ServletContextEvent?) { 129 override fun contextInitialized(sce: ServletContextEvent?) {
130 val dbSchema = sc.getInitParameter(Constants.CTX_ATTR_DB_SCHEMA) ?: DB_DEFAULT_SCHEMA 132 val dbSchema = sc.getInitParameter(Constants.CTX_ATTR_DB_SCHEMA) ?: DB_DEFAULT_SCHEMA
131 sc.getInitParameter(Constants.CTX_ATTR_DB_DIALECT)?.let { dbDialect -> 133 sc.getInitParameter(Constants.CTX_ATTR_DB_DIALECT)?.let { dbDialect ->
132 try { 134 try {
133 dialect = Dialect.valueOf(dbDialect) 135 dialect = Dialect.valueOf(dbDialect)
134 } catch (ex: IllegalArgumentException) { 136 } catch (ex: IllegalArgumentException) {
135 logger().error( 137 logger.error(
136 "Unknown or unsupported database dialect {}. Defaulting to {}.", 138 "Unknown or unsupported database dialect {0}. Defaulting to {1}.",
137 dbDialect, 139 dbDialect,
138 dialect 140 dialect
139 ) 141 )
140 } 142 }
141 } 143 }
142 144
143 dataSource = try { 145 dataSource = try {
144 logger().debug("Trying to access JNDI context ...") 146 logger.debug("Trying to access JNDI context ...")
145 val initialCtx: Context = InitialContext() 147 val initialCtx: Context = InitialContext()
146 val ctx = initialCtx.lookup("java:comp/env") as Context 148 val ctx = initialCtx.lookup("java:comp/env") as Context
147 retrieveDataSource(ctx) 149 retrieveDataSource(ctx)
148 } catch (ex: NamingException) { 150 } catch (ex: NamingException) {
149 logger().error("Cannot access JNDI resources.", ex) 151 logger.error("Cannot access JNDI resources.", ex)
150 null 152 null
151 } catch (ex: ClassCastException) { 153 } catch (ex: ClassCastException) {
152 logger().error("Cannot access JNDI resources.", ex) 154 logger.error("Cannot access JNDI resources.", ex)
153 null 155 null
154 } 156 }
155 157
156 dataSource?.let { checkConnection(it, dbSchema) } 158 dataSource?.let { checkConnection(it, dbSchema) }
157 159
158 sc.setAttribute(SC_ATTR_NAME, this) 160 sc.setAttribute(SC_ATTR_NAME, this)
159 logger().info("Database facade injected into ServletContext.") 161 logger.info("Database facade injected into ServletContext.")
160 } 162 }
161 163
162 override fun contextDestroyed(sce: ServletContextEvent?) { 164 override fun contextDestroyed(sce: ServletContextEvent?) {
163 dataSource = null 165 dataSource = null
164 } 166 }

mercurial