001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/owswatch/ServicesConfigurationWriter.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.portal.owswatch; 038 039 import java.io.BufferedWriter; 040 import java.io.FileWriter; 041 import java.io.IOException; 042 import java.util.Enumeration; 043 import java.util.Map; 044 import java.util.Properties; 045 046 import org.deegree.framework.xml.XMLFragment; 047 import org.deegree.framework.xml.XMLTools; 048 import org.w3c.dom.Element; 049 050 /** 051 * This class is used to write services xml. This xml contains the needed informationfor the ServiceMonitor The main 052 * operations here are add/remove. The file will be rewriten after any of these operations 053 * 054 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a> 055 * @author last edited by: $Author: jmays $ 056 * 057 * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21 Okt 2009) $ 058 */ 059 public class ServicesConfigurationWriter { 060 061 private String servicesUrl = null; 062 063 private String prefix = null; 064 065 /** 066 * @param servicesUrl 067 * @param prefix 068 */ 069 public ServicesConfigurationWriter( String servicesUrl, String prefix ) { 070 this.servicesUrl = servicesUrl; 071 this.prefix = prefix; 072 } 073 074 /** 075 * Build an xml document from the current stored services 076 * 077 * @param services 078 * @return root element with all services 079 */ 080 private Element buildXmlDocument( Map<Integer, ServiceConfiguration> services ) { 081 082 String dotPrefix = prefix + ":"; 083 Element root = XMLTools.create().createElementNS( CommonNamepspaces.DEEGREEWSNS.toASCIIString(), 084 dotPrefix + "Config" ); 085 root.setAttribute( "service_id_sequence", String.valueOf( ServiceConfiguration.getServiceCounter() ) ); 086 087 for ( ServiceConfiguration service : services.values() ) { 088 buildServiceElement( service, root ); 089 } 090 return root; 091 } 092 093 /** 094 * Builds an Element from the given service 095 * 096 * @param service 097 * from where the Element will be built 098 * @param root 099 * where the service to be added 100 * @return the newly build service element 101 */ 102 private Element buildServiceElement( ServiceConfiguration service, Element root ) { 103 104 String dotPrefix = prefix + ":"; 105 Element serviceElem = XMLTools.appendElement( root, CommonNamepspaces.DEEGREEWSNS, dotPrefix 106 + Constants.SERVICE_MONITOR ); 107 serviceElem.setAttribute( "id", String.valueOf( service.getServiceid() ) ); 108 // Appending the basic elements 109 XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + Constants.ACTIVE, 110 String.valueOf( service.isActive() ) ); 111 XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + Constants.TIMEOUT_KEY, 112 String.valueOf( service.getTimeout() ) ); 113 XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + Constants.INTERVAL, 114 String.valueOf( service.getRefreshRate() ) ); 115 XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + Constants.ONLINE_RESOURCE, 116 String.valueOf( service.getOnlineResource() ) ); 117 XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + Constants.SERVICE_NAME, 118 String.valueOf( service.getServiceName() ) ); 119 Element httpMethodElem = XMLTools.appendElement( serviceElem, CommonNamepspaces.DEEGREEWSNS, 120 dotPrefix + Constants.HTTP_METHOD ); 121 httpMethodElem.setAttribute( "type", service.getHttpMethod() ); 122 123 // Appending to the HttpMethod Element the request keys/values 124 Properties props = service.getProperties(); 125 Enumeration keys = props.keys(); 126 while ( keys.hasMoreElements() ) { 127 String key = (String) keys.nextElement(); 128 String value = String.valueOf( props.get( key ) ); 129 XMLTools.appendElement( httpMethodElem, CommonNamepspaces.DEEGREEWSNS, dotPrefix + key, value ); 130 } 131 return serviceElem; 132 } 133 134 /** 135 * Writes the given root to the xml file 136 * 137 * @param root 138 * to be written 139 * @throws IOException 140 * In case the file path is invalid or the file is locked 141 */ 142 private void writeToXmlDocument( Element root ) 143 throws IOException { 144 145 BufferedWriter writer = new BufferedWriter( new FileWriter( servicesUrl ) ); 146 147 try { 148 XMLFragment frag = new XMLFragment( root ); 149 String text = frag.getAsPrettyString(); 150 writer.write( text ); 151 writer.close(); 152 } catch ( Exception e ) { 153 throw new IOException( "An error occured while writing the xml file: " + e.getMessage() ); 154 } 155 156 } 157 158 /** 159 * Writes the root element to the given document 160 * 161 * @param serviceconfigs 162 * 163 * @throws IOException 164 */ 165 public void writeDocument( Map<Integer, ServiceConfiguration> serviceconfigs ) 166 throws IOException { 167 168 Element root = buildXmlDocument( serviceconfigs ); 169 writeToXmlDocument( root ); 170 } 171 }