|
| 1 | +// Copyright (c) 2024 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// If you have any questions regarding licensing, please contact us at |
| 17 | + |
| 18 | +package com.rabbitmq.client.amqp.impl; |
| 19 | + |
| 20 | +import java.io.InputStream; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Properties; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +final class ClientProperties { |
| 27 | + |
| 28 | + private static final Logger LOGGER = LoggerFactory.getLogger(ClientProperties.class); |
| 29 | + |
| 30 | + // We store the version property in an unusual way because relocating the package can rewrite the |
| 31 | + // key in the property |
| 32 | + // file, which results in spurious warnings being emitted at start-up. |
| 33 | + // see https://github.com/rabbitmq/rabbitmq-java-client/issues/436 |
| 34 | + private static final char[] VERSION_PROPERTY = |
| 35 | + new char[] { |
| 36 | + 'c', 'o', 'm', '.', 'r', 'a', 'b', 'b', 'i', 't', 'm', 'q', '.', 'c', 'l', 'i', 'e', 'n', |
| 37 | + 't', '.', 'a', 'm', 'q', 'p', '.', 'v', 'e', 'r', 's', 'i', 'o', 'n' |
| 38 | + }; |
| 39 | + |
| 40 | + public static final String VERSION = getVersion(); |
| 41 | + |
| 42 | + public static final Map<String, Object> DEFAULT_CLIENT_PROPERTIES = |
| 43 | + Map.of( |
| 44 | + "product", "RabbitMQ", |
| 45 | + "version", ClientProperties.VERSION, |
| 46 | + "platform", "Java", |
| 47 | + "copyright", "Copyright (c) 2024 Broadcom Inc. and/or its subsidiaries.", |
| 48 | + "information", |
| 49 | + "Licensed under the Apache License version 2. See https://www.rabbitmq.com/."); |
| 50 | + |
| 51 | + private static String getVersion() { |
| 52 | + String version; |
| 53 | + try { |
| 54 | + version = getVersionFromPropertyFile(); |
| 55 | + } catch (Exception e1) { |
| 56 | + LOGGER.warn("Couldn't get version from property file", e1); |
| 57 | + try { |
| 58 | + version = getVersionFromPackage(); |
| 59 | + } catch (Exception e2) { |
| 60 | + LOGGER.warn("Couldn't get version with Package#getImplementationVersion", e1); |
| 61 | + version = getDefaultVersion(); |
| 62 | + } |
| 63 | + } |
| 64 | + return version; |
| 65 | + } |
| 66 | + |
| 67 | + private static String getVersionFromPropertyFile() throws Exception { |
| 68 | + InputStream inputStream = |
| 69 | + ClientProperties.class |
| 70 | + .getClassLoader() |
| 71 | + .getResourceAsStream("rabbitmq-amqp-1-0-client.properties"); |
| 72 | + Properties version = new Properties(); |
| 73 | + try { |
| 74 | + version.load(inputStream); |
| 75 | + } finally { |
| 76 | + if (inputStream != null) { |
| 77 | + inputStream.close(); |
| 78 | + } |
| 79 | + } |
| 80 | + String propertyName = new String(VERSION_PROPERTY); |
| 81 | + String versionProperty = version.getProperty(propertyName); |
| 82 | + if (versionProperty == null) { |
| 83 | + throw new IllegalStateException("Couldn't find version property in property file"); |
| 84 | + } |
| 85 | + return versionProperty; |
| 86 | + } |
| 87 | + |
| 88 | + private static String getVersionFromPackage() { |
| 89 | + if (ClientProperties.class.getPackage().getImplementationVersion() == null) { |
| 90 | + throw new IllegalStateException("Couldn't get version with Package#getImplementationVersion"); |
| 91 | + } |
| 92 | + return ClientProperties.class.getPackage().getImplementationVersion(); |
| 93 | + } |
| 94 | + |
| 95 | + private static String getDefaultVersion() { |
| 96 | + return "0.0.0"; |
| 97 | + } |
| 98 | + |
| 99 | + private ClientProperties() {} |
| 100 | +} |
0 commit comments