]> source.dussan.org Git - archiva.git/blob
4a939cb491afb422ccb916c537e2be770ca0c00f
[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     @Inject
69     public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
70         throws PlexusSisuBridgeException
71     {
72         this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
73         this.mavenIndexerUtils = mavenIndexerUtils;
74         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
75     }
76
77     public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
78         throws IndexMergerException
79     {
80         StopWatch stopWatch = new StopWatch();
81         stopWatch.reset();
82         stopWatch.start();
83
84         File tempRepoFile = indexMergerRequest.getMergedIndexDirectory();
85
86         String tempRepoId = tempRepoFile.getName();
87
88         try
89         {
90             File indexLocation = new File( tempRepoFile, indexMergerRequest.getMergedIndexPath() );
91             IndexingContext indexingContext =
92                 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
93                                             mavenIndexerUtils.getAllIndexCreators() );
94
95             for ( String repoId : indexMergerRequest.getRepositoriesIds() )
96             {
97                 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
98                 if ( idxToMerge != null )
99                 {
100                     indexingContext.merge( idxToMerge.getIndexDirectory() );
101                 }
102             }
103
104             indexingContext.optimize();
105
106             if ( indexMergerRequest.isPackIndex() )
107             {
108                 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
109                 indexPacker.packIndex( request );
110             }
111             temporaryGroupIndexes.add(
112                 new TemporaryGroupIndex( tempRepoFile, tempRepoId, indexMergerRequest.getGroupId(), indexMergerRequest.getMergedIndexTtl() ) );
113             stopWatch.stop();
114             log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
115                       stopWatch.getTime() );
116             return indexingContext;
117         }
118         catch ( IOException e )
119         {
120             throw new IndexMergerException( e.getMessage(), e );
121         }
122         catch ( UnsupportedExistingLuceneIndexException e )
123         {
124             throw new IndexMergerException( e.getMessage(), e );
125         }
126     }
127
128     @Async
129     public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
130     {
131         if ( temporaryGroupIndex == null )
132         {
133             return;
134         }
135
136         try
137         {
138             IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
139             if ( indexingContext != null )
140             {
141                 indexer.removeIndexingContext( indexingContext, true );
142             }
143             File directory = temporaryGroupIndex.getDirectory();
144             if ( directory != null && directory.exists() )
145             {
146                 FileUtils.deleteDirectory( directory );
147             }
148             temporaryGroupIndexes.remove( temporaryGroupIndex );
149         }
150         catch ( IOException e )
151         {
152             log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
153         }
154     }
155
156     public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
157     {
158         return this.temporaryGroupIndexes;
159     }
160 }