/*
 * ******************************************************************************
 *   Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved.
 *   Licensed under the Apache License, Version 2.0 (the "License"). You may not use
 *   this file except in compliance with the License. A copy of the License is located at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   or in the "license" file accompanying this file.
 *   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *   CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *   specific language governing permissions and limitations under the License.
 * ****************************************************************************
 */

package com.spectralogic.ds3client.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class PropertyUtils {
    private static final Logger LOG = LoggerFactory.getLogger(PropertyUtils.class);

    private static final String SDK_VERSION_PROPERTY_NAME = "version";
    private static final String PROPERTIES_FILE_NAME = "ds3_sdk.properties";

    private static final AtomicReference<String> versionProperty = new AtomicReference<>("");

    private PropertyUtils() {}

    /**
     * Get the sdk version out of a properties file
     * @return The sdk version, if we can find one, an empty string otherwise.
     */
    public static String getSdkVersion() {
        if ( ! Guard.isStringNullOrEmpty(versionProperty.get())) {
            return versionProperty.get();
        }

        final Properties properties = new Properties();

        try (final InputStream propertiesStream = PropertyUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME)) {
            if (propertiesStream != null) {
                properties.load(propertiesStream);
                final String versionFromPropFile = properties.get(SDK_VERSION_PROPERTY_NAME).toString();
                if (!Guard.isStringNullOrEmpty(versionFromPropFile)) {
                    versionProperty.set(versionFromPropFile);
                }
            }
        } catch (final IOException e) {
            LOG.warn("Could not read properties file.", e);
        }

        return versionProperty.get();
    }
}

@@ -1981,16 +1981,170 @@
 tream);%0A
+                final Object versionPropertyObject = properties.get(SDK_VERSION_PROPERTY_NAME);%0A%0A                if (versionPropertyObject != null) %7B%0A    
         
@@ -2236,24 +2236,27 @@
 toString();%0A
+%0A  
             
@@ -2263,13 +2263,17 @@
     
+  
 if (
-!
+ ! 
 Guar
@@ -2314,24 +2314,28 @@
 ropFile)) %7B%0A
+    
             
@@ -2380,24 +2380,46 @@
 mPropFile);%0A
+                    %7D%0A
             
@@ -2465,21 +2465,19 @@
 nal 
-IOException e
+Throwable t
 ) %7B%0A
@@ -2532,17 +2532,17 @@
 file.%22, 
-e
+t
 );%0A     

Fix for https://jira.spectralogic.com/browse/JSDK-245 Null Pointer exception for get system info