]> source.dussan.org Git - archiva.git/blob
9c061349030f6bf7876d6decc18c73d14648ca24
[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 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
23 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.index.NexusIndexer;
28 import org.apache.maven.index.context.IndexingContext;
29 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
30 import org.apache.maven.index.packer.IndexPacker;
31 import org.apache.maven.index.packer.IndexPackingRequest;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Service;
36
37 import javax.inject.Inject;
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.Collection;
41 import java.util.Date;
42 import java.util.List;
43 import java.util.concurrent.CopyOnWriteArrayList;
44
45 /**
46  * @author Olivier Lamy
47  * @since 1.4-M2
48  */
49 @Service( "indexMerger#default" )
50 public class DefaultIndexMerger
51     implements IndexMerger
52 {
53
54     private Logger log = LoggerFactory.getLogger( getClass() );
55
56     @Inject
57     private ManagedRepositoryAdmin managedRepositoryAdmin;
58
59     private MavenIndexerUtils mavenIndexerUtils;
60
61     private NexusIndexer indexer;
62
63     private IndexPacker indexPacker;
64
65     private List<TemporaryIndex> temporaryIndexes = new CopyOnWriteArrayList<TemporaryIndex>();
66
67     @Inject
68     public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
69         throws PlexusSisuBridgeException
70     {
71         this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
72         this.mavenIndexerUtils = mavenIndexerUtils;
73         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
74     }
75
76     public File buildMergedIndex( Collection<String> repositoriesIds, boolean packIndex )
77         throws IndexMergerException
78     {
79         File tempRepoFile = Files.createTempDir();
80         tempRepoFile.deleteOnExit();
81
82         String tempRepoId = tempRepoFile.getName();
83
84         try
85         {
86             File indexLocation = new File( tempRepoFile, ".indexer" );
87             IndexingContext indexingContext =
88                 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
89                                             mavenIndexerUtils.getAllIndexCreators() );
90
91             for ( String repoId : repositoriesIds )
92             {
93                 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
94                 if ( idxToMerge != null )
95                 {
96                     indexingContext.merge( idxToMerge.getIndexDirectory() );
97                 }
98             }
99
100             indexingContext.optimize();
101
102             if ( packIndex )
103             {
104                 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
105                 indexPacker.packIndex( request );
106             }
107             temporaryIndexes.add( new TemporaryIndex( tempRepoFile, tempRepoId ) );
108             return indexingContext.getIndexDirectoryFile();
109         }
110         catch ( IOException e )
111         {
112             throw new IndexMergerException( e.getMessage(), e );
113         }
114         catch ( UnsupportedExistingLuceneIndexException e )
115         {
116             throw new IndexMergerException( e.getMessage(), e );
117         }
118     }
119
120
121     @Scheduled( fixedDelay = 900000 )
122     public void cleanTemporaryIndex()
123     {
124         for ( TemporaryIndex temporaryIndex : temporaryIndexes )
125         {
126             // cleanup files older than 30 minutes
127             if ( new Date().getTime() - temporaryIndex.creationTime > 1800000 )
128             {
129                 try
130                 {
131                     IndexingContext context = indexer.getIndexingContexts().get( temporaryIndex.indexId );
132                     if ( context != null )
133                     {
134                         indexer.removeIndexingContext( context, true );
135                     }
136                     else
137                     {
138                         FileUtils.deleteDirectory( temporaryIndex.directory );
139                     }
140                     temporaryIndexes.remove( temporaryIndex );
141                     log.debug( "remove directory {}", temporaryIndex.directory );
142                 }
143                 catch ( IOException e )
144                 {
145                     log.warn( "failed to remove directory:" + temporaryIndex.directory, e );
146                 }
147             }
148             temporaryIndexes.remove( temporaryIndex );
149         }
150     }
151
152     private static class TemporaryIndex
153     {
154         private long creationTime = new Date().getTime();
155
156         private File directory;
157
158         private String indexId;
159
160         TemporaryIndex( File directory, String indexId )
161         {
162             this.directory = directory;
163             this.indexId = indexId;
164         }
165
166         @Override
167         public int hashCode()
168         {
169             return Long.toString( creationTime ).hashCode();
170         }
171
172         @Override
173         public boolean equals( Object o )
174         {
175             if ( this == o )
176             {
177                 return true;
178             }
179             if ( !( o instanceof TemporaryIndex ) )
180             {
181                 return false;
182             }
183             return this.creationTime == ( (TemporaryIndex) o ).creationTime;
184         }
185     }
186 }