@Test public void testIllegal() throws Exception { ProviderInfo providerInfo = new ProviderInfo("test-name", 0); Application application = new Application(null, providerInfo, "test-group"); DemoKvStore store = new DemoKvStore(); try { new ProviderDelegatingRegister(store, application).register(); fail(); } catch (Throwable ignored) { } providerInfo.setPort(22); new ProviderDelegatingRegister(store, application).register(); application.setServerGroup(null); try { new ProviderDelegatingRegister(store, application).register(); fail(); } catch (Throwable ignored) { } }
public void register() throws Exception { String parentNode = String.join("/", Constants.STORE_ROOT, application.getServerGroup()); ProviderInfo providerInfo = Optional.ofNullable(application.getProviderInfo()) .orElse(new ProviderInfo()); if (providerInfo.getIp() == null) { InetAddress address = HostUtil.getLocalIv4Address(); if (address != null) providerInfo.setIp(address.getHostAddress()); else throw new IllegalStateException("cannot get local host IP address"); } if (providerInfo.getPort() == 0 || providerInfo.getName() == null) { throw new IllegalArgumentException("please check you provider info"); } String storeValue = String.format("%s/%s/%s", providerInfo.getIp(), providerInfo.getPort(), providerInfo.getWeight()); String storeKey = String.join("/", parentNode, providerInfo.getName()); store.createParentNode(parentNode); long reversion = store.put(storeKey, storeValue, true); log.info(String.format("registered server: `%s` in node: `%s`, reversion: %s", storeKey, storeValue, reversion)); }
ProviderDelegatingRegister { public void register() throws Exception { String parentNode = String.join("/", Constants.STORE_ROOT, application.getServerGroup()); ProviderInfo providerInfo = Optional.ofNullable(application.getProviderInfo()) .orElse(new ProviderInfo()); if (providerInfo.getIp() == null) { InetAddress address = HostUtil.getLocalIv4Address(); if (address != null) providerInfo.setIp(address.getHostAddress()); else throw new IllegalStateException("cannot get local host IP address"); } if (providerInfo.getPort() == 0 || providerInfo.getName() == null) { throw new IllegalArgumentException("please check you provider info"); } String storeValue = String.format("%s/%s/%s", providerInfo.getIp(), providerInfo.getPort(), providerInfo.getWeight()); String storeKey = String.join("/", parentNode, providerInfo.getName()); store.createParentNode(parentNode); long reversion = store.put(storeKey, storeValue, true); log.info(String.format("registered server: `%s` in node: `%s`, reversion: %s", storeKey, storeValue, reversion)); } }
ProviderDelegatingRegister { public void register() throws Exception { String parentNode = String.join("/", Constants.STORE_ROOT, application.getServerGroup()); ProviderInfo providerInfo = Optional.ofNullable(application.getProviderInfo()) .orElse(new ProviderInfo()); if (providerInfo.getIp() == null) { InetAddress address = HostUtil.getLocalIv4Address(); if (address != null) providerInfo.setIp(address.getHostAddress()); else throw new IllegalStateException("cannot get local host IP address"); } if (providerInfo.getPort() == 0 || providerInfo.getName() == null) { throw new IllegalArgumentException("please check you provider info"); } String storeValue = String.format("%s/%s/%s", providerInfo.getIp(), providerInfo.getPort(), providerInfo.getWeight()); String storeKey = String.join("/", parentNode, providerInfo.getName()); store.createParentNode(parentNode); long reversion = store.put(storeKey, storeValue, true); log.info(String.format("registered server: `%s` in node: `%s`, reversion: %s", storeKey, storeValue, reversion)); } ProviderDelegatingRegister(KVStore store, Application application); }
ProviderDelegatingRegister { public void register() throws Exception { String parentNode = String.join("/", Constants.STORE_ROOT, application.getServerGroup()); ProviderInfo providerInfo = Optional.ofNullable(application.getProviderInfo()) .orElse(new ProviderInfo()); if (providerInfo.getIp() == null) { InetAddress address = HostUtil.getLocalIv4Address(); if (address != null) providerInfo.setIp(address.getHostAddress()); else throw new IllegalStateException("cannot get local host IP address"); } if (providerInfo.getPort() == 0 || providerInfo.getName() == null) { throw new IllegalArgumentException("please check you provider info"); } String storeValue = String.format("%s/%s/%s", providerInfo.getIp(), providerInfo.getPort(), providerInfo.getWeight()); String storeKey = String.join("/", parentNode, providerInfo.getName()); store.createParentNode(parentNode); long reversion = store.put(storeKey, storeValue, true); log.info(String.format("registered server: `%s` in node: `%s`, reversion: %s", storeKey, storeValue, reversion)); } ProviderDelegatingRegister(KVStore store, Application application); void register(); }
ProviderDelegatingRegister { public void register() throws Exception { String parentNode = String.join("/", Constants.STORE_ROOT, application.getServerGroup()); ProviderInfo providerInfo = Optional.ofNullable(application.getProviderInfo()) .orElse(new ProviderInfo()); if (providerInfo.getIp() == null) { InetAddress address = HostUtil.getLocalIv4Address(); if (address != null) providerInfo.setIp(address.getHostAddress()); else throw new IllegalStateException("cannot get local host IP address"); } if (providerInfo.getPort() == 0 || providerInfo.getName() == null) { throw new IllegalArgumentException("please check you provider info"); } String storeValue = String.format("%s/%s/%s", providerInfo.getIp(), providerInfo.getPort(), providerInfo.getWeight()); String storeKey = String.join("/", parentNode, providerInfo.getName()); store.createParentNode(parentNode); long reversion = store.put(storeKey, storeValue, true); log.info(String.format("registered server: `%s` in node: `%s`, reversion: %s", storeKey, storeValue, reversion)); } ProviderDelegatingRegister(KVStore store, Application application); void register();  }