forked from ozimov/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted run script enum and builder to separate classes
- Loading branch information
Showing
3 changed files
with
151 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> args = buildCommandArgs(); | ||
return new RedisServer(args); | ||
} | ||
|
||
private List<String> buildCommandArgs() { | ||
List<String> args = new ArrayList<String>(); | ||
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; | ||
} | ||
} |