/*
 * Copyright (c) 2013. Knowledge Media Institute - The Open University
 *
 * Licensed 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 uk.ac.open.kmi.iserve.core;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * Module providing support for reading the configuration file and mapping the configuration
 * into {@code @Named} values. iServe modules should install this module if they need general configuration.
 *
 * @author <a href="mailto:carlos.pedrinaci@open.ac.uk">Carlos Pedrinaci</a> (KMi - The Open University)
 * @since 19/09/2013
 */
public class ConfigurationModule extends AbstractModule {

    private static final Logger log = LoggerFactory.getLogger(ConfigurationModule.class);

    private static final String DEFAULT_CONFIG_FILENAME = "config.properties";

    private Properties configProperties;

    public ConfigurationModule() {
        this.configProperties = getProperties(DEFAULT_CONFIG_FILENAME);
    }

    public ConfigurationModule(String configFileName) {
        this.configProperties = getProperties(configFileName);
    }

    public ConfigurationModule(Properties configProperties) {
        this.configProperties = configProperties;
    }

    @Override
    protected void configure() {
        // Bind the configuration file to @named values
        Names.bindProperties(binder(), configProperties);
    }

    private Properties getProperties(String fileName) {
        log.info("Loading configuration from - {}", fileName);
        try {
            Properties properties = new Properties();
            properties.load(getClass().getClassLoader().getResourceAsStream(fileName));
            return properties;
        } catch (IOException ex) {
            log.error("Error obtaining plugin properties", ex);
        }
        return new Properties();
    }
}

@@ -748,32 +748,225 @@
 mes;%0Aimport org.
+apache.commons.configuration.ConfigurationConverter;%0Aimport org.apache.commons.configuration.ConfigurationException;%0Aimport org.apache.commons.configuration.PropertiesConfiguration;%0Aimport org.
 slf4j.Logger;%0Aim
@@ -999,36 +999,8 @@
 ry;%0A
-%0Aimport java.io.IOException;
 %0Aimp
@@ -2242,15 +2242,26 @@
 fo(%22
-Loading
+Attempting to read
  con
@@ -2276,11 +2276,11 @@
 on f
-rom
+ile
  - %7B
@@ -2290,24 +2290,25 @@
  fileName);%0A
+%0A
         try 
@@ -2335,37 +2335,78 @@
 ties
- properties = new Properties(
+Configuration config = new PropertiesConfiguration(%22config.properties%22
 );%0A 
@@ -2416,17 +2416,28 @@
         
-p
+log.debug(%22P
 ropertie
@@ -2441,104 +2441,195 @@
 ties
-.
+ 
 load
-(getClass().getClassLoader().getResourceAsStream(fileName));%0A            return properties;
+ed from - %7B%7D%22, config.getFile().toURI().toASCIIString());%0A            // Return a Properties object with the results%0A            return ConfigurationConverter.getProperties(config);%0A
 %0A   
@@ -2642,18 +2642,29 @@
  catch (
-IO
+Configuration
 Exceptio
@@ -2666,17 +2666,16 @@
 eption e
-x
 ) %7B%0A    
@@ -2703,23 +2703,32 @@
 ror 
-obtaining plugi
+reading the configuratio
 n pr
@@ -2743,22 +2743,15 @@
 %22, e
-x
 );%0A    
-    %7D%0A
     
@@ -2779,16 +2779,26 @@
 ties();%0A
+        %7D%0A
     %7D%0A%7D%0A

Use commons-configuration for parsing of properties for convenience.