]> source.dussan.org Git - archiva.git/blob
68ab59791aab881f10407fa6d058a41d75f04e6d
[archiva.git] /
1 package org.apache.archiva.indexer.merger;
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 com.google.common.io.Files;
22
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.time.StopWatch;
29 import org.apache.maven.index.NexusIndexer;
30 import org.apache.maven.index.context.IndexingContext;
31 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
32 import org.apache.maven.index.packer.IndexPacker;
33 import org.apache.maven.index.packer.IndexPackingRequest;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.scheduling.annotation.Async;
37 import org.springframework.stereotype.Service;
38
39 import javax.inject.Inject;
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.concurrent.CopyOnWriteArrayList;
45
46 /**
47  * @author Olivier Lamy
48  * @since 1.4-M2
49  */
50 @Service("indexMerger#default")
51 public class DefaultIndexMerger
52     implements IndexMerger
53 {
54
55     private Logger log = LoggerFactory.getLogger( getClass() );
56
57     @Inject
58     private ManagedRepositoryAdmin managedRepositoryAdmin;
59
60     private MavenIndexerUtils mavenIndexerUtils;
61
62     private NexusIndexer indexer;
63
64     private IndexPacker indexPacker;
65
66     private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<TemporaryGroupIndex>();
67
68     private List<String> runningGroups = new CopyOnWriteArrayList<String>();
69
70     @Inject
71     public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
72         throws PlexusSisuBridgeException
73     {
74         this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
75         this.mavenIndexerUtils = mavenIndexerUtils;
76         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
77     }
78
79     public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
80         throws IndexMergerException
81     {
82         String groupId = indexMergerRequest.getGroupId();
83
84         if ( runningGroups.contains( groupId ) )
85         {
86             log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
87             return null;
88         }
89
90         runningGroups.add( groupId );
91
92         StopWatch stopWatch = new StopWatch();
93         stopWatch.reset();
94         stopWatch.start();
95
96         File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
97
98         String tempRepoId = mergedIndexDirectory.getName();
99
100         try
101         {
102             File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
103             IndexingContext indexingContext =
104                 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
105                                             mavenIndexerUtils.getAllIndexCreators() );
106
107             for ( String repoId : indexMergerRequest.getRepositoriesIds() )
108             {
109                 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
110                 if ( idxToMerge != null )
111                 {
112                     indexingContext.merge( idxToMerge.getIndexDirectory() );
113                 }
114             }
115
116             indexingContext.optimize();
117
118             if ( indexMergerRequest.isPackIndex() )
119             {
120                 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
121                 indexPacker.packIndex( request );
122             }
123             temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
124                                                                 indexMergerRequest.getMergedIndexTtl() ) );
125             stopWatch.stop();
126             log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
127                       stopWatch.getTime() );
128             return indexingContext;
129         }
130         catch ( IOException e )
131         {
132             throw new IndexMergerException( e.getMessage(), e );
133         }
134         catch ( UnsupportedExistingLuceneIndexException e )
135         {
136             throw new IndexMergerException( e.getMessage(), e );
137         }
138         finally
139         {
140             runningGroups.remove( groupId );
141         }
142     }
143
144     @Async
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     public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
173     {
174         return this.temporaryGroupIndexes;
175     }
176 }