diff -r fd8c40ff78c3 -r 824d4042c857 src/main/java/de/uapcore/lightpit/DatabaseFacade.java --- a/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 15:19:21 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/DatabaseFacade.java Sat May 09 17:01:29 2020 +0200 @@ -1,8 +1,8 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * + * * Copyright 2018 Mike Becker. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * @@ -24,10 +24,12 @@ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. - * + * */ package de.uapcore.lightpit; +import de.uapcore.lightpit.dao.DataAccessObjects; +import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +59,9 @@ */ private static final int DB_TEST_TIMEOUT = 10; + /** + * Specifies the database dialect. + */ public enum Dialect { Postgres } @@ -64,21 +69,21 @@ /** * The database dialect to use. *

- * May be override by context parameter. + * May be overridden by context parameter. * * @see Constants#CTX_ATTR_DB_DIALECT */ private Dialect dialect = Dialect.Postgres; - + /** * The default schema to test against when validating the connection. - * + *

* May be overridden by context parameter. - * + * * @see Constants#CTX_ATTR_DB_SCHEMA */ private static final String DB_DEFAULT_SCHEMA = "lightpit"; - + /** * The attribute name in the Servlet context under which an instance of this class can be found. */ @@ -86,21 +91,31 @@ private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; private DataSource dataSource; - + private DataAccessObjects dataAccessObjects; + /** * Returns the data source. - * + *

* The Optional returned should never be empty. However, if something goes * wrong during initialization, the data source might be absent. * Hence, users of this data source are forced to check the existence. - * + * * @return a data source */ public Optional getDataSource() { // TODO: this should not be an optional, if an empty optional is actually an exception return Optional.ofNullable(dataSource); } - + + /** + * Returns the data access objects. + * + * @return an interface to obtain the data access objects + */ + public DataAccessObjects getDataAccessObjects() { + return dataAccessObjects; + } + public Dialect getSQLDialect() { return dialect; } @@ -138,9 +153,9 @@ @Override public void contextInitialized(ServletContextEvent sce) { ServletContext sc = sce.getServletContext(); - + dataSource = null; - + final String contextName = Optional .ofNullable(sc.getInitParameter(Constants.CTX_ATTR_JNDI_CONTEXT)) .orElse("java:comp/env"); @@ -156,6 +171,8 @@ } } + dataAccessObjects = createDataAccessObjects(dialect); + try { LOG.debug("Trying to access JNDI context {}...", contextName); Context initialCtx = new InitialContext(); @@ -169,13 +186,22 @@ } catch (NamingException | ClassCastException ex) { LOG.error("Cannot access JNDI resources.", ex); } - + sc.setAttribute(SC_ATTR_NAME, this); LOG.info("Database facade injected into ServletContext."); } + private static DataAccessObjects createDataAccessObjects(Dialect dialect) { + switch (dialect) { + case Postgres: + return new PGDataAccessObjects(); + default: + throw new AssertionError("Non-exhaustive switch - this is a bug."); + } + } + @Override public void contextDestroyed(ServletContextEvent sce) { dataSource = null; - } + } }