diff -r 63a31871189e -r fd8c40ff78c3 src/main/java/de/uapcore/lightpit/DatabaseFacade.java
--- a/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 14:58:41 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 15:19:21 2020 +0200
@@ -28,10 +28,9 @@
*/
package de.uapcore.lightpit;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.SQLException;
-import java.util.Optional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@@ -40,31 +39,33 @@
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.util.Optional;
/**
* Provides access to different privilege layers within the database.
*/
@WebListener
public final class DatabaseFacade implements ServletContextListener {
-
+
private static final Logger LOG = LoggerFactory.getLogger(DatabaseFacade.class);
-
+
/**
* Timeout in seconds for the validation test.
*/
private static final int DB_TEST_TIMEOUT = 10;
-
- public static enum Dialect {
- Postgres;
+
+ public enum Dialect {
+ Postgres
}
-
+
/**
* The database dialect to use.
- *
+ *
* May be override by context parameter.
- *
+ *
* @see Constants#CTX_ATTR_DB_DIALECT
*/
private Dialect dialect = Dialect.Postgres;
@@ -82,10 +83,9 @@
* The attribute name in the Servlet context under which an instance of this class can be found.
*/
public static final String SC_ATTR_NAME = DatabaseFacade.class.getName();
- private ServletContext sc;
-
+
private static final String DS_JNDI_NAME = "jdbc/lightpit/app";
- private Optional dataSource;
+ private DataSource dataSource;
/**
* Returns the data source.
@@ -97,14 +97,15 @@
* @return a data source
*/
public Optional getDataSource() {
- return dataSource;
+ // TODO: this should not be an optional, if an empty optional is actually an exception
+ return Optional.ofNullable(dataSource);
}
public Dialect getSQLDialect() {
return dialect;
}
-
- private static void checkConnection(DataSource ds, String testSchema, String errMsg) {
+
+ private static void checkConnection(DataSource ds, String testSchema) {
try (Connection conn = ds.getConnection()) {
if (!conn.isValid(DB_TEST_TIMEOUT)) {
throw new SQLException("Validation check failed.");
@@ -118,25 +119,25 @@
DatabaseMetaData metaData = conn.getMetaData();
LOG.info("Connections as {} to {}/{} ready to go.", metaData.getUserName(), metaData.getURL(), conn.getSchema());
} catch (SQLException ex) {
- LOG.error(errMsg, ex);
+ LOG.error("Checking database connection failed", ex);
}
}
-
- private static Optional retrieveDataSource(Context ctx) {
+
+ private static DataSource retrieveDataSource(Context ctx) {
DataSource ret = null;
try {
- ret = (DataSource)ctx.lookup(DS_JNDI_NAME);
+ ret = (DataSource) ctx.lookup(DS_JNDI_NAME);
LOG.info("Data source retrieved.");
} catch (NamingException ex) {
LOG.error("Data source {} not available.", DS_JNDI_NAME);
LOG.error("Reason for the missing data source: ", ex);
}
- return Optional.ofNullable(ret);
+ return ret;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
- sc = sce.getServletContext();
+ ServletContext sc = sce.getServletContext();
dataSource = null;
@@ -159,10 +160,12 @@
LOG.debug("Trying to access JNDI context {}...", contextName);
Context initialCtx = new InitialContext();
Context ctx = (Context) initialCtx.lookup(contextName);
-
+
dataSource = retrieveDataSource(ctx);
-
- dataSource.ifPresent((ds) -> checkConnection(ds, dbSchema, "Checking database connection failed"));
+
+ if (dataSource != null) {
+ checkConnection(dataSource, dbSchema);
+ }
} catch (NamingException | ClassCastException ex) {
LOG.error("Cannot access JNDI resources.", ex);
}