Skip to content

Commit 904fb97

Browse files
committed
Generalize application information API
In PR #554 application API was implemented in the form of three additional Configuration attributes. This PR to have one Configuration attribute of `ApplicationInfo` interface. Default implementation of which will supply same three fields. `ApplicationInfo` interface is done in such a way that allows to inject any application information into startup message.
1 parent 6762900 commit 904fb97

File tree

5 files changed

+62
-96
lines changed

5 files changed

+62
-96
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.datastax.driver.core;
2+
3+
import java.util.Map;
4+
5+
public interface ApplicationInfo {
6+
/** Adds application information to startup options. */
7+
void addOption(Map<String, String> options);
8+
}

driver-core/src/main/java/com/datastax/driver/core/Cluster.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,32 +1522,13 @@ public Builder withLocalPortRange(int low, int high) {
15221522
}
15231523

15241524
/**
1525-
* Sets application name that will be sent to the server on startup.
1525+
* Sets application information provider, every connection on startup sends this information to
1526+
* the server.
15261527
*
1527-
* @param applicationName name of the application.
1528+
* @param applicationInfo an application information provider.
15281529
*/
1529-
public Builder withApplicationName(String applicationName) {
1530-
configurationBuilder.withApplicationName(applicationName);
1531-
return this;
1532-
}
1533-
1534-
/**
1535-
* Sets application version that will be sent to the server on startup.
1536-
*
1537-
* @param applicationVersion version of the application.
1538-
*/
1539-
public Builder withApplicationVersion(String applicationVersion) {
1540-
configurationBuilder.withApplicationVersion(applicationVersion);
1541-
return this;
1542-
}
1543-
1544-
/**
1545-
* Sets client id that will be sent to the server on startup.
1546-
*
1547-
* @param clientId id of the application.
1548-
*/
1549-
public Builder withClientId(String clientId) {
1550-
configurationBuilder.withClientId(clientId);
1530+
public Builder withApplicationInfo(ApplicationInfo applicationInfo) {
1531+
configurationBuilder.withApplicationInfo(applicationInfo);
15511532
return this;
15521533
}
15531534

@@ -1705,9 +1686,7 @@ private Manager(
17051686
.withThreadingOptions(configuration.getThreadingOptions())
17061687
.withNettyOptions(configuration.getNettyOptions())
17071688
.withCodecRegistry(configuration.getCodecRegistry())
1708-
.withApplicationName(configuration.getApplicationName())
1709-
.withApplicationVersion(configuration.getApplicationVersion())
1710-
.withClientId(configuration.getClientId())
1689+
.withApplicationInfo(configuration.getApplicationInfo())
17111690
.build();
17121691
} else {
17131692
this.configuration = configuration;

driver-core/src/main/java/com/datastax/driver/core/Configuration.java

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
* <li>Netty layer customization options.
3333
* </ul>
3434
*
35-
* This is also where you get the configured policies, though those cannot be changed (they are set
36-
* during the built of the Cluster object).
35+
* <p>This is also where you get the configured policies, though those cannot be changed (they are
36+
* set during the built of the Cluster object).
3737
*/
3838
public class Configuration {
3939

@@ -59,9 +59,7 @@ public static Builder builder() {
5959
private final NettyOptions nettyOptions;
6060
private final CodecRegistry codecRegistry;
6161
private final String defaultKeyspace;
62-
private final String applicationName;
63-
private final String applicationVersion;
64-
private final String clientId;
62+
private final ApplicationInfo applicationInfo;
6563

6664
private Configuration(
6765
Policies policies,
@@ -74,9 +72,7 @@ private Configuration(
7472
NettyOptions nettyOptions,
7573
CodecRegistry codecRegistry,
7674
String defaultKeyspace,
77-
String applicationName,
78-
String applicationVersion,
79-
String clientId) {
75+
ApplicationInfo applicationInfo) {
8076
this.policies = policies;
8177
this.protocolOptions = protocolOptions;
8278
this.poolingOptions = poolingOptions;
@@ -87,9 +83,7 @@ private Configuration(
8783
this.nettyOptions = nettyOptions;
8884
this.codecRegistry = codecRegistry;
8985
this.defaultKeyspace = defaultKeyspace;
90-
this.applicationName = applicationName;
91-
this.applicationVersion = applicationVersion;
92-
this.clientId = clientId;
86+
this.applicationInfo = applicationInfo;
9387
}
9488

9589
/**
@@ -109,9 +103,7 @@ protected Configuration(Configuration toCopy) {
109103
toCopy.getNettyOptions(),
110104
toCopy.getCodecRegistry(),
111105
toCopy.getDefaultKeyspace(),
112-
toCopy.getApplicationName(),
113-
toCopy.getApplicationVersion(),
114-
toCopy.getClientId());
106+
toCopy.getApplicationInfo());
115107
}
116108

117109
void register(Cluster.Manager manager) {
@@ -226,16 +218,8 @@ public String getDefaultKeyspace() {
226218
return defaultKeyspace;
227219
}
228220

229-
public String getApplicationName() {
230-
return applicationName;
231-
}
232-
233-
public String getApplicationVersion() {
234-
return applicationVersion;
235-
}
236-
237-
public String getClientId() {
238-
return clientId;
221+
public ApplicationInfo getApplicationInfo() {
222+
return applicationInfo;
239223
}
240224

241225
/**
@@ -262,42 +246,18 @@ public static class Builder {
262246
private QueryOptions queryOptions;
263247
private ThreadingOptions threadingOptions;
264248
private NettyOptions nettyOptions;
249+
private ApplicationInfo applicationInfo;
265250
private CodecRegistry codecRegistry;
266251
private String defaultKeyspace;
267-
private String applicationName;
268-
private String applicationVersion;
269-
private String clientId;
270-
271-
/**
272-
* Sets application name, to be reported to server
273-
*
274-
* @param applicationName application name.
275-
* @return this builder.
276-
*/
277-
public Builder withApplicationName(String applicationName) {
278-
this.applicationName = applicationName;
279-
return this;
280-
}
281-
282-
/**
283-
* Sets application version, to be reported to server
284-
*
285-
* @param applicationVersion application version.
286-
* @return this builder.
287-
*/
288-
public Builder withApplicationVersion(String applicationVersion) {
289-
this.applicationVersion = applicationVersion;
290-
return this;
291-
}
292252

293253
/**
294-
* Sets client id, to be reported to server
254+
* Sets application information provider.
295255
*
296-
* @param clientId application version.
256+
* @param applicationInfo application information provider.
297257
* @return this builder.
298258
*/
299-
public Builder withClientId(String clientId) {
300-
this.clientId = clientId;
259+
public Builder withApplicationInfo(ApplicationInfo applicationInfo) {
260+
this.applicationInfo = applicationInfo;
301261
return this;
302262
}
303263

@@ -432,9 +392,7 @@ public Configuration build() {
432392
nettyOptions != null ? nettyOptions : NettyOptions.DEFAULT_INSTANCE,
433393
codecRegistry != null ? codecRegistry : CodecRegistry.DEFAULT_INSTANCE,
434394
defaultKeyspace,
435-
applicationName,
436-
applicationVersion,
437-
clientId);
395+
applicationInfo);
438396
}
439397
}
440398
}

driver-core/src/main/java/com/datastax/driver/core/Connection.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ enum State {
154154
new AtomicReference<ConnectionCloseFuture>();
155155

156156
private final AtomicReference<Owner> ownerRef = new AtomicReference<Owner>();
157+
private final ApplicationInfo applicationInfo;
157158

158159
/**
159160
* Create a new connection to a Cassandra node and associate it with the given pool.
@@ -173,6 +174,7 @@ protected Connection(String name, EndPoint endPoint, Factory factory, Owner owne
173174
ListenableFuture<Connection> thisFuture = Futures.immediateFuture(this);
174175
this.defaultKeyspaceAttempt = new SetKeyspaceAttempt(null, thisFuture);
175176
this.targetKeyspace = new AtomicReference<SetKeyspaceAttempt>(defaultKeyspaceAttempt);
177+
this.applicationInfo = factory.configuration.getApplicationInfo();
176178
}
177179

178180
/** Create a new connection to a Cassandra node. */
@@ -505,6 +507,9 @@ private AsyncFunction<Void, Void> onOptionsReady(
505507
public ListenableFuture<Void> apply(Void input) throws Exception {
506508
ProtocolOptions protocolOptions = factory.configuration.getProtocolOptions();
507509
Map<String, String> extraOptions = new HashMap<String, String>();
510+
if (applicationInfo != null) {
511+
applicationInfo.addOption(extraOptions);
512+
}
508513
LwtInfo lwtInfo = getHost().getLwtInfo();
509514
if (lwtInfo != null) {
510515
lwtInfo.addOption(extraOptions);
@@ -517,19 +522,6 @@ public ListenableFuture<Void> apply(Void input) throws Exception {
517522
TabletInfo.addOption(extraOptions);
518523
}
519524

520-
if (factory.configuration.getApplicationName() != null
521-
&& !factory.configuration.getApplicationName().isEmpty()) {
522-
extraOptions.put("APPLICATION_NAME", factory.configuration.getApplicationName());
523-
}
524-
if (factory.configuration.getApplicationVersion() != null
525-
&& !factory.configuration.getApplicationVersion().isEmpty()) {
526-
extraOptions.put("APPLICATION_VERSION", factory.configuration.getApplicationVersion());
527-
}
528-
if (factory.configuration.getClientId() != null
529-
&& !factory.configuration.getClientId().isEmpty()) {
530-
extraOptions.put("CLIENT_ID", factory.configuration.getClientId());
531-
}
532-
533525
Future startupResponseFuture =
534526
write(
535527
new Requests.Startup(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.datastax.driver.core;
2+
3+
import java.util.Map;
4+
5+
public class DefaultApplicationInfo implements ApplicationInfo {
6+
private String applicationName;
7+
private String applicationVersion;
8+
private String clientId;
9+
10+
public DefaultApplicationInfo(
11+
String applicationName, String applicationVersion, String clientId) {
12+
this.applicationName = applicationName;
13+
this.applicationVersion = applicationVersion;
14+
this.clientId = clientId;
15+
}
16+
17+
@Override
18+
public void addOption(Map<String, String> options) {
19+
if (applicationName != null && !applicationName.isEmpty()) {
20+
options.put("APPLICATION_NAME", applicationName);
21+
}
22+
if (applicationVersion != null && !applicationVersion.isEmpty()) {
23+
options.put("APPLICATION_VERSION", applicationVersion);
24+
}
25+
if (clientId != null && !clientId.isEmpty()) {
26+
options.put("CLIENT_ID", clientId);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)