001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/enterprise/servlet/ServiceLookup.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    package org.deegree.enterprise.servlet;
037    
038    import java.util.HashMap;
039    import java.util.Iterator;
040    import java.util.Map;
041    
042    import org.deegree.enterprise.ServiceException;
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    
046    /**
047     * look up class for deegree service handler. The class is implemented as singleton to ensure that
048     * only one instance and so just ine mapping is valid with a service instance
049     *
050     * @version $Revision: 18195 $
051     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
052     * @author last edited by: $Author: mschneider $
053     *
054     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
055     *
056     * @since 2.0
057     */
058    public class ServiceLookup {
059    
060        private static final ILogger LOG = LoggerFactory.getLogger( ServiceLookup.class );
061    
062        private static ServiceLookup lookup = null;
063    
064    
065        private static Map<String, Class<?>> services;
066    
067        /**
068         * @return static wrapper for a private constructor
069         */
070        public static ServiceLookup getInstance() {
071            if ( lookup == null ) {
072                lookup = new ServiceLookup();
073            }
074            return lookup;
075        }
076    
077        private ServiceLookup() {
078            services = new HashMap<String, Class<?>>();
079        }
080    
081        /**
082         * Maps a web service to the given class
083         *
084         * @param key
085         * @param clss
086         */
087        public void addService( String key, Class<?> clss ) {
088            services.put( key, clss );
089        }
090    
091        /**
092         * @param key
093         *            to be removed
094         * @return the handler class to which the key is mapped
095         */
096        public Class<?> removeService( String key ) {
097            return services.remove( key );
098        }
099    
100        /**
101         * @param key
102         *            the webservice of interest
103         * @return its handler class
104         */
105        public Class<?> getService( String key ) {
106            return services.get( key );
107        }
108    
109        /**
110         * @return an iterator over all the webservices names.
111         */
112        public Iterator<String> getIterator() {
113            return services.keySet().iterator();
114        }
115    
116        /**
117         * @param serviceName
118         * @param requestIP
119         * @return the Handler of the webservice
120         * @throws ServiceException
121         */
122        public ServiceDispatcher getHandler( String serviceName, String requestIP )
123                                throws ServiceException {
124            Class<?> handlerClass = services.get( serviceName );
125    
126            if ( handlerClass == null ) {
127                throw new ServiceException( "Unknown service handler for " + "requested service:" //$NON-NLS-1$ //$NON-NLS-2$
128                                            + serviceName );
129            }
130            ServiceDispatcher handler = null;
131            try {
132                handler = (ServiceDispatcher) handlerClass.newInstance();
133                // Very dirty hack to get the ip to the handler.
134                if ( handler != null ) {
135                    if ( "WCTS".equalsIgnoreCase( serviceName ) ) {
136                        ( (WCTSHandler) handler ).setIP( requestIP );
137                    }
138                }
139                LOG.logDebug( "Dispatching request to handler '" + handlerClass.getName() + "'." ); //$NON-NLS-1$ //$NON-NLS-2$
140            } catch ( Exception e ) {
141                LOG.logError( "Can't initialize OGC service:" + serviceName, e ); //$NON-NLS-1$
142                throw new ServiceException( "Can't initialize OGC service:" + serviceName ); //$NON-NLS-1$
143            }
144            return handler;
145        }
146    
147        /**
148         * Clear the map
149         */
150        public void clear() {
151            services.clear();
152        }
153    
154    }