Application.java 5.69 KB
Newer Older
Dennis Schulte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.codecentric.boot.admin.model;

import java.io.Serializable;
19

20 21 22
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

23 24
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Dennis Schulte committed
25 26 27 28 29 30 31

/**
 * The domain model for all registered application at the spring boot admin application.
 */
public class Application implements Serializable {
	private static final long serialVersionUID = 1L;

32 33
	private final String id;
	private final String name;
34 35 36
	private final String managementUrl;
	private final String healthUrl;
	private final String serviceUrl;
37
	private final StatusInfo statusInfo;
Dennis Schulte committed
38

39 40
	protected Application(String healthUrl, String managementUrl, String serviceUrl, String name,
			String id, StatusInfo statusInfo) {
41 42 43
		this.healthUrl = healthUrl;
		this.managementUrl = managementUrl;
		this.serviceUrl = serviceUrl;
44
		this.name = name;
Dennis Schulte committed
45
		this.id = id;
46
		this.statusInfo = statusInfo != null ? statusInfo : StatusInfo.ofUnknown();
Dennis Schulte committed
47 48
	}

49 50 51 52
	@JsonCreator
	public static Application create(@JsonProperty("url") String url,
			@JsonProperty("managementUrl") String managementUrl,
			@JsonProperty("healthUrl") String healthUrl,
53 54
			@JsonProperty("serviceUrl") String serviceUrl, @JsonProperty("name") String name,
			@JsonProperty("id") String id, @JsonProperty("statusInfo") StatusInfo statusInfo) {
55 56 57 58

		Assert.hasText(name, "name must not be empty!");
		if (StringUtils.hasText(url)) {
			// old format
59 60 61
			return new Application(url.replaceFirst("/+$", "") + "/health", url, null, name, id,
					statusInfo);
		} else {
62
			Assert.hasText(healthUrl, "healthUrl must not be empty!");
63
			return new Application(healthUrl, managementUrl, serviceUrl, name, id, statusInfo);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		}
	}

	public static Builder create(String name) {
		return new Builder(name);
	}

	public static Builder create(Application application) {
		return new Builder(application);
	}

	public static class Builder {
		private String id;
		private String name;
		private String managementUrl;
		private String healthUrl;
		private String serviceUrl;
		private StatusInfo statusInfo;

		private Builder(String name) {
			this.name = name;
		}

		private Builder(Application application) {
			this.healthUrl = application.healthUrl;
			this.managementUrl = application.managementUrl;
			this.serviceUrl = application.serviceUrl;
			this.name = application.name;
			this.id = application.id;
			this.statusInfo = application.statusInfo;
		}

		public Builder withName(String name) {
			this.name = name;
			return this;
		}

		public Builder withId(String id) {
			this.id = id;
			return this;
		}

		public Builder withServiceUrl(String serviceUrl) {
			this.serviceUrl = serviceUrl;
			return this;
		}

		public Builder withHealthUrl(String healthUrl) {
			this.healthUrl = healthUrl;
			return this;
		}

		public Builder withManagementUrl(String managementUrl) {
			this.managementUrl = managementUrl;
			return this;
		}

		public Builder withStatusInfo(StatusInfo statusInfo) {
			this.statusInfo = statusInfo;
			return this;
		}

		public Application build() {
127
			return new Application(healthUrl, managementUrl, serviceUrl, name, id, statusInfo);
128 129 130
		}
	}

131 132
	public String getId() {
		return id;
Dennis Schulte committed
133 134
	}

135 136
	public String getName() {
		return name;
Dennis Schulte committed
137 138
	}

139 140
	public String getManagementUrl() {
		return managementUrl;
141 142
	}

143 144 145 146 147 148 149 150
	public String getHealthUrl() {
		return healthUrl;
	}

	public String getServiceUrl() {
		return serviceUrl;
	}

151 152 153 154
	public StatusInfo getStatusInfo() {
		return statusInfo;
	}

155 156
	@Override
	public String toString() {
157 158
		return "Application [id=" + id + ", name=" + name + ", managementUrl=" + managementUrl
				+ ", healthUrl=" + healthUrl + ", serviceUrl=" + serviceUrl + "]";
Dennis Schulte committed
159 160 161 162 163 164
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
165
		result = prime * result + ((healthUrl == null) ? 0 : healthUrl.hashCode());
Johannes Stelzer committed
166
		result = prime * result + ((id == null) ? 0 : id.hashCode());
167
		result = prime * result + ((managementUrl == null) ? 0 : managementUrl.hashCode());
168
		result = prime * result + ((name == null) ? 0 : name.hashCode());
169
		result = prime * result + ((serviceUrl == null) ? 0 : serviceUrl.hashCode());
Dennis Schulte committed
170 171 172 173 174
		return result;
	}

	@Override
	public boolean equals(Object obj) {
175
		if (this == obj) {
Dennis Schulte committed
176
			return true;
177 178
		}
		if (obj == null) {
Dennis Schulte committed
179
			return false;
180 181
		}
		if (getClass() != obj.getClass()) {
Dennis Schulte committed
182
			return false;
183
		}
Dennis Schulte committed
184
		Application other = (Application) obj;
185 186 187 188
		if (healthUrl == null) {
			if (other.healthUrl != null) {
				return false;
			}
189
		} else if (!healthUrl.equals(other.healthUrl)) {
190 191
			return false;
		}
Johannes Stelzer committed
192 193 194 195
		if (id == null) {
			if (other.id != null) {
				return false;
			}
196
		} else if (!id.equals(other.id)) {
197 198 199 200 201 202
			return false;
		}
		if (managementUrl == null) {
			if (other.managementUrl != null) {
				return false;
			}
203
		} else if (!managementUrl.equals(other.managementUrl)) {
Johannes Stelzer committed
204 205
			return false;
		}
206 207
		if (name == null) {
			if (other.name != null) {
Dennis Schulte committed
208
				return false;
209
			}
210
		} else if (!name.equals(other.name)) {
Dennis Schulte committed
211
			return false;
212
		}
213 214
		if (serviceUrl == null) {
			if (other.serviceUrl != null) {
Dennis Schulte committed
215
				return false;
216
			}
217
		} else if (!serviceUrl.equals(other.serviceUrl)) {
Dennis Schulte committed
218
			return false;
219
		}
Dennis Schulte committed
220 221 222 223
		return true;
	}

}