forked from openmrs/openmrs-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMariaDbController.java
More file actions
137 lines (111 loc) · 5.64 KB
/
MariaDbController.java
File metadata and controls
137 lines (111 loc) · 5.64 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
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.standalone;
import java.io.File;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;
import ch.vorburger.exec.ManagedProcessException;
import ch.vorburger.mariadb4j.DB;
import ch.vorburger.mariadb4j.DBConfigurationBuilder;
public class MariaDbController {
public static final String DATABASE_NAME = "openmrs";
private static final String MARIA_DB_BASE_DIR = "database";
private static final String MARIA_DB_DATA_DIR = Paths.get(MARIA_DB_BASE_DIR, "data").toString();
private static final String ROOT_USER = "root";
private static final String ROOT_PASSWORD = "";
private static DB mariaDB;
private static DBConfigurationBuilder mariaDBConfig;
public static String KEY_MARIADB_BASE_DIR = "connection.database.base_dir";
public static String KEY_MARIADB_DATA_DIR = "connection.database.data_dir";
public static void startMariaDB(String port, String userPassword) throws Exception {
startMariaDB(Integer.parseInt(port), userPassword);
}
/**
* Starts MariaDB with the given port and user password. If password is null or blank, defaults to empty string.
*/
public static void startMariaDB(int port, String userPassword) throws Exception {
if (userPassword == null) {
userPassword = "";
}
String os = System.getProperty("os.name").toLowerCase();
boolean isWindows = os.contains("win");
// Build DB configuration
mariaDBConfig = DBConfigurationBuilder.newBuilder();
mariaDBConfig.setPort(port);
mariaDBConfig.setSecurityDisabled(false);
Properties properties = OpenmrsUtil.getRuntimeProperties(StandaloneUtil.getContextName());
String baseDirPath = safeResolveProperty(properties, KEY_MARIADB_BASE_DIR, MARIA_DB_BASE_DIR);
String dataDirPath = safeResolveProperty(properties, KEY_MARIADB_DATA_DIR, MARIA_DB_DATA_DIR);
File baseDir = new File(Paths.get(baseDirPath).toAbsolutePath().toString());
File dataDir = new File(Paths.get(dataDirPath).toAbsolutePath().toString());
mariaDBConfig.setBaseDir(baseDir);
mariaDBConfig.setDataDir(dataDir);
mariaDBConfig.addArg("--max_allowed_packet=96M");
mariaDBConfig.addArg("--collation-server=utf8_general_ci");
mariaDBConfig.addArg("--character-set-server=utf8");
if(isWindows){
// For Windows, we use the ReusableDB class
mariaDB = ReusableDB.openEmbeddedDB(mariaDBConfig.build());
mariaDB.start();
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:" + port + "/", ROOT_USER, ROOT_PASSWORD);
try (Statement stmt = conn.createStatement()) {
stmt.execute("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + ROOT_PASSWORD + "';");
stmt.execute("GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;");
}
} else {
// For Linux and macOS, we use the standard DB class
mariaDB = DB.newEmbeddedDB(mariaDBConfig.build());
mariaDB.start();
// Ensure root user exists and has correct password and privileges
mariaDB.run("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + ROOT_PASSWORD + "';");
mariaDB.run("GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;");
}
// Create the OpenMRS database schema if it doesn't exist
mariaDB.createDB(DATABASE_NAME, ROOT_USER, ROOT_PASSWORD);
// ✅ Create openmrs user and grant permissions
try (Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:" + port + "/", ROOT_USER, ROOT_PASSWORD)) {
try (Statement stmt = connection.createStatement()) {
// Create user if not exists
String createUserSQL = "CREATE USER IF NOT EXISTS 'openmrs'@'localhost' IDENTIFIED BY '" + userPassword + "';";
stmt.executeUpdate(createUserSQL);
// Grant privileges on the openmrs DB
String grantPrivilegesSQL = "GRANT ALL PRIVILEGES ON `" + DATABASE_NAME + "`.* TO 'openmrs'@'localhost' WITH GRANT OPTION;";
stmt.executeUpdate(grantPrivilegesSQL);
// (Optional) Allow openmrs to create users
String grantCreateUserSQL = "GRANT CREATE USER ON *.* TO 'openmrs'@'localhost';";
stmt.executeUpdate(grantCreateUserSQL);
}
}
}
private static String safeResolveProperty(Properties properties, String key, String defaultValue) {
if (properties == null || !properties.containsKey(key)) {
return defaultValue;
}
return properties.getProperty(key, defaultValue);
}
public static void stopMariaDB() throws ManagedProcessException {
if (mariaDB != null) {
mariaDB.stop();
mariaDB = null;
} else {
System.out.println("MariaDB has already been stopped");
}
}
public static String getRootPassword() {
return ROOT_PASSWORD;
}
}