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