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