nl.minbzk.dwr.zoeken.enricher.util.CatalinaContextPropertiesPersister.java Source code

Java tutorial

Introduction

Here is the source code for nl.minbzk.dwr.zoeken.enricher.util.CatalinaContextPropertiesPersister.java

Source

/* Copyright (c) 2010 Ministry of the Interior and Kingdom Relations,
 * the Netherlands. All rights reserved.
 * 
 * This file is part of the MinBZK Search Enricher indexing generator.
 * 
 * Search Enricher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Search Enricher 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 Search Enricher. If not, see <http://www.gnu.org/licenses/>. */

package nl.minbzk.dwr.zoeken.enricher.util;

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 <j.veghel@rijksoverheid.nl>
 */
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
    }
}