From 545f1c40eb9322954330afb002ea5409d2aa7c5e Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 1 Jul 2007 17:55:46 +0000 Subject: [PATCH] HTTPCLIENT-664: First take at version detection based on build-time properties git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@552347 13f79535-47bb-0310-9956-ffa450edef68 --- module-client/pom.xml | 9 +++ .../org/apache/http/client/VersionInfo.java | 74 +++++++++++++++++++ .../http/impl/client/DefaultHttpClient.java | 13 +++- .../org/apache/http/client/version.properties | 1 + 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/client/VersionInfo.java create mode 100644 module-client/src/main/resources/org/apache/http/client/version.properties diff --git a/module-client/pom.xml b/module-client/pom.xml index 725bf3465..1599aff70 100644 --- a/module-client/pom.xml +++ b/module-client/pom.xml @@ -91,6 +91,15 @@ + + + src/main/resources + true + + *.version + + + org.apache.maven.plugins diff --git a/module-client/src/main/java/org/apache/http/client/VersionInfo.java b/module-client/src/main/java/org/apache/http/client/VersionInfo.java new file mode 100644 index 000000000..45285f85d --- /dev/null +++ b/module-client/src/main/java/org/apache/http/client/VersionInfo.java @@ -0,0 +1,74 @@ +package org.apache.http.client; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VersionInfo { + + private static final String RESOURCE = "org/apache/http/client/version.properties"; + private static final String REV = "$Revision$"; + + private static Properties RELEASE_PROPERTIES; + private static String RELEASE_VERSION; + private static Long REV_VERSION; + + private static Properties getReleaseProperties() { + if (RELEASE_PROPERTIES == null) { + try { + ClassLoader cl = VersionInfo.class.getClassLoader(); + InputStream instream = cl.getResourceAsStream(RESOURCE); + try { + Properties props = new Properties(); + props.load(instream); + RELEASE_PROPERTIES = props; + } finally { + instream.close(); + } + } catch (IOException ex) { + // shamelessly munch this exception + } + if (RELEASE_PROPERTIES == null) { + // Create dummy properties instance + RELEASE_PROPERTIES = new Properties(); + } + } + return RELEASE_PROPERTIES; + } + + + public static String getReleaseVersion() { + if (RELEASE_VERSION == null) { + Properties props = getReleaseProperties(); + RELEASE_VERSION = (String) props.get("httpclient.release"); + if (RELEASE_VERSION == null + || RELEASE_VERSION.length() == 0 + || RELEASE_VERSION.equals("${pom.version}")) { + RELEASE_VERSION = "UNKNOWN_SNAPSHOT"; + } + } + return RELEASE_VERSION; + } + + public static long getRevision() { + if (REV_VERSION == null) { + Pattern p = Pattern.compile("\\$Revision: (\\d*) \\$"); + Matcher m = p.matcher(REV); + if (m.matches()) { + String s = m.group(1); + try { + REV_VERSION = new Long(Long.parseLong(s)); + } catch (NumberFormatException ex) { + // shamelessly munch this exception + } + } + if (REV_VERSION == null) { + REV_VERSION = new Long(-1); + } + } + return REV_VERSION.intValue(); + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 4cee9361d..a1d235ce6 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -42,6 +42,7 @@ import org.apache.http.client.HttpState; import org.apache.http.client.RedirectHandler; import org.apache.http.client.RoutedRequest; +import org.apache.http.client.VersionInfo; import org.apache.http.client.params.AuthPolicy; import org.apache.http.client.params.CookiePolicy; import org.apache.http.client.params.HttpClientParams; @@ -121,10 +122,14 @@ public DefaultHttpClient() { protected HttpParams createHttpParams() { HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); - HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0"); - HttpProtocolParams.setUseExpectContinue(params, true); + HttpProtocolParams.setVersion(params, + HttpVersion.HTTP_1_1); + HttpProtocolParams.setContentCharset(params, + HTTP.DEFAULT_CONTENT_CHARSET); + HttpProtocolParams.setUserAgent(params, + "Apache-HttpClient/" + VersionInfo.getReleaseVersion() + " (java 1.4)"); + HttpProtocolParams.setUseExpectContinue(params, + true); return params; } diff --git a/module-client/src/main/resources/org/apache/http/client/version.properties b/module-client/src/main/resources/org/apache/http/client/version.properties new file mode 100644 index 000000000..6568d5781 --- /dev/null +++ b/module-client/src/main/resources/org/apache/http/client/version.properties @@ -0,0 +1 @@ +httpclient.release=${pom.version}