This project has retired. For details please refer to its Attic page.
TemporaryGroupIndexSessionCleaner xref
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      @Override
51      public void sessionCreated( HttpSessionEvent httpSessionEvent )
52      {
53          // ensure the map is here to avoid NPE
54          if ( httpSessionEvent.getSession().getAttribute( TEMPORARY_INDEX_SESSION_KEY ) == null )
55          {
56              httpSessionEvent.getSession().setAttribute( TEMPORARY_INDEX_SESSION_KEY,
57                                                          new HashMap<>() );
58          }
59  
60          if ( indexMerger == null )
61          {
62              WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
63                  httpSessionEvent.getSession().getServletContext() );
64              indexMerger = webApplicationContext.getBean( IndexMerger.class );
65          }
66      }
67  
68      @Override
69      public void sessionDestroyed( HttpSessionEvent httpSessionEvent )
70      {
71          Map<String, TemporaryGroupIndex> tempFilesPerKey =
72              (Map<String, TemporaryGroupIndex>) httpSessionEvent.getSession().getAttribute(
73                  TEMPORARY_INDEX_SESSION_KEY );
74  
75          for ( TemporaryGroupIndex temporaryGroupIndex : tempFilesPerKey.values() )
76          {
77              log.info( "cleanup temporaryGroupIndex {} directory {}", temporaryGroupIndex.getIndexId(),
78                        temporaryGroupIndex.getDirectory().getAbsolutePath() );
79              getIndexMerger( httpSessionEvent ).cleanTemporaryGroupIndex( temporaryGroupIndex );
80          }
81      }
82  
83      private IndexMerger getIndexMerger( HttpSessionEvent httpSessionEvent )
84      {
85          if ( indexMerger == null )
86          {
87              WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
88                  httpSessionEvent.getSession().getServletContext() );
89              indexMerger = webApplicationContext.getBean( IndexMerger.class );
90          }
91          return indexMerger;
92      }
93  }
94