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