001    // $HeadURL:
002    // /cvsroot/deegree/src/org/deegree/ogcwebservices/wcs/WCSServiceFactory.java,v
003    // 1.4 2004/06/18 15:50:30 tf Exp $
004    /*----------------------------------------------------------------------------
005     This file is part of deegree, http://deegree.org/
006     Copyright (C) 2001-2009 by:
007       Department of Geography, University of Bonn
008     and
009       lat/lon GmbH
010    
011     This library is free software; you can redistribute it and/or modify it under
012     the terms of the GNU Lesser General Public License as published by the Free
013     Software Foundation; either version 2.1 of the License, or (at your option)
014     any later version.
015     This library is distributed in the hope that it will be useful, but WITHOUT
016     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
017     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
018     details.
019     You should have received a copy of the GNU Lesser General Public License
020     along with this library; if not, write to the Free Software Foundation, Inc.,
021     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022    
023     Contact information:
024    
025     lat/lon GmbH
026     Aennchenstr. 19, 53177 Bonn
027     Germany
028     http://lat-lon.de/
029    
030     Department of Geography, University of Bonn
031     Prof. Dr. Klaus Greve
032     Postfach 1147, 53001 Bonn
033     Germany
034     http://www.geographie.uni-bonn.de/deegree/
035    
036     e-mail: info@deegree.org
037    ----------------------------------------------------------------------------*/
038    package org.deegree.ogcwebservices.wms;
039    
040    import java.io.IOException;
041    import java.net.URL;
042    
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.i18n.Messages;
046    import org.deegree.ogcwebservices.wcs.configuration.InvalidConfigurationException;
047    import org.deegree.ogcwebservices.wms.configuration.WMSConfigurationDocument;
048    import org.deegree.ogcwebservices.wms.configuration.WMSConfigurationDocument_1_3_0;
049    import org.deegree.ogcwebservices.wms.configuration.WMSConfigurationType;
050    
051    /**
052     *
053     *
054     *
055     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
059     *
060     * @since 2.0
061     *
062     */
063    public final class WMServiceFactory {
064    
065        private static WMSConfigurationType CONFIG;
066    
067        private static final ILogger LOG = LoggerFactory.getLogger( WMServiceFactory.class );
068    
069        private WMServiceFactory() {
070            CONFIG = null;
071        }
072    
073        /**
074         * Creates a new WMS service instance configured with the given configuration.
075         *
076         * @param config
077         * @return a new service instance
078         */
079        public static WMService getWMSInstance( WMSConfigurationType config ) {
080            return new WMService( config );
081        }
082    
083        /**
084         * Sets the default configuration by value.
085         *
086         * @param wmsConfiguration
087         */
088        public static void setConfiguration( WMSConfigurationType wmsConfiguration ) {
089            CONFIG = wmsConfiguration;
090            // if service instance are already created
091            // destroy all instances
092            // create new service instances and put in pool
093    
094            LOG.logInfo( CONFIG.getServiceIdentification().getTitle() + " (" + CONFIG.getVersion()
095                         + ") service pool initialized." );
096        }
097    
098        /**
099         * Sets the default configuration by URL.
100         *
101         * @param serviceConfigurationUrl
102         * @throws InvalidConfigurationException
103         */
104        @SuppressWarnings("null") // the method was designed to play with fire
105        public static void setConfiguration( URL serviceConfigurationUrl )
106                                throws InvalidConfigurationException {
107    
108            try {
109                WMSConfigurationDocument doc = new WMSConfigurationDocument();
110                WMSConfigurationDocument_1_3_0 doc130 = new WMSConfigurationDocument_1_3_0();
111    
112                // changes start here
113                int dc = 50;
114                boolean configured = false;
115                while ( !configured ) {
116                    try {
117                        doc.load( serviceConfigurationUrl );
118    
119                        if ( "1.3.0".equals( doc.getRootElement().getAttribute( "version" ) ) ) {
120                            LOG.logInfo( Messages.getMessage( "WMS_VERSION130" ) );
121                            doc130.load( serviceConfigurationUrl );
122                            doc = null;
123                        } else {
124                            LOG.logInfo( Messages.getMessage( "WMS_VERSIONDEFAULT" ) );
125                            doc130 = null;
126                        }
127    
128                        configured = true;
129                    } catch ( IOException ioe ) {
130                        if ( serviceConfigurationUrl.getProtocol().startsWith( "http" ) && dc > 0 ) {
131                            LOG.logWarning( "No successful connection to the WMS-Configuration-URL, "
132                                            + "trying again in 10 seconds. Will try " + dc + " more times to connect." );
133                            Thread.sleep( 10000 );
134                            dc--;
135                        } else {
136                            throw ( ioe );
137                        }
138                    }
139                }
140                // changes end here
141    
142                WMSConfigurationType conf;
143    
144                if ( doc != null ) {
145                    conf = doc.parseConfiguration();
146                } else {
147                    conf = doc130.parseConfiguration();
148                }
149    
150                WMServiceFactory.setConfiguration( conf );
151    
152            } catch ( Exception e ) {
153                LOG.logError( e.getMessage(), e );
154                throw new InvalidConfigurationException( "WMServiceFactory", e.getMessage() );
155            }
156    
157        }
158    
159        /**
160         * Returns a new WMS service instance configured with a previously set default configuration.
161         *
162         * @return a new service instance
163         */
164        public static WMService getService() {
165            return WMServiceFactory.getWMSInstance( CONFIG );
166        }
167    
168    }