/*
 *  *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2013 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 *  * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


public class PAPropertiesLazyLoader {

    private static Logger logger = Logger.getLogger(PAPropertiesLazyLoader.class);

    private Properties properties;

    private String propertiesFileName;

    /** if properties file is relative, will use this system property value as base folder */
    private String systemPropertyForRelativeFolder;

    /** if properties filename is not defined, will search this system property for a filename */
    private String systemPropertyForFileName;

    /** if properties filename and systemPropertyForFileName are not defined, will use this default file name
     * and search in systemPropertyForRelativeFolder */
    private String pathToRelativePropertiesFile;

    public PAPropertiesLazyLoader(String systemPropertyForRelativeFolder, String systemPropertyForFileName,
            String pathToRelativePropertiesFile) {
        this.systemPropertyForRelativeFolder = systemPropertyForRelativeFolder;
        this.pathToRelativePropertiesFile = pathToRelativePropertiesFile;
        this.systemPropertyForFileName = systemPropertyForFileName;
    }

    public PAPropertiesLazyLoader(String systemPropertyForRelativeFolder, String systemPropertyForFileName,
            String pathToRelativePropertiesFile, String propertiesFileName) {
        this.systemPropertyForRelativeFolder = systemPropertyForRelativeFolder;
        this.pathToRelativePropertiesFile = pathToRelativePropertiesFile;
        this.propertiesFileName = propertiesFileName;
        this.systemPropertyForFileName = systemPropertyForFileName;
    }

    private InputStream resolvePropertiesFile() throws FileNotFoundException {
        String propertiesPath;
        boolean systemPropertyForFileNameSet = false;

        if (propertiesFileName == null) {
            if (System.getProperty(systemPropertyForFileName) != null) {
                propertiesPath = System.getProperty(systemPropertyForFileName);
                systemPropertyForFileNameSet = true;
            } else {
                propertiesPath = pathToRelativePropertiesFile;
            }
        } else {
            propertiesPath = propertiesFileName;
        }

        if (!new File(propertiesPath).isAbsolute()) {
            propertiesPath = System.getProperty(systemPropertyForRelativeFolder) + File.separator +
                propertiesPath;
        }

        if (new File(propertiesPath).exists()) {
            logger.debug("Loading properties from file " + propertiesPath);
            return new FileInputStream(propertiesPath);
        } else {
            if (systemPropertyForFileNameSet) {
                throw new RuntimeException("RM properties file not found : '" + propertiesPath + "'");
            }
            logger.debug("Loading empty properties");
            return null;
        }
    }

    /**
     * Get the properties map or load it if needed.
     *
     * @return the properties map corresponding to the default property file.
     */
    public Properties getProperties() {
        if (properties == null) {
            properties = new Properties();
            try {
                InputStream stream = resolvePropertiesFile();
                if (stream == null) {
                    return properties;
                }
                properties.load(stream);
                stream.close();
                updateWithSystemProperties(properties);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return properties;
    }

    /**
     * Set the user java properties to the PASchedulerProperties.<br/>
     * User properties are defined using the -Dname=value in the java command.
     */
    public static void updateWithSystemProperties(Properties properties) {
        if (properties != null) {
            for (Object o : properties.keySet()) {
                String s = System.getProperty((String) o);
                if (s != null) {
                    properties.setProperty((String) o, s);
                }
            }
        }
    }
}

@@ -1679,16 +1679,25 @@
 private 
+volatile 
 Properti
@@ -1709,17 +1709,64 @@
 operties
-;
+ = null; // double checked locking with volatile
 %0A%0A    pr
@@ -4609,30 +4609,257 @@
         
-if (properties
+// double checked locking with volatile%0A        Properties result = properties; // to avoid volatile access twice%0A        if (result == null) %7B%0A            synchronized (this) %7B%0A                result = properties;%0A                if (result
  == null
@@ -4854,32 +4854,40 @@
 sult == null) %7B%0A
+        
             prop
@@ -4894,16 +4894,25 @@
 erties =
+ result =
  new Pro
@@ -4938,14 +4938,29 @@
     
-try %7B%0A
+        try %7B%0A       
     
@@ -4971,16 +4971,17 @@
         
+ 
 InputStr
@@ -5030,24 +5030,32 @@
             
+        
 if (stream =
@@ -5076,32 +5076,40 @@
                 
+   
+     
 return propertie
@@ -5119,34 +5119,51 @@
                 
+      
-%7D%0A
+  %7D%0A%0A        
                 
@@ -5203,16 +5203,24 @@
         
+        
 stream.c
@@ -5243,16 +5243,24 @@
         
+     
+   
 updateWi
@@ -5304,16 +5304,24 @@
         
+        
 %7D catch 
@@ -5346,32 +5346,40 @@
                 
+ 
+       
 throw new Runtim
@@ -5389,24 +5389,64 @@
 ception(e);%0A
+                    %7D%0A                %7D%0A
             
@@ -5468,34 +5468,30 @@
      return 
-properties
+result
 ;%0A    %7D%0A%0A   

Add Double checked locking on PAPropertiesLazyLoader