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: 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; chris@1: import java.sql.Statement; chris@1: import java.util.HashMap; chris@1: import java.util.Map; chris@1: import org.sonews.daemon.BootstrapConfig; 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: */ chris@1: public final class DatabaseSetup chris@1: { chris@1: chris@1: private static final Map templateMap chris@1: = new HashMap(); chris@1: private static final Map urlMap chris@1: = new HashMap(); chris@1: private static final Map driverMap chris@1: = new HashMap(); chris@1: chris@1: static chris@1: { chris@1: templateMap.put("1", "helpers/database_mysql5_tmpl.sql"); chris@1: templateMap.put("2", "helpers/database_postgresql8_tmpl.sql"); chris@1: chris@1: urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB")); chris@1: urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB")); chris@1: chris@1: driverMap.put("1", "com.mysql.jdbc.Driver"); chris@1: driverMap.put("2", "org.postgresql.Driver"); chris@1: } chris@1: chris@1: public static void main(String[] args) chris@1: throws Exception chris@1: { chris@1: System.out.println("sonews Database setup helper"); chris@1: System.out.println("This program will create a initial database table structure"); chris@1: System.out.println("for the sonews Newsserver."); chris@1: System.out.println("You need to create a database and a db user manually before!"); chris@1: chris@1: System.out.println("Select DBMS type:"); chris@1: System.out.println("[1] MySQL 5.x or higher"); chris@1: System.out.println("[2] PostgreSQL 8.x or higher"); chris@1: System.out.print("Your choice: "); chris@1: chris@1: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); chris@1: String dbmsType = in.readLine(); chris@1: String tmplName = templateMap.get(dbmsType); chris@1: if(tmplName == null) chris@1: { chris@1: System.err.println("Invalid choice. Try again you fool!"); chris@1: main(args); chris@1: return; chris@1: } chris@1: chris@1: // Load JDBC Driver class chris@1: Class.forName(driverMap.get(dbmsType)); chris@1: chris@1: String tmpl = Resource.getAsString(tmplName, true); chris@1: chris@1: System.out.print("Database server hostname (e.g. localhost): "); chris@1: String dbHostname = in.readLine(); chris@1: chris@1: System.out.print("Database name: "); chris@1: String dbName = in.readLine(); chris@1: chris@1: System.out.print("Give name of DB user that can create tables: "); chris@1: String dbUser = in.readLine(); chris@1: chris@1: System.out.print("Password: "); chris@1: String dbPassword = in.readLine(); chris@1: chris@1: String url = urlMap.get(dbmsType) chris@1: .set("HOSTNAME", dbHostname) chris@1: .set("DB", dbName).toString(); chris@1: chris@1: Connection conn = chris@1: DriverManager.getConnection(url, dbUser, dbPassword); chris@1: conn.setAutoCommit(false); chris@1: chris@1: String[] tmplChunks = tmpl.split(";"); chris@1: chris@1: for(String chunk : tmplChunks) chris@1: { chris@1: if(chunk.trim().equals("")) chris@1: continue; chris@1: chris@1: Statement stmt = conn.createStatement(); chris@1: stmt.execute(chunk); chris@1: } chris@1: chris@1: conn.commit(); chris@1: conn.setAutoCommit(true); chris@1: chris@1: BootstrapConfig config = BootstrapConfig.getInstance(); chris@1: config.set(BootstrapConfig.STORAGE_DATABASE, url); chris@1: config.set(BootstrapConfig.STORAGE_DBMSDRIVER, driverMap.get(dbmsType)); chris@1: config.set(BootstrapConfig.STORAGE_PASSWORD, dbPassword); chris@1: config.set(BootstrapConfig.STORAGE_USER, dbUser); chris@1: config.save(); chris@1: chris@1: System.out.println("Ok"); chris@1: } chris@1: chris@1: }