com.seajas.search.utilities.spring.CatalinaContextPropertiesPersister.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.spring.CatalinaContextPropertiesPersister.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.spring;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DefaultPropertiesPersister;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Properties persister which aids in resolving placeholders as Catalina context Environment parameters. Note that the persister does not actually persist anything; it only does retrievals.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class CatalinaContextPropertiesPersister extends DefaultPropertiesPersister {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(CatalinaContextPropertiesPersister.class);

    /**
     * Override XML loading so to inject all Catalina context properties.
     * 
     * @param props
     * @param is
     */
    @Override
    public void loadFromXml(final Properties props, final InputStream is) {
        props.putAll(retrieveContextProperties(is));
    }

    /**
     * Retrieve all context key/value pairs as a Properties set.
     * 
     * @param inputStream
     * @return Properties
     */
    private Properties retrieveContextProperties(final InputStream inputStream) {
        Properties result = new Properties();

        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);

            // Normalize the document

            document.getDocumentElement().normalize();

            // Parse the document content

            NodeList environmentNodes = document.getElementsByTagName("Environment");

            for (int s = 0; s < environmentNodes.getLength(); s++) {
                Node node = environmentNodes.item(s);

                if (node.getNodeType() == Node.ELEMENT_NODE)
                    result.setProperty(((Element) node).getAttribute("name"),
                            ((Element) node).getAttribute("value"));
            }
        } catch (ParserConfigurationException e) {
            logger.error(getClass().getName(), e);
        } catch (SAXException e) {
            logger.error(getClass().getName(), e);
        } catch (IOException e) {
            logger.error(getClass().getName(), e);
        }

        return result;
    }

    @Override
    public void store(final Properties props, final OutputStream os, final String header) throws IOException {
        // Do nothing
    }

    @Override
    public void store(final Properties props, final Writer writer, final String header) throws IOException {
        // Do nothing
    }

    @Override
    public void storeToXml(final Properties props, final OutputStream os, final String header) throws IOException {
        // Do nothing
    }

    @Override
    public void storeToXml(final Properties props, final OutputStream os, final String header,
            final String encoding) throws IOException {
        // Do nothing
    }
}