From c4f91a961818c0ce07b3a13615c620edc504006f Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Mon, 30 Sep 2013 17:17:05 +0200
Subject: [PATCH] Added more detailed version information

---
 .gitignore                                    |  1 +
 pom.xml                                       | 21 ++++++++
 .../textgrid/services/aggregator/Version.java | 50 +++++++++++++++----
 3 files changed, 61 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index 0e2b0a7..afc5c32 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 /.settings
 /.classpath
 /target
+src/main/resources/git.properties
diff --git a/pom.xml b/pom.xml
index dd50323..02b0524 100644
--- a/pom.xml
+++ b/pom.xml
@@ -257,6 +257,27 @@
                         </plugins>
                 </pluginManagement>
                 <plugins>
+                	<plugin>
+                		<groupId>pl.project13.maven</groupId>
+                		<artifactId>git-commit-id-plugin</artifactId>
+                		<version>2.1.5</version>
+                		<executions>
+                			<execution>
+                				<goals>
+                					<goal>revision</goal>
+                				</goals>
+                			</execution>
+                		</executions>
+                		<configuration>
+                			<verbose>true</verbose>
+                			<dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
+                			<generateGitPropertiesFile>true</generateGitPropertiesFile>
+               				<gitDescribe>
+               					<tags>true</tags>
+               					<dirty>-dev</dirty>
+               				</gitDescribe>
+                		</configuration>
+                	</plugin>
                         <plugin>
                                 <groupId>org.apache.maven.plugins</groupId>
                                 <artifactId>maven-compiler-plugin</artifactId>
diff --git a/src/main/java/info/textgrid/services/aggregator/Version.java b/src/main/java/info/textgrid/services/aggregator/Version.java
index 745d07d..134cc96 100644
--- a/src/main/java/info/textgrid/services/aggregator/Version.java
+++ b/src/main/java/info/textgrid/services/aggregator/Version.java
@@ -1,8 +1,10 @@
 package info.textgrid.services.aggregator;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.util.Properties;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
@@ -14,16 +16,33 @@
 
 @Path("/version")
 public class Version {
-	
-	
+
 	private final ITextGridRep repository;
 	private final String version;
+	private Properties gitInfo;
 
 	public Version(final ITextGridRep repository, final String version) {
 		this.repository = repository;
 		this.version = version;
 	}
-	
+
+	private Properties loadGitInfo() {
+		if (gitInfo == null) {
+			gitInfo = new Properties();
+			final InputStream inputStream = getClass().getResourceAsStream(
+					"/git.properties");
+			if (inputStream != null) {
+				try {
+					gitInfo.load(inputStream);
+				} catch (final IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+		}
+		return gitInfo;
+	}
+
 	@GET
 	// @Path("/")
 	@Produces("text/html")
@@ -32,19 +51,29 @@ public StreamingOutput get() {
 
 			@Override
 			public void write(final OutputStream outputStream)
-					throws IOException,
-					WebApplicationException {
+					throws IOException, WebApplicationException {
 				final PrintStream out = new PrintStream(outputStream, true);
 				out.println("<!DOCTYPE html><html><head><meta charset='utf-8'/><title>Aggregator Version Report</title></head><body>");
-				out.printf("<h1>TextGrid Aggregator Version %s</h1>\n", version);
-				out.printf("<p>TextGridRep Configuration Endpoint: <a href='%1$s'>%1$s</a></p>\n\n",
+				out.printf("<h1>TextGrid Aggregator</h1>\n", version);
+				final Properties info = loadGitInfo();
+				out.printf(
+						"<p style='border: 1px solid #ccf; padding: 6px;'>Version: <em>%s</em> (<strong>%s</strong>) from branch <em>%s</em> as of <em>%s</em>, built on <em>%s</em>.</p>",
+						version,
+						info.getProperty("git.commit.id.describe", "?"),
+						info.getProperty("git.branch", "?"),
+						info.getProperty("git.commit.time", "?"),
+						info.getProperty("git.build.time", "?"));
+
+				out.printf(
+						"<p>TextGridRep Configuration Endpoint: <a href='%1$s'>%1$s</a></p>\n\n",
 						repository.getCONF_ENDPOINT());
 				out.print("<h3>Repository Configuration:</h3>\n<table><tr><td>");
-				out.print(Joiner.on("</td></tr>\n<tr><td>").withKeyValueSeparator("</td><td>")
+				out.print(Joiner.on("</td></tr>\n<tr><td>")
+						.withKeyValueSeparator("</td><td>")
 						.join(repository.getConfig()));
 				out.println("</td></tr></table>");
-				out.printf("<p>Using TG-crud version: <strong>%s</strong>\n", repository
-						.getCRUDService().getVersion());
+				out.printf("<p>Using TG-crud version: <strong>%s</strong>\n",
+						repository.getCRUDService().getVersion());
 
 				out.println("</body></html>");
 				out.close();
@@ -52,6 +81,5 @@ public void write(final OutputStream outputStream)
 			}
 		};
 	}
-	
 
 }
-- 
GitLab