Create data manager as application-scoped bean on context startup.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 13:28:39 +0000 (15:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 13:28:39 +0000 (15:28 +0200)
pom.xml
src/main/java/net/pterodactylus/demoscenemusic/core/Core.java [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml.template

diff --git a/pom.xml b/pom.xml
index 4fa2e1c..62fab6c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <version>5.1.10</version>
                        <scope>provided</scope>
                </dependency>
+               <dependency>
+                       <groupId>javax.servlet</groupId>
+                       <artifactId>servlet-api</artifactId>
+                       <version>2.5</version>
+                       <scope>provided</scope>
+               </dependency>
        </dependencies>
        <build>
                <finalName>demoscene-music</finalName>
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/Core.java b/src/main/java/net/pterodactylus/demoscenemusic/core/Core.java
new file mode 100644 (file)
index 0000000..4ec90da
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * DemosceneMusic - Core.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.core;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.sql.DataSource;
+
+import net.pterodactylus.demoscenemusic.data.DataManager;
+import net.pterodactylus.util.database.AbstractDatabase;
+import net.pterodactylus.util.database.Database;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Core implements ServletContextListener {
+
+       private DataManager dataManager;
+
+       /**
+        * {@inheritDoc}
+        */
+       public void contextInitialized(ServletContextEvent servletContextEvent) {
+               Context context;
+               try {
+                       context = new InitialContext();
+                       DataSource dataSource = (DataSource) context.lookup("java:/comp/env/jdbc/demosceneMusic");
+                       Database database = AbstractDatabase.fromDataSource(dataSource);
+                       dataManager = new DataManager(database);
+                       servletContextEvent.getServletContext().setAttribute("DataManager", dataManager);
+               } catch (NamingException ne1) {
+                       servletContextEvent.getServletContext().log("Could not create database connection.", ne1);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void contextDestroyed(ServletContextEvent arg0) {
+               /* do nothing. */
+       }
+
+}
index 0a62e06..12eea53 100644 (file)
@@ -4,6 +4,11 @@
        <display-name>DemosceneMusic</display-name>
        <description>Database for all demoscene music</description>
 
+       <listener>
+               <description>Listener for context-related events</description>
+               <listener-class>net.pterodactylus.demoscenemusic.core.Core</listener-class>
+       </listener>
+
        <context-param>
                <description>The directory where the streaming files (MP3, Vorbis, etc.) are stored.</description>
                <param-name>storage.streamed</param-name>