|
15 | 15 |
|
16 | 16 | package com.rabbitmq.client.impl;
|
17 | 17 |
|
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.Properties; |
| 23 | + |
18 | 24 | /**
|
19 | 25 | * Publicly available Client Version information
|
20 | 26 | */
|
21 | 27 | 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(); |
25 | 28 |
|
| 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 | + } |
26 | 75 | }
|
0 commit comments