|
19 | 19 | import akka.cluster.Member;
|
20 | 20 | import com.arpnetworking.clusteraggregator.ClusterStatusCache;
|
21 | 21 | import com.arpnetworking.commons.builder.OvalBuilder;
|
| 22 | +import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory; |
| 23 | +import com.arpnetworking.steno.Logger; |
| 24 | +import com.arpnetworking.steno.LoggerFactory; |
22 | 25 | import com.fasterxml.jackson.annotation.JsonProperty;
|
23 | 26 | import com.fasterxml.jackson.core.JsonGenerator;
|
24 | 27 | import com.fasterxml.jackson.databind.JsonSerializer;
|
25 | 28 | import com.fasterxml.jackson.databind.SerializerProvider;
|
26 | 29 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
| 30 | +import com.google.common.base.Charsets; |
27 | 31 | import com.google.common.base.Optional;
|
28 | 32 | import com.google.common.collect.Iterables;
|
| 33 | +import com.google.common.io.Resources; |
| 34 | +import net.sf.oval.constraint.NotEmpty; |
| 35 | +import net.sf.oval.constraint.NotNull; |
29 | 36 | import org.joda.time.Period;
|
30 | 37 | import scala.collection.JavaConversions;
|
31 | 38 |
|
@@ -70,6 +77,18 @@ public Optional<List<ShardAllocation>> getAllocations() {
|
70 | 77 | return _allocations.transform(Collections::unmodifiableList);
|
71 | 78 | }
|
72 | 79 |
|
| 80 | + public String getVersion() { |
| 81 | + return VERSION_INFO.getVersion(); |
| 82 | + } |
| 83 | + |
| 84 | + public String getName() { |
| 85 | + return VERSION_INFO.getName(); |
| 86 | + } |
| 87 | + |
| 88 | + public String getSha() { |
| 89 | + return VERSION_INFO.getSha(); |
| 90 | + } |
| 91 | + |
73 | 92 | private StatusResponse(final Builder builder) {
|
74 | 93 | if (builder._clusterState == null) {
|
75 | 94 | _clusterLeader = null;
|
@@ -101,6 +120,31 @@ private <T> Optional<T> flatten(final Optional<Optional<T>> value) {
|
101 | 120 | private final Map<Period, PeriodMetrics> _localMetrics;
|
102 | 121 | private final Optional<List<ShardAllocation>> _allocations;
|
103 | 122 |
|
| 123 | + private static final VersionInfo VERSION_INFO; |
| 124 | + private static final Logger LOGGER = LoggerFactory.getLogger(StatusResponse.class); |
| 125 | + |
| 126 | + static { |
| 127 | + StatusResponse.VersionInfo versionInfo = null; |
| 128 | + try { |
| 129 | + versionInfo = |
| 130 | + ObjectMapperFactory.getInstance().readValue( |
| 131 | + Resources.toString(Resources.getResource("status.json"), Charsets.UTF_8), |
| 132 | + StatusResponse.VersionInfo.class); |
| 133 | + } catch (final IOException e) { |
| 134 | + LOGGER.error() |
| 135 | + .setMessage("Resource load failure") |
| 136 | + .addData("resource", "status.json") |
| 137 | + .setThrowable(e) |
| 138 | + .log(); |
| 139 | + versionInfo = new StatusResponse.VersionInfo.Builder() |
| 140 | + .setVersion("UNKNOWN") |
| 141 | + .setName("UNKNOWN") |
| 142 | + .setSha("") |
| 143 | + .build(); |
| 144 | + } |
| 145 | + VERSION_INFO = versionInfo; |
| 146 | + } |
| 147 | + |
104 | 148 | /**
|
105 | 149 | * Builder for a {@link StatusResponse}.
|
106 | 150 | */
|
@@ -162,6 +206,90 @@ public Builder setLocalMetrics(final Map<Period, PeriodMetrics> value) {
|
162 | 206 | private Map<Period, PeriodMetrics> _localMetrics;
|
163 | 207 | }
|
164 | 208 |
|
| 209 | + /** |
| 210 | + * Represents the model for the version of the service currently running. |
| 211 | + * |
| 212 | + * @author Brandon Arp (brandon dot arp at smartsheet dot com) |
| 213 | + */ |
| 214 | + private static final class VersionInfo { |
| 215 | + public String getName() { |
| 216 | + return _name; |
| 217 | + } |
| 218 | + |
| 219 | + public String getVersion() { |
| 220 | + return _version; |
| 221 | + } |
| 222 | + |
| 223 | + public String getSha() { |
| 224 | + return _sha; |
| 225 | + } |
| 226 | + |
| 227 | + private VersionInfo(final Builder builder) { |
| 228 | + _name = builder._name; |
| 229 | + _version = builder._version; |
| 230 | + _sha = builder._sha; |
| 231 | + } |
| 232 | + |
| 233 | + private String _name; |
| 234 | + private String _version; |
| 235 | + private String _sha; |
| 236 | + |
| 237 | + /** |
| 238 | + * Builder for a {@link VersionInfo}. |
| 239 | + */ |
| 240 | + private static final class Builder extends OvalBuilder<VersionInfo> { |
| 241 | + /** |
| 242 | + * Public constructor. |
| 243 | + */ |
| 244 | + private Builder() { |
| 245 | + super(VersionInfo::new); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * Sets the name of the application. Required. Cannot be null. Cannot be empty. |
| 250 | + * |
| 251 | + * @param value The name of the application. |
| 252 | + * @return This builder. |
| 253 | + */ |
| 254 | + public Builder setName(final String value) { |
| 255 | + _name = value; |
| 256 | + return this; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Sets the name or tag of the version. Required. Cannot be null. Cannot be empty. |
| 261 | + * |
| 262 | + * @param value The name of the version. |
| 263 | + * @return This builder. |
| 264 | + */ |
| 265 | + public Builder setVersion(final String value) { |
| 266 | + _version = value; |
| 267 | + return this; |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Sets the SHA. Required. Cannot be null. Cannot be empty. |
| 272 | + * |
| 273 | + * @param value The SHA. |
| 274 | + * @return This builder. |
| 275 | + */ |
| 276 | + public Builder setSha(final String value) { |
| 277 | + _sha = value; |
| 278 | + return this; |
| 279 | + } |
| 280 | + |
| 281 | + @NotNull |
| 282 | + @NotEmpty |
| 283 | + private String _name; |
| 284 | + @NotNull |
| 285 | + @NotEmpty |
| 286 | + private String _version; |
| 287 | + @NotNull |
| 288 | + @NotEmpty |
| 289 | + private String _sha; |
| 290 | + } |
| 291 | + } |
| 292 | + |
165 | 293 | private static final class MemberSerializer extends JsonSerializer<Member> {
|
166 | 294 | /**
|
167 | 295 | * {@inheritDoc}
|
|
0 commit comments