This project has retired. For details please refer to its
        
        Attic page.
      
 
DefaultIndexMerger xref
1   package org.apache.archiva.indexer.merger;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
22  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
23  import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
24  import org.apache.commons.io.FileUtils;
25  import org.apache.commons.lang.time.StopWatch;
26  import org.apache.maven.index.NexusIndexer;
27  import org.apache.maven.index.context.IndexingContext;
28  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
29  import org.apache.maven.index.packer.IndexPacker;
30  import org.apache.maven.index.packer.IndexPackingRequest;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.scheduling.annotation.Async;
34  import org.springframework.stereotype.Service;
35  
36  import javax.inject.Inject;
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.Collection;
40  import java.util.List;
41  import java.util.concurrent.CopyOnWriteArrayList;
42  
43  
44  
45  
46  
47  @Service("indexMerger#default")
48  public class DefaultIndexMerger
49      implements IndexMerger
50  {
51  
52      private Logger log = LoggerFactory.getLogger( getClass() );
53  
54      private MavenIndexerUtils mavenIndexerUtils;
55  
56      private NexusIndexer indexer;
57  
58      private IndexPacker indexPacker;
59  
60      private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
61  
62      private List<String> runningGroups = new CopyOnWriteArrayList<String>();
63  
64      @Inject
65      public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
66          throws PlexusSisuBridgeException
67      {
68          this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
69          this.mavenIndexerUtils = mavenIndexerUtils;
70          indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
71      }
72  
73      @Override
74      public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
75          throws IndexMergerException
76      {
77          String groupId = indexMergerRequest.getGroupId();
78  
79          if ( runningGroups.contains( groupId ) )
80          {
81              log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
82              return null;
83          }
84  
85          runningGroups.add( groupId );
86  
87          StopWatch stopWatch = new StopWatch();
88          stopWatch.reset();
89          stopWatch.start();
90  
91          File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
92  
93          String tempRepoId = mergedIndexDirectory.getName();
94  
95          try
96          {
97              File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
98              IndexingContext indexingContext =
99                  indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
100                                             mavenIndexerUtils.getAllIndexCreators() );
101 
102             for ( String repoId : indexMergerRequest.getRepositoriesIds() )
103             {
104                 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
105                 if ( idxToMerge != null )
106                 {
107                     indexingContext.merge( idxToMerge.getIndexDirectory() );
108                 }
109             }
110 
111             indexingContext.optimize();
112 
113             if ( indexMergerRequest.isPackIndex() )
114             {
115                 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
116                 indexPacker.packIndex( request );
117             }
118 
119             if ( indexMergerRequest.isTemporary() )
120             {
121                 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
122                                                                     indexMergerRequest.getMergedIndexTtl() ) );
123             }
124             stopWatch.stop();
125             log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
126                       stopWatch.getTime() );
127             return indexingContext;
128         }
129         catch ( IOException e )
130         {
131             throw new IndexMergerException( e.getMessage(), e );
132         }
133         catch ( UnsupportedExistingLuceneIndexException e )
134         {
135             throw new IndexMergerException( e.getMessage(), e );
136         }
137         finally
138         {
139             runningGroups.remove( groupId );
140         }
141     }
142 
143     @Async
144     @Override
145     public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
146     {
147         if ( temporaryGroupIndex == null )
148         {
149             return;
150         }
151 
152         try
153         {
154             IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
155             if ( indexingContext != null )
156             {
157                 indexer.removeIndexingContext( indexingContext, true );
158             }
159             File directory = temporaryGroupIndex.getDirectory();
160             if ( directory != null && directory.exists() )
161             {
162                 FileUtils.deleteDirectory( directory );
163             }
164             temporaryGroupIndexes.remove( temporaryGroupIndex );
165         }
166         catch ( IOException e )
167         {
168             log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
169         }
170     }
171 
172     @Override
173     public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
174     {
175         return this.temporaryGroupIndexes;
176     }
177 }