Skip to content

Commit

Permalink
[HWKMETRICS-128] Move the manifest information code to a util class s…
Browse files Browse the repository at this point in the history
…o it can be used in multiple locations. Also update the base handler to make use of it.
  • Loading branch information
Stefan Negrea committed Aug 26, 2015
1 parent af73b08 commit a54dbed
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -29,6 +32,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.wordnik.swagger.annotations.ApiOperation;
Expand All @@ -45,17 +49,14 @@ public class BaseHandler {
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Returns some basic information about the Hawkular Metrics service.",
response = String.class, responseContainer = "Map")
public Response baseJSON(@Context ServletContext context) {

String version = context.getInitParameter("hawkular.metrics.version");
if (version == null) {
version = "undefined";
}
public Response baseJSON(@Context ServletContext servletContext) {
Map<String, Object> hawkularMetricsProperties = new HashMap<>();
hawkularMetricsProperties.put("name", "Hawkular-Metrics");

HawkularMetricsBase hawkularMetrics = new HawkularMetricsBase();
hawkularMetrics.version = version;
hawkularMetricsProperties.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(hawkularMetrics).build();
return Response.ok(hawkularMetricsProperties).build();
}

@GET
Expand All @@ -66,25 +67,4 @@ public void baseHTML(@Context ServletContext context) throws Exception {
HttpServletResponse response = ResteasyProviderFactory.getContextData(HttpServletResponse.class);
request.getRequestDispatcher("/static/index.html").forward(request,response);
}

private class HawkularMetricsBase {

String name = "Hawkular-Metrics";
String version;

@SuppressWarnings("unused")
public String getName() {
return name;
}

@SuppressWarnings("unused")
public void setVersion(String version) {
this.version = version;
}

@SuppressWarnings("unused")
public String getVersion() {
return version;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

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;
Expand All @@ -34,6 +31,7 @@

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle.State;
import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;

import com.wordnik.swagger.annotations.ApiOperation;

Expand All @@ -46,9 +44,6 @@ public class StatusHandler {

public static final String PATH = "/status";

@Context
ServletContext servletContext;

@Inject
private MetricsServiceLifecycle metricsServiceLifecycle;

Expand All @@ -57,26 +52,15 @@ public class StatusHandler {
@GET
@ApiOperation(value = "Returns the current status for various components.",
response = String.class, responseContainer = "Map")
public Response status() {
public Response status(@Context ServletContext servletContext) {
Map<String, Object> status = new HashMap<>();

State metricState = metricsServiceLifecycle.getState();
status.put(METRICSSERVICE_NAME, metricState.toString());

this.getVersionInformation(status);
status.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

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
@@ -0,0 +1,57 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.util;

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

import javax.servlet.ServletContext;

public class ManifestInformation {

public static final List<String> VERSION_ATTRIBUTES = Collections
.unmodifiableList(Arrays.asList("Implementation-Version", "Built-From-Git-SHA1"));

public static Map<String, String> getManifestInformation(ServletContext servletContext, String... attributes) {
return getManifestInformation(servletContext, Arrays.asList(attributes));
}

public static Map<String, String> getManifestInformation(ServletContext servletContext, List<String> attributes) {
Map<String, String> manifestAttributes = new HashMap<>();
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
for (String attribute : attributes) {
manifestAttributes.put(attribute, attr.getValue(attribute));
}
} catch (Exception e) {
for (String attribute : attributes) {
if (manifestAttributes.get(attribute) == null) {
manifestAttributes.put(attribute, "Unknown");
}
}
}

return manifestAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -29,6 +32,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.wordnik.swagger.annotations.ApiOperation;
Expand All @@ -45,17 +49,14 @@ public class BaseHandler {
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Returns some basic information about the Hawkular Metrics service.",
response = String.class, responseContainer = "Map")
public Response baseJSON(@Context ServletContext context) {

String version = context.getInitParameter("hawkular.metrics.version");
if (version == null) {
version = "undefined";
}
public Response baseJSON(@Context ServletContext servletContext) {
Map<String, Object> hawkularMetricsProperties = new HashMap<>();
hawkularMetricsProperties.put("name", "Hawkular-Metrics");

HawkularMetricsBase hawkularMetrics = new HawkularMetricsBase();
hawkularMetrics.version = version;
hawkularMetricsProperties.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(hawkularMetrics).build();
return Response.ok(hawkularMetricsProperties).build();
}

@GET
Expand All @@ -66,22 +67,4 @@ public void baseHTML(@Context ServletContext context) throws Exception {
HttpServletResponse response = ResteasyProviderFactory.getContextData(HttpServletResponse.class);
request.getRequestDispatcher("/static/index.html").forward(request,response);
}

private class HawkularMetricsBase {

String name = "Hawkular-Metrics";
String version;

public String getName() {
return name;
}

public void setVersion(String version) {
this.version = version;
}

public String getVersion() {
return version;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

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;
Expand All @@ -34,6 +31,7 @@

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle.State;
import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;

import com.wordnik.swagger.annotations.ApiOperation;

Expand All @@ -46,9 +44,6 @@ public class StatusHandler {

public static final String PATH = "/status";

@Context
ServletContext servletContext;

@Inject
private MetricsServiceLifecycle metricsServiceLifecycle;

Expand All @@ -57,26 +52,17 @@ public class StatusHandler {
@GET
@ApiOperation(value = "Returns the current status for various components.",
response = String.class, responseContainer = "Map")
public Response status() {
public Response status(@Context ServletContext servletContext) {
Map<String, Object> status = new HashMap<>();

State metricState = metricsServiceLifecycle.getState();
status.put(METRICSSERVICE_NAME, metricState.toString());

this.getVersionInformation(status);
status.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

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
@@ -0,0 +1,57 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.metrics.api.jaxrs.util;

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

import javax.servlet.ServletContext;

public class ManifestInformation {

public static final List<String> VERSION_ATTRIBUTES = Collections
.unmodifiableList(Arrays.asList("Implementation-Version", "Built-From-Git-SHA1"));

public static Map<String, String> getManifestInformation(ServletContext servletContext, String... attributes) {
return getManifestInformation(servletContext, Arrays.asList(attributes));
}

public static Map<String, String> getManifestInformation(ServletContext servletContext, List<String> attributes) {
Map<String, String> manifestAttributes = new HashMap<>();
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
for (String attribute : attributes) {
manifestAttributes.put(attribute, attr.getValue(attribute));
}
} catch (Exception e) {
for (String attribute : attributes) {
if (manifestAttributes.get(attribute) == null) {
manifestAttributes.put(attribute, "Unknown");
}
}
}

return manifestAttributes;
}
}

0 comments on commit a54dbed

Please sign in to comment.