Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
edited title
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Populate the list basisbased on which datacenter code is running on

added 59 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48
public enum DatacenterEnum {
 DHP("/datacenter/dhp"), SLP("/datacenter/slp"), LTR("/datacenter/ltr");
 private static final Random random = new Random();
 private static final DatacenterEnum[] VALUES = values();
 private static final int SIZE = VALUES.length;
 private static final DatacenterEnum ourLocation = findLocation();
 private static final String LOCAL_PATH = ourLocation.findLocalPath();
 private static final Set<String> REMOTE_PATH = ourLocation.findRemotePath();
 private String value;
 private DatacenterEnum(String value) {
 this.value = value;
 }
 public String value() {
 return value;
 }
 public static String forCode(int code) {
 return (code >= 0 && code < SIZE) ? VALUES[code].name() : null;
 }
 private static DatacenterEnum findLocation() {
 StringOptional<String> ourhost = getHostNameOfServer();
 if (ourhost != null.isPresent()) {
 if (isDevHost(ourhost.get())) {
 return DHP;
 }
 for (DatacenterEnum dc : VALUES) {
 String namepart = "." + dc.name().toLowerCase() + ".";
 if (ourhost.get().indexOf(namepart) >= 0) {
 return dc;
 }
 }
 // this means the call is coming from some other datacenter apart from DHP, SLP and LTR, 
 // so we will randomly select DHP, SLP or LTR
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 // if it comes here then it means somehow, we failed to find the hostname.
 // so we will randomly select DHP, SLP or LTR 
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 private String findLocalPath() {
 String path = DatacenterEnum.DHP.value();
 if (DataUtils.isProduction()) {
 path = ourLocation.value();
 }
 return path;
 }
 private Set<String> findRemotePath() {
 Set<String> remotePath = new HashSet<String>();
 if (DataUtils.isProduction()) {
 // contains all DatacenterEnum except ourLocation
 Set<DatacenterEnum> remoteSet = EnumSet.complementOf(EnumSet.of(ourLocation));
 for (DatacenterEnum dc : remoteSet) {
 remotePath.add(dc.value());
 }
 } else {
 remotePath.add(DatacenterEnum.DHP.value());
 }
 return remotePath;
 }
 public static Set<String> getAllPaths(DataFlowEnum dataType) {
 Set<String> allPaths = new LinkedHashSet<String>();
 String prefix = dataType.equals(DataFlowEnum.PARTIAL) ? DataFlowEnum.PARTIAL.value() : DataFlowEnum.TEMP.value();
 allPaths.add(prefix + LOCAL_PATH);
 for (String path : REMOTE_PATH) {
 allPaths.add(prefix + path);
 }
 return allPaths;
 } 
 private static final StringOptional<String> getHostNameOfServer() {
 try {
 return Optional.of(InetAddress.getLocalHost().getCanonicalHostName().toLowerCase());
 } catch (UnknownHostException ex) {
 // logging error
 }
 return Optional.absent();
 return null;}
 }
 private static boolean isDevHost(String hostName) {
 return hostName.indexOf(".dev.") >= 0;
 }
 public static DatacenterEnum getCurrentDatacenter() {
 return ourLocation;
 }
}
public enum DatacenterEnum {
 DHP("/datacenter/dhp"), SLP("/datacenter/slp"), LTR("/datacenter/ltr");
 private static final Random random = new Random();
 private static final DatacenterEnum[] VALUES = values();
 private static final int SIZE = VALUES.length;
 private static final DatacenterEnum ourLocation = findLocation();
 private static final String LOCAL_PATH = ourLocation.findLocalPath();
 private static final Set<String> REMOTE_PATH = ourLocation.findRemotePath();
 private String value;
 private DatacenterEnum(String value) {
 this.value = value;
 }
 public String value() {
 return value;
 }
 public static String forCode(int code) {
 return (code >= 0 && code < SIZE) ? VALUES[code].name() : null;
 }
 private static DatacenterEnum findLocation() {
 String ourhost = getHostNameOfServer();
 if (ourhost != null) {
 if (isDevHost(ourhost)) {
 return DHP;
 }
 for (DatacenterEnum dc : VALUES) {
 String namepart = "." + dc.name().toLowerCase() + ".";
 if (ourhost.indexOf(namepart) >= 0) {
 return dc;
 }
 }
 // this means the call is coming from some other datacenter apart from DHP, SLP and LTR, 
 // so we will randomly select DHP, SLP or LTR
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 // if it comes here then it means somehow, we failed to find the hostname.
 // so we will randomly select DHP, SLP or LTR 
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 private String findLocalPath() {
 String path = DatacenterEnum.DHP.value();
 if (DataUtils.isProduction()) {
 path = ourLocation.value();
 }
 return path;
 }
 private Set<String> findRemotePath() {
 Set<String> remotePath = new HashSet<String>();
 if (DataUtils.isProduction()) {
 // contains all DatacenterEnum except ourLocation
 Set<DatacenterEnum> remoteSet = EnumSet.complementOf(EnumSet.of(ourLocation));
 for (DatacenterEnum dc : remoteSet) {
 remotePath.add(dc.value());
 }
 } else {
 remotePath.add(DatacenterEnum.DHP.value());
 }
 return remotePath;
 }
 public static Set<String> getAllPaths(DataFlowEnum dataType) {
 Set<String> allPaths = new LinkedHashSet<String>();
 String prefix = dataType.equals(DataFlowEnum.PARTIAL) ? DataFlowEnum.PARTIAL.value() : DataFlowEnum.TEMP.value();
 allPaths.add(prefix + LOCAL_PATH);
 for (String path : REMOTE_PATH) {
 allPaths.add(prefix + path);
 }
 return allPaths;
 } 
 private static final String getHostNameOfServer() {
 try {
 return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
 } catch (UnknownHostException ex) {
 // logging error
 }
 return null;
 }
 private static boolean isDevHost(String hostName) {
 return hostName.indexOf(".dev.") >= 0;
 }
 public static DatacenterEnum getCurrentDatacenter() {
 return ourLocation;
 }
}
public enum DatacenterEnum {
 DHP("/datacenter/dhp"), SLP("/datacenter/slp"), LTR("/datacenter/ltr");
 private static final Random random = new Random();
 private static final DatacenterEnum[] VALUES = values();
 private static final int SIZE = VALUES.length;
 private static final DatacenterEnum ourLocation = findLocation();
 private static final String LOCAL_PATH = ourLocation.findLocalPath();
 private static final Set<String> REMOTE_PATH = ourLocation.findRemotePath();
 private String value;
 private DatacenterEnum(String value) {
 this.value = value;
 }
 public String value() {
 return value;
 }
 public static String forCode(int code) {
 return (code >= 0 && code < SIZE) ? VALUES[code].name() : null;
 }
 private static DatacenterEnum findLocation() {
 Optional<String> ourhost = getHostNameOfServer();
 if (ourhost.isPresent()) {
 if (isDevHost(ourhost.get())) {
 return DHP;
 }
 for (DatacenterEnum dc : VALUES) {
 String namepart = "." + dc.name().toLowerCase() + ".";
 if (ourhost.get().indexOf(namepart) >= 0) {
 return dc;
 }
 }
 // this means the call is coming from some other datacenter apart from DHP, SLP and LTR, 
 // so we will randomly select DHP, SLP or LTR
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 // if it comes here then it means somehow, we failed to find the hostname.
 // so we will randomly select DHP, SLP or LTR 
 return DataUtils.isProduction() ? VALUES[random.nextInt(SIZE)] : DHP;
 }
 private String findLocalPath() {
 String path = DatacenterEnum.DHP.value();
 if (DataUtils.isProduction()) {
 path = ourLocation.value();
 }
 return path;
 }
 private Set<String> findRemotePath() {
 Set<String> remotePath = new HashSet<String>();
 if (DataUtils.isProduction()) {
 // contains all DatacenterEnum except ourLocation
 Set<DatacenterEnum> remoteSet = EnumSet.complementOf(EnumSet.of(ourLocation));
 for (DatacenterEnum dc : remoteSet) {
 remotePath.add(dc.value());
 }
 } else {
 remotePath.add(DatacenterEnum.DHP.value());
 }
 return remotePath;
 }
 public static Set<String> getAllPaths(DataFlowEnum dataType) {
 Set<String> allPaths = new LinkedHashSet<String>();
 String prefix = dataType.equals(DataFlowEnum.PARTIAL) ? DataFlowEnum.PARTIAL.value() : DataFlowEnum.TEMP.value();
 allPaths.add(prefix + LOCAL_PATH);
 for (String path : REMOTE_PATH) {
 allPaths.add(prefix + path);
 }
 return allPaths;
 } 
 private static final Optional<String> getHostNameOfServer() {
 try {
 return Optional.of(InetAddress.getLocalHost().getCanonicalHostName().toLowerCase());
 } catch (UnknownHostException ex) {
 // logging error
  return Optional.absent();
 }
 }
 private static boolean isDevHost(String hostName) {
 return hostName.indexOf(".dev.") >= 0;
 }
 public static DatacenterEnum getCurrentDatacenter() {
 return ourLocation;
 }
}
deleted 9 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48
Loading
deleted 2 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48
Loading
Source Link
david
  • 2k
  • 8
  • 32
  • 48
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /