chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: package org.sonews.util; chris@1: chris@1: import java.io.BufferedReader; chris@1: import java.io.InputStreamReader; chris@1: import java.sql.Connection; chris@1: import java.sql.DriverManager; cli@44: import java.sql.SQLException; chris@1: import java.sql.Statement; chris@1: import java.util.HashMap; chris@1: import java.util.Map; chris@1: import org.sonews.util.io.Resource; chris@1: chris@1: /** chris@1: * Database setup utility class. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ cli@44: public final class DatabaseSetup { chris@1: cli@37: private static final Map templateMap = new HashMap(); cli@37: private static final Map urlMap = new HashMap(); cli@37: private static final Map driverMap = new HashMap(); chris@1: cli@37: static { cli@37: templateMap.put("1", "helpers/database_mysql5_tmpl.sql"); cli@37: templateMap.put("2", "helpers/database_postgresql8_tmpl.sql"); cli@44: templateMap.put("3", "helpers/database_hsqldb_tmpl.sql"); chris@1: cli@37: urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB")); cli@37: urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB")); cli@37: cli@37: driverMap.put("1", "com.mysql.jdbc.Driver"); cli@37: driverMap.put("2", "org.postgresql.Driver"); cli@44: driverMap.put("3", "org.hsqldb.jdbcDriver"); cli@37: } cli@37: cli@37: public static void main(String[] args) cli@44: throws Exception { cli@44: cli@44: loadJDBCDriver(); cli@37: cli@44: if (args.length == 0) { cli@44: System.out.println("sonews Database setup helper"); cli@44: System.out.println("This program will create a initial database table structure"); cli@44: System.out.println("for the sonews Newsserver."); cli@44: System.out.println("You need to create a database and a db user manually before!"); cli@37: cli@44: System.out.println("Select DBMS type:"); cli@44: System.out.println("[1] MySQL 5.x or higher"); cli@44: System.out.println("[2] PostgreSQL 8.x or higher"); cli@44: System.out.print("Your choice: "); cli@44: cli@44: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); cli@44: String dbmsType = in.readLine(); cli@44: String tmplName = templateMap.get(dbmsType); cli@44: if (tmplName == null) { cli@44: System.err.println("Invalid choice. Try again you fool!"); cli@44: main(args); cli@44: return; cli@44: } cli@44: cli@44: String tmpl = Resource.getAsString(tmplName, true); cli@44: cli@44: System.out.print("Database server hostname (e.g. localhost): "); cli@44: String dbHostname = in.readLine(); cli@44: cli@44: System.out.print("Database name: "); cli@44: String dbName = in.readLine(); cli@44: cli@44: System.out.print("Give name of DB user that can create tables: "); cli@44: String dbUser = in.readLine(); cli@44: cli@44: System.out.print("Password: "); cli@44: String dbPassword = in.readLine(); cli@44: cli@44: String url = urlMap.get(dbmsType).set("HOSTNAME", dbHostname).set("DB", dbName).toString(); cli@44: createTables(tmpl, url, dbUser, dbPassword); cli@44: cli@44: // TODO: Create config file cli@44: cli@44: } else if(args.length == 4) { cli@44: String tmplName = args[0]; cli@44: String url = args[1]; cli@44: String dbUser = args[2]; cli@44: String dbPassword = args[3]; cli@44: cli@44: String tmpl = Resource.getAsString(tmplName, true); cli@44: createTables(tmpl, url, dbUser, dbPassword); cli@44: } else { cli@44: System.out.println("Wrong number of parameters!"); cli@37: } cli@37: cli@44: System.out.println("Ok"); cli@44: } cli@37: cli@44: public static void createTables(String tmpl, String url, String dbUser, String dbPassword) cli@44: throws SQLException { cli@37: Connection conn = cli@44: DriverManager.getConnection(url, dbUser, dbPassword); cli@37: cli@37: String[] tmplChunks = tmpl.split(";"); cli@37: cli@37: for (String chunk : tmplChunks) { cli@37: if (chunk.trim().equals("")) { cli@37: continue; cli@37: } cli@37: cli@37: Statement stmt = conn.createStatement(); cli@37: stmt.execute(chunk); cli@37: } cli@37: cli@37: conn.commit(); cli@44: conn.close(); cli@44: } cli@37: cli@44: public static void loadJDBCDriver() { cli@44: for(String className : driverMap.values()) { cli@44: try { cli@44: Class.forName(className); cli@44: } catch (ClassNotFoundException ex) { cli@44: System.out.println("Could not load JDBC driver: " + className); cli@44: } cli@44: } cli@37: } chris@1: }