-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostCfg.java
More file actions
executable file
·178 lines (162 loc) · 4.56 KB
/
HostCfg.java
File metadata and controls
executable file
·178 lines (162 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package database;
/************************************************
* Reads HOSTS.cfg and returns connection
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import util.ErrorReport;
import util.Globals;
import util.LogTime;
public class HostCfg {
private DBConn mDB=null;
private String DBprefix= Globals.DBprefix;
private String hostFile = Globals.hostFile;
private String DBhost="localhost"; // do we want to make this more general?
private String DBuser="", DBpass="", DBname="";
public HostCfg() {}
public boolean readHosts() {
try {
File f = new File(hostFile);
if (!f.exists()) {
hostFormat(hostFile + " not found");
return false;
}
BufferedReader reader = new BufferedReader (new FileReader ( hostFile ) );
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) continue;
if (line.equals("")) continue;
String [] tok = line.split("=");
if (tok.length!=2) System.out.println("Invalid line: " + line);
else {
String key = tok[0].toLowerCase();
String val = tok[1].trim();
if (key.contains("user")) DBuser=val;
else if (key.contains("pass")) DBpass=val;
else if (key.contains("host")) continue;
else System.out.println("Invalid line: " + line);
}
}
if (DBuser.equals("") || DBpass.equals("")) {
hostFormat("Incorrect " + hostFile);
return false;
}
return true;
}
catch (Exception e) {
e.printStackTrace();
hostFormat("Error reading HOSTS.cfg");
}
return false;
}
/**********************************************************
* Database open/create/exists
*/
public DBConn openDB(String dbSuffix, int action) {
try {
if (DBuser.equals("")) {
if (!readHosts()) return null;
}
DBname = dbSuffix;
boolean doesDBExist = existsDB(dbSuffix); // add prefix if needed
if (!doesDBExist) {
if (LogTime.yesNo("Create database " + DBname))
return createDB();
else {
LogTime.PrtWarn("User terminated");
return null;
}
}
if (action<=1) {
if (!LogTime.yesNo("Delete database '" + DBname + "' and recreate ")) LogTime.die("Users request");
DBConn.deleteMysqlDB(DBhost, DBname, DBuser, DBpass);
System.out.println("Database '" + DBname + "' deleted");
return createDB();
}
else {
LogTime.PrtSpMsg(1, "Use existing database " + DBname + " on " + DBhost);
mDB = getDBConnection();
return mDB;
}
}
catch (Exception e) {
ErrorReport.die(e, "Cannot open " + DBname + " on " + DBhost);
}
return null;
}
private DBConn createDB() {
try {
DBConn.createMysqlDB(DBhost, DBname, DBuser, DBpass);
mDB = getDBConnection();
LogTime.PrtSpMsg(1, "Loading schema database");
new Schema(mDB);
return mDB;
}
catch (Exception e) {
ErrorReport.prtError(e, "Cannot load schema");
return null;
}
}
public DBConn openDB(String dbName) {
try {
if (DBuser.equals("")) {
if (!readHosts()) return null;
}
if (!existsDB(dbName)) {
LogTime.PrtError("Database does not exist: " + dbName + " on " + DBhost);
return null;
}
return getDBConnection();
}
catch (Exception e) {
ErrorReport.prtError(e, "Cannot open " + DBname + " on " + DBhost);
}
return null;
}
public DBConn getDBConnection() throws Exception
{
String dbstr = "jdbc:mysql://" + DBhost + "/" + DBname;
mDB = new DBConn(dbstr, DBuser, DBpass);
return mDB;
}
public boolean existsDB(String db)
{
try {
if (DBuser.equals("")) {
if (!readHosts()) return false;
}
db = db.trim();
if (db.startsWith(DBprefix)) DBname = db;
else DBname= DBprefix + db;
return DBConn.checkMysqlDB(DBhost, DBname, DBuser, DBpass);
}
catch (Exception e) {
ErrorReport.prtError(e, "Checking database: " + db);
return false;
}
}
public void deleteDB(String db) {
if (DBuser.equals("")) {
if (!readHosts()) return;
}
try {
DBConn.deleteMysqlDB(DBhost,db, DBuser, DBpass);
}
catch (Exception e) {
ErrorReport.prtError(e, "Could not delete database: " + db);
}
}
public String getDBhost() {return DBhost;}
public String getDBuser() { return DBuser;}
public String getDBname() { return DBname;}
public String getDBpass() { return DBpass;}
private void hostFormat(String msg) {
System.out.println(msg);
System.out.println(hostFile + " must contain:");
System.out.println("user = <mysql write access userid>");
System.out.println("password = <mysql write assess password");
System.out.println(" with the userid and password for your mySQL database");
}
}