View Javadoc

1   package org.apache.archiva.webdav.util;
2   /*
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
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
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
18   * under the License.
19   */
20  
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;
27  
28  import javax.servlet.http.HttpSessionEvent;
29  import javax.servlet.http.HttpSessionListener;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * this http session listener will delete repository group index requested by a user
35   * at this end of the http session
36   *
37   * @author Olivier Lamy
38   * @since 1.4-M2
39   */
40  public class TemporaryGroupIndexSessionCleaner
41      implements HttpSessionListener
42  {
43  
44      private Logger log = LoggerFactory.getLogger( getClass() );
45  
46      private IndexMerger indexMerger;
47  
48      public static final String TEMPORARY_INDEX_SESSION_KEY = TemporaryGroupIndexSessionCleaner.class.getName();
49  
50      public void sessionCreated( HttpSessionEvent httpSessionEvent )
51      {
52          // ensure the map is here to avoid NPE
53          if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )
54          {
55              httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
56                                                          new HashMap<String, TemporaryGroupIndex>() );
57          }
58  
59          if ( indexMerger == null )
60          {
61              WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
62                  httpSessionEvent.getSession().getServletContext() );
63              indexMerger = webApplicationContext.getBean( IndexMerger.class );
64          }
65      }
66  
67      public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
68      {
69          Map<String, TemporaryGroupIndex> tempFilesPerKey =
70              (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
71                  TEMPORARY_INDEX_SESSION_KEY );
72  
73          for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
74          {
75              log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
76                        temporaryGroupIndex.getDirectory().getAbsolutePath() );
77              getIndexMerger( httpSessionEvent ).cleanTemporaryGroupIndex( temporaryGroupIndex );
78          }
79      }
80  
81      private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )
82      {
83          if ( indexMerger == null )
84          {
85              WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
86                  httpSessionEvent.getSession().getServletContext() );
87              indexMerger = webApplicationContext.getBean( IndexMerger.class );
88          }
89          return indexMerger;
90      }
91  }
92