org.zodiac.monitor.console.model.Registration Maven / Gradle / Ivy
package org.zodiac.monitor.console.model;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import org.springframework.lang.Nullable;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
public class Registration implements Serializable {
private static final long serialVersionUID = 6251381108615591675L;
private final String name;
@Nullable
private final String managementUrl;
private final String healthUrl;
@Nullable
private final String serviceUrl;
private final String source;
private final Map metadata;
//@lombok.Builder(builderClassName = "Builder", toBuilder = true)
public Registration(String name, @Nullable String managementUrl, String healthUrl, @Nullable String serviceUrl,
String source) {
AssertUtil.notBlank(name, "'name' must not be empty.");
AssertUtil.notBlank(healthUrl, "'healthUrl' must not be empty.");
AssertUtil.isTrue(checkUrl(healthUrl), "'healthUrl' is not valid: " + healthUrl);
AssertUtil.isTrue(StrUtil.isEmpty(managementUrl) || checkUrl(managementUrl),
"'managementUrl' is not valid: " + managementUrl);
AssertUtil.isTrue(StrUtil.isEmpty(serviceUrl) || checkUrl(serviceUrl),
"'serviceUrl' is not valid: " + serviceUrl);
this.name = name;
this.managementUrl = managementUrl;
this.healthUrl = healthUrl;
this.serviceUrl = serviceUrl;
this.source = source;
this.metadata = CollUtil.concurrentMap();
metadata.put("startup", Instant.now().toString());
}
public String getName() {
return name;
}
public String getManagementUrl() {
return managementUrl;
}
public String getHealthUrl() {
return healthUrl;
}
public String getServiceUrl() {
return serviceUrl;
}
public String getSource() {
return source;
}
public Map getMetadata() {
return Collections.unmodifiableMap(metadata);
}
public Builder toBuilder() {
return new Builder();
}
/**
* Checks the syntax of the given URL.
*
* @param url The URL.
* @return true, if valid.
*/
private boolean checkUrl(String url) {
try {
URI uri = new URI(url);
return uri.isAbsolute();
} catch (URISyntaxException e) {
return false;
}
}
@Override
public String toString() {
return "Registration [name=" + name + ", managementUrl=" + managementUrl + ", healthUrl=" + healthUrl
+ ", serviceUrl=" + serviceUrl + ", source=" + source + "]";
}
public static Registration.Builder create(String name, String healthUrl) {
return builder().name(name).healthUrl(healthUrl);
}
public static Registration.Builder copyOf(Registration registration) {
return registration.toBuilder();
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
private String managementUrl;
private String healthUrl;
private String serviceUrl;
private String source;
private Builder() {
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder managementUrl(String managementUrl) {
this.managementUrl = managementUrl;
return this;
}
public Builder healthUrl(String healthUrl) {
this.healthUrl = healthUrl;
return this;
}
public Builder serviceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
return this;
}
public Builder source(String source) {
this.source = source;
return this;
}
public Registration build() {
return new Registration(name, managementUrl, healthUrl, serviceUrl, source);
}
}
}