001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wfs/WFServiceFactory.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    package org.deegree.ogcwebservices.wfs;
037    
038    import java.io.IOException;
039    import java.net.URL;
040    
041    import org.deegree.framework.log.ILogger;
042    import org.deegree.framework.log.LoggerFactory;
043    import org.deegree.framework.xml.InvalidConfigurationException;
044    import org.deegree.i18n.Messages;
045    import org.deegree.io.datastore.DatastoreException;
046    import org.deegree.io.datastore.LockManager;
047    import org.deegree.ogcwebservices.OGCWebServiceException;
048    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
049    import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
050    import org.xml.sax.SAXException;
051    
052    /**
053     * Factory class for creating instances of {@link WFService}.
054     *
055     * TODO manage several instances of different WFServices in a pool
056     *
057     * @see WFService
058     *
059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
060     * @author last edited by: $Author: mschneider $
061     *
062     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
063     */
064    public class WFServiceFactory {
065    
066        private static final ILogger LOG = LoggerFactory.getLogger( WFServiceFactory.class );
067    
068        private static WFSConfiguration CONFIG;
069    
070        private static boolean lockManagerInitialized;
071    
072        private WFServiceFactory() {
073            // prevent instantiation
074        }
075    
076        /**
077         *
078         * @return a WFService instance.
079         * @throws OGCWebServiceException
080         */
081        public static WFService createInstance()
082                                throws OGCWebServiceException {
083            if ( CONFIG == null ) {
084                throw new OGCWebServiceException( WFServiceFactory.class.getName(),
085                                                  "configuration has not been initialized" );
086            }
087            synchronized ( WFServiceFactory.class ) {
088                if ( !lockManagerInitialized ) {
089                    try {
090                        LockManager.initialize( CONFIG.getDeegreeParams().getLockManagerDirectory() );
091                    } catch ( DatastoreException e ) {
092                        LOG.logError( e.getMessage(), e );
093                        throw new OGCWebServiceException( "WFSServiceFactory", e.getMessage() );
094                    }
095                    lockManagerInitialized = true;
096                }
097            }
098            return new WFService( CONFIG );
099        }
100    
101        /**
102         *
103         * @param wfsConfiguration
104         * @return a WFSService instance created from the given configuration.
105         * @throws OGCWebServiceException
106         */
107        public static WFService createInstance( WFSConfiguration wfsConfiguration )
108                                throws OGCWebServiceException {
109            LOG.logDebug( "Creating WFService instance." );
110            synchronized ( WFServiceFactory.class ) {
111                if ( !lockManagerInitialized ) {
112                    try {
113                        LockManager.initialize( wfsConfiguration.getDeegreeParams().getLockManagerDirectory() );
114                    } catch ( DatastoreException e ) {
115                        LOG.logError( e.getMessage(), e );
116                        throw new OGCWebServiceException( "WFSServiceFactory", e.getMessage() );
117                    }
118                    lockManagerInitialized = true;
119                }
120            }
121            return new WFService( wfsConfiguration );
122        }
123    
124        /**
125         * Sets the <code>WFSConfiguration</code>. Afterwards, all <code>WFService</code> instances returned by
126         * <code>createInstance()</code> will use this configuration.
127         *
128         * @param config
129         * @throws InvalidConfigurationException
130         */
131        public synchronized static void setConfiguration( WFSConfiguration config )
132                                throws InvalidConfigurationException {
133            validateConfiguration( config );
134            CONFIG = config;
135            // TODO: if service instances have already been created
136            // - destroy all instances
137            // - create new service instances and put them in the pool
138        }
139    
140        /**
141         *
142         * @param serviceConfigurationURL
143         * @throws InvalidConfigurationException
144         * @throws IOException
145         */
146        public synchronized static void setConfiguration( URL serviceConfigurationURL )
147                                throws InvalidConfigurationException, IOException {
148    
149            try {
150                WFSConfigurationDocument cd = new WFSConfigurationDocument();
151                cd.load( serviceConfigurationURL );
152                setConfiguration( cd.getConfiguration() );
153            } catch ( InvalidConfigurationException e ) {
154                throw new InvalidConfigurationException( "WFSServiceFactory", e.getMessage() );
155            } catch ( SAXException e ) {
156                throw new InvalidConfigurationException( "WFSServiceFactory", e.getMessage() );
157            }
158    
159        }
160    
161        /**
162         * @return CONFIG
163         */
164        public static WFSConfiguration getConfiguration() {
165            return CONFIG;
166        }
167    
168        private static void validateConfiguration( WFSConfiguration config )
169                                throws InvalidConfigurationException {
170            String[] versions = config.getServiceIdentification().getServiceTypeVersions();
171            for ( String version : versions ) {
172                if ( !WFService.VERSION.equals( version ) ) {
173                    String msg = Messages.getMessage( "WFS_CONF_UNSUPPORTED_VERSION", version, WFService.VERSION );
174                    throw new InvalidConfigurationException( msg );
175                }
176            }
177        }
178    }