Skip to content

Commit 68b00b0

Browse files
committed
Add client properties in connection
Copyright, licence, version, etc.
1 parent b9348cf commit 68b00b0

File tree

5 files changed

+112
-5
lines changed

5 files changed

+112
-5
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/AmqpConnection.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
package com.rabbitmq.client.amqp.impl;
1919

2020
import static com.rabbitmq.client.amqp.Resource.State.*;
21-
import static java.util.Collections.singletonMap;
2221

2322
import com.rabbitmq.client.amqp.*;
2423
import com.rabbitmq.client.amqp.ObservationCollector;
2524
import com.rabbitmq.client.amqp.impl.Utils.StopWatch;
2625
import com.rabbitmq.client.amqp.metrics.MetricsCollector;
2726
import java.time.Duration;
2827
import java.util.ArrayList;
28+
import java.util.LinkedHashMap;
2929
import java.util.List;
30+
import java.util.Map;
3031
import java.util.concurrent.*;
3132
import java.util.concurrent.atomic.AtomicBoolean;
3233
import java.util.concurrent.atomic.AtomicLong;
@@ -184,8 +185,12 @@ private org.apache.qpid.protonj2.client.Connection connect(
184185
connectionOptions.idleTimeout(
185186
connectionSettings.idleTimeout().toMillis(), TimeUnit.MILLISECONDS);
186187
connectionOptions.disconnectedHandler(disconnectHandler);
187-
if (name != null) {
188-
connectionOptions.properties(singletonMap("connection_name", name));
188+
if (name == null) {
189+
connectionOptions.properties(ClientProperties.DEFAULT_CLIENT_PROPERTIES);
190+
} else {
191+
Map<String, Object> props = new LinkedHashMap<>(ClientProperties.DEFAULT_CLIENT_PROPERTIES);
192+
props.put("connection_name", name);
193+
connectionOptions.properties(Map.copyOf(props));
189194
}
190195
if (connectionSettings.tlsEnabled()) {
191196
DefaultConnectionSettings.DefaultTlsSettings<?> tlsSettings =
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.rabbitmq.client.amqp.version = ${project.version}

src/test/java/com/rabbitmq/client/amqp/impl/AmqpTestInfrastructureExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void beforeEach(ExtensionContext context) throws Exception {
6161
Field connectionField = field(context.getTestClass().get(), "connection");
6262
if (connectionField != null) {
6363
connectionField.setAccessible(true);
64-
Connection connection = env.connectionBuilder().build();
64+
Connection connection =
65+
((AmqpConnectionBuilder) env.connectionBuilder()).name(TestUtils.name(context)).build();
6566
connectionField.set(context.getTestInstance().get(), connection);
6667
store(context).put("connection", connection);
6768
}

src/test/java/com/rabbitmq/client/amqp/impl/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static String name(TestInfo info) {
267267
return name(info.getTestClass().get(), info.getTestMethod().get());
268268
}
269269

270-
private static String name(ExtensionContext context) {
270+
static String name(ExtensionContext context) {
271271
return name(context.getTestInstance().get().getClass(), context.getTestMethod().get());
272272
}
273273

0 commit comments

Comments
 (0)