Skip to content

Commit 4be8a61

Browse files
committed
Read client version from version.properties file
1 parent f470bf3 commit 4be8a61

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

java-client/src/main/java/co/elastic/clients/base/UserAgent.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,42 @@
1919

2020
package co.elastic.clients.base;
2121

22+
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.util.Collections;
2325
import java.util.Map;
26+
import java.util.Properties;
2427

2528
/**
2629
* Models a user agent, consisting of a name, version,
2730
* and optional key-value metadata.
2831
*/
2932
public class UserAgent {
3033

31-
// TODO: move this to a resource file
3234
static final String DEFAULT_NAME = "elasticsearch-java";
3335

34-
// TODO: move this to a resource file
35-
static final String DEFAULT_VERSION = "1.2.3";
36+
// The client version is loaded from the 'version.properties' file
37+
static final String DEFAULT_VERSION;
38+
static {
39+
InputStream in = UserAgent.class.getResourceAsStream("/co.elastic.clients.elasticsearch/version.properties");
40+
if (in != null) {
41+
Properties prop = new Properties();
42+
String version;
43+
try {
44+
prop.load(in);
45+
version = prop.getProperty("version", "?");
46+
} catch (IOException e) {
47+
// Unable to read properties file
48+
version = "?";
49+
}
50+
DEFAULT_VERSION = version;
51+
}
52+
else {
53+
// Unable to locate properties file
54+
DEFAULT_VERSION = "?";
55+
}
56+
// TODO: log error if DEFAULT_VERSION now equals "?"
57+
}
3658

3759
// Default user agent, constructed from default repo name and version
3860
public static final UserAgent DEFAULT = new UserAgent(DEFAULT_NAME, DEFAULT_VERSION);

0 commit comments

Comments
 (0)