package org.apache.archiva.webdav.util;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.archiva.indexer.merger.IndexMerger;
import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * this http session listener will delete repository group index requested by a user
 * at this end of the http session
 *
 * @author Olivier Lamy
 * @since 1.4-M2
 */
public class TemporaryGroupIndexSessionCleaner
    implements HttpSessionListener
{

    private Logger log = LoggerFactory.getLogger( getClass() );

    private IndexMerger indexMerger;

    public static final String TEMPORARY_INDEX_SESSION_KEY = TemporaryGroupIndexSessionCleaner.class.getName();

    public void sessionCreated( HttpSessionEvent httpSessionEvent )
    {
        // ensure the map is here to avoid NPE
        httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
                                                    new HashMap<String, TemporaryGroupIndex>() );
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
            httpSessionEvent.getSession().getServletContext() );
        indexMerger = webApplicationContext.getBean( IndexMerger.class );
    }

    public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
    {
        Map<String, TemporaryGroupIndex> tempFilesPerKey =
            (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
                TEMPORARY_INDEX_SESSION_KEY );

        for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
        {
            log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
                      temporaryGroupIndex.getDirectory().getAbsolutePath() );
            indexMerger.cleanTemporaryGroupIndex( temporaryGroupIndex );
        }
    }
}


@@ -1887,16 +1887,127 @@
 oid NPE%0A
+        if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )%0A        %7B%0A    
         
@@ -2079,16 +2079,20 @@
 ON_KEY,%0A
+    
         
@@ -2177,24 +2177,84 @@
 Index%3E() );%0A
+        %7D%0A%0A        if ( indexMerger == null )%0A        %7B%0A    
         WebA
@@ -2360,32 +2360,36 @@
 xt(%0A            
+    
 httpSessionEvent
@@ -2425,32 +2425,36 @@
 xt() );%0A        
+  
+  
 indexMerger = we
@@ -2499,24 +2499,34 @@
 er.class );%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3072,33 +3072,36 @@
  );%0A            
-i
+getI
 ndexMerger.clean
@@ -3094,16 +3094,36 @@
 exMerger
+( httpSessionEvent )
 .cleanTe
@@ -3179,12 +3179,450 @@
  %7D%0A    %7D
+%0A%0A    private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )%0A    %7B%0A        if ( indexMerger == null )%0A        %7B%0A            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(%0A                httpSessionEvent.getSession().getServletContext() );%0A            indexMerger = webApplicationContext.getBean( IndexMerger.class );%0A        %7D%0A        return indexMerger;%0A    %7D
 %0A%7D%0A%0A

fix possible NPE when restarting a tomcat with serialized session