]> source.dussan.org Git - archiva.git/blob
31d0a9f96cc48db8c8313473574466c3bc9ca31e
[archiva.git] /
1 package org.apache.maven.archiva.scheduled.executors;
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 License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
32 import org.codehaus.plexus.taskqueue.Task;
33 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
34 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.sonatype.nexus.index.ArtifactContext;
38 import org.sonatype.nexus.index.ArtifactContextProducer;
39 import org.sonatype.nexus.index.ArtifactInfo;
40 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
41 import org.sonatype.nexus.index.IndexerEngine;
42 import org.sonatype.nexus.index.NexusIndexer;
43 import org.sonatype.nexus.index.context.DefaultIndexingContext;
44 import org.sonatype.nexus.index.context.IndexingContext;
45 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
46 import org.sonatype.nexus.index.packer.IndexPacker;
47 import org.sonatype.nexus.index.packer.IndexPackingRequest;
48
49 /**
50  * ArchivaIndexingTaskExecutor
51  * 
52  * Executes all indexing tasks. Adding, updating and removing artifacts from the index are all performed by
53  * this executor. Add and update artifact in index tasks are added in the indexing task queue by the NexusIndexerConsumer while
54  * remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
55  * 
56  * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
57  */
58 public class ArchivaIndexingTaskExecutor
59     implements TaskExecutor, Initializable
60 {
61     private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
62
63     /**
64      * @plexus.requirement
65      */
66     private IndexerEngine indexerEngine;
67     
68     /**
69      * @plexus.requirement
70      */
71     private ArchivaConfiguration archivaConfiguration;
72     
73     /**
74      * @plexus.requirement
75      */
76     private IndexPacker indexPacker;
77     
78     private ArtifactContextProducer artifactContextProducer;
79         
80     public void executeTask( Task task )
81         throws TaskExecutionException
82     {
83         synchronized( indexerEngine )
84         {
85             ArtifactIndexingTask indexingTask = ( ArtifactIndexingTask ) task;
86             
87             ManagedRepositoryConfiguration repository =
88                 archivaConfiguration.getConfiguration().findManagedRepositoryById( indexingTask.getRepositoryId() );
89     
90             String indexDir = repository.getIndexDir();
91             File managedRepository = new File( repository.getLocation() );
92             
93             File indexDirectory = null;
94             if( indexDir != null && !"".equals( indexDir ) )
95             {
96                 indexDirectory = new File( repository.getIndexDir() );
97             }
98             else
99             {
100                 indexDirectory = new File( managedRepository, ".indexer" );
101             }
102             
103             IndexingContext context = null;
104             try
105             {
106                 context =
107                     new DefaultIndexingContext( repository.getId(), repository.getId(), managedRepository,
108                                             indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
109                 context.setSearchable( repository.isScanned() );
110                 
111                 File artifactFile = indexingTask.getResourceFile();                
112                 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
113                 
114                 if( ac != null )
115                 {   
116                     if( indexingTask.getAction().equals( ArtifactIndexingTask.ADD ) )
117                     {
118                         boolean add = true;
119                         IndexReader r = context.getIndexReader();      
120                         for ( int i = 0; i < r.numDocs(); i++ )
121                         {
122                             if ( !r.isDeleted( i ) )
123                             {
124                                 Document d = r.document( i );          
125                                 String uinfo = d.get( ArtifactInfo.UINFO );                                
126                                 if( ac.getArtifactInfo().getUinfo().equals( uinfo ) )
127                                 {
128                                     add = false;
129                                     break;
130                                 }
131                             }
132                         }
133                         
134                         if( add )
135                         {   
136                             log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
137                             indexerEngine.index( context, ac );
138                         }
139                         else
140                         {
141                             log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
142                             indexerEngine.update( context, ac );
143                         }
144                     }
145                     else
146                     {                           
147                         log.debug( "removing artifact '" + ac.getArtifactInfo() + "' from index.." );
148                         indexerEngine.remove( context, ac );
149                     }
150                     
151                     final File indexLocation = new File( managedRepository, ".index" );
152                     IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
153                     indexPacker.packIndex( request );
154                 }                
155             }
156             catch ( IOException e )
157             {
158                 throw new TaskExecutionException( "Error occurred while executing indexing task '" +
159                     indexingTask.getName() + "'" );
160             }
161             catch ( UnsupportedExistingLuceneIndexException e )
162             {
163                 throw new TaskExecutionException( "Unsupported Lucene index format: " + e.getMessage() );
164             }
165             finally
166             {
167                 if( context != null )
168                 {
169                     try
170                     {
171                         context.close( false );
172                     }
173                     catch ( IOException e )
174                     {
175                         throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
176                     }
177                 }
178             }
179         }
180     }
181
182     public void initialize()
183         throws InitializationException
184     {
185         log.info( "Initialized " + this.getClass().getName() );
186         
187         artifactContextProducer = new DefaultArtifactContextProducer();
188     }
189
190     public void setIndexerEngine( IndexerEngine indexerEngine )
191     {
192         this.indexerEngine = indexerEngine;
193     }
194
195     public void setIndexPacker( IndexPacker indexPacker )
196     {
197         this.indexPacker = indexPacker;
198     }
199
200     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
201     {
202         this.archivaConfiguration = archivaConfiguration;
203     }
204
205 }