26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 * |
27 * |
28 */ |
28 */ |
29 package de.uapcore.lightpit; |
29 package de.uapcore.lightpit; |
30 |
30 |
31 import de.uapcore.lightpit.dao.DataAccessObjects; |
|
32 import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects; |
|
33 import org.slf4j.Logger; |
31 import org.slf4j.Logger; |
34 import org.slf4j.LoggerFactory; |
32 import org.slf4j.LoggerFactory; |
35 |
33 |
36 import javax.naming.Context; |
34 import javax.naming.Context; |
37 import javax.naming.InitialContext; |
35 import javax.naming.InitialContext; |
89 */ |
87 */ |
90 public static final String SC_ATTR_NAME = DatabaseFacade.class.getName(); |
88 public static final String SC_ATTR_NAME = DatabaseFacade.class.getName(); |
91 |
89 |
92 private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; |
90 private static final String DS_JNDI_NAME = "jdbc/lightpit/app"; |
93 private DataSource dataSource; |
91 private DataSource dataSource; |
94 private DataAccessObjects dataAccessObjects; |
|
95 |
92 |
96 /** |
93 /** |
97 * Returns the data source. |
94 * Returns the data source. |
98 * <p> |
|
99 * The Optional returned should never be empty. However, if something goes |
|
100 * wrong during initialization, the data source might be absent. |
|
101 * Hence, users of this data source are forced to check the existence. |
|
102 * |
95 * |
103 * @return a data source |
96 * @return a data source |
104 */ |
97 */ |
105 public Optional<DataSource> getDataSource() { |
98 public DataSource getDataSource() { |
106 // TODO: this should not be an optional, if an empty optional is actually an exception |
99 return dataSource; |
107 return Optional.ofNullable(dataSource); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Returns the data access objects. |
|
112 * |
|
113 * @return an interface to obtain the data access objects |
|
114 */ |
|
115 public DataAccessObjects getDataAccessObjects() { |
|
116 return dataAccessObjects; |
|
117 } |
100 } |
118 |
101 |
119 public Dialect getSQLDialect() { |
102 public Dialect getSQLDialect() { |
120 return dialect; |
103 return dialect; |
121 } |
104 } |
169 } catch (IllegalArgumentException ex) { |
152 } catch (IllegalArgumentException ex) { |
170 LOG.error("Unknown or unsupported database dialect {}. Defaulting to {}.", dbDialect, dialect); |
153 LOG.error("Unknown or unsupported database dialect {}. Defaulting to {}.", dbDialect, dialect); |
171 } |
154 } |
172 } |
155 } |
173 |
156 |
174 dataAccessObjects = createDataAccessObjects(dialect); |
|
175 |
|
176 try { |
157 try { |
177 LOG.debug("Trying to access JNDI context {}...", contextName); |
158 LOG.debug("Trying to access JNDI context {}...", contextName); |
178 Context initialCtx = new InitialContext(); |
159 Context initialCtx = new InitialContext(); |
179 Context ctx = (Context) initialCtx.lookup(contextName); |
160 Context ctx = (Context) initialCtx.lookup(contextName); |
180 |
161 |
189 |
170 |
190 sc.setAttribute(SC_ATTR_NAME, this); |
171 sc.setAttribute(SC_ATTR_NAME, this); |
191 LOG.info("Database facade injected into ServletContext."); |
172 LOG.info("Database facade injected into ServletContext."); |
192 } |
173 } |
193 |
174 |
194 private static DataAccessObjects createDataAccessObjects(Dialect dialect) { |
|
195 switch (dialect) { |
|
196 case Postgres: |
|
197 return new PGDataAccessObjects(); |
|
198 default: |
|
199 throw new AssertionError("Non-exhaustive switch - this is a bug."); |
|
200 } |
|
201 } |
|
202 |
|
203 @Override |
175 @Override |
204 public void contextDestroyed(ServletContextEvent sce) { |
176 public void contextDestroyed(ServletContextEvent sce) { |
205 dataSource = null; |
177 dataSource = null; |
206 } |
178 } |
207 } |
179 } |