Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java @ 443:da21c256a0ba
"horizontale Schnittprofile" are now configured via conf.xml
gnv-artifacts/trunk@491 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 29 Dec 2009 14:54:17 +0000 |
parents | 52e031261eaa |
children | 85f48e287fb3 |
line wrap: on
line source
/** * */ package de.intevation.gnv.artifacts.context; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.File; import java.util.Properties; import java.util.HashMap; import java.util.ArrayList; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; 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.chart.XMLChartTheme; import de.intevation.gnv.artifacts.cache.CacheFactory; import de.intevation.gnv.raster.Palette; import de.intevation.gnv.raster.PaletteManager; import de.intevation.artifacts.ArtifactContextFactory; import de.intevation.artifactdatabase.Config; import de.intevation.artifactdatabase.XMLUtils; /** * @author Tim Englich (tim.englich@intevation.de) * @author Ingo Weinzierl (iweinzierl@intevation.de) * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) */ public class GNVArtifactContextFactory implements ArtifactContextFactory { /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(GNVArtifactContext.class); public static final String XPATH_GEOBACKEND_CONFIGURATION = "artifact-database/geo-backend/backend-configuration"; public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = "artifact-database/geo-backend/query-configuration"; private final static String CACHECONFIGNODEPATH = "/artifact-database/ehcache/configuration"; private final static String CHARTCONFIGNODEPATH = "/artifact-database/gnv/charttemplate/configuration"; public final static String PALETTES_PATH = "/artifact-database/gnv/palettes"; public final static String PALETTE_ITEMS = "palette"; public final static String HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES = "/artifact-database/gnv/horizontal-cross-section-profile/samples/@number"; /** * Constructor */ public GNVArtifactContextFactory() { super(); log.debug("GNVArtifactContextFactory.Constructor"); } /** * @see de.intevation.artifacts.ArtifactContextFactory#createArtifactContext(org.w3c.dom.Document) */ public Object createArtifactContext(Document config) { GNVArtifactContext returnValue = null; try { log.debug("GNVArtifactContextFactory.createArtifactContext"); log.info("Initialisation of the Geo-BackendConnectionPool"); String backendConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_CONFIGURATION); backendConfigurationFile = Config.replaceConfigDir( backendConfigurationFile); Properties properties = getProperties(backendConfigurationFile); ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance(); cpf.initializeConnectionPool(properties); log.info("Initialisation of the QueryContainer"); String queryConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_QUERYCONFIGURATION); queryConfigurationFile = Config.replaceConfigDir( queryConfigurationFile); Properties queryProperties = getProperties(queryConfigurationFile); QueryContainerFactory qcf = QueryContainerFactory.getInstance(); qcf.initializeQueryContainer(queryProperties); log.info("Initialisation of the Cache"); String cacheConfigurationFile = Config.getStringXPath(config, CACHECONFIGNODEPATH); cacheConfigurationFile = Config.replaceConfigDir( cacheConfigurationFile); CacheFactory cf = CacheFactory.getInstance(); cf.initializeCache(cacheConfigurationFile); returnValue = new GNVArtifactContext(config); configurePalettes(config, returnValue); configureChartTemplate(config, returnValue); configureHorizontalCrossSectionProfile(config, returnValue); } catch (FileNotFoundException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } catch (QueryContainerException e) { log.error(e, e); } return returnValue; } protected void configureHorizontalCrossSectionProfile( Document config, GNVArtifactContext context ) { log.info("configuration of horizontal cross section profile"); String numSamples = Config.getStringXPath( config, HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES); Integer samples = GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES; if (numSamples == null) { log.warn("No number of samples found."); } else { try { samples = Integer.valueOf(numSamples); } catch (NumberFormatException nfe) { log.warn("Invalid integer for number of samples"); } } log.info("# horizontal cross section profile samples: " + samples); context.put( GNVArtifactContext.HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES_KEY, samples); } protected void configureChartTemplate( Document config, GNVArtifactContext context ) { log.info("Initialisation of chart template"); String chartConfigFile = Config.getStringXPath( config, CHARTCONFIGNODEPATH ); XMLChartTheme theme = new XMLChartTheme("XMLChartTheme"); if (chartConfigFile == null) { log.warn("no configuration file for chart template found"); } else { chartConfigFile = Config.replaceConfigDir(chartConfigFile); log.debug("Parse xml configuration of " + chartConfigFile); Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile)); if (tmpl != null) { theme.applyXMLConfiguration(tmpl); } else { log.error( "Cannot load chart template from '" + chartConfigFile + "'"); } } context.put(GNVArtifactContext.CHART_TEMPLATE_KEY, theme); } protected void configurePalettes( Document config, GNVArtifactContext context ) { log.info("configure palettes"); HashMap<Integer, PaletteManager> palettes = new HashMap(); Element node = (Element)Config.getNodeXPath(config, PALETTES_PATH); if (node == null) { log.error("No palettes found"); } else { NodeList pals = node.getElementsByTagName(PALETTE_ITEMS); for (int i = 0, N = pals==null?0:pals.getLength(); i < N; ++i) { Element pal = (Element)pals.item(i); String name = pal.getAttribute("name"); String description = pal.getAttribute("description"); String filename = pal.getAttribute("file"); String parameterIds = pal.getAttribute("parameter-ids"); if (name == null || (name = name.trim()).length() == 0) { log.error("Palette has no 'name' attribute."); } else if (filename == null || (filename = filename.trim()).length() == 0) { log.error("Palette '" + name + "' has no 'file' attribute."); } else if (parameterIds == null || (parameterIds = parameterIds.trim()).length() == 0) { log.error("no parameter ids given for '" + name + "'"); } else { ArrayList<Integer> ids = new ArrayList<Integer>(); for (String idString: parameterIds.split("[\t ,]+")) { try { ids.add(Integer.valueOf(idString)); } catch (NumberFormatException nfe) { log.error( "parameter id '" + idString + "' is integer"); } } filename = Config.replaceConfigDir(filename); Document document = XMLUtils.parseDocument( new File(filename)); if (document == null) { log.error("Cannot load palette file '" + filename + "'"); } else { PaletteManager manager = new PaletteManager( name, description, new Palette(document)); for (Integer id: ids) { palettes.put(id, manager); } } } } // for all palettes } context.put(GNVArtifactContext.PALETTES_KEY, palettes); } /** * @param filePath * @return * @throws FileNotFoundException * @throws IOException */ private Properties getProperties(String filePath) throws FileNotFoundException, IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); Properties properties = new Properties(); properties.load(inputStream); return properties; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ioe) {} } } } }