src/org/sonews/util/DatabaseSetup.java
author cli
Wed, 14 Sep 2011 12:54:40 +0200
changeset 60 39f1fadf50a0
parent 44 5d7d1adf387f
permissions -rwxr-xr-x
Add libcommons-codec-java to Debian dependencies.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
package org.sonews.util;
chris@1
    19
chris@1
    20
import java.io.BufferedReader;
chris@1
    21
import java.io.InputStreamReader;
chris@1
    22
import java.sql.Connection;
chris@1
    23
import java.sql.DriverManager;
cli@44
    24
import java.sql.SQLException;
chris@1
    25
import java.sql.Statement;
chris@1
    26
import java.util.HashMap;
chris@1
    27
import java.util.Map;
chris@1
    28
import org.sonews.util.io.Resource;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * Database setup utility class.
chris@1
    32
 * @author Christian Lins
chris@1
    33
 * @since sonews/0.5.0
chris@1
    34
 */
cli@44
    35
public final class DatabaseSetup {
chris@1
    36
cli@37
    37
	private static final Map<String, String> templateMap = new HashMap<String, String>();
cli@37
    38
	private static final Map<String, StringTemplate> urlMap = new HashMap<String, StringTemplate>();
cli@37
    39
	private static final Map<String, String> driverMap = new HashMap<String, String>();
chris@1
    40
cli@37
    41
	static {
cli@37
    42
		templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
cli@37
    43
		templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
cli@44
    44
		templateMap.put("3", "helpers/database_hsqldb_tmpl.sql");
chris@1
    45
cli@37
    46
		urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
cli@37
    47
		urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
cli@37
    48
cli@37
    49
		driverMap.put("1", "com.mysql.jdbc.Driver");
cli@37
    50
		driverMap.put("2", "org.postgresql.Driver");
cli@44
    51
		driverMap.put("3", "org.hsqldb.jdbcDriver");
cli@37
    52
	}
cli@37
    53
cli@37
    54
	public static void main(String[] args)
cli@44
    55
			throws Exception {
cli@44
    56
		
cli@44
    57
		loadJDBCDriver();
cli@37
    58
cli@44
    59
		if (args.length == 0) {
cli@44
    60
			System.out.println("sonews Database setup helper");
cli@44
    61
			System.out.println("This program will create a initial database table structure");
cli@44
    62
			System.out.println("for the sonews Newsserver.");
cli@44
    63
			System.out.println("You need to create a database and a db user manually before!");
cli@37
    64
cli@44
    65
			System.out.println("Select DBMS type:");
cli@44
    66
			System.out.println("[1] MySQL 5.x or higher");
cli@44
    67
			System.out.println("[2] PostgreSQL 8.x or higher");
cli@44
    68
			System.out.print("Your choice: ");
cli@44
    69
cli@44
    70
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
cli@44
    71
			String dbmsType = in.readLine();
cli@44
    72
			String tmplName = templateMap.get(dbmsType);
cli@44
    73
			if (tmplName == null) {
cli@44
    74
				System.err.println("Invalid choice. Try again you fool!");
cli@44
    75
				main(args);
cli@44
    76
				return;
cli@44
    77
			}
cli@44
    78
cli@44
    79
			String tmpl = Resource.getAsString(tmplName, true);
cli@44
    80
cli@44
    81
			System.out.print("Database server hostname (e.g. localhost): ");
cli@44
    82
			String dbHostname = in.readLine();
cli@44
    83
cli@44
    84
			System.out.print("Database name: ");
cli@44
    85
			String dbName = in.readLine();
cli@44
    86
cli@44
    87
			System.out.print("Give name of DB user that can create tables: ");
cli@44
    88
			String dbUser = in.readLine();
cli@44
    89
cli@44
    90
			System.out.print("Password: ");
cli@44
    91
			String dbPassword = in.readLine();
cli@44
    92
cli@44
    93
			String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString();
cli@44
    94
			createTables(tmpl, url, dbUser, dbPassword);
cli@44
    95
cli@44
    96
			// TODO: Create config file
cli@44
    97
cli@44
    98
		} else if(args.length == 4) {
cli@44
    99
			String tmplName = args[0];
cli@44
   100
			String url = args[1];
cli@44
   101
			String dbUser = args[2];
cli@44
   102
			String dbPassword = args[3];
cli@44
   103
cli@44
   104
			String tmpl = Resource.getAsString(tmplName, true);
cli@44
   105
			createTables(tmpl, url, dbUser, dbPassword);
cli@44
   106
		} else {
cli@44
   107
			System.out.println("Wrong number of parameters!");
cli@37
   108
		}
cli@37
   109
cli@44
   110
		System.out.println("Ok");
cli@44
   111
	}
cli@37
   112
cli@44
   113
	public static void createTables(String tmpl, String url, String dbUser, String dbPassword)
cli@44
   114
			throws SQLException {
cli@37
   115
		Connection conn =
cli@44
   116
				DriverManager.getConnection(url, dbUser, dbPassword);
cli@37
   117
cli@37
   118
		String[] tmplChunks = tmpl.split(";");
cli@37
   119
cli@37
   120
		for (String chunk : tmplChunks) {
cli@37
   121
			if (chunk.trim().equals("")) {
cli@37
   122
				continue;
cli@37
   123
			}
cli@37
   124
cli@37
   125
			Statement stmt = conn.createStatement();
cli@37
   126
			stmt.execute(chunk);
cli@37
   127
		}
cli@37
   128
cli@37
   129
		conn.commit();
cli@44
   130
		conn.close();
cli@44
   131
	}
cli@37
   132
cli@44
   133
	public static void loadJDBCDriver() {
cli@44
   134
		for(String className : driverMap.values()) {
cli@44
   135
			try {
cli@44
   136
				Class.forName(className);
cli@44
   137
			} catch (ClassNotFoundException ex) {
cli@44
   138
				System.out.println("Could not load JDBC driver: " + className);
cli@44
   139
			}
cli@44
   140
		}
cli@37
   141
	}
chris@1
   142
}