-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.java
104 lines (85 loc) · 2.8 KB
/
Config.java
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
/**
* @brief creation of 'properties' file
* @author Yannis Exidaridis <[email protected]>
*/
package albums;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
* @author yannis
*/
public class Config {
private final String CONFIG_FILENAME = "dbconfig.properties";
Properties DBConfig;
InputStream ConfigFile = null;
FileOutputStream NewConfigFile = null;
public Config() {
DBConfig = new Properties();
}
/**
* @return read success or failure if file not exists
* @brief open dbconfig.properties
*/
public Boolean ReadConfig() {
try {
ConfigFile = new FileInputStream(CONFIG_FILENAME);
} catch (FileNotFoundException ex) {
return false;
}
try {
DBConfig.load(ConfigFile);
} catch (IOException ex) {
return false;
}
return true;
}
/**
* @return success or failure
* @brief create dbconfig.properties
* @param db_url
* @param db_name
* @param db_username
* @param db_password
*/
public Boolean CreateConfig(String db_url, String db_name, String db_username, String db_password) {
try {
NewConfigFile = new FileOutputStream(CONFIG_FILENAME);
String line_1 = "database_url = " + db_url + "\r\n";
String line_2 = "database_name = " + db_name + "\r\n";
String line_3 = "database_username = " + db_username + "\r\n";
String line_4 = "database_password = " + db_password;
String line_5 = "pdf_font = ";
String line_6 = "mysql_exec = /usr/bin/mysqldump";
String file_contents = line_1 + line_2 + line_3 + line_4 + line_5 + line_6;
byte[] byte_content = file_contents.getBytes();
NewConfigFile.write(byte_content);
NewConfigFile.close();
} catch (IOException ex) {
return false;
}
return true;
}
public String get_database_url() {
return DBConfig.getProperty("database_url");
}
public String get_database_name() {
return DBConfig.getProperty("database_name");
}
public String get_database_username() {
return DBConfig.getProperty("database_username");
}
public String get_database_password() {
return DBConfig.getProperty("database_password");
}
public String get_pdf_font() {
return DBConfig.getProperty("pdf_font");
}
public String get_mysql_exec() {
return DBConfig.getProperty("mysql_exec");
}
}