package uk.ac.ebi.arrayexpress.app;

/*
 * Copyright 2009-2011 European Molecular Biology Laboratory
 *
 * 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.
 *
 */

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConversionException;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

public class ApplicationPreferences
{
    // logging machinery
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private String prefsFileName;
    private Configuration prefs;

    public ApplicationPreferences( String fileName )
    {
        this.prefsFileName = fileName;
    }

    public void initialize()
    {
        load();
    }

    public void terminate()
    {
        if (null != prefs) {
            prefs = null;
        }
    }

    public String getString( String key )
    {
        return prefs.getString(key);
    }

    public String[] getStringArray( String key )
    {
        String value = prefs.getString(key);

        return value.contains(",") ? value.split("\\s*,\\s*") : new String[]{value};
    }

    public Integer getInteger( String key )
    {
        Integer value = null;
        try {
            value = prefs.getInt(key);
        } catch (ConversionException x) {
            logger.error(x.getMessage());
        } catch (NoSuchElementException x) {
            logger.error(x.getMessage());
        } catch (Exception x) {
            logger.error("Caught an exception:", x);
        }
        return value;
    }

    public Long getLong( String key )
    {
        Long value = null;
        try {
            value = prefs.getLong(key);
        } catch (ConversionException x) {
            logger.error(x.getMessage());
        } catch (NoSuchElementException x) {
            logger.error(x.getMessage());
        } catch (Exception x) {
            logger.error("Caught an exception:", x);
        }
        return value;
    }

    public Boolean getBoolean( String key )
    {
        Boolean value = null;
        try {
            value = prefs.getBoolean(key);
        } catch (NoSuchElementException x) {
            logger.error(x.getMessage());
        } catch (Exception x) {
            logger.error("Caught an exception:", x);
        }

        return value;
    }

    public Configuration getConfSubset( String key )
    {
        return prefs.subset(key);
    }

    private void load()
    {
        // todo: what to do if file is not there? must be a clear error message + shutdown
        InputStream prefsStream = null;
        try {
            XMLConfiguration.setDefaultListDelimiter('\uffff');
            XMLConfiguration xmlConfig = new XMLConfiguration();

            prefsStream = Application.getInstance().getResource(
                    "/WEB-INF/classes/" + prefsFileName + ".xml"
            ).openStream();
            xmlConfig.load(prefsStream);

            prefs = xmlConfig;
        } catch (Exception x) {
            logger.error("Caught an exception:", x);
        } finally {
            if (null != prefsStream) {
                try {
                    prefsStream.close();
                } catch (IOException x) {
                    logger.error("Caught an exception:", x);
                }
            }
        }
    }
}

@@ -890,16 +890,37 @@
 ctory;%0A%0A
+import java.io.File;%0A
 import j
@@ -967,16 +967,37 @@
 Stream;%0A
+import java.net.URL;%0A
 import j
@@ -3401,27 +3401,28 @@
         
+URL 
 prefs
-Stream
+URL
  = Appli
@@ -3519,16 +3519,18 @@
 + %22.xml%22
+);
 %0A       
@@ -3534,17 +3534,559 @@
         
-)
+%0A            if (prefsURL == null) %7B%0A            %09logger.info(%22Unable to find resource /WEB-INF/classes/%22 + prefsFileName + %22.xml%22);%0A            %09//fallback to using a system property to locate the file%0A            %09String filename = System.getProperty(%22biosamples.preferences.file%22);%0A            %09logger.info(%22Got value of system property biosamples.preferences.file %22+filename);%0A            %09File prefsFile = new File(filename);%0A            %09prefsURL = prefsFile.toURI().toURL();%0A            %7D%0A            %0A            prefsStream = prefsURL
 .openStr

Add fallback to config file location via property