Skip to content

Commit 8a898f9

Browse files
authored
fix: Load version.properties file as resource stream to fix loading when packaged as jar. (#1813)
1 parent 1557a63 commit 8a898f9

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

powertools-core/src/main/java/software/amazon/lambda/powertools/core/internal/UserAgentConfigurator.java

+25-21
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv;
1818

19-
import java.io.FileInputStream;
2019
import java.io.IOException;
21-
import java.net.URL;
20+
import java.io.InputStream;
2221
import java.util.Properties;
22+
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
2525

26-
2726
/**
2827
* Can be used to create a string that can server as a User-Agent suffix in requests made with the AWS SDK clients
2928
*/
@@ -54,32 +53,36 @@ static String getProjectVersion() {
5453
return getVersionFromProperties(VERSION_PROPERTIES_FILENAME, VERSION_KEY);
5554
}
5655

57-
5856
/**
5957
* Retrieves the project version from a properties file.
6058
* The file should be in the resources folder.
6159
* The version is retrieved from the property with the given key.
6260
*
63-
* @param propertyFileName the name of the properties file
64-
* @param versionKey the key of the property that contains the version
61+
* @param propertyFileName
62+
* the name of the properties file
63+
* @param versionKey
64+
* the key of the property that contains the version
6565
* @return the version of the project as configured in the given properties file
6666
*/
6767
static String getVersionFromProperties(String propertyFileName, String versionKey) {
68-
69-
URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName);
70-
if (propertiesFileURI != null) {
71-
try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) {
72-
Properties properties = new Properties();
73-
properties.load(fis);
74-
String version = properties.getProperty(versionKey);
75-
if (version != null && !version.isEmpty()) {
76-
return version;
77-
}
78-
} catch (IOException e) {
68+
try (final InputStream is = Thread.currentThread().getContextClassLoader()
69+
.getResourceAsStream(propertyFileName)) {
70+
if (is == null) {
7971
LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
80-
LOG.debug("Exception:", e);
72+
return NA;
8173
}
74+
75+
Properties properties = new Properties();
76+
properties.load(is);
77+
String version = properties.getProperty(versionKey);
78+
if (version != null && !version.isEmpty()) {
79+
return version;
80+
}
81+
} catch (IOException e) {
82+
LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
83+
LOG.debug("Exception:", e);
8284
}
85+
8386
return NA;
8487
}
8588

@@ -90,9 +93,10 @@ static String getVersionFromProperties(String propertyFileName, String versionKe
9093
* The PT_EXEC_ENV is automatically retrieved from the AWS_EXECUTION_ENV environment variable.
9194
* If it AWS_EXECUTION_ENV is not set, PT_EXEC_ENV defaults to "NA"
9295
*
93-
* @param ptFeature a custom feature to be added to the user agent string (e.g. idempotency).
94-
* If null or empty, the default PT_FEATURE is used.
95-
* The default PT_FEATURE is "no-op".
96+
* @param ptFeature
97+
* a custom feature to be added to the user agent string (e.g. idempotency).
98+
* If null or empty, the default PT_FEATURE is used.
99+
* The default PT_FEATURE is "no-op".
96100
* @return the user agent string
97101
*/
98102
public static String getUserAgent(String ptFeature) {

0 commit comments

Comments
 (0)