view flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java @ 5779:ebec12def170

Datacage: Add a pool of builders to make it multi threadable. XML DOM is not thread safe. Therefore the old implementation only allowed one thread to use the builder at a time. As the complexity of the configuration has increased over time this has become a bottleneck of the whole application because it took quiet some time to build a result. Furthermore the builder code path is visited very frequent. So many concurrent requests were piled up resulting in long waits for the users. To mitigate this problem a round robin pool of builders is used now. Each of the pooled builders has an independent copy of the XML template and can be run in parallel. The number of builders is determined by the system property 'flys.datacage.pool.size'. It defaults to 4.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 21 Apr 2013 12:48:09 +0200
parents ae60bb7b8085
children
line wrap: on
line source
package de.intevation.flys.utils;

import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.MultiLineString;

import de.intevation.flys.artifacts.model.LayerInfo;
import de.intevation.flys.artifacts.model.RiverFactory;

import de.intevation.flys.model.River;
import de.intevation.flys.model.RiverAxis;

import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.List;

import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import org.apache.velocity.Template;
import org.hibernate.HibernateException;

public class RiverMapfileGenerator extends MapfileGenerator {

    public static final String XPATH_RIVERMAP_RIVER_PROJECTION =
            "/artifact-database/rivermap/river[@name=$name]/srid/@value";

    public static final String XPATH_RIVERMAP_SHAPEFILE_DIR =
            "/artifact-database/rivermap/shapefile-path/@value";

    public static final String XPATH_RIVERMAP_VELOCITY_LOGFILE =
            "/artifact-database/rivermap/velocity/logfile/@path";

    public static final String XPATH_RIVERMAP_MAPSERVER_URL =
            "/artifact-database/rivermap/mapserver/server/@path";

    public static final String XPATH_RIVERMAP_MAPFILE_PATH =
            "/artifact-database/rivermap/mapserver/mapfile/@path";

    public static final String XPATH_RIVERMAP_MAPFILE_TEMPLATE =
            "/artifact-database/rivermap/mapserver/map-template/@path";

    public static final String XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH =
            "/artifact-database/rivermap/mapserver/templates/@path";

    public static final Pattern DB_URL_PATTERN =
        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");

    public static final Pattern DB_PSQL_URL_PATTERN =
        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");

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

    /**
     * Generate river axis mapfile.
     */
    @Override
    public void generate() {
        logger.debug("generate()");

        List<River>  rivers     = RiverFactory.getRivers();
        List<String> riverFiles = new ArrayList<String>();

        for (River river : rivers) {
            // We expect that every river has only one RiverAxis.
            // This is not correct but currently the case here, see
            // RiverAxis.java.
            List<RiverAxis> riverAxis = null;
            try {
                riverAxis = RiverAxis.getRiverAxis(river.getName());
            }
            catch (HibernateException iae) {
                logger.error("No valid riveraxis found for " + river.getName());
                continue;
            }

            if (riverAxis == null) {
                logger.warn("River " + river.getName() + " has no river axis!");
                continue;
            }
            if (riverAxis.get(0).getGeom() == null) {
                logger.warn("River " + river.getName() +
                    " has no riveraxis geometry!");
                continue;
            }
            MultiLineString geom = riverAxis.get(0).getGeom();
            Envelope extent = geom.getEnvelopeInternal();

            createRiverAxisLayer(
                    river.getName(),
                    river.getId(),
                    Integer.toString(geom.getSRID()),
                    extent.getMinX() + " " +
                    extent.getMinY() + " " +
                    extent.getMaxX() + " " +
                    extent.getMaxY());

            riverFiles.add("river-" + river.getName() + ".map");
        }
        writeMapfile(riverFiles);
    }

    protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) {
        LayerInfo layerInfo = new LayerInfo();
        layerInfo.setName(riverName);
        layerInfo.setConnection(MapUtils.getConnection());
        layerInfo.setConnectionType(MapUtils.getConnectionType());
        layerInfo.setSrid(srid);
        layerInfo.setExtent(extend);
        layerInfo.setType("line");
        // FIXME: Use templates for that
        if (FLYSUtils.isUsingOracle()) {
            layerInfo.setData("geom FROM river_axes USING SRID " + srid);
        } else {
            layerInfo.setData("geom FROM river_axes");
        }
        layerInfo.setFilter("river_id = " + riverID + " and kind_id = 1");
        layerInfo.setTitle(riverName + " RiverAxis");

        File layerFile = new File("river-" + riverName + ".map");
        Template template = getTemplateByName("riveraxis-layer.vm");
        if (template == null) {
            logger.warn("Template riveraxis-layer.vm not found.");
            return;
        }

        try {
            writeLayer(layerInfo, layerFile, template);
        }
        catch (FileNotFoundException e) {
            logger.warn(e.getLocalizedMessage(), e);
        }
    }

    @Override
    protected String getVelocityLogfile() {
        return FLYSUtils.getXPathString(XPATH_RIVERMAP_VELOCITY_LOGFILE);
    }

    @Override
    protected String getMapserverTemplatePath() {
        return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH);
    }

    @Override
    public String getMapserverUrl() {
        return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_URL);
    }

    @Override
    protected String getMapfilePath() {
        return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_PATH);
    }

    @Override
    protected String getMapfileTemplate() {
        return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_TEMPLATE);
    }
}

http://dive4elements.wald.intevation.org