Skip to content

Commit

Permalink
Merge pull request kstyrc#29 from kstyrc/os_detection_fix
Browse files Browse the repository at this point in the history
Os detection fix + ability to override redis exec per os/arch
  • Loading branch information
kstyrc committed Apr 2, 2015
2 parents b2776f9 + e5b0cae commit b242f1d
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 65 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ redisServer.start();
redisServer.stop();
```

You can also provide RedisServer with your own redis executable to run:
You can also provide RedisServer with your own executable:
```java
// 1) given explicit file (os-independence broken!)
RedisServer redisServer = new RedisServer("/path/to/your/redis", 6379);

// 2) given os-independent matrix
RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
.override(OS.UNIX, "/path/to/unix/redis")
.override(OS.WINDOWS, Architecture.x86, "/path/to/windows/redis")
.override(OS.Windows, Architecture.x86_64, "/path/to/windows/redis")
.override(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis")
.override(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis")

RedisServer redisServer = new RedisServer(customProvider, 6379);
```

You can also use fluent API to create RedisServer:
```java
RedisServer redisServer = RedisServer.builder()
.executable("/path/to/your/redis")
.redisExecProvider(customRedisProvider)
.port(6379)
.slaveOf("locahost", 6378)
.configFile("/path/to/your/redis.conf")
Expand All @@ -32,7 +43,7 @@ RedisServer redisServer = RedisServer.builder()
Or even create simple redis.conf file from scratch:
```java
RedisServer redisServer = RedisServer.builder()
.executable("/path/to/your/redis")
.redisExecProvider(customRedisProvider)
.port(6379)
.slaveOf("locahost", 6378)
.setting("daemonize no")
Expand Down Expand Up @@ -135,9 +146,9 @@ Contributors
Changelog
==============

### 0.5
* OS detection fix (not ready)
* redis binary per OS/arch pair (not ready)
### 0.5
* OS detection fix
* redis binary per OS/arch pair

### 0.4
* Updated for Java 8
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/redis/embedded/RedisExecProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package redis.embedded;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import redis.embedded.util.Architecture;
import redis.embedded.util.JarUtil;
import redis.embedded.util.OS;
import redis.embedded.util.OsArchitecture;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class RedisExecProvider {

private final Map<OsArchitecture, String> executables = Maps.newHashMap();

public static RedisExecProvider defaultProvider() {
return new RedisExecProvider();
}

private RedisExecProvider() {
initExecutables();
}

private void initExecutables() {
executables.put(OsArchitecture.WINDOWS_x86, "redis-server.exe");
executables.put(OsArchitecture.WINDOWS_x86_64, "redis-server-64.exe");

executables.put(OsArchitecture.UNIX_x86, "redis-server");
executables.put(OsArchitecture.UNIX_x86_64, "redis-server");

executables.put(OsArchitecture.MAC_OS_X_x86, "redis-server.app");
executables.put(OsArchitecture.MAC_OS_X_x86_64, "redis-server.app");
}

public RedisExecProvider override(OS os, String executable) {
Preconditions.checkNotNull(executable);
for (Architecture arch : Architecture.values()) {
override(os, arch, executable);
}
return this;
}

public RedisExecProvider override(OS os, Architecture arch, String executable) {
Preconditions.checkNotNull(executable);
executables.put(new OsArchitecture(os, arch), executable);
return this;
}

public File get() throws IOException {
OsArchitecture osArch = OsArchitecture.detect();
String executablePath = executables.get(osArch);
return fileExists(executablePath) ?
new File(executablePath) :
JarUtil.extractExecutableFromJar(executablePath);

}

private boolean fileExists(String executablePath) {
return new File(executablePath).exists();
}
}
33 changes: 0 additions & 33 deletions src/main/java/redis/embedded/RedisRunScriptEnum.java

This file was deleted.

17 changes: 5 additions & 12 deletions src/main/java/redis/embedded/RedisSentinelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import redis.embedded.exceptions.RedisBuildingException;
import redis.embedded.util.JarUtil;

import java.io.File;
import java.io.IOException;
Expand All @@ -21,6 +20,7 @@ public class RedisSentinelBuilder {
private static final String PORT_LINE = "port %d";

private File executable;
private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider();
private Integer port = 26379;
private int masterPort = 6379;
private String masterName = "mymaster";
Expand All @@ -32,13 +32,8 @@ public class RedisSentinelBuilder {

private StringBuilder redisConfigBuilder;

public RedisSentinelBuilder executable(File executable) {
this.executable = executable;
return this;
}

public RedisSentinelBuilder executable(String executable) {
this.executable = new File(executable);
public RedisSentinelBuilder redisExecProvider(RedisExecProvider redisExecProvider) {
this.redisExecProvider = redisExecProvider;
return this;
}

Expand Down Expand Up @@ -110,10 +105,8 @@ private void tryResolveConfAndExec() {
if (sentinelConf == null) {
resolveSentinelConf();
}
if (executable == null) {
executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
}
} catch (IOException e) {
executable = redisExecProvider.get();
} catch (Exception e) {
throw new RedisBuildingException("Could not build sentinel instance", e);
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/redis/embedded/RedisServer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package redis.embedded;

import redis.embedded.util.JarUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -18,7 +16,7 @@ public RedisServer() throws IOException {

public RedisServer(Integer port) throws IOException {
super(port);
File executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
File executable = RedisExecProvider.defaultProvider().get();
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
Expand All @@ -33,6 +31,14 @@ public RedisServer(File executable, Integer port) {
);
}

public RedisServer(RedisExecProvider redisExecProvider, Integer port) throws IOException {
super(port);
this.args = Arrays.asList(
redisExecProvider.get().getAbsolutePath(),
"--port", Integer.toString(port)
);
}

RedisServer(List<String> args, int port) {
super(port);
this.args = new ArrayList<>(args);
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/redis/embedded/RedisServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.base.Strings;
import com.google.common.io.Files;
import redis.embedded.exceptions.RedisBuildingException;
import redis.embedded.util.JarUtil;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,22 +16,18 @@ public class RedisServerBuilder {
private static final String CONF_FILENAME = "embedded-redis-server";

private File executable;
private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider();
private Integer port = 6379;
private InetSocketAddress slaveOf;
private String redisConf;

private StringBuilder redisConfigBuilder;

public RedisServerBuilder executable(File executable) {
this.executable = executable;
public RedisServerBuilder redisExecProvider(RedisExecProvider redisExecProvider) {
this.redisExecProvider = redisExecProvider;
return this;
}

public RedisServerBuilder executable(String executable) {
this.executable = new File(executable);
return this;
}


public RedisServerBuilder port(Integer port) {
this.port = port;
return this;
Expand Down Expand Up @@ -77,6 +72,7 @@ public RedisServer build() {
}

public void reset() {
this.executable = null;
this.redisConfigBuilder = null;
this.slaveOf = null;
this.redisConf = null;
Expand All @@ -98,8 +94,10 @@ private void resolveConfAndExec() throws IOException {
redisConf = redisConfigFile.getAbsolutePath();
}

if (executable == null) {
executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
try {
executable = redisExecProvider.get();
} catch (Exception e) {
throw new RedisBuildingException("Failed to resolve executable", e);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/redis/embedded/exceptions/OsDetectionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package redis.embedded.exceptions;

public class OsDetectionException extends RuntimeException {
public OsDetectionException(String message) {
super(message);
}

public OsDetectionException(Throwable cause) {
super(cause);
}
}
6 changes: 6 additions & 0 deletions src/main/java/redis/embedded/util/Architecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package redis.embedded.util;

public enum Architecture {
x86,
x86_64
}
7 changes: 7 additions & 0 deletions src/main/java/redis/embedded/util/OS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package redis.embedded.util;

public enum OS {
WINDOWS,
UNIX,
MAC_OS_X
}
Loading

0 comments on commit b242f1d

Please sign in to comment.