]> source.dussan.org Git - archiva.git/blob
0598987327b77ba0e139ecc008a15e2924db6e23
[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.MavenIndexerUtils;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.lucene.search.BooleanClause;
31 import org.apache.lucene.search.BooleanQuery;
32 import org.apache.maven.index.ArtifactContext;
33 import org.apache.maven.index.ArtifactContextProducer;
34 import org.apache.maven.index.FlatSearchRequest;
35 import org.apache.maven.index.FlatSearchResponse;
36 import org.apache.maven.index.MAVEN;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
39 import org.apache.maven.index.context.IndexCreator;
40 import org.apache.maven.index.context.IndexingContext;
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     @Inject
82     private ManagedRepositoryAdmin managedRepositoryAdmin;
83
84     private NexusIndexer nexusIndexer;
85
86     private List<? extends IndexCreator> allIndexCreators;
87
88     @PostConstruct
89     public void initialize()
90         throws PlexusSisuBridgeException
91     {
92         log.info( "Initialized {}", this.getClass().getName() );
93
94         artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
95
96         indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
97
98         nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
99
100         allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
101     }
102
103     public void executeTask( Task task )
104         throws TaskExecutionException
105     {
106         synchronized ( nexusIndexer )
107         {
108             ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
109
110             ManagedRepository repository = indexingTask.getRepository();
111             IndexingContext context = indexingTask.getContext();
112
113             if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
114                 && indexingTask.isExecuteOnEntireRepo() )
115             {
116                 try
117                 {
118                     nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
119                 }
120                 catch ( IOException e )
121                 {
122                     throw new TaskExecutionException( "Error scan repository " + repository, e );
123                 }
124                 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
125                 finishIndexingTask( indexingTask, repository, context );
126             }
127             else
128             {
129                 // create context if not a repo scan request
130                 if ( !indexingTask.isExecuteOnEntireRepo() )
131                 {
132                     try
133                     {
134                         log.debug( "Creating indexing context on resource: {}",
135                                    indexingTask.getResourceFile().getPath() );
136                         context = managedRepositoryAdmin.createIndexContext( repository );
137                     }
138                     catch ( RepositoryAdminException e )
139                     {
140                         log.error( "Error occurred while creating context: " + e.getMessage() );
141                         throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
142                                                           e );
143                     }
144                 }
145
146                 if ( context == null || context.getIndexDirectory() == null )
147                 {
148                     throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
149                 }
150
151                 try
152                 {
153                     File artifactFile = indexingTask.getResourceFile();
154                     ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
155
156                     if ( ac != null )
157                     {
158                         if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
159                         {
160                             //IndexSearcher s = context.getIndexSearcher();
161                             //String uinfo = ac.getArtifactInfo().getUinfo();
162                             //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
163
164                             BooleanQuery q = new BooleanQuery();
165                             q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
166                                 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
167                             q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
168                                 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
169                             q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
170                                 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
171                             if ( ac.getArtifactInfo().classifier != null )
172                             {
173                                 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
174                                     ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
175                             }
176                             if ( ac.getArtifactInfo().packaging != null )
177                             {
178                                 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
179                                     ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
180                             }
181                             FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
182                             FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
183                             if ( flatSearchResponse.getResults().isEmpty() )
184                             {
185                                 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
186                                 nexusIndexer.addArtifactToIndex( ac, context );
187                             }
188                             else
189                             {
190                                 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
191                                 // TODO check if update exists !!
192                                 nexusIndexer.deleteArtifactFromIndex( ac, context );
193                                 nexusIndexer.addArtifactToIndex( ac, context );
194                             }
195
196                             context.updateTimestamp();
197
198                             // close the context if not a repo scan request
199                             if ( !indexingTask.isExecuteOnEntireRepo() )
200                             {
201                                 log.debug( "Finishing indexing task on resource file : {}",
202                                            indexingTask.getResourceFile().getPath() );
203                                 finishIndexingTask( indexingTask, repository, context );
204                             }
205                         }
206                         else
207                         {
208                             log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
209                             nexusIndexer.deleteArtifactFromIndex( ac, context );
210                         }
211                     }
212                 }
213                 catch ( IOException e )
214                 {
215                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
216                                e );
217                     throw new TaskExecutionException(
218                         "Error occurred while executing indexing task '" + indexingTask + "'", e );
219                 }
220                 catch ( IllegalArtifactCoordinateException e )
221                 {
222                     log.error( "Error occurred while getting artifact context: " + e.getMessage() );
223                     throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
224                 }
225             }
226         }
227     }
228
229     private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
230                                      IndexingContext context )
231         throws TaskExecutionException
232     {
233         try
234         {
235
236             context.optimize();
237
238             File managedRepository = new File( repository.getLocation() );
239             String indexDirectory = repository.getIndexDirectory();
240             final File indexLocation = StringUtils.isBlank( indexDirectory )
241                 ? new File( managedRepository, ".indexer" )
242                 : new File( indexDirectory );
243             IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
244             indexPacker.packIndex( request );
245             context.updateTimestamp( true );
246
247             log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
248
249         }
250         catch ( IOException e )
251         {
252             log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
253             throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
254                                               e );
255         }
256     }
257
258     public void setIndexPacker( IndexPacker indexPacker )
259     {
260         this.indexPacker = indexPacker;
261     }
262
263     public PlexusSisuBridge getPlexusSisuBridge()
264     {
265         return plexusSisuBridge;
266     }
267
268     public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
269     {
270         this.plexusSisuBridge = plexusSisuBridge;
271     }
272 }