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