]> source.dussan.org Git - archiva.git/blob
ad047ff24be4e625fba6485b44952ebbc235448e
[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.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.lucene.search.BooleanClause;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.index.ArtifactContext;
30 import org.apache.maven.index.ArtifactContextProducer;
31 import org.apache.maven.index.DefaultArtifactContextProducer;
32 import org.apache.maven.index.FlatSearchRequest;
33 import org.apache.maven.index.FlatSearchResponse;
34 import org.apache.maven.index.MAVEN;
35 import org.apache.maven.index.NexusIndexer;
36 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
37 import org.apache.maven.index.context.IndexCreator;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
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.codehaus.plexus.taskqueue.Task;
44 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
45 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Service;
49
50 import javax.annotation.PostConstruct;
51 import javax.inject.Inject;
52 import java.io.File;
53 import java.io.IOException;
54 import java.util.List;
55
56 /**
57  * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
58  * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
59  * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
60  * <p/>
61  * plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
62  * instantiation-strategy="singleton"
63  */
64 @Service( "taskExecutor#indexing" )
65 public class ArchivaIndexingTaskExecutor
66     implements TaskExecutor
67 {
68     private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
69
70     /**
71      * plexus.requirement
72      */
73     private IndexPacker indexPacker;
74
75     private ArtifactContextProducer artifactContextProducer;
76
77     @Inject
78     private PlexusSisuBridge plexusSisuBridge;
79
80     @Inject
81     private MavenIndexerUtils mavenIndexerUtils;
82
83     private NexusIndexer nexusIndexer;
84
85     private List<? extends IndexCreator> allIndexCreators;
86
87     @PostConstruct
88     public void initialize()
89         throws PlexusSisuBridgeException
90     {
91         log.info( "Initialized {}", this.getClass().getName() );
92
93         artifactContextProducer = new DefaultArtifactContextProducer();
94
95         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
96
97         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
98
99         allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
100     }
101
102     public void executeTask( Task task )
103         throws TaskExecutionException
104     {
105         synchronized ( nexusIndexer )
106         {
107             ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
108
109             ManagedRepositoryConfiguration repository = indexingTask.getRepository();
110             IndexingContext context = indexingTask.getContext();
111
112             if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
113                 && indexingTask.isExecuteOnEntireRepo() )
114             {
115                 try
116                 {
117                     nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
118                 }
119                 catch ( IOException e )
120                 {
121                     throw new TaskExecutionException( "Error scan repository " + repository, e );
122                 }
123                 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
124                 finishIndexingTask( indexingTask, repository, context );
125             }
126             else
127             {
128                 // create context if not a repo scan request
129                 if ( !indexingTask.isExecuteOnEntireRepo() )
130                 {
131                     try
132                     {
133                         log.debug( "Creating indexing context on resource: {}",
134                                    indexingTask.getResourceFile().getPath() );
135                         context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
136                     }
137                     catch ( IOException e )
138                     {
139                         log.error( "Error occurred while creating context: " + e.getMessage() );
140                         throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
141                                                           e );
142                     }
143                     catch ( UnsupportedExistingLuceneIndexException e )
144                     {
145                         log.error( "Error occurred while creating context: " + e.getMessage() );
146                         throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
147                                                           e );
148                     }
149                 }
150
151                 if ( context == null || context.getIndexDirectory() == null )
152                 {
153                     throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
154                 }
155
156                 try
157                 {
158                     File artifactFile = indexingTask.getResourceFile();
159                     ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
160
161                     if ( ac != null )
162                     {
163                         if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
164                         {
165                             //IndexSearcher s = context.getIndexSearcher();
166                             //String uinfo = ac.getArtifactInfo().getUinfo();
167                             //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
168
169                             BooleanQuery q = new BooleanQuery();
170                             q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
171                                 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
172                             q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
173                                 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
174                             q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
175                                 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
176                             if ( ac.getArtifactInfo().classifier != null )
177                             {
178                                 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
179                                     ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
180                             }
181                             if ( ac.getArtifactInfo().packaging != null )
182                             {
183                                 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
184                                     ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
185                             }
186                             FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
187                             FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
188                             if ( flatSearchResponse.getResults().isEmpty() )
189                             {
190                                 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
191                                 nexusIndexer.addArtifactToIndex( ac, context );
192                             }
193                             else
194                             {
195                                 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
196                                 // TODO check if update exists !!
197                                 nexusIndexer.deleteArtifactFromIndex( ac, context );
198                                 nexusIndexer.addArtifactToIndex( ac, context );
199                             }
200
201                             //nexusIndexer.scan( context, true );
202
203                             context.updateTimestamp();
204
205                             // close the context if not a repo scan request
206                             if ( !indexingTask.isExecuteOnEntireRepo() )
207                             {
208                                 log.debug( "Finishing indexing task on resource file : {}",
209                                            indexingTask.getResourceFile().getPath() );
210                                 finishIndexingTask( indexingTask, repository, context );
211                             }
212                         }
213                         else
214                         {
215                             log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
216                             nexusIndexer.deleteArtifactFromIndex( ac, context );
217                         }
218                     }
219                 }
220                 catch ( IOException e )
221                 {
222                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
223                                e );
224                     throw new TaskExecutionException(
225                         "Error occurred while executing indexing task '" + indexingTask + "'", e );
226                 }
227                 catch ( IllegalArtifactCoordinateException e )
228                 {
229                     log.error( "Error occurred while getting artifact context: " + e.getMessage() );
230                     throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
231                 }
232             }
233         }
234     }
235
236     private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepositoryConfiguration repository,
237                                      IndexingContext context )
238         throws TaskExecutionException
239     {
240         try
241         {
242
243             context.optimize();
244
245
246             File managedRepository = new File( repository.getLocation() );
247             final File indexLocation = new File( managedRepository, ".index" );
248             IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
249             indexPacker.packIndex( request );
250
251             log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
252
253         }
254         catch ( IOException e )
255         {
256             log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
257             throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
258                                               e );
259         }
260         finally
261         {
262             /*
263             olamy don't close it anymore as it nullify IndexSearcher
264             if ( context != null )
265             {
266                 try
267                 {
268                     context.close( false );
269                 }
270                 catch ( IOException e )
271                 {
272                     log.error( "Error occurred while closing context: " + e.getMessage() );
273                     throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
274                 }
275             }
276             */
277         }
278     }
279
280     public void setIndexPacker( IndexPacker indexPacker )
281     {
282         this.indexPacker = indexPacker;
283     }
284
285     public PlexusSisuBridge getPlexusSisuBridge()
286     {
287         return plexusSisuBridge;
288     }
289
290     public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
291     {
292         this.plexusSisuBridge = plexusSisuBridge;
293     }
294 }