[jetrix-cvs] SF.net SVN: jetrix:[774] jetrix/trunk
Brought to you by:
smanux
From: <sm...@us...> - 2008-11-11 23:12:24
|
Revision: 774 http://jetrix.svn.sourceforge.net/jetrix/?rev=774&view=rev Author: smanux Date: 2008-11-11 23:12:19 +0000 (Tue, 11 Nov 2008) Log Message: ----------- Implementation of the database connection Modified Paths: -------------- jetrix/trunk/build.xml jetrix/trunk/pom.xml jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java jetrix/trunk/src/admin/server.jsp jetrix/trunk/src/etc/tetrinet-server.dtd jetrix/trunk/src/java/net/jetrix/Server.java jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java Added Paths: ----------- jetrix/trunk/lib/commons-dbcp-1.2.2.jar jetrix/trunk/lib/commons-pool-1.4.jar jetrix/trunk/src/java/net/jetrix/DataSourceManager.java jetrix/trunk/src/java/net/jetrix/config/DataSourceConfig.java Modified: jetrix/trunk/build.xml =================================================================== --- jetrix/trunk/build.xml 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/build.xml 2008-11-11 23:12:19 UTC (rev 774) @@ -151,6 +151,8 @@ <antcall target="pack.lib" inheritAll="true"><param name="library" value="commons-digester"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="commons-beanutils"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="commons-lang-2.0-light"/></antcall> + <antcall target="pack.lib" inheritAll="true"><param name="library" value="commons-pool-1.4"/></antcall> + <antcall target="pack.lib" inheritAll="true"><param name="library" value="commons-dbcp-1.2.2"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="jetty"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="servlet-light"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="jasper-runtime"/></antcall> Added: jetrix/trunk/lib/commons-dbcp-1.2.2.jar =================================================================== (Binary files differ) Property changes on: jetrix/trunk/lib/commons-dbcp-1.2.2.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: jetrix/trunk/lib/commons-pool-1.4.jar =================================================================== (Binary files differ) Property changes on: jetrix/trunk/lib/commons-pool-1.4.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: jetrix/trunk/pom.xml =================================================================== --- jetrix/trunk/pom.xml 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/pom.xml 2008-11-11 23:12:19 UTC (rev 774) @@ -117,6 +117,18 @@ </dependency> <dependency> + <groupId>commons-pool</groupId> + <artifactId>commons-pool</artifactId> + <version>1.4</version> + </dependency> + + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.2.2</version> + </dependency> + + <dependency> <groupId>jetty</groupId> <artifactId>jetty</artifactId> <version>4.1-rc1</version> @@ -148,7 +160,7 @@ <directory>src/etc</directory> <targetPath>/</targetPath> <includes> - <include>*.xml</include> + <include>**/*.xml</include> <include>*.dtd</include> </includes> </testResource> Modified: jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java =================================================================== --- jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java 2008-11-11 23:12:19 UTC (rev 774) @@ -108,6 +108,19 @@ String pattern = request.getParameter("pattern"); Banlist.getInstance().unban(pattern); } + else if ("datasource.update".equals(action)) + { + DataSourceConfig datasourceConfig = new DataSourceConfig(); + datasourceConfig.setDriver(request.getParameter("driver")); + datasourceConfig.setUrl(request.getParameter("url")); + datasourceConfig.setUsername(request.getParameter("username")); + datasourceConfig.setPassword(request.getParameter("password")); + datasourceConfig.setMinIdle(Integer.parseInt(request.getParameter("minIdle"))); + datasourceConfig.setMaxActive(Integer.parseInt(request.getParameter("maxActive"))); + + config.setDataSource(datasourceConfig); + DataSourceManager.getInstance().setDataSource(datasourceConfig); + } else if ("gc".equals(action)) { System.gc(); Modified: jetrix/trunk/src/admin/server.jsp =================================================================== --- jetrix/trunk/src/admin/server.jsp 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/admin/server.jsp 2008-11-11 23:12:19 UTC (rev 774) @@ -40,8 +40,8 @@ <table class="thin" style="width: 600px"> <tr> - <td width="20%">Version</td> - <td width="80%"> + <th width="25%">Version</th> + <td width="75%"> <%= ServerConfig.VERSION %> <% if (VersionService.isNewVersionAvailable()) { %> <span style="color: red; padding-left: 2em">new version available (<%= VersionService.getLatestVersion() %>), <a href="http://jetrix.sourceforge.net">download it</a> now!</span> @@ -49,11 +49,11 @@ </td> </tr> <tr> - <td>Name</td> + <th>Name</th> <td><input class="thin" type="text" name="name" value="<%= conf.getName() != null ? conf.getName() : "" %>"></td> </tr> <tr> - <td>Host</td> + <th>Host</th> <td> <select name="host"> <option value="[ALL]">All Interfaces</option> @@ -79,28 +79,26 @@ </td> </tr> <tr> - <td>Max Players</td> + <th>Max Players</th> <td><input class="thin" type="text" name="maxPlayers" value="<%= conf.getMaxPlayers() %>"></td> </tr> <tr> - <td>Max Connections</td> + <th>Max Connections</th> <td><input class="thin" type="text" name="maxConnections" value="<%= conf.getMaxConnections() %>"></td> </tr> <tr> - <td>Operator Password</td> + <th>Operator Password</th> <td><input class="thin" type="text" name="opPassword" value="<%= conf.getOpPassword() != null ? conf.getOpPassword() : "" %>"></td> </tr> <tr> - <td>Administrator Password</td> + <th>Administrator Password</th> <td><input class="thin" type="text" name="adminPassword" value="<%= conf.getAdminPassword() != null ? conf.getAdminPassword() : "" %>"></td> </tr> <tr> - <td>Default Language</td> + <th>Default Language</th> <td> -<% Iterator locales = Language.getLocales().iterator(); %> <select class="thin" name="locale"> -<% while (locales.hasNext()) { - Locale locale = (Locale) locales.next(); %> +<% for (Locale locale : Language.getLocales()) { %> <option value="<%= locale.getLanguage() %>" <%= conf.getLocale().equals(locale) ? "selected" : "" %>> <%= locale.getLanguage().toUpperCase() %> - <%= locale.getDisplayLanguage() %> </option> @@ -109,7 +107,7 @@ </td> </tr> <tr> - <td>Status</td> + <th>Status</th> <td> <table> <tr> @@ -122,7 +120,7 @@ </td> </tr> <tr> - <td valign="top">Message of the day</td> + <th valign="top">Message of the day</th> <td><textarea class="thin" name="motd" cols="20" rows="5" style="width: 100%"><%= conf.getMessageOfTheDay() %></textarea></td> </tr> </table> @@ -391,21 +389,62 @@ <table class="thin" style="width: 400px"> <tr> - <td width="30%">Uptime</td> + <th width="35%">Uptime</th> <td><%= conf.getStatistics().getUpTime() %> day<%= conf.getStatistics().getUpTime() > 1 ? "s" : "" %></td> </tr> <tr> - <td>Number of Connections</td> + <th>Number of Connections</th> <td><%= conf.getStatistics().getConnectionCount() %></td> </tr> <tr> - <td>Games Played</td> + <th>Games Played</th> <td><%= conf.getStatistics().getGameCount() %></td> </tr> </table> </div> + <div class="tab-page" style="height: 400px"> + <h2 class="tab">Database</h2> + + <form id="datasource" action="/servlet/<%= ServerAction.class.getName() %>"> + <input type="hidden" name="action" value="datasource.update"> + + <table class="thin" style="width: 400px"> + <tr> + <th width="30%">Driver</th> + <td><input class="thin" type="text" name="driver" value="<%= conf.getDataSource().getDriver() %>" style="width: 98%"></td> + </tr> + <tr> + <th>URL</th> + <td><input class="thin" type="text" name="url" value="<%= conf.getDataSource().getUrl() %>" style="width: 98%"></td> + </tr> + <tr> + <th>Username</th> + <td><input class="thin" type="text" name="username" value="<%= conf.getDataSource().getUsername() %>"></td> + </tr> + <tr> + <th>Password</th> + <td><input class="thin" type="text" name="password" value="<%= conf.getDataSource().getPassword() %>"></td> + </tr> + <tr> + <th>Min Idle</th> + <td><input class="thin" type="text" name="minIdle" value="<%= conf.getDataSource().getMinIdle() %>"></td> + </tr> + <tr> + <th>Max Active</th> + <td><input class="thin" type="text" name="maxActive" value="<%= conf.getDataSource().getMaxActive() %>"></td> + </tr> + </table> + + <br> + + <input type="submit" value="Update"> + + </form> + </div> + + <div class="tab-page" style="height: 400px"> <h2 class="tab">System</h2> <% @@ -417,7 +456,7 @@ <table class="thin" style="width: 600px"> <tr> - <td width="30%">Java Virtual Machine</td> + <th width="30%">Java Virtual Machine</th> <td> <%= System.getProperty("java.vm.name") %> (build <%= System.getProperty("java.vm.version") %>, @@ -425,14 +464,14 @@ </td> </tr> <tr> - <td>Java Runtime Environment</td> + <th>Java Runtime Environment</th> <td> <%= System.getProperty("java.runtime.name") %> (build <%= System.getProperty("java.vm.version") %>) </td> </tr> <tr> - <td>Operating System</td> + <th>Operating System</th> <td> <%= System.getProperty("os.name") %> <%= System.getProperty("os.version") %> @@ -440,7 +479,7 @@ </td> </tr> <tr> - <td>Processor(s)</td> + <th>Processor(s)</th> <td><%= runtime.availableProcessors() %></td> </tr> </table> @@ -450,15 +489,15 @@ <table class="thin" style="width: 600px"> <tr> - <td width="30%">Total Memory</td> + <th width="30%">Total Memory</th> <td><%= df.format(runtime.totalMemory()/1024d/1024d) %> Mb</td> </tr> <tr> - <td>Max Memory</td> + <th>Max Memory</th> <td><%= df.format(runtime.maxMemory()/1024d/1024d) %> Mb</td> </tr> <tr> - <td>Free Memory</td> + <th>Free Memory</th> <td><%= df.format(runtime.freeMemory()/1024d/1024d) %> Mb</td> </tr> </table> Modified: jetrix/trunk/src/etc/tetrinet-server.dtd =================================================================== --- jetrix/trunk/src/etc/tetrinet-server.dtd 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/etc/tetrinet-server.dtd 2008-11-11 23:12:19 UTC (rev 774) @@ -67,8 +67,10 @@ <!ELEMENT nick (#PCDATA)> <!ELEMENT team (#PCDATA)> -<!ELEMENT datasource (driver, url, username, password?)> +<!ELEMENT datasource (driver, url, username, password?, min-idle?, max-active?)> <!ELEMENT driver (#PCDATA)> <!ELEMENT url (#PCDATA)> <!ELEMENT username (#PCDATA)> <!ELEMENT password (#PCDATA)> +<!ELEMENT min-idle (#PCDATA)> +<!ELEMENT max-active (#PCDATA)> Added: jetrix/trunk/src/java/net/jetrix/DataSourceManager.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/DataSourceManager.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/DataSourceManager.java 2008-11-11 23:12:19 UTC (rev 774) @@ -0,0 +1,128 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2008 Emmanuel Bourg + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix; + +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.sql.SQLException; +import javax.sql.DataSource; + +import org.apache.commons.dbcp.BasicDataSource; + +import net.jetrix.config.DataSourceConfig; + +/** + * Manage the connection pools to the databases. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + * @since 0.3 + */ +public final class DataSourceManager +{ + private Logger log = Logger.getLogger(getClass().getName()); + + private static DataSourceManager instance = new DataSourceManager(); + + private Map<String, DataSource> datasources = new HashMap<String, DataSource>(); + + /** Key of the default datasource. */ + private static final String DEFAULT_DATASOURCE = "DEFAULT"; + + private DataSourceManager() + { + } + + public static DataSourceManager getInstance() + { + return instance; + } + + /** + * Returns the default datasource. + */ + public DataSource getDataSource() + { + return getDataSource(DEFAULT_DATASOURCE); + } + + public DataSource getDataSource(String environnement) + { + return (DataSource) datasources.get(environnement); + } + + /** + * Configure the default datasource. + */ + public void setDataSource(DataSourceConfig config) + { + setDataSource(config, DEFAULT_DATASOURCE); + } + + /** + * Configure a datasource. + * + * @param config the configuration of the datasource + * @param environment the environment of the datasource + */ + public void setDataSource(DataSourceConfig config, String environment) + { + try + { + Class.forName(config.getDriver()); + } + catch (ClassNotFoundException e) + { + log.warning("Unable to find the database driver (" + config.getDriver() + "), put the related jar in the lib directory"); + return; + } + + try + { + // close the previous datasource if necessary + if (datasources.containsKey(environment)) + { + BasicDataSource datasource = (BasicDataSource) datasources.get(environment); + datasource.close(); + } + + BasicDataSource datasource = new BasicDataSource(); + datasource.setDefaultAutoCommit(false); + + datasource.setDriverClassName(config.getDriver()); + datasource.setUrl(config.getUrl()); + datasource.setUsername(config.getUsername()); + datasource.setPassword(config.getPassword()); + datasource.setMinIdle(config.getMinIdle() != 0 ? config.getMinIdle() : 1); + datasource.setMaxActive(config.getMaxActive() != 0 ? config.getMaxActive() : 50); + + // attempts to open the connection + datasource.getConnection().close(); + + datasources.put(environment, datasource); + } + catch (Exception e) + { + log.log(Level.SEVERE, "Unable to configure the datasource '" + environment + "'", e); + } + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/DataSourceManager.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Modified: jetrix/trunk/src/java/net/jetrix/Server.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Server.java 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/java/net/jetrix/Server.java 2008-11-11 23:12:19 UTC (rev 774) @@ -89,6 +89,13 @@ // prepare the loggers LogManager.init(); + + // open the database connection + if (config.getDataSource() != null) + { + log.info("Initializing the datasource..."); + DataSourceManager.getInstance().setDataSource(config.getDataSource()); + } // display the systray icon (windows only) SystrayManager.open(); Added: jetrix/trunk/src/java/net/jetrix/config/DataSourceConfig.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/DataSourceConfig.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/config/DataSourceConfig.java 2008-11-11 23:12:19 UTC (rev 774) @@ -0,0 +1,96 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2008 Emmanuel Bourg + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix.config; + +/** + * Configuration for a pool of connections to a database. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class DataSourceConfig +{ + private String url; + private String driver; + private String username; + private String password; + private int minIdle; + private int maxActive; + + public String getUrl() + { + return url; + } + + public void setUrl(String url) + { + this.url = url; + } + + public String getDriver() + { + return driver; + } + + public void setDriver(String driver) + { + this.driver = driver; + } + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } + + public int getMinIdle() + { + return minIdle; + } + + public void setMinIdle(int minIdle) + { + this.minIdle = minIdle; + } + + public int getMaxActive() + { + return maxActive; + } + + public void setMaxActive(int maxActive) + { + this.maxActive = maxActive; + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/config/DataSourceConfig.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Modified: jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java 2008-11-11 23:12:19 UTC (rev 774) @@ -67,11 +67,8 @@ private int status; private Statistics statistics; - // datasource parameters - private String dataSourceDriver; - private String dataSourceURL; - private String dataSourceUsername; - private String dataSourcePassword; + // datasource configuration + private DataSourceConfig datasourceConfig; private URL serverConfigURL; private URL channelsConfigURL; @@ -273,23 +270,47 @@ out.println(" </ban>"); out.println(); - out.println(" <!-- Database connection parameters -->"); - out.println(" <datasource>"); - out.println(" <!-- The class of the JDBC driver used -->"); - out.println(" <driver>" + dataSourceDriver + "</driver>"); - out.println(); - out.println(" <!-- The URL of the database (jdbc:<type>://<hostname>:<port>/<database>) -->"); - out.println(" <url>" + dataSourceURL + "</url>"); - out.println(); - out.println(" <!-- The username connecting to the database -->"); - out.println(" <username>" + dataSourceUsername + "</username>"); - out.println(); - out.println(" <!-- The password of the user -->"); - out.println(" <password>" + dataSourcePassword + "</password>"); - out.println(" </datasource>"); - out.println(); + if (datasourceConfig != null) + { + out.println(" <!-- Database connection parameters -->"); + out.println(" <datasource>"); + out.println(" <!-- The class of the JDBC driver used -->"); + out.println(" <driver>" + datasourceConfig.getDriver() + "</driver>"); + out.println(); + out.println(" <!-- The URL of the database (jdbc:<type>://<hostname>:<port>/<database>) -->"); + out.println(" <url>" + datasourceConfig.getUrl() + "</url>"); + out.println(); + out.println(" <!-- The username connecting to the database -->"); + out.println(" <username>" + datasourceConfig.getUsername() + "</username>"); + out.println(); + out.println(" <!-- The password of the user -->"); + if (datasourceConfig.getPassword() != null) + { + out.println(" <password>" + datasourceConfig.getPassword() + "</password>"); + } + else + { + out.println(" <password/>"); + } + if (datasourceConfig.getMinIdle() != 0) + { + out.println(); + out.println(" <!-- The minimum number of idle connections -->"); + out.println(" <min-idle>" + datasourceConfig.getMinIdle() + "</min-idle>"); + } + if (datasourceConfig.getMaxActive() != 0) + { + out.println(); + out.println(" <!-- The maximum number of active connections -->"); + out.println(" <max-active>" + datasourceConfig.getMaxActive() + "</max-active>"); + } + out.println(" </datasource>"); + out.println(); + } + out.println("</tetrinet-server>"); + out.flush(); out.close(); // save the channels.xml file @@ -911,65 +932,16 @@ /** * @since 0.3 */ - public String getDataSourceDriver() + public DataSourceConfig getDataSource() { - return dataSourceDriver; + return datasourceConfig; } /** * @since 0.3 */ - public void setDataSourceDriver(String driver) + public void setDataSource(DataSourceConfig datasourceConfig) { - this.dataSourceDriver = driver; + this.datasourceConfig = datasourceConfig; } - - /** - * @since 0.3 - */ - public String getDataSourceURL() - { - return dataSourceURL; - } - - /** - * @since 0.3 - */ - public void setDataSourceURL(String url) - { - this.dataSourceURL = url; - } - - /** - * @since 0.3 - */ - public String getDataSourceUsername() - { - return dataSourceUsername; - } - - /** - * @since 0.3 - */ - public void setDataSourceUsername(String username) - { - this.dataSourceUsername = username; - } - - /** - * @since 0.3 - */ - public String getDataSourcePassword() - { - return dataSourcePassword; - } - - /** - * @since 0.3 - */ - public void setDataSourcePassword(String password) - { - this.dataSourcePassword = password; - } - } Modified: jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java 2008-11-11 23:03:21 UTC (rev 773) +++ jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java 2008-11-11 23:12:19 UTC (rev 774) @@ -74,10 +74,14 @@ digester.addCallMethod("tetrinet-server/ban/host", "addBannedHost", 0); // datasource - digester.addCallMethod("*/datasource/driver", "setDataSourceDriver", 0); - digester.addCallMethod("*/datasource/url", "setDataSourceURL", 0); - digester.addCallMethod("*/datasource/username", "setDataSourceUsername", 0); - digester.addCallMethod("*/datasource/password", "setDataSourcePassword", 0); + digester.addObjectCreate("*/datasource", "net.jetrix.config.DataSourceConfig"); + digester.addSetNext("*/datasource", "setDataSource", "net.jetrix.config.DataSourceConfig"); + digester.addCallMethod("*/datasource/driver", "setDriver", 0); + digester.addCallMethod("*/datasource/url", "setUrl", 0); + digester.addCallMethod("*/datasource/username", "setUsername", 0); + digester.addCallMethod("*/datasource/password", "setPassword", 0); + digester.addCallMethod("*/datasource/min-idle", "setMinIdle", 0); + digester.addCallMethod("*/datasource/max-active", "setMaxActive", 0); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |