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