From c2f0d1538e3af4569723742144296caf291d4989 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 18 Apr 2012 15:28:39 +0200 Subject: [PATCH] Create data manager as application-scoped bean on context startup. --- pom.xml | 6 +++ .../pterodactylus/demoscenemusic/core/Core.java | 63 ++++++++++++++++++++++ src/main/webapp/WEB-INF/web.xml.template | 5 ++ 3 files changed, 74 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/core/Core.java diff --git a/pom.xml b/pom.xml index 4fa2e1c..62fab6c 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,12 @@ 5.1.10 provided + + javax.servlet + servlet-api + 2.5 + provided + demoscene-music 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 index 0000000..4ec90da --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/Core.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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. */ + } + +} diff --git a/src/main/webapp/WEB-INF/web.xml.template b/src/main/webapp/WEB-INF/web.xml.template index 0a62e06..12eea53 100644 --- a/src/main/webapp/WEB-INF/web.xml.template +++ b/src/main/webapp/WEB-INF/web.xml.template @@ -4,6 +4,11 @@ DemosceneMusic Database for all demoscene music + + Listener for context-related events + net.pterodactylus.demoscenemusic.core.Core + + The directory where the streaming files (MP3, Vorbis, etc.) are stored. storage.streamed -- 2.7.4