/*
 * #%L
 * bitrepository-common
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common;

import java.io.InputStream;

import javax.xml.bind.JAXBException;

/**
 * General class for instantiating configurations based on xml files
 */
public final class ConfigurationFactory {

    /**
     * This is configuration file the <code>\ConfigurationFactory</code> will look , unless another name has been 
     * specified.
     */
    public static final String DEFAULT_CONFIGURATION_CLASSPATH_LOCATION = 
        "configuration/xml/%s-configuration.xml";
    
    /**
     * This is test configuration file the <code>ConfigurationFactory</code> will look for
     */
    public static final String DEFAULT_TEST_CONFIGURATION_CLASSPATH_LOCATION = 
        "configuration/xml/%s-test-configuration.xml";
    
    /**
     * Loads the configuration for a module. 
     * 
     * The following prioritized sequence is used to find the configuration file. 
     * <ol>
     * <li>Tries to load the 'configuration/xml/%s-test-configuration.xml' file from the classpath,
     *  where %s is the modules name.</li>
     * <li>Tries to load the 'configuration/xml/%s--configuration.xml' file from the classpath,
     *  where %s is the modules name.</li>
     * </ol>
     * 
     * The modules name is retrieved via. the moduleCharacteristics objects getLowerCaseNameWithHyphen method.
     * 
     * @param moduleCharacteristics Used for extracting name strings for the module
     * @param configurationClass The class to load the configuration to. 
     * The namespace for this class (defined in the Jaxb generated package-info.java) is also used for the xml loading.
     * @return Returns a new instance of the indicated configuration loaded from file
     */
    public <T> T loadConfiguration(ModuleCharacteristics moduleCharacteristics, Class<T> configurationClass) {

        String defaultFileName = String.format(DEFAULT_CONFIGURATION_CLASSPATH_LOCATION, 
                moduleCharacteristics.getLowerCaseName());
        String defaultTestFileName = String.format(DEFAULT_TEST_CONFIGURATION_CLASSPATH_LOCATION, 
                        moduleCharacteristics.getLowerCaseName());

        InputStream configStream = ClassLoader.getSystemResourceAsStream(defaultTestFileName);
        if (configStream == null) {
            configStream = ClassLoader.getSystemResourceAsStream(defaultFileName);
        }
        if (configStream == null) {
            throw new ConfigurationException("Failed to find " + defaultTestFileName + " or " + defaultFileName + 
                    " in classpath");
        }
        try {
            return (T) JaxbHelper.loadXml(configurationClass, configStream);
        } catch (JAXBException e) {
            throw new ConfigurationException("Failed to load the configuration from " + configStream, 
                    e);
        }
    }
}

@@ -1129,37 +1129,32 @@
 s configuration 
-file 
 the %3Ccode%3E%5CConfi
@@ -1190,55 +1190,45 @@
 ook 
-, unless another name has been %0A     * specifie
+for, if no test configuration is foun
 d.%0A 
@@ -1406,21 +1406,16 @@
 uration 
-file 
 the %3Ccod
@@ -1457,16 +1457,17 @@
 look for
+.
 %0A     */
@@ -2692,24 +2692,33 @@
  default
-FileName
+ClassPathLocation
  = Strin
@@ -2854,24 +2854,33 @@
 aultTest
-FileName
+ClassPathLocation
  = Strin
@@ -3088,24 +3088,33 @@
 aultTest
-FileName
+ClassPathLocation
 );%0A     
@@ -3216,24 +3216,33 @@
 (default
-FileName
+ClassPathLocation
 );%0A     
@@ -3362,66 +3362,84 @@
 Test
-FileName + %22 or %22 + defaultFileName + %0A                   
+ClassPathLocation + %22 or %22 + %0A                    defaultClassPathLocation +
  %22 i

Convert command line Mockup configuration to file based configuration: Last changes after rework.