1 package org.apache.archiva.webdav.util;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.indexer.merger.IndexMerger;
22 import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.web.context.WebApplicationContext;
26 import org.springframework.web.context.support.WebApplicationContextUtils;
28 import javax.servlet.http.HttpSessionEvent;
29 import javax.servlet.http.HttpSessionListener;
30 import java.util.HashMap;
34 * this http session listener will delete repository group index requested by a user
35 * at this end of the http session
37 * @author Olivier Lamy
40 public class TemporaryGroupIndexSessionCleaner
41 implements HttpSessionListener
44 private Logger log = LoggerFactory.getLogger( getClass() );
46 private IndexMerger indexMerger;
48 public static final String TEMPORARY_INDEX_SESSION_KEY = TemporaryGroupIndexSessionCleaner.class.getName();
51 public void sessionCreated( HttpSessionEvent httpSessionEvent )
53 // ensure the map is here to avoid NPE
54 if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )
56 httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
60 if ( indexMerger == null )
62 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
63 httpSessionEvent.getSession().getServletContext() );
64 indexMerger = webApplicationContext.getBean( IndexMerger.class );
69 public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
71 @SuppressWarnings( "unchecked" ) Map<String, TemporaryGroupIndex> tempFilesPerKey =
72 (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
73 TEMPORARY_INDEX_SESSION_KEY );
75 for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
77 log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
78 temporaryGroupIndex.getDirectory().toAbsolutePath() );
79 getIndexMerger( httpSessionEvent ).cleanTemporaryGroupIndex( temporaryGroupIndex );
83 private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )
85 if ( indexMerger == null )
87 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
88 httpSessionEvent.getSession().getServletContext() );
89 indexMerger = webApplicationContext.getBean( IndexMerger.class );