1.1 --- a/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 14:58:41 2020 +0200 1.2 +++ b/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 15:19:21 2020 +0200 1.3 @@ -28,10 +28,9 @@ 1.4 */ 1.5 package de.uapcore.lightpit; 1.6 1.7 -import java.sql.Connection; 1.8 -import java.sql.DatabaseMetaData; 1.9 -import java.sql.SQLException; 1.10 -import java.util.Optional; 1.11 +import org.slf4j.Logger; 1.12 +import org.slf4j.LoggerFactory; 1.13 + 1.14 import javax.naming.Context; 1.15 import javax.naming.InitialContext; 1.16 import javax.naming.NamingException; 1.17 @@ -40,31 +39,33 @@ 1.18 import javax.servlet.ServletContextListener; 1.19 import javax.servlet.annotation.WebListener; 1.20 import javax.sql.DataSource; 1.21 -import org.slf4j.Logger; 1.22 -import org.slf4j.LoggerFactory; 1.23 +import java.sql.Connection; 1.24 +import java.sql.DatabaseMetaData; 1.25 +import java.sql.SQLException; 1.26 +import java.util.Optional; 1.27 1.28 /** 1.29 * Provides access to different privilege layers within the database. 1.30 */ 1.31 @WebListener 1.32 public final class DatabaseFacade implements ServletContextListener { 1.33 - 1.34 + 1.35 private static final Logger LOG = LoggerFactory.getLogger(DatabaseFacade.class); 1.36 - 1.37 + 1.38 /** 1.39 * Timeout in seconds for the validation test. 1.40 */ 1.41 private static final int DB_TEST_TIMEOUT = 10; 1.42 - 1.43 - public static enum Dialect { 1.44 - Postgres; 1.45 + 1.46 + public enum Dialect { 1.47 + Postgres 1.48 } 1.49 - 1.50 + 1.51 /** 1.52 * The database dialect to use. 1.53 - * 1.54 + * <p> 1.55 * May be override by context parameter. 1.56 - * 1.57 + * 1.58 * @see Constants#CTX_ATTR_DB_DIALECT 1.59 */ 1.60 private Dialect dialect = Dialect.Postgres; 1.61 @@ -82,10 +83,9 @@ 1.62 * The attribute name in the Servlet context under which an instance of this class can be found. 1.63 */ 1.64 public static final String SC_ATTR_NAME = DatabaseFacade.class.getName(); 1.65 - private ServletContext sc; 1.66 - 1.67 + 1.68 private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; 1.69 - private Optional<DataSource> dataSource; 1.70 + private DataSource dataSource; 1.71 1.72 /** 1.73 * Returns the data source. 1.74 @@ -97,14 +97,15 @@ 1.75 * @return a data source 1.76 */ 1.77 public Optional<DataSource> getDataSource() { 1.78 - return dataSource; 1.79 + // TODO: this should not be an optional, if an empty optional is actually an exception 1.80 + return Optional.ofNullable(dataSource); 1.81 } 1.82 1.83 public Dialect getSQLDialect() { 1.84 return dialect; 1.85 } 1.86 - 1.87 - private static void checkConnection(DataSource ds, String testSchema, String errMsg) { 1.88 + 1.89 + private static void checkConnection(DataSource ds, String testSchema) { 1.90 try (Connection conn = ds.getConnection()) { 1.91 if (!conn.isValid(DB_TEST_TIMEOUT)) { 1.92 throw new SQLException("Validation check failed."); 1.93 @@ -118,25 +119,25 @@ 1.94 DatabaseMetaData metaData = conn.getMetaData(); 1.95 LOG.info("Connections as {} to {}/{} ready to go.", metaData.getUserName(), metaData.getURL(), conn.getSchema()); 1.96 } catch (SQLException ex) { 1.97 - LOG.error(errMsg, ex); 1.98 + LOG.error("Checking database connection failed", ex); 1.99 } 1.100 } 1.101 - 1.102 - private static Optional<DataSource> retrieveDataSource(Context ctx) { 1.103 + 1.104 + private static DataSource retrieveDataSource(Context ctx) { 1.105 DataSource ret = null; 1.106 try { 1.107 - ret = (DataSource)ctx.lookup(DS_JNDI_NAME); 1.108 + ret = (DataSource) ctx.lookup(DS_JNDI_NAME); 1.109 LOG.info("Data source retrieved."); 1.110 } catch (NamingException ex) { 1.111 LOG.error("Data source {} not available.", DS_JNDI_NAME); 1.112 LOG.error("Reason for the missing data source: ", ex); 1.113 } 1.114 - return Optional.ofNullable(ret); 1.115 + return ret; 1.116 } 1.117 1.118 @Override 1.119 public void contextInitialized(ServletContextEvent sce) { 1.120 - sc = sce.getServletContext(); 1.121 + ServletContext sc = sce.getServletContext(); 1.122 1.123 dataSource = null; 1.124 1.125 @@ -159,10 +160,12 @@ 1.126 LOG.debug("Trying to access JNDI context {}...", contextName); 1.127 Context initialCtx = new InitialContext(); 1.128 Context ctx = (Context) initialCtx.lookup(contextName); 1.129 - 1.130 + 1.131 dataSource = retrieveDataSource(ctx); 1.132 - 1.133 - dataSource.ifPresent((ds) -> checkConnection(ds, dbSchema, "Checking database connection failed")); 1.134 + 1.135 + if (dataSource != null) { 1.136 + checkConnection(dataSource, dbSchema); 1.137 + } 1.138 } catch (NamingException | ClassCastException ex) { 1.139 LOG.error("Cannot access JNDI resources.", ex); 1.140 }