Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 1065:cf421645a2d3
Made cache statistics available as MBeans (eg in JConsole).
gnv-artifacts/trunk@1157 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 04 Jun 2010 08:47:52 +0000 |
parents | 04967dc9c83f |
children | 7096a2e13676 |
line wrap: on
line source
package de.intevation.gnv.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Date; import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import de.intevation.artifactdatabase.XMLUtils; import de.intevation.artifacts.ArtifactNamespaceContext; import de.intevation.artifacts.CallContext; import de.intevation.gnv.artifacts.context.GNVArtifactContext; import de.intevation.gnv.wms.LayerInfo; /** * This class provides some methods to create files storing meta information * about wms layers and a map service which serves these layers. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class MetaWriter { private static Logger logger = Logger.getLogger(MetaWriter.class); public static final String NODE_MAPSERVER = "mapserver"; public static final String NODE_SERVER = "server"; public static final String NODE_MAP = "map"; public static final String NODE_TTL = "ttl"; public static final String META_FILE_NAME = "meta.xml"; public static final String XPATH_META = "/art:meta"; /** * Constructor. */ private MetaWriter() { } /** * Just create a new document and insert the root node. * * @return the meta document with necessary root node. */ public static Document initMeta() { Document meta = XMLUtils.newDocument(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( meta, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element root = creator.create("meta"); meta.appendChild(root); return meta; } public static boolean insertLayer( CallContext context, Document document, String name, String title, String data, String model, String type, String status ) { if (document == null) { document = initMeta(); } Node meta = (Node) XMLUtils.xpath( document, XPATH_META, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); if (meta == null) { logger.error("No meta node was found in the given document. " + "Cannot insert layer!"); return false; } XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element layerEl = creator.create(LayerInfo.LAYER); Element nameEl = creator.create(LayerInfo.LAYER_NAME); Element titleEl = creator.create(LayerInfo.LAYER_TITLE); Element dataEl = creator.create(LayerInfo.LAYER_DATA); Element modelEl = creator.create(LayerInfo.LAYER_MODEL); Element typeEl = creator.create(LayerInfo.LAYER_TYPE); Element statusEl = creator.create(LayerInfo.LAYER_STATUS); modelEl.setTextContent(model); nameEl.setTextContent(name); titleEl.setTextContent(title); typeEl.setTextContent(type); statusEl.setTextContent(status); dataEl.setTextContent(data); layerEl.appendChild(modelEl); layerEl.appendChild(nameEl); layerEl.appendChild(titleEl); layerEl.appendChild(dataEl); layerEl.appendChild(typeEl); layerEl.appendChild(statusEl); if (logger.isDebugEnabled()) { logger.debug("--------------- WMS LAYER PARAMS ---------------"); logger.debug("Name : " + name); logger.debug("Title : " + title); logger.debug("Data : " + data); logger.debug("Type : " + type); logger.debug("Model : " + model); logger.debug("Status: " + status); } meta.appendChild(layerEl); return true; } /** * Method to write the <i>meta</i> document down to a file. * * @param path The destination of the file. * @param meta The xml document storing the meta information. */ public static boolean writeMetaFile(String path, Document meta) { try { File metaFile = new File(path, META_FILE_NAME); if (metaFile.exists()) { logger.info("Delete old meta information file."); metaFile.delete(); } if (!metaFile.createNewFile() || !metaFile.canWrite()) { logger.error("Error while writing meta file: "+metaFile.toString()); return false; } OutputStream out = null; boolean success = false; try { out = new FileOutputStream(metaFile); success = XMLUtils.toStream(meta, out); } finally { if (out != null) { try { out.close(); } catch (IOException ioe) {} } } if (!success && metaFile.exists()) { metaFile.delete(); } return success; } catch (FileNotFoundException fnfe) { logger.error(fnfe); return false; } catch (IOException ioe) { logger.error(ioe, ioe); return false; } } /** * Append meta information about the mapservice itself. * * @param callContext The CallContext object. * @param document The meta information document. * @param meta The element where the new information need to be appended to. */ public static void insertAbstractMeta( CallContext callContext, Document document ) { if (document == null) { document = initMeta(); } Node meta = (Node) XMLUtils.xpath( document, XPATH_META, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); GNVArtifactContext context = (GNVArtifactContext) callContext.globalContext(); String server = (String) context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY); String map = (String) context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); Long time = callContext.getTimeToLive(); time = time != null ? time + new Date().getTime() : null; String ttl = time != null ? time.toString() : null; if (logger.isDebugEnabled()) { logger.debug("MAPSERVER PATH: " + server); logger.debug("MAP PATH: " + map); logger.debug("TTL: " + ttl); } Element mapserver = creator.create(NODE_MAPSERVER); Element serverPath = creator.create(NODE_SERVER); Element mapPath = creator.create(NODE_MAP); Element timetolive = creator.create(NODE_TTL); mapPath.setTextContent(map); serverPath.setTextContent(server); timetolive.setTextContent(ttl); mapserver.appendChild(serverPath); mapserver.appendChild(mapPath); mapserver.appendChild(timetolive); meta.appendChild(mapserver); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :