001    //$Header: /raid/cvs-repos/cvsroot/lgv_3g/src/de/latlon/lgv3d/WPVSClientConfig.java,v 1.1 2007/03/09 10:40:46 ap Exp $
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2007 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023    
024     Contact:
025    
026     Andreas Poth
027     lat/lon GmbH
028     Aennchenstr. 19
029     53177 Bonn
030     Germany
031     E-Mail: poth@lat-lon.de
032    
033     Prof. Dr. Klaus Greve
034     Department of Geography
035     University of Bonn
036     Meckenheimer Allee 166
037     53115 Bonn
038     Germany
039     E-Mail: greve@giub.uni-bonn.de
040    
041     ---------------------------------------------------------------------------*/
042    
043    package org.deegree.portal.common;
044    
045    import java.io.IOException;
046    import java.io.InputStream;
047    import java.net.MalformedURLException;
048    import java.net.URL;
049    import java.util.ArrayList;
050    import java.util.List;
051    import java.util.Properties;
052    
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.model.crs.CoordinateSystem;
056    import org.deegree.ogcwebservices.OWSUtils;
057    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
058    import org.deegree.ogcwebservices.wpvs.capabilities.Dataset;
059    import org.deegree.ogcwebservices.wpvs.capabilities.WPVSCapabilities;
060    import org.deegree.ogcwebservices.wpvs.capabilities.WPVSCapabilitiesDocument;
061    import org.xml.sax.SAXException;
062    
063    /**
064     * TODO add documentation here
065     * 
066     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
067     * @author last edited by: $Author: ap $
068     * 
069     * @version $Revision: 1.1 $, $Date: 2007/03/09 10:40:46 $
070     */
071    public class WPVSClientConfig {
072    
073        private final String[] initialBBox;
074        
075        private final String initialBBoxString;
076        
077        private final String wpvsBaseURL;
078    
079        private final int viewHeight;
080    
081        private final int viewWidth;
082    
083        private final int overViewHeight;
084    
085        private final int overViewWidth;
086        
087        private final String[] availableDatasets;
088    
089        private final WPVSCapabilities WPVS_Capabilities;
090    
091        private final String WMS_GetMap_Fragment;
092    
093        private static WPVSClientConfig clientConfig;
094    
095        private final Properties clientOptions;
096        
097        private final String defaultCRS;
098    
099        private final String propertieFile = "wpvsclient.properties";
100    
101        private final String elevationModel;
102        
103        private final String serviceIdentification;
104        
105        private final int distanceAboveSeaLevel;
106    
107        private final int initialPitch;
108    
109        private final int initialRoll;
110    
111        private final int initialYaw;
112        
113        private final int initialHeight;
114    
115        private final int initialDistance;
116    
117        private static ILogger LOG = LoggerFactory.getLogger( WPVSClientConfig.class );
118    
119        private WPVSClientConfig() throws SAXException, InvalidCapabilitiesException, IOException {
120            // Singleton pattern
121            clientOptions = new Properties();
122            try {
123                InputStream clientProps = WPVSClientConfig.class.getResourceAsStream( "/" + propertieFile );
124                if ( clientProps == null ) {
125                    clientProps = WPVSClientConfig.class.getResourceAsStream( propertieFile );
126                }
127                clientOptions.load( clientProps );
128            } catch ( IOException e ) {
129                LOG.logError( e.getMessage(), e );
130            }
131    
132            String value = clientOptions.getProperty( "initialBBox" );
133            if( value == null || "".equals( value.trim() ) ){
134                LOG.logError( "The property 'initialBBox' can not be found or has no value. Please insert it into the wpvsclient.properties." );
135                throw new IllegalArgumentException( "The property 'initialBBox' can not be found or has no value. Please insert it into the wpvsclient.properties." );
136            }
137            
138            initialBBox = value.split( "," );
139            if( initialBBox.length != 4 ){
140                LOG.logError( "The property 'initialBBox' must have exactly 4 values seperated by commata (',')." );
141                throw new IllegalArgumentException( "The property 'initialBBox' must have exactly 4 values seperated by commata (',')." );
142    
143            }
144            initialBBoxString = value;
145    
146            String wpvsAddress = clientOptions.getProperty( "wpvsService" );
147            if( wpvsAddress == null ){
148                LOG.logError( "The property 'wpvsService' can not be found or has no value. Please insert it into the wpvsclient.properties." );
149                throw new IllegalArgumentException( "The property 'wpvsService' can not be found or has no value. Please insert it into the wpvsclient.properties." );
150            }
151    
152            wpvsBaseURL = OWSUtils.validateHTTPGetBaseURL( wpvsAddress );
153            
154            URL wpvsAddressURL = null;
155            try {
156                wpvsAddressURL = new URL( wpvsAddress + "request=GetCapabilities&service=WPVS" );
157            } catch ( MalformedURLException e ) {
158                throw new IllegalArgumentException( "The value ("+wpvsAddress + ") of property 'WPVS_Capabilities' is not a valid URL because: " + e.getMessage() );
159            }
160            WPVSCapabilitiesDocument capsDoc = new WPVSCapabilitiesDocument();
161            try {
162                capsDoc.load( wpvsAddressURL );
163            } catch ( IOException e ) {
164                LOG.logError( "Error while contacting the wpvs at location: '" + wpvsAddress + "' please make sure, the server is a valid wpvs.");
165                throw e;
166            }
167            WPVS_Capabilities = (WPVSCapabilities) capsDoc.parseCapabilities();
168            
169            String serviceID = WPVS_Capabilities.getServiceIdentification().getName();
170            if( serviceID == null ){
171                serviceIdentification = "Unknown WPVS service";
172            } else {
173                serviceIdentification = serviceID;
174            }
175    
176            //find the datasets and a default crs.
177            Dataset ds = WPVS_Capabilities.getDataset();
178            if( ds == null ){
179                throw new IllegalArgumentException( "Found no rootdataset in the capabilitiesdocument, this may not be, please make sure, the server at location: " + wpvsBaseURL + " is a valid wpvs." );
180            }
181            List<Dataset> foundSets = new ArrayList<Dataset>();
182            foundSets.add( ds );
183            findDataSets( ds, foundSets );
184            availableDatasets = new String[foundSets.size()];
185            String elevationModeltmp = null;
186            for( int i = 0; i < foundSets.size(); ++i ){
187                availableDatasets[i] = foundSets.get(i).getName();
188                if( elevationModeltmp==null && foundSets.get(i).getElevationModel()!= null ){
189                    LOG.logInfo( "Using elevationModel of dataset: " + availableDatasets[i] );
190                    elevationModeltmp = foundSets.get(i).getElevationModel().getName();
191                }
192    
193            }
194            
195            if( elevationModeltmp == null ){
196                LOG.logInfo( "Found no elevationModel in the capabilitiesdocument, though correct it is a little awkward, please make sure, the server at location: " + wpvsBaseURL + " is configured correctly." );
197            }
198            
199            elevationModel = elevationModeltmp;
200            
201            value = clientOptions.getProperty( "defaultCRS" );
202            if( value == null || "".equals( value.trim() )){
203                LOG.logInfo( "The property 'defaultCRS' can not be found or has no value. Trying to find in datasets." );
204                CoordinateSystem[] crs = ds.getCrs();
205                if( crs == null || crs.length == 0 ){
206                    LOG.logInfo( "No crs's found in datasets, setting defaultCRS to EPSG:4326." );
207                    defaultCRS = "EPGS:4326";
208                } else {
209                    defaultCRS = crs[0].getFormattedString();
210                }
211            } else {
212                defaultCRS = value.trim();
213            } 
214    
215            
216    
217            WMS_GetMap_Fragment = clientOptions.getProperty( "WMS_GetMap_Fragment" );
218            if( WMS_GetMap_Fragment == null ){
219                LOG.logError( "The property WMS_GetMap_Fragment can not be found or has no value. Please insert it into the wpvsclient.properties." );
220                throw new IllegalArgumentException( "The property WMS_GetMap_Fragment can not be found or has no value. Please insert it into the wpvsclient.properties." );
221            }
222    
223            value = clientOptions.getProperty( "viewHeight" );
224            if( value == null ){
225                LOG.logInfo( "The property 'viewHeight' can not be found or has no value. Setting viewHeight to a value of 600." );
226                viewHeight = 600;
227            } else {
228                viewHeight = Integer.parseInt( value );
229            }
230            
231            value = clientOptions.getProperty( "viewWidth" );
232            if( value == null ){
233                LOG.logInfo( "The property 'viewWidth' can not be found or has no value. Setting viewWidth to a value of 800." );
234                viewWidth = 800;
235            } else {
236                viewWidth = Integer.parseInt( value );
237            }
238            
239            value = clientOptions.getProperty( "overViewHeight" );
240            if( value == null ){
241                LOG.logInfo( "The property 'overViewHeight' can not be found or has no value. Setting overViewHeight to a value of 150." );
242                overViewHeight = 150;
243            } else {
244                overViewHeight = Integer.parseInt( value );
245            }
246            
247            value = clientOptions.getProperty( "overViewWidth" );
248            if( value == null ){
249                LOG.logInfo( "The property 'overViewWidth' can not be found or has no value. Setting overViewWidth to a value of 150." );
250                overViewWidth = 150;
251            } else {
252                overViewWidth = Integer.parseInt( value );
253            }
254            
255            value = clientOptions.getProperty( "distanceAboveSeaLevel" );
256            if( value == null ){
257                LOG.logInfo( "The property 'distanceAboveSeaLevel' can not be found or has no value. Setting initialElevation to a value of 150." );
258                distanceAboveSeaLevel = 0;
259            } else {
260                distanceAboveSeaLevel = Integer.parseInt( value );
261            }
262            value = clientOptions.getProperty( "initialHeight" );
263            if( value == null ){
264                LOG.logInfo( "The property 'initialHeight' can not be found or has no value. Setting initialElevation to a value of 150." );
265                initialHeight = 150;
266            } else {
267                initialHeight = Integer.parseInt( value );
268            }
269            
270            value = clientOptions.getProperty( "initialPitch" );
271            if( value == null ){
272                LOG.logInfo( "The property 'initialPitch' can not be found or has no value. Setting initialPitch to a value of 50." );
273                initialPitch = 50;
274            } else {
275                initialPitch = Integer.parseInt( value );
276            }
277            
278            value = clientOptions.getProperty( "initialRoll" );
279            if( value == null ){
280                LOG.logInfo( "The property 'initialRoll' can not be found or has no value. Setting initialRoll to a value of 0." );
281                initialRoll = 0;
282            } else {
283                initialRoll = Integer.parseInt( value );
284            }
285            
286            value = clientOptions.getProperty( "initialYaw" );
287            if( value == null ){
288                LOG.logInfo( "The property 'initialYaw' can not be found or has no value. Setting initialYaw to a value of 180." );
289                initialYaw = 180;
290            } else {
291                initialYaw = Integer.parseInt( value );
292            }
293                
294            value = clientOptions.getProperty( "initialDistance" );
295            if( value == null ){
296                LOG.logInfo( "The property 'initialDistance' can not be found or has no value. Setting initialDistance to a value of 1000." );
297                initialDistance = 1000;
298            } else {
299                initialDistance = Integer.parseInt( value );
300            }
301            
302    
303            
304            
305    
306        }
307        
308        private void findDataSets( Dataset rootSet, List<Dataset> foundSets ){
309            if( rootSet != null ){
310                Dataset[] childSets = rootSet.getDatasets();
311                for( Dataset ds : childSets ){
312                    if( !foundSets.contains( ds ) ){
313                        foundSets.add( ds );
314                        findDataSets( ds, foundSets );
315                    }
316                }
317            }
318        }
319    
320        /**
321         * @return a WPVSClientConfig instance following a singleton pattern.
322         * @throws SAXException if the creation of the capabilities document fails
323         * @throws IOException if the wpvs capabilities document cannot be read
324         * @throws InvalidCapabilitiesException if the wpvs capabilitiesdocument cannot be parsed.
325         */
326        public static synchronized WPVSClientConfig getInstance() throws InvalidCapabilitiesException, IOException, SAXException {
327            if ( clientConfig == null ) {
328                clientConfig = new WPVSClientConfig();
329            }
330            return clientConfig;
331        }
332    
333        /**
334         * 
335         * @return initial bounding box for GetView request
336         */
337        public final String[] getInitialBBox() {
338            return initialBBox;
339        }
340        
341    
342        /**
343         * 
344         * @return WMS GetMap request fragment for overview map
345         */
346        public final String getWmsGetMapFragment() {
347            return WMS_GetMap_Fragment;
348        }
349    
350        /**
351         * 
352         * @return capabilities of the WPVS
353         */
354        public final WPVSCapabilities getWpvsCapabilities() {
355            return WPVS_Capabilities;
356        }
357    
358        /**
359         * @return the overViewHeight.
360         */
361        public final int getOverViewHeight() {
362            return overViewHeight;
363        }
364    
365        /**
366         * @return the overViewWidth.
367         */
368        public final int getOverViewWidth() {
369            return overViewWidth;
370        }
371    
372        /**
373         * @return the viewHeight.
374         */
375        public final int getViewHeight() {
376            return viewHeight;
377        }
378    
379        /**
380         * @return the viewWidth.
381         */
382        public final int getViewWidth() {
383            return viewWidth;
384        }
385    
386        /**
387         * @return the wpvsBaseURL.
388         */
389        public final String getWpvsBaseURL() {
390            return wpvsBaseURL;
391        }
392    
393        /**
394         * @return the availableDatasets.
395         */
396        public final String[] getAvailableDatasets() {
397            return availableDatasets;
398        }
399    
400        /**
401         * @return the defaultCRS.
402         */
403        public final String getDefaultCRS() {
404            return defaultCRS;
405        }
406    
407        /**
408         * @return the elevationModel.
409         */
410        public final String getElevationModel() {
411            return elevationModel;
412        }
413    
414        /**
415         * @return the serviceIdentification.
416         */
417        public final String getServiceIdentification() {
418            return serviceIdentification;
419        }
420    
421        /**
422         * @return the initialElevation.
423         */
424        public final int getDistanceAboveSeaLevel() {
425            return distanceAboveSeaLevel;
426        }
427    
428        /**
429         * @return the initialBBoxString.
430         */
431        public final String getInitialBBoxAsString() {
432            return initialBBoxString;
433        }
434    
435        /**
436         * @return the initialPitch.
437         */
438        public final int getInitialPitch() {
439            return initialPitch;
440        }
441    
442        /**
443         * @return the initialDistance.
444         */
445        public final int getInitialDistance() {
446            return initialDistance;
447        }
448    
449        /**
450         * @return the initialRoll.
451         */
452        public final int getInitialRoll() {
453            return initialRoll;
454        }
455    
456        /**
457         * @return the initialYaw.
458         */
459        public final int getInitialYaw() {
460            return initialYaw;
461        }
462    
463        /**
464         * @return the initialHeight.
465         */
466        public final int getInitialHeight() {
467            return initialHeight;
468        }
469    
470    }