Skip to content

Commit

Permalink
Add a simple status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Nov 10, 2015
1 parent 0da119a commit 53dafd4
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
@@ -0,0 +1,67 @@
/*
* Copyright 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.alerts.rest;

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

import javax.enterprise.context.ApplicationScoped;
import javax.servlet.ServletContext;

import com.google.common.collect.ImmutableList;

/**
* Manifest extraction.
* Credits to Hawkular Metrics team.
*
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@ApplicationScoped
public class ManifestUtil {
private static final String IMPLEMENTATION_VERSION = "Implementation-Version";
private static final String BUILT_FROM_GIT = "Built-From-Git-SHA1";

private static final List<String> VERSION_ATTRIBUTES = ImmutableList.of(IMPLEMENTATION_VERSION,
BUILT_FROM_GIT);

private Map<String, String> manifestInformation = new HashMap<>();

public Map<String, String> getFrom(ServletContext servletContext) {
if (!manifestInformation.containsKey(IMPLEMENTATION_VERSION) &&
!manifestInformation.containsKey(BUILT_FROM_GIT)) {
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
for (String attribute : VERSION_ATTRIBUTES) {
manifestInformation.put(attribute, attr.getValue(attribute));
}
} catch (Exception e) {
for (String attribute : VERSION_ATTRIBUTES) {
if (manifestInformation.get(attribute) == null) {
manifestInformation.put(attribute, "Unknown");
}
}
}
}
return manifestInformation;
}
}
@@ -0,0 +1,71 @@
/*
* Copyright 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.alerts.rest;

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

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

import javax.ejb.EJB;
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.alerts.api.services.DefinitionsService;

import com.wordnik.swagger.annotations.Api;

/**
* REST endpoint for status
*
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@Path("/status")
@Api(value = "/status", description = "Status of Alerts Service")
public class StatusHandler {
private static final String STATUS = "status";
private static final String STARTED = "STARTED";
private static final String FAILED = "FAILED";

@EJB
DefinitionsService definitionsService;

@Inject
ManifestUtil manifestUtil;

@GET
@Path("/")
@Produces(APPLICATION_JSON)
public Response status(@Context ServletContext servletContext) {
Map<String, String> status = new HashMap<>();
status.putAll(manifestUtil.getFrom(servletContext));
try {
definitionsService.getActionPlugins();
status.put(STATUS, STARTED);
} catch (Exception e) {
status.put(STATUS, FAILED);
}
return ResponseUtil.ok(status);
}

}

1 comment on commit 53dafd4

@pilhuhn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.