Skip to content

Commit eece978

Browse files
Refactor env() so that when PATH doesn't exsist a NullPointerException isn't thrown
1 parent 4befc78 commit eece978

File tree

1 file changed

+25
-9
lines changed
  • databricks-sdk-java/src/main/java/com/databricks/sdk/core

1 file changed

+25
-9
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/UserAgent.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,21 @@ public String getValue() {
3333

3434
private static final ArrayList<Info> otherInfo = new ArrayList<>();
3535

36-
// TODO: check if reading from
37-
// /META-INF/maven/com.databricks/databrics-sdk-java/pom.properties
38-
// or getClass().getPackage().getImplementationVersion() is enough.
39-
private static final String version = "0.79.0";
36+
// Try to get version from package metadata or fallback to hardcoded value
37+
private static final String version;
38+
static {
39+
String v = null;
40+
try {
41+
Package pkg = UserAgent.class.getPackage();
42+
if (pkg != null) {
43+
v = pkg.getImplementationVersion();
44+
}
45+
} catch (Exception ignored) {}
46+
if (v == null) {
47+
v = "0.79.0";
48+
}
49+
version = v;
50+
}
4051

4152
public static void withProduct(String product, String productVersion) {
4253
UserAgent.product = product;
@@ -233,11 +244,16 @@ private static String cicdProvider() {
233244

234245
private static Environment env() {
235246
if (env == null) {
236-
env =
237-
new Environment(
238-
System.getenv(),
239-
System.getenv("PATH").split(File.pathSeparator),
240-
System.getProperty("os.name"));
247+
Map<String, String> sysEnv = System.getenv();
248+
if (sysEnv == null) {
249+
sysEnv = Collections.emptyMap();
250+
}
251+
String pathVar = sysEnv.get("PATH");
252+
String[] pathArr = pathVar != null ? pathVar.split(File.pathSeparator) : new String[0];
253+
env = new Environment(
254+
sysEnv,
255+
pathArr,
256+
System.getProperty("os.name"));
241257
}
242258
return env;
243259
}

0 commit comments

Comments
 (0)