//$Header: /home/deegree/jail/deegreerepository/deegree/src/org/deegree/conf/services/wfsconf/WFSConfConfiguration.java,v 1.13 2006/08/24 06:38:30 poth Exp $
/*---------------- FILE HEADER ------------------------------------------
This file is part of deegree.
Copyright (C) 2001-2006 by:
EXSE, Department of Geography, University of Bonn
http://www.giub.uni-bonn.de/deegree/
lat/lon GmbH
http://www.lat-lon.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact:
Andreas Poth
lat/lon GmbH
Aennchenstraße 19
53177 Bonn
Germany
E-Mail: poth@lat-lon.de
Prof. Dr. Klaus Greve
Department of Geography
University of Bonn
Meckenheimer Allee 166
53115 Bonn
Germany
E-Mail: greve@giub.uni-bonn.de
---------------------------------------------------------------------------*/
package org.deegree.conf.services.wfsconf;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.Properties;
import org.deegree.framework.xml.InvalidConfigurationException;
import org.deegree.framework.xml.XMLFragment;
import org.deegree.framework.xml.XMLParsingException;
import org.deegree.framework.xml.XMLTools;
import org.deegree.ogcbase.CommonNamespaces;
import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/**
* ...
*
* @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
*
*/
public class WFSConfConfiguration {
private static WFSConfConfiguration INSTANCE;
private WFSConfigurationDocument wfsCapabilitiesDoc;
private File wfsDataSourceDirectory;
private File sourceDataSourceDirectory;
private final String registryFilename = "ft2datastores.reg";
private File registry;
private Properties properties;
private static final String POLITE_REMINDER = "This file is automatically generated."
+ " Please do not edit, unless you want to break your service.";
public static WFSConfConfiguration getInstance(){
if( INSTANCE == null ) {
synchronized( WFSConfConfiguration.class ) {
if( INSTANCE == null ) {
INSTANCE = new WFSConfConfiguration();
}
}
}
return INSTANCE;
}
private WFSConfConfiguration(){
// empty
}
public void initFromUrl( URL configFile ) throws InvalidConfigurationException {
XMLFragment xmlFragment = new XMLFragment();
try {
xmlFragment.load( configFile );
String templDir =
XMLTools.getRequiredNodeAsString( xmlFragment.getRootElement(),
"./WFSCapabilitiesFile",
CommonNamespaces.getNamespaceContext() );
File dummy = new File( configFile.getFile() );
wfsCapabilitiesDoc = new WFSConfigurationDocument();
URL url = xmlFragment.resolve( new File( templDir ).toURL().toExternalForm() );
//File f = new File( templDir );
this.wfsCapabilitiesDoc.load( url );
createDatasourceDirectories( xmlFragment.getRootElement());
createRegistryFile( dummy.getParent() );
} catch (Exception e) {
e.printStackTrace();
throw new InvalidConfigurationException( "Error initializing GeoDBConf Configuration",
e );
}
}
private void createDatasourceDirectories( Element root )
throws XMLParsingException {
String templDir =
XMLTools.getRequiredNodeAsString( root,
"./SourceDatasourceDirectory",
CommonNamespaces.getNamespaceContext() );
this.sourceDataSourceDirectory = new File( templDir );
templDir =
XMLTools.getRequiredNodeAsString( root,
"./WFSDatasourceDirectory",
CommonNamespaces.getNamespaceContext() );
this.wfsDataSourceDirectory =
new File( templDir );
}
public File getSourceDataSourceDirectory() {
return this.sourceDataSourceDirectory;
}
public File getWFSDataSourceDirectory() {
return this.wfsDataSourceDirectory;
}
public WFSConfigurationDocument getWfsCapabilities() {
return this.wfsCapabilitiesDoc;
}
private void createRegistryFile( String parentDir ) {
registry = new File( parentDir + File.separator + this.registryFilename );
// properties = new Properties();
// properties.load( new FileInputStream( registry ) );
}
public synchronized void registerFeatureType( String featureType, String datastoreName ) throws FileNotFoundException, IOException{
properties = new Properties();
properties.load( new FileInputStream( registry ) );
if ( properties.getProperty( featureType) != null ){
throw new RuntimeException( "Feature type '" + featureType
+ "' is already registered.");
}
//TODO chech if datastoreName also available???
properties.put( featureType, datastoreName );
FileOutputStream fos = new FileOutputStream( registry );
properties.store( fos, POLITE_REMINDER );
fos.close();
}
public synchronized void deregisterFeatureType( String featureType ) throws FileNotFoundException, IOException{
properties = new Properties();
properties.load( new FileInputStream( registry ) );
properties.remove( featureType );
FileOutputStream fos = new FileOutputStream( registry );
properties.store( fos, POLITE_REMINDER );
fos.close();
}
/**
* @throws IOException
* @throws SAXException
*
*/
public synchronized void saveCapabilites() throws IOException, SAXException {
URL u = this.wfsCapabilitiesDoc.getSystemId();
// save
FileWriter fw = new FileWriter( this.wfsCapabilitiesDoc.getSystemId().getFile() );
Writer writer = new BufferedWriter( fw );
this.wfsCapabilitiesDoc.write(writer);
writer.close();
fw.close();
//reload
this.wfsCapabilitiesDoc = new WFSConfigurationDocument();
this.wfsCapabilitiesDoc.load( u );
}
}
/* ********************************************************************
Changes to this class. What the people have been up to:
$Log: WFSConfConfiguration.java,v $
Revision 1.13 2006/08/24 06:38:30 poth
File header corrected
Revision 1.12 2006/06/28 20:20:15 poth
some code clean ups
Revision 1.11 2006/04/06 20:25:32 poth
*** empty log message ***
Revision 1.10 2006/03/30 21:20:29 poth
*** empty log message ***
Revision 1.9 2006/01/10 11:15:18 deshmukh
Changes made to the comments. Spelling mistake corrected.
Revision 1.8 2005/12/21 17:30:10 poth
no message
Revision 1.7 2005/12/09 16:48:47 taddei
operations change capabilities too
Revision 1.6 2005/12/08 15:33:59 taddei
removed println
Revision 1.5 2005/12/07 14:46:51 taddei
added target directory; added registry file
Revision 1.4 2005/12/05 13:11:06 taddei
paths are now relative
Revision 1.3 2005/12/01 14:14:38 taddei
corrected styling to match deegree style guide, code clean up
Revision 1.2 2005/12/01 13:12:07 taddei
added missing headers and footers, changed way how configuration is initialized
********************************************************************** */