001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2008 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix;
021
022 import java.util.HashMap;
023 import java.util.Map;
024 import java.util.logging.Level;
025 import java.util.logging.Logger;
026 import javax.sql.DataSource;
027
028 import org.apache.commons.dbcp.BasicDataSource;
029
030 import net.jetrix.config.DataSourceConfig;
031
032 /**
033 * Manage the connection pools to the databases.
034 *
035 * @author Emmanuel Bourg
036 * @version $Revision: 797 $, $Date: 2009-02-18 15:03:17 +0100 (Wed, 18 Feb 2009) $
037 * @since 0.3
038 */
039 public final class DataSourceManager
040 {
041 private Logger log = Logger.getLogger(getClass().getName());
042
043 private static DataSourceManager instance = new DataSourceManager();
044
045 private Map<String, DataSource> datasources = new HashMap<String, DataSource>();
046
047 /** Key of the default datasource. */
048 public static final String DEFAULT_DATASOURCE = "DEFAULT";
049
050 /** Default number of the minimum idle connections */
051 public static final int DEFAULT_MIN_IDLE = 1;
052
053 /** Default number of maximum active connections */
054 public static final int DEFAULT_MAX_ACTIVE = 50;
055
056 private DataSourceManager()
057 {
058 }
059
060 public static DataSourceManager getInstance()
061 {
062 return instance;
063 }
064
065 /**
066 * Returns the default datasource.
067 */
068 public DataSource getDataSource()
069 {
070 return getDataSource(DEFAULT_DATASOURCE);
071 }
072
073 public DataSource getDataSource(String environnement)
074 {
075 return datasources.get(environnement);
076 }
077
078 /**
079 * Configure the default datasource.
080 */
081 public void setDataSource(DataSourceConfig config)
082 {
083 setDataSource(config, DEFAULT_DATASOURCE);
084 }
085
086 /**
087 * Configure a datasource.
088 *
089 * @param config the configuration of the datasource
090 * @param environment the environment of the datasource
091 */
092 public void setDataSource(DataSourceConfig config, String environment)
093 {
094 try
095 {
096 Class.forName(config.getDriver());
097 }
098 catch (ClassNotFoundException e)
099 {
100 log.warning("Unable to find the database driver (" + config.getDriver() + "), put the related jar in the lib directory");
101 return;
102 }
103
104 try
105 {
106 // close the previous datasource if necessary
107 if (datasources.containsKey(environment))
108 {
109 BasicDataSource datasource = (BasicDataSource) datasources.get(environment);
110 datasource.close();
111 }
112
113 BasicDataSource datasource = new BasicDataSource();
114 datasource.setDefaultAutoCommit(false);
115
116 datasource.setDriverClassName(config.getDriver());
117 datasource.setUrl(config.getUrl());
118 datasource.setUsername(config.getUsername());
119 datasource.setPassword(config.getPassword());
120 datasource.setMinIdle(config.getMinIdle() != 0 ? config.getMinIdle() : DEFAULT_MIN_IDLE);
121 datasource.setMaxActive(config.getMaxActive() != 0 ? config.getMaxActive() : DEFAULT_MAX_ACTIVE);
122
123 // attempts to open the connection
124 datasource.getConnection().close();
125
126 datasources.put(environment, datasource);
127 }
128 catch (Exception e)
129 {
130 log.log(Level.SEVERE, "Unable to configure the datasource '" + environment + "'", e);
131 }
132 }
133 }