From 25573132a07481b8c0b855fe525257a21b97fa9b Mon Sep 17 00:00:00 2001 From: Tomasz Bak Date: Sun, 22 Feb 2015 22:45:56 -0800 Subject: [PATCH] Add more columns to jar info page. JarsInfoResource is extended to provide full manifest info resources endpoint, but the UI part is still not implemented. --- .../resources/JarsInfoResource.java | 124 +++++++++++++----- .../src/main/resources/webadmin/jars/index.js | 17 ++- 2 files changed, 104 insertions(+), 37 deletions(-) diff --git a/karyon2-admin-web/src/main/java/netflix/adminresources/resources/JarsInfoResource.java b/karyon2-admin-web/src/main/java/netflix/adminresources/resources/JarsInfoResource.java index a70fc19f..bab5d5af 100644 --- a/karyon2-admin-web/src/main/java/netflix/adminresources/resources/JarsInfoResource.java +++ b/karyon2-admin-web/src/main/java/netflix/adminresources/resources/JarsInfoResource.java @@ -16,14 +16,9 @@ package netflix.adminresources.resources; -import com.google.common.annotations.Beta; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -37,6 +32,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.google.common.annotations.Beta; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author pkamath * @author Nitesh Kant @@ -50,21 +51,42 @@ public class JarsInfoResource { private static final String JAR_PATTERN = "^jar:file:(.+)!/META-INF/MANIFEST.MF$"; + private final List jarManifests; + private final ArrayList jarInfos; + + public JarsInfoResource() { + jarManifests = loadJarManifests(); + jarInfos = new ArrayList<>(); + for (JarManifest jm : jarManifests) { + jarInfos.add(jm.toJarInfo()); + } + } + @GET public Response getAllJarsInfo() { - List jarInfo = getJarInfo(); GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls(); Gson gson = gsonBuilder.create(); - String propsJson = gson.toJson(new KaryonAdminResponse(jarInfo)); + String propsJson = gson.toJson(new KaryonAdminResponse(jarInfos)); + return Response.ok(propsJson).build(); + } + + @GET + @Path("/{id}") + public Response getJarManifest(@PathParam("id") int jarId) { + GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls(); + Gson gson = gsonBuilder.create(); + String propsJson = gson.toJson(new KaryonAdminResponse(jarManifests.get(jarId))); return Response.ok(propsJson).build(); } - private List getJarInfo() { - List toReturn = new ArrayList(); + private static List loadJarManifests() { + List jarManifests = new ArrayList<>(); + Pattern pattern = Pattern.compile(JAR_PATTERN); try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Enumeration urls = cl.getResources("META-INF/MANIFEST.MF"); + int id = 0; while (urls.hasMoreElements()) { URL manifestURL = urls.nextElement(); InputStream is = manifestURL.openStream(); @@ -73,37 +95,64 @@ private List getJarInfo() { if (matcher.matches()) { key = matcher.group(1); } - Attributes mainAttributes = new Manifest(is).getMainAttributes(); - toReturn.add(new JarInfo(key, mainAttributes)); + jarManifests.add(new JarManifest(id, key, new Manifest(is))); is.close(); + id++; } } catch (Exception e) { logger.error("Failed to load environment jar information.", e); } - return toReturn; + + return jarManifests; + } + + private static class JarManifest { + private final int id; + private final String jarName; + private final Manifest manifest; + + private JarManifest(int id, String jarName, Manifest manifest) { + this.id = id; + this.jarName = jarName; + this.manifest = manifest; + } + + public String getJarName() { + return jarName; + } + + public Manifest getManifest() { + return manifest; + } + + public JarInfo toJarInfo() { + return new JarInfo(id, jarName, manifest.getMainAttributes()); + } } private static class JarInfo { public static final String MANIFEST_VERSION = "Manifest-Version"; public static final String CREATED_BY = "Created-By"; - public static final String UNAVAILABLE = "Unavailable"; - @SuppressWarnings("unused") - private String jar; - @SuppressWarnings("unused") - private String createdBy = UNAVAILABLE; - @SuppressWarnings("unused") - private String manifestVersion = UNAVAILABLE; - - public JarInfo(String key, Attributes mainAttributes) { - jar = key; - if (null != mainAttributes.getValue(MANIFEST_VERSION)) { - manifestVersion = String.valueOf(mainAttributes.getValue(MANIFEST_VERSION)); - } + public static final String BUILD_DATE = "Build-Date"; + public static final String BUILD_NUMBER = "Build-Number"; + public static final String BUILT_BY = "Built-By"; + public static final String UNAVAILABLE = "-"; - if (null != mainAttributes.getValue(CREATED_BY)) { - createdBy = String.valueOf(mainAttributes.getValue(CREATED_BY)); - } + private final int id; + private final String jar; + private final String createdBy; + private final String buildDate; + private final String buildNumber; + private final String builtBy; + + private JarInfo(int id, String jar, Attributes mainAttributes) { + this.id = id; + this.jar = jar; + createdBy = valueOf(mainAttributes, CREATED_BY); + buildDate = valueOf(mainAttributes, BUILD_DATE); + buildNumber = valueOf(mainAttributes, BUILD_NUMBER); + builtBy = valueOf(mainAttributes, BUILT_BY); } public String getJar() { @@ -114,8 +163,21 @@ public String getCreatedBy() { return createdBy; } - public String getManifestVersion() { - return manifestVersion; + public String getBuildDate() { + return buildDate; + } + + public String getBuildNumber() { + return buildNumber; + } + + public String getBuiltBy() { + return builtBy; + } + + private static String valueOf(Attributes mainAttributes, String tag) { + String value = mainAttributes.getValue(tag); + return value == null ? UNAVAILABLE : value; } } } diff --git a/karyon2-admin-web/src/main/resources/webadmin/jars/index.js b/karyon2-admin-web/src/main/resources/webadmin/jars/index.js index 8c30931b..ce221d9f 100644 --- a/karyon2-admin-web/src/main/resources/webadmin/jars/index.js +++ b/karyon2-admin-web/src/main/resources/webadmin/jars/index.js @@ -7,9 +7,11 @@ $(document).ready(function() { var oTable = $('#jars-table').dataTable( { "aoColumns": [ - { "sTitle": "Jar" , "mDataProp" : "name", "sDefaultContent": "", "sWidth" : "30%" }, - { "sTitle": "Owner" , "mDataProp" : "libraryOwner", "sDefaultContent": "", "sWidth" : "10%" }, - { "sTitle": "Version" , "mDataProp" : "implementationVersion", "sDefaultContent": "", "sWidth" : "10%" } + { "sTitle": "Jar" , "mDataProp" : "name", "sDefaultContent": "", "sWidth" : "30%" }, + { "sTitle": "Created By" , "mDataProp" : "createdBy", "sDefaultContent": "", "sWidth" : "10%" }, + { "sTitle": "Build date" , "mDataProp" : "buildDate", "sDefaultContent": "", "sWidth" : "6%" }, + { "sTitle": "Build number" , "mDataProp" : "buildNumber", "sDefaultContent": "", "sWidth" : "5%" }, + { "sTitle": "Built by" , "mDataProp" : "builtBy", "sDefaultContent": "", "sWidth" : "5%" } ], "sAjaxSource": source, "fnServerData": function ( sSource, aoData, fnCallback ) { @@ -26,9 +28,12 @@ $(document).ready(function() { var items = []; $.each(json.data, function(i, obj) { items.push({ + 'id' : obj.id, 'name' : obj.jar, - 'libraryOwner' : obj.createdBy, - 'implementationVersion' : obj.manifestVersion + 'createdBy' : obj.createdBy, + 'buildDate' : obj.buildDate, + 'buildNumber' : obj.buildNumber, + 'builtBy' : obj.builtBy }); }); @@ -50,7 +55,7 @@ $(document).ready(function() { return ""; } }); - + $(".bse-filter").val(""); $("#jars-table_filter").hide();