]> source.dussan.org Git - archiva.git/blob
3d9a0b199552e25dcf104bcd860d311f59331180
[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.beans.ManagedRepository;
24 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.lucene.search.BooleanClause;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.maven.index.ArtifactContext;
31 import org.apache.maven.index.ArtifactContextProducer;
32 import org.apache.maven.index.DefaultArtifactContextProducer;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
41 import org.apache.maven.index.expr.SourcedSearchExpression;
42 import org.apache.maven.index.packer.IndexPacker;
43 import org.apache.maven.index.packer.IndexPackingRequest;
44 import org.codehaus.plexus.taskqueue.Task;
45 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
46 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Service;
50
51 import javax.annotation.PostConstruct;
52 import javax.inject.Inject;
53 import java.io.File;
54 import java.io.IOException;
55 import java.util.List;
56
57 /**
58  * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
59  * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
60  * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
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 = plexusSisuBridge.lookup( ArtifactContextProducer.class );
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             ManagedRepository 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                             context.updateTimestamp();
200
201                             // close the context if not a repo scan request
202                             if ( !indexingTask.isExecuteOnEntireRepo() )
203                             {
204                                 log.debug( "Finishing indexing task on resource file : {}",
205                                            indexingTask.getResourceFile().getPath() );
206                                 finishIndexingTask( indexingTask, repository, context );
207                             }
208                         }
209                         else
210                         {
211                             log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
212                             nexusIndexer.deleteArtifactFromIndex( ac, context );
213                         }
214                     }
215                 }
216                 catch ( IOException e )
217                 {
218                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
219                                e );
220                     throw new TaskExecutionException(
221                         "Error occurred while executing indexing task '" + indexingTask + "'", e );
222                 }
223                 catch ( IllegalArtifactCoordinateException e )
224                 {
225                     log.error( "Error occurred while getting artifact context: " + e.getMessage() );
226                     throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
227                 }
228             }
229         }
230     }
231
232     private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
233                                      IndexingContext context )
234         throws TaskExecutionException
235     {
236         try
237         {
238
239             context.optimize();
240
241             File managedRepository = new File( repository.getLocation() );
242             String indexDirectory = repository.getIndexDirectory();
243             final File indexLocation = StringUtils.isBlank( indexDirectory )
244                 ? new File( managedRepository, ".indexer" )
245                 : new File( indexDirectory );
246             IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
247             indexPacker.packIndex( request );
248             context.updateTimestamp( true );
249
250             log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
251
252         }
253         catch ( IOException e )
254         {
255             log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
256             throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
257                                               e );
258         }
259     }
260
261     public void setIndexPacker( IndexPacker indexPacker )
262     {
263         this.indexPacker = indexPacker;
264     }
265
266     public PlexusSisuBridge getPlexusSisuBridge()
267     {
268         return plexusSisuBridge;
269     }
270
271     public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
272     {
273         this.plexusSisuBridge = plexusSisuBridge;
274     }
275 }