1.1 --- a/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 15:19:21 2020 +0200 1.2 +++ b/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 17:01:29 2020 +0200 1.3 @@ -1,8 +1,8 @@ 1.4 /* 1.5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 + * 1.8 * Copyright 2018 Mike Becker. All rights reserved. 1.9 - * 1.10 + * 1.11 * Redistribution and use in source and binary forms, with or without 1.12 * modification, are permitted provided that the following conditions are met: 1.13 * 1.14 @@ -24,10 +24,12 @@ 1.15 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.16 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.17 * POSSIBILITY OF SUCH DAMAGE. 1.18 - * 1.19 + * 1.20 */ 1.21 package de.uapcore.lightpit; 1.22 1.23 +import de.uapcore.lightpit.dao.DataAccessObjects; 1.24 +import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects; 1.25 import org.slf4j.Logger; 1.26 import org.slf4j.LoggerFactory; 1.27 1.28 @@ -57,6 +59,9 @@ 1.29 */ 1.30 private static final int DB_TEST_TIMEOUT = 10; 1.31 1.32 + /** 1.33 + * Specifies the database dialect. 1.34 + */ 1.35 public enum Dialect { 1.36 Postgres 1.37 } 1.38 @@ -64,21 +69,21 @@ 1.39 /** 1.40 * The database dialect to use. 1.41 * <p> 1.42 - * May be override by context parameter. 1.43 + * May be overridden by context parameter. 1.44 * 1.45 * @see Constants#CTX_ATTR_DB_DIALECT 1.46 */ 1.47 private Dialect dialect = Dialect.Postgres; 1.48 - 1.49 + 1.50 /** 1.51 * The default schema to test against when validating the connection. 1.52 - * 1.53 + * <p> 1.54 * May be overridden by context parameter. 1.55 - * 1.56 + * 1.57 * @see Constants#CTX_ATTR_DB_SCHEMA 1.58 */ 1.59 private static final String DB_DEFAULT_SCHEMA = "lightpit"; 1.60 - 1.61 + 1.62 /** 1.63 * The attribute name in the Servlet context under which an instance of this class can be found. 1.64 */ 1.65 @@ -86,21 +91,31 @@ 1.66 1.67 private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; 1.68 private DataSource dataSource; 1.69 - 1.70 + private DataAccessObjects dataAccessObjects; 1.71 + 1.72 /** 1.73 * Returns the data source. 1.74 - * 1.75 + * <p> 1.76 * The Optional returned should never be empty. However, if something goes 1.77 * wrong during initialization, the data source might be absent. 1.78 * Hence, users of this data source are forced to check the existence. 1.79 - * 1.80 + * 1.81 * @return a data source 1.82 */ 1.83 public Optional<DataSource> getDataSource() { 1.84 // TODO: this should not be an optional, if an empty optional is actually an exception 1.85 return Optional.ofNullable(dataSource); 1.86 } 1.87 - 1.88 + 1.89 + /** 1.90 + * Returns the data access objects. 1.91 + * 1.92 + * @return an interface to obtain the data access objects 1.93 + */ 1.94 + public DataAccessObjects getDataAccessObjects() { 1.95 + return dataAccessObjects; 1.96 + } 1.97 + 1.98 public Dialect getSQLDialect() { 1.99 return dialect; 1.100 } 1.101 @@ -138,9 +153,9 @@ 1.102 @Override 1.103 public void contextInitialized(ServletContextEvent sce) { 1.104 ServletContext sc = sce.getServletContext(); 1.105 - 1.106 + 1.107 dataSource = null; 1.108 - 1.109 + 1.110 final String contextName = Optional 1.111 .ofNullable(sc.getInitParameter(Constants.CTX_ATTR_JNDI_CONTEXT)) 1.112 .orElse("java:comp/env"); 1.113 @@ -156,6 +171,8 @@ 1.114 } 1.115 } 1.116 1.117 + dataAccessObjects = createDataAccessObjects(dialect); 1.118 + 1.119 try { 1.120 LOG.debug("Trying to access JNDI context {}...", contextName); 1.121 Context initialCtx = new InitialContext(); 1.122 @@ -169,13 +186,22 @@ 1.123 } catch (NamingException | ClassCastException ex) { 1.124 LOG.error("Cannot access JNDI resources.", ex); 1.125 } 1.126 - 1.127 + 1.128 sc.setAttribute(SC_ATTR_NAME, this); 1.129 LOG.info("Database facade injected into ServletContext."); 1.130 } 1.131 1.132 + private static DataAccessObjects createDataAccessObjects(Dialect dialect) { 1.133 + switch (dialect) { 1.134 + case Postgres: 1.135 + return new PGDataAccessObjects(); 1.136 + default: 1.137 + throw new AssertionError("Non-exhaustive switch - this is a bug."); 1.138 + } 1.139 + } 1.140 + 1.141 @Override 1.142 public void contextDestroyed(ServletContextEvent sce) { 1.143 dataSource = null; 1.144 - } 1.145 + } 1.146 }