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