Skip to content

Commit

Permalink
Self-validate presence of sdk_version.
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <[email protected]>
  • Loading branch information
artursouza committed Jan 19, 2025
1 parent d7d372a commit fe044cd
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions sdk/src/main/java/io/dapr/utils/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,43 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

public final class Version {

private static String sdkVersion = null;
private static volatile AtomicReference<String> sdkVersion = new AtomicReference<>();

/**
* Retrieves sdk version from resources.
*
* @return String version of sdk.
*/
public static String getSdkVersion() {
var version = sdkVersion.get();

if (sdkVersion != null) {
return sdkVersion;
if ((version != null) && !version.isBlank()) {
return version;
}

try (InputStream input = Version.class.getResourceAsStream("/sdk_version.properties");) {
try (InputStream input = Version.class.getResourceAsStream("/sdk_version.properties")) {
Properties properties = new Properties();
properties.load(input);
sdkVersion = "dapr-sdk-java/v" + properties.getProperty("sdk_version", "unknown");
var v = properties.getProperty("sdk_version", null);
if (v == null) {
throw new IllegalStateException("Did not find sdk_version property!");
}

if (v.isBlank()) {
throw new IllegalStateException("Property sdk_version cannot be blank.");
}

version = "dapr-sdk-java/v" + v;
sdkVersion.set(version);
} catch (IOException e) {
sdkVersion = "unknown";
throw new IllegalStateException("Could not load sdk_version property!", e);
}

return sdkVersion;
return version;
}

}

0 comments on commit fe044cd

Please sign in to comment.