|
| 1 | +// Copyright (c) 2018-Present Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.perf; |
| 17 | + |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.Properties; |
| 23 | + |
| 24 | +/** |
| 25 | + * Current version of the tool. |
| 26 | + * Tries to get version information from a specific property file |
| 27 | + * and falls back to manifest information if the file cannot be loaded. |
| 28 | + */ |
| 29 | +public class Version { |
| 30 | + |
| 31 | + public static final String VERSION, BUILD, BUILD_TIMESTAMP; |
| 32 | + |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(Version.class); |
| 34 | + |
| 35 | + static { |
| 36 | + VERSION = getVersion(); |
| 37 | + BUILD = getBuild(); |
| 38 | + BUILD_TIMESTAMP = getBuildTimestamp(); |
| 39 | + } |
| 40 | + |
| 41 | + private static final String getVersion() { |
| 42 | + String version; |
| 43 | + try { |
| 44 | + version = getValueFromPropertyFile("com.rabbitmq.perf.version"); |
| 45 | + } catch (Exception e1) { |
| 46 | + LOGGER.warn("Couldn't get version from property file", e1); |
| 47 | + try { |
| 48 | + version = getVersionFromPackage(); |
| 49 | + } catch (Exception e2) { |
| 50 | + LOGGER.warn("Couldn't get version with Package#getImplementationVersion", e1); |
| 51 | + version = getDefaultVersion(); |
| 52 | + } |
| 53 | + } |
| 54 | + return version; |
| 55 | + } |
| 56 | + |
| 57 | + private static final String getBuild() { |
| 58 | + String build; |
| 59 | + try { |
| 60 | + build = getValueFromPropertyFile("com.rabbitmq.perf.build"); |
| 61 | + } catch (Exception e) { |
| 62 | + LOGGER.warn("Couldn't get build from property file", e); |
| 63 | + build = getDefaultBuild(); |
| 64 | + } |
| 65 | + return build; |
| 66 | + } |
| 67 | + |
| 68 | + private static final String getBuildTimestamp() { |
| 69 | + String build; |
| 70 | + try { |
| 71 | + build = getValueFromPropertyFile("com.rabbitmq.perf.build.timestamp"); |
| 72 | + } catch (Exception e) { |
| 73 | + LOGGER.warn("Couldn't get build timestamp from property file", e); |
| 74 | + build = getDefaultBuildTimestamp(); |
| 75 | + } |
| 76 | + return build; |
| 77 | + } |
| 78 | + |
| 79 | + private static final String getValueFromPropertyFile(String key) throws Exception { |
| 80 | + InputStream inputStream = Version.class.getClassLoader().getResourceAsStream("rabbitmq-perf-test.properties"); |
| 81 | + Properties version = new Properties(); |
| 82 | + try { |
| 83 | + version.load(inputStream); |
| 84 | + } finally { |
| 85 | + if (inputStream != null) { |
| 86 | + inputStream.close(); |
| 87 | + } |
| 88 | + } |
| 89 | + if (version.getProperty(key) == null) { |
| 90 | + throw new IllegalStateException("Coulnd't find " + key + " property in property file"); |
| 91 | + } |
| 92 | + return version.getProperty(key); |
| 93 | + } |
| 94 | + |
| 95 | + private static final String getVersionFromPackage() { |
| 96 | + if (Version.class.getPackage().getImplementationVersion() == null) { |
| 97 | + throw new IllegalStateException("Couldn't get version with Package#getImplementationVersion"); |
| 98 | + } |
| 99 | + return Version.class.getPackage().getImplementationVersion(); |
| 100 | + } |
| 101 | + |
| 102 | + private static final String getDefaultVersion() { |
| 103 | + return "0.0.0"; |
| 104 | + } |
| 105 | + |
| 106 | + private static final String getDefaultBuild() { |
| 107 | + return "unknown"; |
| 108 | + } |
| 109 | + |
| 110 | + private static final String getDefaultBuildTimestamp() { |
| 111 | + return "unknown"; |
| 112 | + } |
| 113 | +} |
0 commit comments