@@ -26,9 +26,11 @@ public final class PropertyUtils {
2626 private static final Logger LOG = LoggerFactory .getLogger (PropertyUtils .class );
2727
2828 private static final String SDK_VERSION_PROPERTY_NAME = "version" ;
29+ private static final String GIT_COMMIT_HASH_PROPERTY_NAME = "git.commitHash" ;
2930 private static final String PROPERTIES_FILE_NAME = "ds3_sdk.properties" ;
3031
3132 private static final AtomicReference <String > versionProperty = new AtomicReference <>("" );
33+ private static final AtomicReference <String > gitCommitHashProperty = new AtomicReference <>("" );
3234
3335 private PropertyUtils () {}
3436
@@ -37,29 +39,47 @@ private PropertyUtils() {}
3739 * @return The sdk version, if we can find one, an empty string otherwise.
3840 */
3941 public static String getSdkVersion () {
40- if ( ! Guard .isStringNullOrEmpty (versionProperty .get ())) {
41- return versionProperty .get ();
42+ return getPropertyFromPropertiesFile (SDK_VERSION_PROPERTY_NAME , versionProperty );
43+ }
44+
45+ private static String getPropertyFromPropertiesFile (final String propertyName , final AtomicReference <String > propertyValue ) {
46+ if ( ! Guard .isStringNullOrEmpty (propertyValue .get ())) {
47+ return propertyValue .get ();
48+ }
49+
50+ final String propertyFromPropertiesFile = getPropertyFromPropertiesFile (propertyName );
51+
52+ if ( ! Guard .isStringNullOrEmpty (propertyFromPropertiesFile )) {
53+ propertyValue .set (propertyFromPropertiesFile );
4254 }
4355
56+ return propertyValue .get ();
57+ }
58+
59+ private static String getPropertyFromPropertiesFile (final String propertyName ) {
4460 final Properties properties = new Properties ();
4561
4662 try (final InputStream propertiesStream = PropertyUtils .class .getClassLoader ().getResourceAsStream (PROPERTIES_FILE_NAME )) {
4763 if (propertiesStream != null ) {
4864 properties .load (propertiesStream );
49- final Object versionPropertyObject = properties .get (SDK_VERSION_PROPERTY_NAME );
50-
51- if (versionPropertyObject != null ) {
52- final String versionFromPropFile = properties .get (SDK_VERSION_PROPERTY_NAME ).toString ();
65+ final Object propertyObject = properties .get (propertyName );
5366
54- if ( ! Guard .isStringNullOrEmpty (versionFromPropFile )) {
55- versionProperty .set (versionFromPropFile );
56- }
67+ if (propertyObject != null ) {
68+ return properties .get (propertyName ).toString ();
5769 }
5870 }
5971 } catch (final Throwable t ) {
6072 LOG .warn ("Could not read properties file." , t );
6173 }
6274
63- return versionProperty .get ();
75+ return "" ;
76+ }
77+
78+ /**
79+ * Get the git commit hash from a properties file.
80+ * @return The git commit hash, if we can find it, an empty string otherwise.
81+ */
82+ public static String getGitCommitHash () {
83+ return getPropertyFromPropertiesFile (GIT_COMMIT_HASH_PROPERTY_NAME , gitCommitHashProperty );
6484 }
6585}
0 commit comments