org/sonews/util/DatabaseSetup.java
author cli
Sun, 09 May 2010 12:38:46 +0200
changeset 30 146b3275b792
parent 3 2fdc9cc89502
permissions -rw-r--r--
The StorageBackendException is catched two times so that the user see the "500 internal server error" only at the second catch (probably fixes #5).
     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 
    19 package org.sonews.util;
    20 
    21 import java.io.BufferedReader;
    22 import java.io.InputStreamReader;
    23 import java.sql.Connection;
    24 import java.sql.DriverManager;
    25 import java.sql.Statement;
    26 import java.util.HashMap;
    27 import java.util.Map;
    28 import org.sonews.config.Config;
    29 import org.sonews.util.io.Resource;
    30 
    31 /**
    32  * Database setup utility class.
    33  * @author Christian Lins
    34  * @since sonews/0.5.0
    35  */
    36 public final class DatabaseSetup 
    37 {
    38 
    39   private static final Map<String, String> templateMap 
    40     = new HashMap<String, String>();
    41   private static final Map<String, StringTemplate> urlMap
    42     = new HashMap<String, StringTemplate>();
    43   private static final Map<String, String> driverMap
    44     = new HashMap<String, String>();
    45   
    46   static
    47   {
    48     templateMap.put("1", "helpers/database_mysql5_tmpl.sql");
    49     templateMap.put("2", "helpers/database_postgresql8_tmpl.sql");
    50     
    51     urlMap.put("1", new StringTemplate("jdbc:mysql://%HOSTNAME/%DB"));
    52     urlMap.put("2", new StringTemplate("jdbc:postgresql://%HOSTNAME/%DB"));
    53     
    54     driverMap.put("1", "com.mysql.jdbc.Driver");
    55     driverMap.put("2", "org.postgresql.Driver");
    56   }
    57   
    58   public static void main(String[] args)
    59     throws Exception
    60   {
    61     System.out.println("sonews Database setup helper");
    62     System.out.println("This program will create a initial database table structure");
    63     System.out.println("for the sonews Newsserver.");
    64     System.out.println("You need to create a database and a db user manually before!");
    65     
    66     System.out.println("Select DBMS type:");
    67     System.out.println("[1] MySQL 5.x or higher");
    68     System.out.println("[2] PostgreSQL 8.x or higher");
    69     System.out.print("Your choice: ");
    70     
    71     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    72     String dbmsType = in.readLine();
    73     String tmplName = templateMap.get(dbmsType);
    74     if(tmplName == null)
    75     {
    76       System.err.println("Invalid choice. Try again you fool!");
    77       main(args);
    78       return;
    79     }
    80     
    81     // Load JDBC Driver class
    82     Class.forName(driverMap.get(dbmsType));
    83     
    84     String tmpl = Resource.getAsString(tmplName, true);
    85     
    86     System.out.print("Database server hostname (e.g. localhost): ");
    87     String dbHostname = in.readLine();
    88     
    89     System.out.print("Database name: ");
    90     String dbName = in.readLine();
    91 
    92     System.out.print("Give name of DB user that can create tables: ");
    93     String dbUser = in.readLine();
    94 
    95     System.out.print("Password: ");
    96     String dbPassword = in.readLine();
    97     
    98     String url = urlMap.get(dbmsType)
    99       .set("HOSTNAME", dbHostname)
   100       .set("DB", dbName).toString();
   101     
   102     Connection conn = 
   103       DriverManager.getConnection(url, dbUser, dbPassword);
   104     conn.setAutoCommit(false);
   105     
   106     String[] tmplChunks = tmpl.split(";");
   107     
   108     for(String chunk : tmplChunks)
   109     {
   110       if(chunk.trim().equals(""))
   111       {
   112         continue;
   113       }
   114       
   115       Statement stmt = conn.createStatement();
   116       stmt.execute(chunk);
   117     }
   118     
   119     conn.commit();
   120     conn.setAutoCommit(true);
   121     
   122     // Create config file
   123     
   124     System.out.println("Ok");
   125   }
   126   
   127 }