This project has retired. For details please refer to its Attic page.
Source code
001package 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
021import org.apache.archiva.indexer.merger.IndexMerger;
022import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.web.context.WebApplicationContext;
026import org.springframework.web.context.support.WebApplicationContextUtils;
027
028import javax.servlet.http.HttpSessionEvent;
029import javax.servlet.http.HttpSessionListener;
030import java.util.HashMap;
031import 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 */
040public 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    @Override
051    public void sessionCreated( HttpSessionEvent httpSessionEvent )
052    {
053        // ensure the map is here to avoid NPE
054        if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )
055        {
056            httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
057                                                        new HashMap<>() );
058        }
059
060        if ( indexMerger == null )
061        {
062            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
063                httpSessionEvent.getSession().getServletContext() );
064            indexMerger = webApplicationContext.getBean( IndexMerger.class );
065        }
066    }
067
068    @Override
069    public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
070    {
071        Map<String, TemporaryGroupIndex> tempFilesPerKey =
072            (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
073                TEMPORARY_INDEX_SESSION_KEY );
074
075        for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
076        {
077            log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
078                      temporaryGroupIndex.getDirectory().getAbsolutePath() );
079            getIndexMerger( httpSessionEvent ).cleanTemporaryGroupIndex( temporaryGroupIndex );
080        }
081    }
082
083    private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )
084    {
085        if ( indexMerger == null )
086        {
087            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
088                httpSessionEvent.getSession().getServletContext() );
089            indexMerger = webApplicationContext.getBean( IndexMerger.class );
090        }
091        return indexMerger;
092    }
093}
094