Skip to content

Commit

Permalink
[HWKMETRICS-128] Update status handler to return war manifest informa…
Browse files Browse the repository at this point in the history
…tion regarding current running version.
  • Loading branch information
Stefan Negrea committed Aug 25, 2015
1 parent 780823c commit f09b9a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
Expand All @@ -41,6 +46,9 @@ public class StatusHandler {

public static final String PATH = "/status";

@Context
ServletContext servletContext;

@Inject
private MetricsServiceLifecycle metricsServiceLifecycle;

Expand All @@ -55,6 +63,20 @@ public Response status() {
State metricState = metricsServiceLifecycle.getState();
status.put(METRICSSERVICE_NAME, metricState.toString());

this.getVersionInformation(status);

return Response.ok(status).build();
}

private void getVersionInformation(Map<String, Object> status) {
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
status.put("Implementation-Version", attr.getValue("Implementation-Version"));
status.put("Built-From-Git-SHA1", attr.getValue("Built-From-Git-SHA1"));
} catch (Exception e) {
status.put("Implementation-Version", "Unknown");
status.put("Built-From-Git-SHA1", "Unknown");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
Expand All @@ -41,6 +46,9 @@ public class StatusHandler {

public static final String PATH = "/status";

@Context
ServletContext servletContext;

@Inject
private MetricsServiceLifecycle metricsServiceLifecycle;

Expand All @@ -55,6 +63,20 @@ public Response status() {
State metricState = metricsServiceLifecycle.getState();
status.put(METRICSSERVICE_NAME, metricState.toString());

this.getVersionInformation(status);

return Response.ok(status).build();
}

private void getVersionInformation(Map<String, Object> status) {
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
status.put("Implementation-Version", attr.getValue("Implementation-Version"));
status.put("Built-From-Git-SHA1", attr.getValue("Built-From-Git-SHA1"));
} catch(Exception e){
status.put("Implementation-Version", "Unknown");
status.put("Built-From-Git-SHA1", "Unknown");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.hawkular.metrics.rest

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotEquals
import static org.junit.Assert.assertNotNull

import org.junit.Test

Expand All @@ -30,8 +32,12 @@ class StatusITest extends RESTTest {

assertEquals(200, response.status);

def expectedData = ["MetricsService" : "STARTED"];
def expectedState = ["MetricsService" : "STARTED"];

assertEquals(expectedData, response.data);
assertEquals(expectedState.MetricsService, response.data.MetricsService);
assertNotNull(response.data["Implementation-Version"]);
assertNotNull(response.data["Built-From-Git-SHA1"]);
assertNotEquals("Unknown", response.data["Implementation-Version"]);
assertNotEquals("Unknown", response.data["Built-From-Git-SHA1"]);
}
}

0 comments on commit f09b9a7

Please sign in to comment.