001    package org.apache.archiva.webdav.util;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    
021    import org.apache.archiva.indexer.merger.IndexMerger;
022    import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.springframework.web.context.WebApplicationContext;
026    import org.springframework.web.context.support.WebApplicationContextUtils;
027    
028    import javax.servlet.http.HttpSessionEvent;
029    import javax.servlet.http.HttpSessionListener;
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    /**
034     * this http session listener will delete repository group index requested by a user
035     * at this end of the http session
036     *
037     * @author Olivier Lamy
038     * @since 1.4-M2
039     */
040    public class TemporaryGroupIndexSessionCleaner
041        implements HttpSessionListener
042    {
043    
044        private Logger log = LoggerFactory.getLogger( getClass() );
045    
046        private IndexMerger indexMerger;
047    
048        public static final String TEMPORARY_INDEX_SESSION_KEY = TemporaryGroupIndexSessionCleaner.class.getName();
049    
050        public void sessionCreated( HttpSessionEvent httpSessionEvent )
051        {
052            // ensure the map is here to avoid NPE
053            if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )
054            {
055                httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
056                                                            new HashMap<String, TemporaryGroupIndex>() );
057            }
058    
059            if ( indexMerger == null )
060            {
061                WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
062                    httpSessionEvent.getSession().getServletContext() );
063                indexMerger = webApplicationContext.getBean( IndexMerger.class );
064            }
065        }
066    
067        public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
068        {
069            Map<String, TemporaryGroupIndex> tempFilesPerKey =
070                (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
071                    TEMPORARY_INDEX_SESSION_KEY );
072    
073            for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
074            {
075                log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
076                          temporaryGroupIndex.getDirectory().getAbsolutePath() );
077                getIndexMerger( httpSessionEvent ).cleanTemporaryGroupIndex( temporaryGroupIndex );
078            }
079        }
080    
081        private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )
082        {
083            if ( indexMerger == null )
084            {
085                WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
086                    httpSessionEvent.getSession().getServletContext() );
087                indexMerger = webApplicationContext.getBean( IndexMerger.class );
088            }
089            return indexMerger;
090        }
091    }
092