001 //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $ 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 package org.deegree.portal.cataloguemanager.control; 037 038 import java.io.BufferedReader; 039 import java.io.File; 040 import java.io.FileInputStream; 041 import java.io.FileOutputStream; 042 import java.io.FileReader; 043 import java.io.IOException; 044 import java.net.URI; 045 import java.nio.charset.Charset; 046 import java.sql.Connection; 047 import java.sql.Statement; 048 import java.util.List; 049 import java.util.Properties; 050 051 import org.deegree.enterprise.control.ajax.AbstractListener; 052 import org.deegree.enterprise.control.ajax.ResponseHandler; 053 import org.deegree.enterprise.control.ajax.WebEvent; 054 import org.deegree.framework.log.ILogger; 055 import org.deegree.framework.log.LoggerFactory; 056 import org.deegree.framework.util.FileUtils; 057 import org.deegree.framework.xml.NamespaceContext; 058 import org.deegree.framework.xml.XMLFragment; 059 import org.deegree.framework.xml.XMLParsingException; 060 import org.deegree.framework.xml.XMLTools; 061 import org.deegree.io.DBConnectionPool; 062 import org.deegree.ogcbase.CommonNamespaces; 063 import org.w3c.dom.Element; 064 import org.xml.sax.SAXException; 065 066 /** 067 * TODO add class documentation here 068 * 069 * @author <a href="mailto:name@deegree.org">Andreas Poth</a> 070 * @author last edited by: $Author: admin $ 071 * 072 * @version $Revision: $, $Date: $ 073 */ 074 public class DoConfigurationBlobListener extends AbstractListener { 075 076 private static final ILogger LOG = LoggerFactory.getLogger( DoConfigurationListener.class ); 077 078 private static final String[] sqlOracle = new String[] { "oracle/create_blob_searchTables.sql" }; 079 080 private static final String[] sqlPostgis = new String[] { "postgis/drop_blob_searchTables.sql", 081 "postgis/create_blob_searchTables.sql" }; 082 083 private XMLFragment xml = null; 084 085 private static NamespaceContext nsc = null; 086 static { 087 if ( nsc == null ) { 088 nsc = CommonNamespaces.getNamespaceContext(); 089 nsc.addNamespace( "md", URI.create( "http://www.deegree.org/cataloguemanager" ) ); 090 } 091 } 092 093 private WebEvent event; 094 095 /* 096 * (non-Javadoc) 097 * 098 * @see 099 * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent 100 * , org.deegree.enterprise.control.ajax.ResponseHandler) 101 */ 102 public void actionPerformed( WebEvent event, ResponseHandler resp ) 103 throws IOException { 104 this.event = event; 105 String cswurl = (String) event.getParameter().get( "cswurl" ); 106 String url = (String) event.getParameter().get( "url" ); 107 String db = (String) event.getParameter().get( "db" ); 108 String user = (String) event.getParameter().get( "user" ); 109 String password = (String) event.getParameter().get( "pw" ); 110 String sid = (String) event.getParameter().get( "sid" ); 111 String driver = null; 112 String database = null; 113 String[] sql = null; 114 if ( db.equalsIgnoreCase( "postgres" ) ) { 115 driver = "org.postgresql.Driver"; 116 database = "jdbc:postgresql://" + url + '/' + sid; 117 sql = sqlPostgis; 118 } else if ( db.equalsIgnoreCase( "oracle" ) ) { 119 driver = "oracle.jdbc.OracleDriver"; 120 database = "jdbc:oracle:thin:@" + url + ':' + sid; 121 sql = sqlOracle; 122 } else { 123 resp.writeAndClose( "ERROR: not supported database type" ); 124 return; 125 } 126 boolean newTables = (Boolean) event.getParameter().get( "newTables" ); 127 boolean transactions = (Boolean) event.getParameter().get( "transactions" ); 128 boolean searchClient = (Boolean) event.getParameter().get( "searchClient" ); 129 boolean editor = (Boolean) event.getParameter().get( "editor" ); 130 if ( newTables ) { 131 try { 132 createTables( driver, database, user, password, sql ); 133 } catch ( Exception e ) { 134 resp.writeAndClose( "ERROR: can not create database: " + e.getMessage() ); 135 return; 136 } 137 } 138 139 xml = new XMLFragment(); 140 try { 141 xml.load( new File( event.getAbsolutePath( "./WEB-INF/web.xml" ) ).toURI().toURL() ); 142 } catch ( SAXException e ) { 143 // should never happen 144 e.printStackTrace(); 145 } 146 147 try { 148 doCSWConfiguration( driver, database, user, password, cswurl, db, transactions ); 149 } catch ( Exception e ) { 150 resp.writeAndClose( "ERROR: can not do CSW configuration: " + e.getMessage() ); 151 return; 152 } 153 154 doSearchClientConfiguration( searchClient, cswurl ); 155 156 doMetadataEditorConfiguration( cswurl ); 157 158 markAsConfigured( searchClient, editor ); 159 160 resp.writeAndClose( "success" ); 161 } 162 163 /** 164 * 165 * @param searchClient 166 * @param editor 167 * @throws IOException 168 */ 169 private void markAsConfigured( boolean searchClient, boolean editor ) 170 throws IOException { 171 String s = event.getAbsolutePath( "WEB-INF/conf/setup/catalogueManager_config.properties" ); 172 Properties p = new Properties(); 173 FileInputStream fis = new FileInputStream( s ); 174 p.load( fis ); 175 fis.close(); 176 p.put( "configured", "true" ); 177 p.put( "searchClient", "" + searchClient ); 178 p.put( "editor", "" + editor ); 179 FileOutputStream fos = new FileOutputStream( s ); 180 p.store( fos, null ); 181 fos.close(); 182 183 } 184 185 /** 186 * @param searchClient 187 * @param cswurl 188 */ 189 private void doSearchClientConfiguration( boolean searchClient, String cswurl ) { 190 // TODO Auto-generated method stub 191 192 } 193 194 /** 195 * @param cswurl 196 * @throws IOException 197 */ 198 private void doMetadataEditorConfiguration( String cswurl ) 199 throws IOException { 200 String s = event.getAbsolutePath( "WEB-INF/conf/cataloguemanager/cataloguemanager.xml" ); 201 XMLFragment cm = new XMLFragment(); 202 try { 203 cm.load( new File( s ).toURI().toURL() ); 204 String xpath = "md:CatalogueService/md:onlineResource"; 205 Element elem = XMLTools.getRequiredElement( cm.getRootElement(), xpath, nsc ); 206 elem.setAttribute( "xlink:href", cswurl ); 207 //XMLTools.setNodeValue( elem, cswurl ); 208 FileUtils.writeToFile( s, cm.getAsPrettyString() ); 209 } catch ( Exception e ) { 210 // should never happen 211 e.printStackTrace(); 212 throw new IOException( e.getMessage() ); 213 } 214 } 215 216 /** 217 * 218 * @param driver 219 * @param url 220 * @param user 221 * @param password 222 * @param cswurl 223 * @param db 224 * @param transactions 225 * @throws XMLParsingException 226 */ 227 private void doCSWConfiguration( String driver, String url, String user, String password, String cswurl, String db, 228 boolean transactions ) 229 throws Exception { 230 // update CSW capabilities/configuration with desired URL 231 String xpath = "./servlet[servlet-name ='owservice']/init-param[param-name ='csw.config']/param-value"; 232 String configFile = XMLTools.getRequiredNodeAsString( xml.getRootElement(), xpath, nsc ); 233 configFile = event.getAbsolutePath( configFile ); 234 XMLFragment csw = new XMLFragment( new File( configFile ).toURI().toURL() ); 235 xpath = "ows:OperationsMetadata/ows:Operation/ows:DCP/ows:HTTP/ows:Get"; 236 List<Element> elements = XMLTools.getElements( csw.getRootElement(), xpath, nsc ); 237 for ( Element element : elements ) { 238 element.setAttributeNS( CommonNamespaces.XLNNS.toASCIIString(), "xlink:href", cswurl ); 239 } 240 xpath = "ows:OperationsMetadata/ows:Operation/ows:DCP/ows:HTTP/ows:Post"; 241 elements = XMLTools.getElements( csw.getRootElement(), xpath, nsc ); 242 for ( Element element : elements ) { 243 element.setAttributeNS( CommonNamespaces.XLNNS.toASCIIString(), "xlink:href", cswurl ); 244 } 245 246 // delete already existing feature type configurations 247 FileUtils.writeToFile( configFile, csw.getAsPrettyString(), Charset.defaultCharset().displayName() ); 248 String ftFile = "WEB-INF/conf/csw/featuretypes/csw_blob_postgres.xsd"; 249 ftFile = event.getAbsolutePath( ftFile ); 250 File tmp = new File( ftFile ); 251 if ( tmp.exists() ) { 252 tmp.delete(); 253 } 254 ftFile = "WEB-INF/conf/csw/featuretypes/csw_blob_oracle.xsd"; 255 ftFile = event.getAbsolutePath( ftFile ); 256 tmp = new File( ftFile ); 257 if ( tmp.exists() ) { 258 tmp.delete(); 259 } 260 261 // set database connection informations and feature type configurations 262 if ( db.equalsIgnoreCase( "postgres" ) ) { 263 ftFile = "WEB-INF/conf/csw/featuretypes/csw_blob_postgres.xsd.ignore"; 264 } else if ( db.equalsIgnoreCase( "oracle" ) ) { 265 ftFile = "WEB-INF/conf/csw/featuretypes/csw_blob_oracle.xsd.ignore"; 266 } 267 268 ftFile = event.getAbsolutePath( ftFile ); 269 XMLFragment ftXML = new XMLFragment( new File( ftFile ).toURI().toURL() ); 270 xpath = "xs:annotation/xs:appinfo/dgjdbc:JDBCConnection/dgjdbc:Driver"; 271 Element element = (Element) XMLTools.getRequiredNode( ftXML.getRootElement(), xpath, nsc ); 272 XMLTools.setNodeValue( element, driver ); 273 xpath = "xs:annotation/xs:appinfo/dgjdbc:JDBCConnection/dgjdbc:Url"; 274 element = (Element) XMLTools.getRequiredNode( ftXML.getRootElement(), xpath, nsc ); 275 XMLTools.setNodeValue( element, url ); 276 xpath = "xs:annotation/xs:appinfo/dgjdbc:JDBCConnection/dgjdbc:User"; 277 element = (Element) XMLTools.getRequiredNode( ftXML.getRootElement(), xpath, nsc ); 278 XMLTools.setNodeValue( element, user ); 279 xpath = "xs:annotation/xs:appinfo/dgjdbc:JDBCConnection/dgjdbc:Password"; 280 element = (Element) XMLTools.getRequiredNode( ftXML.getRootElement(), xpath, nsc ); 281 XMLTools.setNodeValue( element, password ); 282 FileUtils.writeToFile( ftFile.substring( 0, ftFile.length() - 7 ), ftXML.getAsPrettyString(), 283 Charset.defaultCharset().displayName() ); 284 285 adaptProperties( driver, url, user, password, cswurl ); 286 287 } 288 289 /** 290 * @param driver 291 * @param url 292 * @param user 293 * @param password 294 * @param cswurl 295 * @throws IOException 296 */ 297 private void adaptProperties( String driver, String url, String user, String password, String cswurl ) 298 throws IOException { 299 File file = new File( 300 event.getAbsolutePath( "./WEB-INF/classes/org/deegree/ogcwebservices/csw/csw202.properties" ) ); 301 try { 302 FileInputStream fis = new FileInputStream( file ); 303 Properties prop = new Properties(); 304 prop.load( fis ); 305 fis.close(); 306 307 prop.put( "db.driver", driver ); 308 prop.put( "db.url", url ); 309 prop.put( "db.user", user ); 310 prop.put( "db.password", password ); 311 prop.put( "csw.url", cswurl ); 312 313 FileOutputStream fos = new FileOutputStream( file ); 314 prop.store( fos, null ); 315 fos.close(); 316 } catch ( Exception e ) { 317 LOG.logWarning( file + ": " + e.getMessage() ); 318 } 319 file = new File( event.getAbsolutePath( "./WEB-INF/classes/harvestrepository.properties" ) ); 320 try { 321 FileInputStream fis = new FileInputStream( file ); 322 Properties prop = new Properties(); 323 prop.load( fis ); 324 fis.close(); 325 326 prop.put( "harvester.Driver", driver ); 327 prop.put( "harvester.Url", url ); 328 prop.put( "harvester.User", user ); 329 prop.put( "harvester.Password", password ); 330 331 FileOutputStream fos = new FileOutputStream( file ); 332 prop.store( fos, null ); 333 fos.close(); 334 } catch ( Exception e ) { 335 LOG.logWarning( file + ": " + e.getMessage() ); 336 } 337 338 } 339 340 private void createTables( String driver, String url, String user, String password, String[] sql ) 341 throws Exception { 342 343 for ( String script : sql ) { 344 BufferedReader br = null; 345 try { 346 String s = event.getAbsolutePath( "WEB-INF/scripts/sql/" + script ); 347 LOG.logDebug( "load ", s ); 348 br = new BufferedReader( new FileReader( s ) ); 349 } catch ( Exception e ) { 350 throw new Exception( "can not open database creation script: " + script ); 351 } 352 String thisLine, sqlQuery; 353 Statement stmt = null; 354 DBConnectionPool pool = DBConnectionPool.getInstance(); 355 Connection conn = null; 356 try { 357 conn = pool.acquireConnection( driver, url, user, password ); 358 stmt = conn.createStatement(); 359 sqlQuery = ""; 360 while ( ( thisLine = br.readLine() ) != null ) { 361 362 // Skip comments and empty lines 363 if ( thisLine.length() > 0 && thisLine.charAt( 0 ) == '-' || thisLine.length() == 0 ) { 364 continue; 365 } 366 sqlQuery = sqlQuery + " " + thisLine.trim(); 367 // If one command complete 368 if ( sqlQuery.charAt( sqlQuery.length() - 1 ) == ';' ) { 369 sqlQuery = sqlQuery.replace( ';', ' ' ); // Remove the ; since jdbc complains 370 LOG.logDebug( "execute ", sqlQuery ); 371 try { 372 stmt.execute( sqlQuery ); 373 } catch ( Exception ex ) { 374 LOG.logInfo( "executed: ", sqlQuery ); 375 LOG.logError( ex.getMessage() ); 376 } 377 sqlQuery = ""; 378 } 379 } 380 } catch ( Exception ex ) { 381 br.close(); 382 throw ex; 383 } finally { 384 try { 385 if ( conn != null ) { 386 pool.releaseConnection( conn, driver, url, user, password ); 387 } 388 } catch ( Exception e ) { 389 // do nothing 390 } 391 } 392 br.close(); 393 } 394 395 } 396 397 }