view geo-backend/src/main/java/de/intevation/gnv/geobackend/config/Configuration.java @ 1144:f444cfce0b3d

dummy merge for repo head
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:15:22 +0200
parents ebeb56428409
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.config;

import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;

import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;

import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException;

import de.intevation.gnv.geobackend.util.XMLUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public final class Configuration {

    public static final String CONFIGURATION_ROOT  = "geo-backend";
    public static final String XPATH_QUERIES       = "query-configuration";
    public static final String XPATH_BACKEND       = "backend-configuration";
    public static final String XPATH_CACHE         = "cache";
    public static final String XPATH_CACHE_ENABLED = "@enabled";
    public static final String XPATH_CACHE_CONFIG  = "configuration";

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

    private static Configuration instance;

    private Node   config;
    private String configDir;
    private String placeholder;

    private boolean cacheEnabled;
    private String  cacheConfiguration;



    /**
     * Returns an instance of Configuration.
     *
     * @return an instance of Configuration.
     */
    public static Configuration getInstance() {
        if (instance == null)
            instance = new Configuration();

        return instance;
    }


    /**
     * Constructor that creates a new Configuration object with disabled cache.
     */
    public Configuration() {
        cacheEnabled = false;
    }


    /**
     * Initialize the geo-backend before it is ready to be used. This method
     * calls other <i>init*</i> methods.
     *
     * @param conf A configuration document. This document should contain a node
     * <i>geo-backend</i>. If there are more nodes named <i>geo-backend</i>, the
     * first node is used.
     * @param configDir The path to the root configuration directory.
     * @param placeholder The placeholder used in the configuration document for
     * the root configuration directory.
     */
    public void init(Document conf, String configDir, String placeholder)
    throws QueryContainerException, FileNotFoundException, IOException
    {
        this.config      = conf;
        this.configDir   = configDir;
        this.placeholder = placeholder;

        NodeList root = conf.getElementsByTagName(CONFIGURATION_ROOT);
        if (root == null || root.getLength() == 0) {
            logger.error("No valid configuration for this geobackend given!");
            return;
        }

        initQueries(root.item(0));
        initConnection(root.item(0));
        initCache(root.item(0));
    }


    /**
     * Initialize sql statements.
     *
     * @param conf The geo-backend configuration node.
     */
    protected void initQueries(Node conf)
    throws FileNotFoundException, IOException, QueryContainerException
    {
        String queriesFile = (String) XMLUtils.xpath(
            conf, XPATH_QUERIES, XPathConstants.STRING, null);

        queriesFile = replaceConfigDir(queriesFile);
        logger.info("Initialize queries: " + queriesFile);

        Properties queries        = getProperties(queriesFile);
        QueryContainerFactory qcf = QueryContainerFactory.getInstance();
        qcf.initializeQueryContainer(queries);
    }


    /**
     * Initialize necessary objects used for the database connection.
     *
     * @param connection The geo-backend configuration node.
     */
    protected void initConnection(Node connection)
    throws FileNotFoundException, IOException
    {
        String config = (String)XMLUtils.xpath(
            connection, XPATH_BACKEND, XPathConstants.STRING, null);

        config = replaceConfigDir(config);
        logger.info("Initialize database connection: " + config);

        Properties properties     = getProperties(config);
        ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance();
        cpf.initializeConnectionPool(properties);
    }


    /**
     * Initialize necessary objects used for the sql cache.
     *
     * @param conf The geo-backend configuration node.
     */
    protected void initCache(Node conf) {
        Node cache = (Node) XMLUtils.xpath(
            conf, XPATH_CACHE, XPathConstants.NODE, null);

        String on = (String) XMLUtils.xpath(
            cache, XPATH_CACHE_ENABLED, XPathConstants.STRING, null);

        boolean enabled = Boolean.parseBoolean(on);

        if (enabled) {
            String config = (String) XMLUtils.xpath(
                cache, XPATH_CACHE_CONFIG, XPathConstants.STRING, null);

            if (config != null && config.length() > 0) {
                config = replaceConfigDir(config);
                logger.info("Initialize sql cache with config: " + config);

                this.cacheConfiguration = config;
                this.cacheEnabled       = true;
            }
            else {
                logger.error("SQL cache is enabled, " +
                    "but no configuration was found.");
            }
        }
        else {
            logger.info("SQL cache is disabled.");
        }
    }


    /**
     * Replace placeholder in the configuration. Placeholder are used for the
     * base configuration directory.
     *
     * @param path A string that might contain placeholders.
     *
     * @return <i>path</i> with replaced placeholder.
     */
    protected String replaceConfigDir(String path) {
        return path.replace(placeholder, configDir);
    }


    /**
     * Read a file that contains properties and return a Java Properties object.
     *
     * @param path Path to a properties file.
     *
     * @return the properties contained in the file.
     */
    protected Properties getProperties(String path)
    throws    FileNotFoundException, IOException
    {
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(path);
            Properties properties = new Properties();
            properties.load(inputStream);

            return properties;
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException ioe) {
                }
            }
        }
    }


    /**
     * Returns the state of the cache.
     *
     * @return true if sql queries are cache, otherwise false.
     */
    public boolean isCacheEnabled() {
        return cacheEnabled;
    }


    /**
     * Returns the path of the cache configuration file.
     *
     * @return configuration path.
     */
    public String getCacheConfiguration() {
        return cacheConfiguration;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org