001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/WPVService.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     Aennchenstraße 19
030     53177 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    
044    package org.deegree.ogcwebservices.wpvs;
045    
046    import java.lang.reflect.Constructor;
047    import java.lang.reflect.InvocationTargetException;
048    import java.util.HashMap;
049    import java.util.Set;
050    import java.util.concurrent.ConcurrentHashMap;
051    
052    import org.deegree.framework.log.ILogger;
053    import org.deegree.framework.log.LoggerFactory;
054    import org.deegree.framework.trigger.TriggerProvider;
055    import org.deegree.ogcwebservices.OGCWebService;
056    import org.deegree.ogcwebservices.OGCWebServiceException;
057    import org.deegree.ogcwebservices.OGCWebServiceRequest;
058    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
059    import org.deegree.ogcwebservices.wpvs.configuration.WPVSConfiguration;
060    import org.deegree.ogcwebservices.wpvs.operation.Get3DFeatureInfo;
061    import org.deegree.ogcwebservices.wpvs.operation.GetView;
062    import org.deegree.ogcwebservices.wpvs.operation.WPVSGetCapabilities;
063    
064    /**
065     * A <code>WPVService</code> deligates the clients Requests. If the rquest is an instance of
066     * GetView a (configured) GetViewHandler is instantiated to create an perspective image.
067     * 
068     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
069     * @author last edited by: $Author: apoth $
070     * 
071     * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
072     * 
073     */
074    public class WPVService implements OGCWebService {
075    
076        private static final TriggerProvider TP = TriggerProvider.create( WPVService.class );
077    
078        private static final ILogger LOG = LoggerFactory.getLogger( WPVService.class );
079    
080        private WPVSConfiguration configuration;
081    
082        private ConcurrentHashMap<String, GetViewHandler> getViewHandlers;
083    
084        private String defaultSplitter;
085    
086        /**
087         * Creates a new instance of <code>WPVService</code> from the configuration.
088         * 
089         * @param configuration
090         * @throws OGCWebServiceException
091         *             if instantiating of the configured GetViewHandlers fails.
092         */
093        WPVService( WPVSConfiguration configuration ) {
094            this.configuration = configuration;
095            HashMap<String, String> configuredHandlers = HandlerMapping.getConfiguredGetViewHandlers();
096            Set<String> keys = configuredHandlers.keySet();
097            defaultSplitter = configuration.getDeegreeParams().getDefaultSplitter();
098            // if no defaulthandler in the configuration QUAD will be the Defaultsplitter
099            getViewHandlers = new ConcurrentHashMap<String, GetViewHandler>();
100            for ( String key : keys ) {
101                try {
102                    getViewHandlers.put( key, createHandler( configuredHandlers.get( key ) ) );
103                } catch ( OGCWebServiceException e ) {
104                    LOG.logError( e.getLocalizedMessage(), e );
105                }
106            }
107            // an error occurred while instantiating or no handlers in the bundle or the defaultsplitter
108            // was not in the bundle
109            if ( getViewHandlers.isEmpty() || !getViewHandlers.containsKey( defaultSplitter ) ) {
110                getViewHandlers.put( defaultSplitter, new DefaultGetViewHandler( this ) );
111            }
112    
113        }
114    
115        /**
116         * Returns the capabilities of this service.
117         * 
118         * @return the capabilities, this is an instance of <code>WPVSCapabilities</code>
119         */
120        public OGCCapabilities getCapabilities() {
121            return this.configuration;
122        }
123    
124        /**
125         * Performs the handling of the passed OGCWebServiceEvent directly and returns the result to the
126         * calling class/ method.
127         * 
128         * @param request
129         *            WFS request to perform
130         * 
131         * @throws OGCWebServiceException
132         */
133        public Object doService( OGCWebServiceRequest request )
134                                throws OGCWebServiceException {
135    
136            request = (OGCWebServiceRequest) TP.doPreTrigger( this, request )[0];
137    
138            Object response = null;
139    
140            if ( request instanceof WPVSGetCapabilities ) {
141                // TODO implement partial responses (if only certain sections are requested)
142                response = getCapabilities();
143            } else if ( request instanceof GetView ) {
144    
145                String splitter = ( (GetView) request ).getVendorSpecificParameter( "SPLITTER" );
146                if ( splitter == null || splitter.trim().equals( "" ) )
147                    splitter = defaultSplitter;
148                GetViewHandler gvh = getViewHandlers.get( splitter );
149                if ( gvh == null ) {
150                    gvh = getViewHandlers.get( defaultSplitter );
151                }
152                // if ( "QUAD".equals( ) {
153                // gvh = createHandler( HandlerMapping.getString( "WPVService.GETVIEW.QUAD" ) );
154                // } else {
155                // gvh = createHandler( HandlerMapping.getString( "WPVService.GETVIEW.BOX" ) );
156                // }
157                response = gvh.handleRequest( (GetView) request );
158    
159            } else if ( request instanceof Get3DFeatureInfo ) {
160                Get3DFeatureInfoHandler get3DFeatureInfoHandler = new DefaultGet3DFeatureInfoHandler( this );
161                response = get3DFeatureInfoHandler.handleRequest( (Get3DFeatureInfo) request );
162    
163            } else {
164    
165                throw new OGCWebServiceException( getClass().getName(), "Unknown request type: "
166                                                                        + request.getClass().getName() );
167            }
168    
169            return TP.doPostTrigger( this, response )[0];
170        }
171    
172        @SuppressWarnings("unchecked")
173        private GetViewHandler createHandler( String className )
174                                throws OGCWebServiceException {
175    
176            // describes the signature of the required constructor
177            // Class[] cl = new Class[1];
178            // cl[0] = WPVService.class;
179    
180            // set parameter to submitt to the constructor
181            // Object[] o = new Object[1];
182            // o[0] = this;
183    
184            GetViewHandler handler = null;
185    
186            try {
187                // get constructor
188                Class creator = Class.forName( className );
189                Constructor<GetViewHandler> con = creator.getConstructor( WPVService.class );
190    
191                // call constructor and instantiate a new DataStore
192                handler = con.newInstance( this );
193            } catch ( ClassCastException cce ) {
194                throw new OGCWebServiceException( "The requested class " + className + " is not of type GetViewHandler! \n"
195                                                  + cce.toString() );
196            } catch ( ClassNotFoundException cce ) {
197                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n" + cce.toString() );
198            } catch ( NoSuchMethodException nsme ) {
199                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n" + nsme.toString() );
200            } catch ( InstantiationException ie ) {
201                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n" + ie.toString() );
202            } catch ( IllegalAccessException iae ) {
203                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n" + iae.toString() );
204            } catch ( InvocationTargetException ite ) {
205                throw (OGCWebServiceException) ite.getCause();
206            } catch ( Exception e ) {
207                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n" + e.toString() );
208            }
209    
210            return handler;
211        }
212    
213        /**
214         * Returns a GetViewHandler which is configured to handle the given kind of splitter.
215         * 
216         * @param splitter
217         *            the name of the splitter (supported are QUAD and BBOX)
218         * @return the configured GetViewHandler or the default GetViewHandler if the given String is
219         *         null or empty or not known.
220         */
221        public GetViewHandler getGetViewHandler( String splitter ) {
222            if ( splitter == null || splitter.trim().equals( "" ) )
223                return getViewHandlers.get( defaultSplitter );
224            GetViewHandler gvh = getViewHandlers.get( splitter );
225            if ( gvh == null ) {
226                gvh = getViewHandlers.get( defaultSplitter );
227            }
228            return gvh;
229        }
230    
231        protected WPVSConfiguration getConfiguration() {
232            return configuration;
233        }
234    
235    }