Skip to content

Commit 928c0aa

Browse files
abrousseau001vorburger
authored andcommitted
WIP - Initial ideas for homebrew support
1 parent 316ca2b commit 928c0aa

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/DB.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class DB {
5656

5757
private static final Logger logger = LoggerFactory.getLogger(DB.class);
5858

59+
private static final String homebrewInstallationPath = "/opt/homebrew/bin/brew";
60+
5961
protected final DBConfiguration configuration;
6062

6163
private File baseDir;
@@ -407,22 +409,35 @@ public synchronized void stop() throws ManagedProcessException {
407409
* on the configuration.
408410
*/
409411
protected void unpackEmbeddedDb() {
410-
if (configuration.getBinariesClassPathLocation() == null) {
411-
logger.info("Not unpacking any embedded database (as BinariesClassPathLocation configuration is null)");
412-
return;
412+
// Check for Homebrew, before doing anything else
413+
// If HomeBrew is not installed, then throw a runtime error, similar to how the below extraction method
414+
// does below for other operating systems
415+
if(configuration.isMacOs() && !Util.doesExecutableExistAndIsExecutable(new File(homebrewInstallationPath)))
416+
{
417+
throw new RuntimeException("Homebrew must be installed on the system before using this library");
418+
}
419+
else {
420+
// All other OS except macOS needs to have a place where the binaries are to be extracted
421+
if (configuration.getBinariesClassPathLocation() == null) {
422+
logger.info("Not unpacking any embedded database (as BinariesClassPathLocation configuration is null)");
423+
return;
424+
}
413425
}
414426

415-
try {
416-
Util.extractFromClasspathToFile(configuration.getBinariesClassPathLocation(), baseDir);
417-
if (!configuration.isWindows()) {
418-
Util.forceExecutable(configuration.getExecutable(PrintDefaults));
419-
Util.forceExecutable(configuration.getExecutable(InstallDB));
420-
Util.forceExecutable(configuration.getExecutable(Server));
421-
Util.forceExecutable(configuration.getExecutable(Dump));
422-
Util.forceExecutable(configuration.getExecutable(Client));
427+
// Windows, Linux, and any other supported OS can have their binaries extracted as normal
428+
else if(!configuration.isMacOs()) {
429+
try {
430+
Util.extractFromClasspathToFile(configuration.getBinariesClassPathLocation(), baseDir);
431+
if (!configuration.isWindows()) {
432+
Util.forceExecutable(configuration.getExecutable(PrintDefaults));
433+
Util.forceExecutable(configuration.getExecutable(InstallDB));
434+
Util.forceExecutable(configuration.getExecutable(Server));
435+
Util.forceExecutable(configuration.getExecutable(Dump));
436+
Util.forceExecutable(configuration.getExecutable(Client));
437+
}
438+
} catch (IOException e) {
439+
throw new RuntimeException("Error unpacking embedded DB", e);
423440
}
424-
} catch (IOException e) {
425-
throw new RuntimeException("Error unpacking embedded DB", e);
426441
}
427442
}
428443

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/DBConfiguration.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public interface DBConfiguration {
9393
**/
9494
boolean isWindows();
9595

96+
/**
97+
* Whether running on macOS (this is used to determine whether or not to check for Homebrew)
98+
*
99+
* @return returns boolean isMacOs
100+
*/
101+
boolean isMacOs();
102+
96103
List<String> getArgs();
97104

98105
String getOSLibraryEnvironmentVarName();
@@ -132,6 +139,8 @@ static class Impl implements DBConfiguration {
132139
private final String tmpDir;
133140
private final boolean isDeletingTemporaryBaseAndDataDirsOnShutdown;
134141
private final boolean isWindows;
142+
143+
private final boolean isMacOs;
135144
private final List<String> args;
136145
private final String osLibraryEnvironmentVarName;
137146
private final String defaultCharacterSet;
@@ -141,7 +150,7 @@ static class Impl implements DBConfiguration {
141150
private final Map<Executable, Supplier<File>> executables;
142151

143152
Impl(int port, String socket, String binariesClassPathLocation, String baseDir, String libDir, String dataDir, String tmpDir,
144-
boolean isWindows, List<String> args, String osLibraryEnvironmentVarName, boolean isSecurityDisabled,
153+
boolean isWindows, boolean isMacOs, List<String> args, String osLibraryEnvironmentVarName, boolean isSecurityDisabled,
145154
boolean isDeletingTemporaryBaseAndDataDirsOnShutdown, Function<String, String> getURL, String defaultCharacterSet,
146155
Map<Executable, Supplier<File>> executables, ManagedProcessListener listener) {
147156
this.port = port;
@@ -153,6 +162,7 @@ static class Impl implements DBConfiguration {
153162
this.tmpDir = tmpDir;
154163
this.isDeletingTemporaryBaseAndDataDirsOnShutdown = isDeletingTemporaryBaseAndDataDirsOnShutdown;
155164
this.isWindows = isWindows;
165+
this.isMacOs = isMacOs;
156166
this.args = args;
157167
this.osLibraryEnvironmentVarName = osLibraryEnvironmentVarName;
158168
this.isSecurityDisabled = isSecurityDisabled;
@@ -198,6 +208,8 @@ static class Impl implements DBConfiguration {
198208
return isWindows;
199209
}
200210

211+
@Override public boolean isMacOs() { return isMacOs; };
212+
201213
@Override public List<String> getArgs() {
202214
return args;
203215
}

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/DBConfigurationBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public DBConfigurationBuilder setSocket(String socket) {
206206
public DBConfiguration build() {
207207
frozen = true;
208208
return new DBConfiguration.Impl(_getPort(), _getSocket(), _getBinariesClassPathLocation(), getBaseDir(),
209-
getLibDir(), _getDataDir(), _getTmpDir(), isWindows(), _getArgs(), _getOSLibraryEnvironmentVarName(),
209+
getLibDir(), _getDataDir(), _getTmpDir(), isWindows(), isMacOs(), _getArgs(), _getOSLibraryEnvironmentVarName(),
210210
isSecurityDisabled(), isDeletingTemporaryBaseAndDataDirsOnShutdown(), this::getURL,
211211
getDefaultCharacterSet(), _getExecutables(), getProcessListener());
212212
}
@@ -395,6 +395,10 @@ public boolean isWindows() {
395395
return WINX64.equals(getOS());
396396
}
397397

398+
public boolean isMacOs(){
399+
return OSX.equals(getOS());
400+
}
401+
398402
protected String getExtension() {
399403
return isWindows() ? ".exe" : "";
400404
}

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/Util.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public static boolean isTemporaryDirectory(String directory) {
8989
return directory.startsWith(SystemUtils.JAVA_IO_TMPDIR);
9090
}
9191

92+
/**
93+
* Method to check for the existence of a prerequisite executable, like Homebrew on macOS,
94+
* and determine if it is executable
95+
* @param executableFile file that is being checked for
96+
* @return boolean if the file exists and is executable
97+
*/
98+
public static boolean doesExecutableExistAndIsExecutable(File executableFile){
99+
return executableFile.exists() && executableFile.canExecute();
100+
}
101+
92102
public static void forceExecutable(File executableFile) throws IOException {
93103
if (executableFile.exists()) {
94104
if (!executableFile.canExecute()) {

0 commit comments

Comments
 (0)