16
16
17
17
import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getenv ;
18
18
19
- import java .io .FileInputStream ;
20
19
import java .io .IOException ;
21
- import java .net . URL ;
20
+ import java .io . InputStream ;
22
21
import java .util .Properties ;
22
+
23
23
import org .slf4j .Logger ;
24
24
import org .slf4j .LoggerFactory ;
25
25
26
-
27
26
/**
28
27
* Can be used to create a string that can server as a User-Agent suffix in requests made with the AWS SDK clients
29
28
*/
@@ -54,32 +53,36 @@ static String getProjectVersion() {
54
53
return getVersionFromProperties (VERSION_PROPERTIES_FILENAME , VERSION_KEY );
55
54
}
56
55
57
-
58
56
/**
59
57
* Retrieves the project version from a properties file.
60
58
* The file should be in the resources folder.
61
59
* The version is retrieved from the property with the given key.
62
60
*
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
65
65
* @return the version of the project as configured in the given properties file
66
66
*/
67
67
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 ) {
79
71
LOG .warn ("Unable to read {} file. Using default version." , propertyFileName );
80
- LOG . debug ( "Exception:" , e ) ;
72
+ return NA ;
81
73
}
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 );
82
84
}
85
+
83
86
return NA ;
84
87
}
85
88
@@ -90,9 +93,10 @@ static String getVersionFromProperties(String propertyFileName, String versionKe
90
93
* The PT_EXEC_ENV is automatically retrieved from the AWS_EXECUTION_ENV environment variable.
91
94
* If it AWS_EXECUTION_ENV is not set, PT_EXEC_ENV defaults to "NA"
92
95
*
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".
96
100
* @return the user agent string
97
101
*/
98
102
public static String getUserAgent (String ptFeature ) {
0 commit comments