001package io.ebean.docker.commands;
002
003import java.io.OutputStreamWriter;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Locale;
007import java.util.Properties;
008
009public class RedisContainer extends BaseContainer {
010
011  /**
012   * Create the RedisContainer with configuration via properties.
013   */
014  public static RedisContainer create(String redisVersion, Properties properties) {
015    return new RedisContainer(new RedisConfig(redisVersion, properties));
016  }
017
018  public RedisContainer(RedisConfig config) {
019    super(config);
020  }
021
022  @Override
023  boolean checkConnectivity() {
024
025    String osName = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
026    if (osName.indexOf("win") > -1) {
027      return doWindowsCheck();
028    }
029
030    try {
031      ProcessBuilder pb = new ProcessBuilder("nc", "localhost", Integer.toString(config.getPort()));
032      pb.redirectErrorStream(true);
033
034      Process process = pb.start();
035
036      OutputStreamWriter ow = new OutputStreamWriter(process.getOutputStream());
037      ow.write("PING");
038      ow.flush();
039      ow.close();
040
041      return process.waitFor() == 0;
042
043    } catch (Exception e) {
044      e.printStackTrace();
045      return false;
046    }
047  }
048
049  private boolean doWindowsCheck() {
050    try {
051      // Oh well ...
052      Thread.sleep(20);
053      return true;
054    } catch (Exception e) {
055      return true;
056    }
057  }
058
059  protected ProcessBuilder runProcess() {
060
061    List<String> args = dockerRun();
062    args.add(config.image);
063    return createProcessBuilder(args);
064  }
065
066}