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