src/main/java/de/uapcore/lightpit/DatabaseFacade.java

Sun, 10 May 2020 10:58:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 10 May 2020 10:58:31 +0200
changeset 37
fecda0f466e6
parent 34
824d4042c857
child 38
cf85ef18f231
permissions
-rw-r--r--

adds data model for projects and versions

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

mercurial