001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wfs/WFServiceFactory.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.ogcwebservices.wfs;
044    
045    import java.io.IOException;
046    import java.net.URL;
047    
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.xml.InvalidConfigurationException;
051    import org.deegree.i18n.Messages;
052    import org.deegree.io.datastore.DatastoreException;
053    import org.deegree.io.datastore.LockManager;
054    import org.deegree.ogcwebservices.OGCWebServiceException;
055    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
056    import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
057    import org.xml.sax.SAXException;
058    
059    /**
060     * Factory class for creating instances of {@link WFService}.
061     * 
062     * TODO manage several instances of different WFServices in a pool
063     * 
064     * @see WFService
065     * 
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067     * @author last edited by: $Author: apoth $
068     * 
069     * @version $Revision: 9348 $, $Date: 2007-12-27 17:59:14 +0100 (Do, 27 Dez 2007) $
070     */
071    public class WFServiceFactory {
072    
073        private static final ILogger LOG = LoggerFactory.getLogger( WFServiceFactory.class );
074    
075        private static WFSConfiguration CONFIG;
076    
077        private static boolean lockManagerInitialized;
078    
079        private WFServiceFactory() {
080            // prevent instantiation
081        }
082    
083        /**
084         * 
085         * @return
086         * @throws OGCWebServiceException
087         */
088        public static WFService createInstance()
089                                throws OGCWebServiceException {
090            if ( CONFIG == null ) {
091                throw new OGCWebServiceException( WFServiceFactory.class.getName(),
092                                                  "configuration has not been initialized" );
093            }
094            if ( !lockManagerInitialized ) {
095                // TODO synchronize using other object
096                synchronized ( LOG ) {
097                    try {
098                        LockManager.initialize( CONFIG.getDeegreeParams().getLockManagerDirectory() );
099                    } catch ( DatastoreException e ) {
100                        LOG.logError( e.getMessage(), e );
101                        throw new OGCWebServiceException( "WFSServiceFactory", e.getMessage() );
102                    }
103                }
104                lockManagerInitialized = true;
105            }
106            return new WFService( CONFIG );
107        }
108    
109        /**
110         * 
111         * @param wfsConfiguration
112         * @return
113         * @throws OGCWebServiceException
114         */
115        public static WFService createInstance( WFSConfiguration wfsConfiguration )
116                                throws OGCWebServiceException {
117            LOG.logDebug( "Creating WFService instance." );
118            if ( !lockManagerInitialized ) {
119                // TODO synchronize using other object
120                synchronized ( LOG ) {
121                    try {
122                        LockManager.initialize( wfsConfiguration.getDeegreeParams().getLockManagerDirectory() );
123                    } catch ( DatastoreException e ) {
124                        LOG.logError( e.getMessage(), e );
125                        throw new OGCWebServiceException( "WFSServiceFactory", e.getMessage() );
126                    }
127                }
128                lockManagerInitialized = true;
129            }
130            return new WFService( wfsConfiguration );
131        }
132    
133        /**
134         * Sets the <code>WFSConfiguration</code>. Afterwards, all <code>WFService</code> instances
135         * returned by <code>createInstance()</code> will use this configuration.
136         * 
137         * @param config
138         * @throws InvalidConfigurationException
139         */
140        public synchronized static void setConfiguration( WFSConfiguration config )
141                                throws InvalidConfigurationException {
142            validateConfiguration( config );
143            CONFIG = config;
144            // TODO: if service instances have already been created
145            // - destroy all instances
146            // - create new service instances and put them in the pool
147        }
148    
149        /**
150         * 
151         * @param serviceConfigurationURL
152         * @throws InvalidConfigurationException
153         * @throws IOException
154         */
155        public synchronized static void setConfiguration( URL serviceConfigurationURL )
156                                throws InvalidConfigurationException, IOException {
157            
158            try {
159                WFSConfigurationDocument cd = new WFSConfigurationDocument();
160                cd.load( serviceConfigurationURL );
161                setConfiguration( cd.getConfiguration() );
162            } catch ( InvalidConfigurationException e ) {
163                throw new InvalidConfigurationException( "WFSServiceFactory", e.getMessage() );
164            } catch ( SAXException e ) {
165                throw new InvalidConfigurationException( "WFSServiceFactory", e.getMessage() );
166            }
167            
168        }
169    
170        /**
171         * @return CONFIG
172         */
173        public static WFSConfiguration getConfiguration() {
174            return CONFIG;
175        }
176    
177        private static void validateConfiguration( WFSConfiguration config )
178                                throws InvalidConfigurationException {
179            String[] versions = config.getServiceIdentification().getServiceTypeVersions();
180            for ( String version : versions ) {
181                if ( !WFService.VERSION.equals( version ) ) {
182                    String msg = Messages.getMessage( "WFS_CONF_UNSUPPORTED_VERSION", version, WFService.VERSION );
183                    throw new InvalidConfigurationException( msg );
184                }
185            }
186        }
187    }