]> source.dussan.org Git - archiva.git/blob
1943e6cc7a54e86a4d1d3d4c697b1d29504d2952
[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 ) );
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                     FileUtils.deleteDirectory( temporaryIndex.directory );
132                     temporaryIndexes.remove( temporaryIndex );
133                     log.debug( "remove directory {}", temporaryIndex.directory );
134                 }
135                 catch ( IOException e )
136                 {
137                     log.warn( "failed to remove directory:" + temporaryIndex.directory, e );
138                 }
139             }
140             temporaryIndexes.remove( temporaryIndex );
141         }
142     }
143
144     private static class TemporaryIndex
145     {
146         private long creationTime = new Date().getTime();
147
148         private File directory;
149
150         TemporaryIndex( File directory )
151         {
152             this.directory = directory;
153         }
154
155         @Override
156         public int hashCode()
157         {
158             return Long.toString( creationTime ).hashCode();
159         }
160
161         @Override
162         public boolean equals( Object o )
163         {
164             if ( this == o )
165             {
166                 return true;
167             }
168             if ( !( o instanceof TemporaryIndex ) )
169             {
170                 return false;
171             }
172             return this.creationTime == ( (TemporaryIndex) o ).creationTime;
173         }
174     }
175 }