RegistryCenterFactory { public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final Optional<String> digest) { Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace, Charsets.UTF_8); if (digest.isPresent()) { hasher.putString(digest.get(), Charsets.UTF_8); } HashCode hashCode = hasher.hash(); if (registryCenterMap.containsKey(hashCode)) { return registryCenterMap.get(hashCode); } ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace); if (digest.isPresent()) { zkConfig.setDigest(digest.get()); } CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig); result.init(); registryCenterMap.putIfAbsent(hashCode, result); return result; }  static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final Optional<String> digest);  }
@Test public void assertCreateCoordinatorRegistryCenterWithoutDigest() throws ReflectiveOperationException { ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent())); assertThat(zkConfig.getNamespace(), is("namespace")); assertNull(zkConfig.getDigest()); }
@Test public void assertCreateCoordinatorRegistryCenterWithDigest() throws ReflectiveOperationException { ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "namespace", Optional.of("digest"))); assertThat(zkConfig.getNamespace(), is("namespace")); assertThat(zkConfig.getDigest(), is("digest")); }
@Test public void assertCreateCoordinatorRegistryCenterFromCache() throws ReflectiveOperationException { RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "otherNamespace", Optional.<String>absent()); ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "otherNamespace", Optional.<String>absent())); assertThat(zkConfig.getNamespace(), is("otherNamespace")); assertNull(zkConfig.getDigest()); }