001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/enterprise/servlet/CSWHarvestingContextListener.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.net.URL;
039    import java.util.Arrays;
040    import java.util.Collections;
041    import java.util.List;
042    
043    import javax.servlet.ServletContext;
044    import javax.servlet.ServletContextEvent;
045    import javax.servlet.ServletContextListener;
046    
047    import org.deegree.framework.log.ILogger;
048    import org.deegree.framework.log.LoggerFactory;
049    import org.deegree.framework.util.WebappResourceResolver;
050    import org.deegree.ogcwebservices.OGCWebServiceException;
051    import org.deegree.ogcwebservices.csw.CSWFactory;
052    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilities;
053    import org.deegree.ogcwebservices.csw.manager.Manager_2_0_0;
054    import org.deegree.ogcwebservices.csw.manager.Manager_2_0_2;
055    
056    /**
057     * This class can be used to start up CSW harvesting thread when a servlet context will be
058     * initialized and to free all assigned resources if it will be destroyed.<br>
059     * For this it has to be registered to a servelt context making a &lt;listener&gt; entry to web.xml
060     * 
061     * <pre>
062     *  &lt;web-app&gt;
063     *   &lt;listener&gt;
064     *       &lt;listener-class&gt;com.listeners.MyContextListener&lt;/listener-class&gt;
065     *   &lt;/listener&gt;
066     *   &lt;servlet&gt;
067     *   ...
068     *   &lt;/servlet&gt;
069     *   &lt;servlet-mapping&gt;
070     *    ...
071     *   &lt;/servlet-mapping&gt;
072     *  &lt;/web-app&gt;
073     * &lt;pre&gt;
074     * &#064;version $Revision: 19475 $
075     * &#064;author &lt;a href=&quot;mailto:poth@lat-lon.de&quot;&gt;Andreas Poth&lt;/a&gt;
076     * &#064;author last edited by: $Author: lbuesching $
077     * &#064;version 1.0. $Revision: 19475 $, $Date: 2009-09-02 14:51:48 +0200 (Mi, 02. Sep 2009) $
078     * 
079     * @since 2.0
080     * 
081     */
082    public class CSWHarvestingContextListener implements ServletContextListener {
083    
084        private static final ILogger LOG = LoggerFactory.getLogger( CSWHarvestingContextListener.class );
085    
086        /**
087         * @param event
088         */
089        public void contextDestroyed( ServletContextEvent event ) {
090            // Manager.stopAllHarvester();
091        }
092    
093        /**
094         * @param event
095         */
096        public void contextInitialized( ServletContextEvent event ) {
097            ServletContext sc = event.getServletContext();
098            String s = sc.getInitParameter( "CSW.config" );
099            if ( s == null ) {
100                s = sc.getInitParameter( "csw.config" );
101            }
102    
103            try {
104                URL url = WebappResourceResolver.resolveFileLocation( s, sc, LOG );
105                CSWFactory.setConfiguration( url );
106            } catch ( Exception e ) {
107                LOG.logError( e.getMessage(), e );
108                throw new RuntimeException( Messages.getString( "CSWHarvestingContextListener.ONINIT" ) );
109            }
110    
111            String version = "2.0.0";
112            try {
113                if ( CSWFactory.getService().getCapabilities() instanceof CatalogueCapabilities ) {
114                    CatalogueCapabilities caps = (CatalogueCapabilities) CSWFactory.getService().getCapabilities();
115                    List<String> versions = Arrays.asList( caps.getServiceIdentification().getServiceTypeVersions() );
116                    Collections.sort( versions );
117                    version = versions.get( versions.size() - 1 );
118                }
119            } catch ( OGCWebServiceException e ) {
120                LOG.logError( Messages.getString( "CSWHarvestingContextListener.ONDETECTINGVERSION" ), e );
121            }
122    
123            if ( "2.0.2".equals( version ) ) {
124                Manager_2_0_2.startAllHarvester( version );
125            } else {
126                Manager_2_0_0.startAllHarvester( version );
127            }
128        }
129    
130    }