Create data directory helper on load.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / core / Core.java
1 /*
2  * DemosceneMusic - Core.java - Copyright © 2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.core;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.NamingException;
23 import javax.servlet.ServletContextEvent;
24 import javax.servlet.ServletContextListener;
25 import javax.sql.DataSource;
26
27 import net.pterodactylus.demoscenemusic.data.DataManager;
28 import net.pterodactylus.util.database.AbstractDatabase;
29 import net.pterodactylus.util.database.Database;
30
31 import org.openid4java.consumer.ConsumerManager;
32
33 /**
34  * TODO
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class Core implements ServletContextListener {
39
40         private DataManager dataManager;
41
42         /** The data directory. */
43         private DataDirectory dataDirectory;
44
45         private final ConsumerManager consumerManager = new ConsumerManager();
46
47         public DataManager getDataManager() {
48                 return dataManager;
49         }
50
51         /**
52          * @return the consumerManager
53          */
54         public ConsumerManager getConsumerManager() {
55                 return consumerManager;
56         }
57
58         /**
59          * Returns the data directory helper.
60          *
61          * @return The data directory
62          */
63         public DataDirectory getDataDirectory() {
64                 return dataDirectory;
65         }
66
67         //
68         // SERVLETCONTEXTLISTENER METHODS
69         //
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public void contextInitialized(ServletContextEvent servletContextEvent) {
76                 servletContextEvent.getServletContext().setAttribute("core", this);
77                 dataDirectory = new DataDirectory(servletContextEvent.getServletContext().getInitParameter("data-directory"));
78                 try {
79                         Context context = new InitialContext();
80                         DataSource dataSource = (DataSource) context.lookup("java:/comp/env/jdbc/demosceneMusic");
81                         Database database = AbstractDatabase.fromDataSource(dataSource);
82                         dataManager = new DataManager(database);
83                         servletContextEvent.getServletContext().setAttribute("dataManager", dataManager);
84                 } catch (NamingException ne1) {
85                         servletContextEvent.getServletContext().log("Could not create database connection.", ne1);
86                 }
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         @Override
93         public void contextDestroyed(ServletContextEvent arg0) {
94                 /* do nothing. */
95         }
96
97 }