/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License 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 org.apache.stratos.autoscaler.util;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.autoscaler.Constants;

import java.io.File;
import java.util.HashMap;

/**
 * This class contains utility methods for read configuration file.
 */
public class ConfUtil {

	private static Log log = LogFactory.getLog(ConfUtil.class);

	private XMLConfiguration config;

	//To maintain the map of config files
	private static HashMap<String, ConfUtil> instanceMap = new HashMap<String, ConfUtil>();

	private ConfUtil(String configFilePath) {
		try {

			File confFile = new File(configFilePath);
			config = new XMLConfiguration(confFile);
		} catch (ConfigurationException e) {
			log.error("Unable to load configuration file", e);
			config = new XMLConfiguration();  // continue with default values
		}
	}

	/**
	 * Get the instance of the configuration file
	 *
	 * @param configFilePath Configuration file name
	 * @return ConfUtil instance
	 */
	public static ConfUtil getInstance(String configFilePath) {

		if (configFilePath == null || configFilePath.isEmpty()) {
			configFilePath = Constants.AUTOSCALER_CONFIG_FILE_NAME;
		}
		ConfUtil instance = instanceMap.get(configFilePath);
		if (instance == null) {
			instance = new ConfUtil(configFilePath);
			instanceMap.put(configFilePath, instance);
		}
		return instance;
	}

	/**
	 * Get configurations
	 *
	 * @return XMLConfiguration
	 */
	public XMLConfiguration getConfiguration() {
		return config;
	}

}

@@ -1100,16 +1100,58 @@
 nstants;
+%0Aimport org.wso2.carbon.utils.CarbonUtils;
 %0A%0Aimport
@@ -1517,16 +1517,61 @@
 Util%3E();
+%0A%09private static Object mutex = new Object();
 %0A%0A%09priva
@@ -1620,52 +1620,308 @@
  %7B%0A%0A
-%09%09%09File confFile = new File(configFilePath);
+            File confFile;%0A            if (configFilePath != null && !configFilePath.isEmpty()) %7B%0A                confFile = new File(configFilePath);%0A%0A            %7D else %7B%0A                confFile = new File(CarbonUtils.getCarbonConfigDirPath(),Constants.AUTOSCALER_CONFIG_FILE_NAME);%0A            %7D%0A
 %0A%09%09%09
@@ -1961,16 +1961,17 @@
 fFile);%0A
+%0A
 %09%09%7D catc
@@ -2341,140 +2341,67 @@
 %0A%0A%09%09
-if (configFilePath == null %7C%7C configFilePath.isEmpty()) %7B%0A%09%09%09configFilePath = Constants.AUTOSCALER_CONFIG_FILE_NAME;%0A%09%09%7D%0A%09%09ConfUtil 
+ConfUtil instance = instanceMap.get(configFilePath);%0A%09%09if (
 inst
@@ -2410,42 +2410,46 @@
 ce =
- instanceMap.get(configFilePath);%0A
+= null) %7B%0A%09%09%09synchronized (mutex) %7B%0A%09%09
 %09%09if
@@ -2470,16 +2470,18 @@
 null) %7B%0A
+%09%09
 %09%09%09insta
@@ -2516,16 +2516,18 @@
 ePath);%0A
+%09%09
 %09%09%09insta
@@ -2564,16 +2564,27 @@
 tance);%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09re

update with the carbon configuration path