]> source.dussan.org Git - archiva.git/blob
ed455b08b85a5717bc619da2a60da2b875948952
[archiva.git] /
1 package org.apache.archiva.scheduler.indexing;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the Li
16  * cense is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied.  See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
28 import org.apache.archiva.redback.components.taskqueue.Task;
29 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
30 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.maven.index.ArtifactContext;
34 import org.apache.maven.index.ArtifactContextProducer;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.NexusIndexer;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.SourcedSearchExpression;
41 import org.apache.maven.index.packer.IndexPacker;
42 import org.apache.maven.index.packer.IndexPackingRequest;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
46
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
49 import java.io.File;
50 import java.io.IOException;
51
52 /**
53  * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
54  * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
55  * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
56  */
57 @Service ( "taskExecutor#indexing" )
58 public class ArchivaIndexingTaskExecutor
59     implements TaskExecutor
60 {
61     private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
62
63     private IndexPacker indexPacker;
64
65     private ArtifactContextProducer artifactContextProducer;
66
67     @Inject
68     private PlexusSisuBridge plexusSisuBridge;
69
70     @Inject
71     private ManagedRepositoryAdmin managedRepositoryAdmin;
72
73     private NexusIndexer nexusIndexer;
74
75     @PostConstruct
76     public void initialize()
77         throws PlexusSisuBridgeException
78     {
79         log.info( "Initialized {}", this.getClass().getName() );
80
81         artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
82
83         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
84
85         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
86
87     }
88
89     /**
90      * depending on current {@link Task} you have.
91      * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action.FINISH} && isExecuteOnEntireRepo:
92      * repository will be scanned.
93      *
94      * @param task
95      * @throws TaskExecutionException
96      */
97     public void executeTask( Task task )
98         throws TaskExecutionException
99     {
100         ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
101
102         ManagedRepository repository = indexingTask.getRepository();
103         IndexingContext context = indexingTask.getContext();
104
105         if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
106             && indexingTask.isExecuteOnEntireRepo() )
107         {
108             try
109             {
110                 long start = System.currentTimeMillis();
111                 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
112                 long end = System.currentTimeMillis();
113                 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(),
114                           indexingTask.isOnlyUpdate(), ( end - start ) );
115             }
116             catch ( IOException e )
117             {
118                 throw new TaskExecutionException( "Error scan repository " + repository, e );
119             }
120             log.debug( "Finishing indexing task on repo: {}", repository.getId() );
121             finishIndexingTask( indexingTask, repository, context );
122         }
123         else
124         {
125             // create context if not a repo scan request
126             if ( !indexingTask.isExecuteOnEntireRepo() )
127             {
128                 try
129                 {
130                     log.debug( "Creating indexing context on resource: {}", ( indexingTask.getResourceFile() == null
131                         ? "none"
132                         : indexingTask.getResourceFile().getPath() ) );
133                     context = managedRepositoryAdmin.createIndexContext( repository );
134                 }
135                 catch ( RepositoryAdminException e )
136                 {
137                     log.error( "Error occurred while creating context: " + e.getMessage() );
138                     throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(), e );
139                 }
140             }
141
142             if ( context == null || context.getIndexDirectory() == null )
143             {
144                 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
145             }
146
147             try
148             {
149                 File artifactFile = indexingTask.getResourceFile();
150                 if ( artifactFile == null )
151                 {
152                     log.debug( "no artifact pass in indexing task so skip it" );
153                 }
154                 else
155                 {
156                     ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
157
158                     if ( ac != null )
159                     {
160                         // MRM-1779 pom must be indexed too
161                         // TODO make that configurable?
162                         if ( artifactFile.getPath().endsWith( ".pom" ) )
163                         {
164                             ac.getArtifactInfo().fextension = "pom";
165                             ac.getArtifactInfo().packaging = "pom";
166                             ac.getArtifactInfo().classifier = "pom";
167                         }
168                         if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
169                         {
170                             //IndexSearcher s = context.getIndexSearcher();
171                             //String uinfo = ac.getArtifactInfo().getUinfo();
172                             //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
173
174                             BooleanQuery q = new BooleanQuery();
175                             q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
176                                 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
177                             q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
178                                 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
179                             q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
180                                 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
181                             if ( ac.getArtifactInfo().classifier != null )
182                             {
183                                 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
184                                     ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
185                             }
186                             if ( ac.getArtifactInfo().packaging != null )
187                             {
188                                 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
189                                     ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
190                             }
191                             FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
192                             FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
193                             if ( flatSearchResponse.getResults().isEmpty() )
194                             {
195                                 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
196                                 nexusIndexer.addArtifactToIndex( ac, context );
197                             }
198                             else
199                             {
200                                 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
201                                 // TODO check if update exists !!
202                                 nexusIndexer.deleteArtifactFromIndex( ac, context );
203                                 nexusIndexer.addArtifactToIndex( ac, context );
204                             }
205
206                             context.updateTimestamp();
207                             context.commit();
208
209
210                         }
211                         else
212                         {
213                             log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
214                             nexusIndexer.deleteArtifactFromIndex( ac, context );
215                         }
216                     }
217                 }
218                 // close the context if not a repo scan request
219                 if ( !indexingTask.isExecuteOnEntireRepo() )
220                 {
221                     log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null
222                         ? indexingTask.getResourceFile().getPath()
223                         : " none " );
224                     finishIndexingTask( indexingTask, repository, context );
225                 }
226             }
227             catch ( IOException e )
228             {
229                 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
230                            e );
231                 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
232                                                   e );
233             }
234         }
235
236     }
237
238     private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
239                                      IndexingContext context )
240         throws TaskExecutionException
241     {
242         try
243         {
244
245             context.optimize();
246
247             if ( !repository.isSkipPackedIndexCreation() )
248             {
249
250                 IndexPackingRequest request = new IndexPackingRequest( context, context.getIndexDirectoryFile() );
251                 indexPacker.packIndex( request );
252                 context.updateTimestamp( true );
253
254                 log.debug( "Index file packaged at '{}'.", context.getIndexDirectoryFile() );
255             }
256             else
257             {
258                 log.debug( "skip packed index creation" );
259             }
260         }
261         catch ( IOException e )
262         {
263             log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
264             throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
265                                               e );
266         }
267     }
268
269     public void setIndexPacker( IndexPacker indexPacker )
270     {
271         this.indexPacker = indexPacker;
272     }
273
274     public PlexusSisuBridge getPlexusSisuBridge()
275     {
276         return plexusSisuBridge;
277     }
278
279     public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
280     {
281         this.plexusSisuBridge = plexusSisuBridge;
282     }
283 }