diff --git a/src/main/java/redis/embedded/RedisRunScriptEnum.java b/src/main/java/redis/embedded/RedisRunScriptEnum.java new file mode 100644 index 00000000..614aaa98 --- /dev/null +++ b/src/main/java/redis/embedded/RedisRunScriptEnum.java @@ -0,0 +1,36 @@ +package redis.embedded; + +/** +* Created by piotrturek on 22/01/15. +*/ +enum RedisRunScriptEnum { + WINDOWS_32("redis-server.exe"), + WINDOWS_64("redis-server-64.exe"), + UNIX("redis-server"), + MACOSX("redis-server.app"); + + private final String runScript; + + private RedisRunScriptEnum(String runScript) { + this.runScript = runScript; + } + + public static String getRedisRunScript() { + String osName = System.getProperty("os.name").toLowerCase(); + String osArch = System.getProperty("os.arch").toLowerCase(); + + if (osName.contains("win")) { + if (osArch.contains("64")) { + return WINDOWS_64.runScript; + } else { + return WINDOWS_32.runScript; + } + } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { + return UNIX.runScript; + } else if ("Mac OS X".equalsIgnoreCase(osName)) { + return MACOSX.runScript; + } else { + throw new RuntimeException("Unsupported os/architecture...: " + osName + " on " + osArch); + } + } +} diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index 87aaf725..18051e19 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -1,151 +1,18 @@ package redis.embedded; -import com.google.common.base.Strings; -import com.google.common.io.Files; import redis.embedded.util.JarUtil; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.net.InetSocketAddress; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class RedisServer { - private static enum RedisRunScriptEnum { - WINDOWS_32("redis-server.exe"), - WINDOWS_64("redis-server-64.exe"), - UNIX("redis-server"), - MACOSX("redis-server.app"); - - private final String runScript; - - private RedisRunScriptEnum(String runScript) { - this.runScript = runScript; - } - - public static String getRedisRunScript() { - String osName = System.getProperty("os.name").toLowerCase(); - String osArch = System.getProperty("os.arch").toLowerCase(); - - if (osName.contains("win")) { - if (osArch.contains("64")) { - return WINDOWS_64.runScript; - } else { - return WINDOWS_32.runScript; - } - } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { - return UNIX.runScript; - } else if ("Mac OS X".equalsIgnoreCase(osName)) { - return MACOSX.runScript; - } else { - throw new RuntimeException("Unsupported os/architecture...: " + osName + " on " + osArch); - } - } - } - - public static class Builder { - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - private File executable; - private Integer port; - private InetSocketAddress slaveOf; - private String redisConf; - - private StringBuilder redisConfigBuilder; - - public Builder executable(File executable) { - this.executable = executable; - return this; - } - - public Builder executable(String executable) { - this.executable = new File(executable); - return this; - } - - public Builder port(Integer port) { - this.port = port; - return this; - } - - public Builder slaveOf(String hostname, Integer port) { - this.slaveOf = new InetSocketAddress(hostname, port); - return this; - } - - public Builder slaveOf(InetSocketAddress slaveOf) { - this.slaveOf = slaveOf; - return this; - } - - public Builder configFile(String redisConf) { - if (redisConfigBuilder != null) { - throw new RuntimeException("Redis configuration is already partially build using setting(String) method!"); - } - this.redisConf = redisConf; - return this; - } - - public Builder setting(String configLine) { - if (redisConf != null) { - throw new RuntimeException("Redis configuration is already set using redis conf file!"); - } - - if (redisConfigBuilder == null) { - redisConfigBuilder = new StringBuilder(); - } - - redisConfigBuilder.append(configLine); - redisConfigBuilder.append(LINE_SEPARATOR); - return this; - } - - public RedisServer build() throws IOException { - if (redisConf == null && redisConfigBuilder != null) { - File redisConfigFile = File.createTempFile("embedded-redis", ".conf"); - redisConfigFile.deleteOnExit(); - Files.write(redisConfigBuilder.toString(), redisConfigFile, Charset.forName("UTF-8")); - redisConf = redisConfigFile.getAbsolutePath(); - } - - if (executable == null) { - executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); - } - - List args = buildCommandArgs(); - return new RedisServer(args); - } - - private List buildCommandArgs() { - List args = new ArrayList(); - args.add(executable.getAbsolutePath()); - - if (!Strings.isNullOrEmpty(redisConf)) { - args.add(redisConf); - } - - if (port != null) { - args.add("--port"); - args.add(Integer.toString(port)); - } - - if (slaveOf != null) { - args.add("--slaveof"); - args.add(slaveOf.getHostName()); - args.add(Integer.toString(slaveOf.getPort())); - } - - return args; - } - } - - private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*"; + private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*"; private final List args; @@ -167,12 +34,12 @@ public RedisServer(File executable, Integer port) { ); } - private RedisServer(List args) { + RedisServer(List args) { this.args = new ArrayList(args); } - public static Builder builder() { - return new Builder(); + public static RedisServerBuilder builder() { + return new RedisServerBuilder(); } public boolean isActive() { diff --git a/src/main/java/redis/embedded/RedisServerBuilder.java b/src/main/java/redis/embedded/RedisServerBuilder.java new file mode 100644 index 00000000..64268a35 --- /dev/null +++ b/src/main/java/redis/embedded/RedisServerBuilder.java @@ -0,0 +1,111 @@ +package redis.embedded; + +import com.google.common.base.Strings; +import com.google.common.io.Files; +import redis.embedded.util.JarUtil; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by piotrturek on 22/01/15. + */ +public class RedisServerBuilder { + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + private File executable; + private Integer port; + private InetSocketAddress slaveOf; + private String redisConf; + + private StringBuilder redisConfigBuilder; + + public RedisServerBuilder executable(File executable) { + this.executable = executable; + return this; + } + + public RedisServerBuilder executable(String executable) { + this.executable = new File(executable); + return this; + } + + public RedisServerBuilder port(Integer port) { + this.port = port; + return this; + } + + public RedisServerBuilder slaveOf(String hostname, Integer port) { + this.slaveOf = new InetSocketAddress(hostname, port); + return this; + } + + public RedisServerBuilder slaveOf(InetSocketAddress slaveOf) { + this.slaveOf = slaveOf; + return this; + } + + public RedisServerBuilder configFile(String redisConf) { + if (redisConfigBuilder != null) { + throw new RuntimeException("Redis configuration is already partially build using setting(String) method!"); + } + this.redisConf = redisConf; + return this; + } + + public RedisServerBuilder setting(String configLine) { + if (redisConf != null) { + throw new RuntimeException("Redis configuration is already set using redis conf file!"); + } + + if (redisConfigBuilder == null) { + redisConfigBuilder = new StringBuilder(); + } + + redisConfigBuilder.append(configLine); + redisConfigBuilder.append(LINE_SEPARATOR); + return this; + } + + public RedisServer build() throws IOException { + if (redisConf == null && redisConfigBuilder != null) { + File redisConfigFile = File.createTempFile("embedded-redis", ".conf"); + redisConfigFile.deleteOnExit(); + Files.write(redisConfigBuilder.toString(), redisConfigFile, Charset.forName("UTF-8")); + redisConf = redisConfigFile.getAbsolutePath(); + } + + if (executable == null) { + executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); + } + + List args = buildCommandArgs(); + return new RedisServer(args); + } + + private List buildCommandArgs() { + List args = new ArrayList(); + args.add(executable.getAbsolutePath()); + + if (!Strings.isNullOrEmpty(redisConf)) { + args.add(redisConf); + } + + if (port != null) { + args.add("--port"); + args.add(Integer.toString(port)); + } + + if (slaveOf != null) { + args.add("--slaveof"); + args.add(slaveOf.getHostName()); + args.add(Integer.toString(slaveOf.getPort())); + } + + return args; + } +}