Skip to content

Commit bd1d73f

Browse files
committed
Load client version in a more reliable way
The code tries to read the version from a property file (with a unique name) and falls back to Package#getImplementationVersion in case of error. If Package#getImplementationVersion fails too, a default hardcoded value is returned. Warns are logged for every failure. This should make the retrieval more reliable in different contexts (e.g. uber-JARs, OSGi). References #334
1 parent 455987d commit bd1d73f

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/main/java/com/rabbitmq/client/impl/ClientVersion.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,61 @@
1515

1616
package com.rabbitmq.client.impl;
1717

18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.io.InputStream;
22+
import java.util.Properties;
23+
1824
/**
1925
* Publicly available Client Version information
2026
*/
2127
public class ClientVersion {
22-
/** Full version string */
23-
public static final String VERSION = ClientVersion.class.getPackage().getImplementationVersion() == null ?
24-
"0.0.0" : ClientVersion.class.getPackage().getImplementationVersion();
2528

29+
private static final Logger LOGGER = LoggerFactory.getLogger(ClientVersion.class);
30+
31+
public static final String VERSION;
32+
33+
static {
34+
String version;
35+
try {
36+
version = getVersionFromPropertyFile();
37+
} catch (Exception e1) {
38+
LOGGER.warn("Couldn't get version from property file", e1);
39+
try {
40+
version = getVersionFromPackage();
41+
} catch (Exception e2) {
42+
LOGGER.warn("Couldn't get version with Package#getImplementationVersion", e1);
43+
version = getDefaultVersion();
44+
}
45+
}
46+
VERSION = version;
47+
}
48+
49+
private static final String getVersionFromPropertyFile() throws Exception {
50+
InputStream inputStream = ClientVersion.class.getClassLoader().getResourceAsStream("rabbitmq-amqp-client.properties");
51+
Properties version = new Properties();
52+
try {
53+
version.load(inputStream);
54+
} finally {
55+
if (inputStream != null) {
56+
inputStream.close();
57+
}
58+
}
59+
if (version.getProperty("com.rabbitmq.client.version") == null) {
60+
throw new IllegalStateException("Coulnd't find version property in property file");
61+
}
62+
return version.getProperty("com.rabbitmq.client.version");
63+
}
64+
65+
private static final String getVersionFromPackage() {
66+
if (ClientVersion.class.getPackage().getImplementationVersion() == null) {
67+
throw new IllegalStateException("Couldn't get version with Package#getImplementationVersion");
68+
}
69+
return ClientVersion.class.getPackage().getImplementationVersion();
70+
}
71+
72+
private static final String getDefaultVersion() {
73+
return "0.0.0";
74+
}
2675
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.rabbitmq.client.version = ${project.version}

src/main/resources/version.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# here for backward compatibility
2+
# use rabbitmq-amqp-client.properties to add or read properties
13
com.rabbitmq.client.version = ${project.version}

0 commit comments

Comments
 (0)