]> source.dussan.org Git - archiva.git/blob
61a3f34603ff714a511b7c2fad09488944baf690
[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.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
31 import org.codehaus.plexus.taskqueue.Task;
32 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
33 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.sonatype.nexus.index.ArtifactContext;
37 import org.sonatype.nexus.index.ArtifactContextProducer;
38 import org.sonatype.nexus.index.ArtifactInfo;
39 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
40 import org.sonatype.nexus.index.IndexerEngine;
41 import org.sonatype.nexus.index.context.IndexingContext;
42 import org.sonatype.nexus.index.packer.IndexPacker;
43 import org.sonatype.nexus.index.packer.IndexPackingRequest;
44
45 /**
46  * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
47  * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
48  * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
49  * 
50  * @todo Nexus specifics shouldn't be in the archiva-scheduled module
51  * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
52  *                   instantiation-strategy="singleton"
53  */
54 public class ArchivaIndexingTaskExecutor
55     implements TaskExecutor, Initializable
56 {
57     private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
58
59     /**
60      * @plexus.requirement
61      */
62     private IndexerEngine indexerEngine;
63
64     /**
65      * @plexus.requirement
66      */
67     private IndexPacker indexPacker;
68
69     private ArtifactContextProducer artifactContextProducer;
70
71     public void executeTask( Task task )
72         throws TaskExecutionException
73     {
74         synchronized ( indexerEngine )
75         {
76             ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
77
78             ManagedRepositoryConfiguration repository = indexingTask.getRepository();
79             IndexingContext context = indexingTask.getContext();
80
81             if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() ) )
82             {
83                 try
84                 {
85                     context.optimize();
86
87                     File managedRepository = new File( repository.getLocation() );
88                     final File indexLocation = new File( managedRepository, ".index" );
89                     IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
90                     indexPacker.packIndex( request );
91
92                     log.debug( "Index file packaged at '" + indexLocation.getPath() + "'." );
93                 }
94                 catch ( IOException e )
95                 {
96                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
97                     throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
98                         + "'", e );
99                 }
100                 finally
101                 {
102                     if ( context != null )
103                     {
104                         try
105                         {
106                             context.close( false );
107                         }
108                         catch ( IOException e )
109                         {
110                             log.error( "Error occurred while closing context: " + e.getMessage() );
111                             throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
112                         }
113                     }
114                 }
115             }
116             else
117             {
118                 if ( context.getIndexDirectory() == null )
119                 {
120                     throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
121                 }
122                 
123                 try
124                 {
125                     File artifactFile = indexingTask.getResourceFile();
126                     ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
127
128                     if ( ac != null )
129                     {
130                         if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
131                         {
132                             boolean add = true;
133                             IndexReader r = context.getIndexReader();
134                             for ( int i = 0; i < r.numDocs(); i++ )
135                             {
136                                 if ( !r.isDeleted( i ) )
137                                 {
138                                     Document d = r.document( i );
139                                     String uinfo = d.get( ArtifactInfo.UINFO );
140                                     if ( ac.getArtifactInfo().getUinfo().equals( uinfo ) )
141                                     {
142                                         add = false;
143                                         break;
144                                     }
145                                 }
146                             }
147
148                             if ( add )
149                             {
150                                 log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
151                                 indexerEngine.index( context, ac );
152                                 context.getIndexWriter().commit();
153                             }
154                             else
155                             {
156                                 log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
157                                 indexerEngine.update( context, ac );
158                                 context.getIndexWriter().commit();
159                             }
160                         }
161                         else
162                         {
163                             log.debug( "Removing artifact '" + ac.getArtifactInfo() + "' from index.." );
164                             indexerEngine.remove( context, ac );
165                             context.getIndexWriter().commit();
166                         }
167                     }
168                 }
169                 catch ( IOException e )
170                 {
171                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
172                     throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
173                         + "'", e );
174                 }
175             }
176         }
177     }
178
179     public void initialize()
180         throws InitializationException
181     {
182         log.info( "Initialized " + this.getClass().getName() );
183
184         artifactContextProducer = new DefaultArtifactContextProducer();
185     }
186
187     public void setIndexerEngine( IndexerEngine indexerEngine )
188     {
189         this.indexerEngine = indexerEngine;
190     }
191
192     public void setIndexPacker( IndexPacker indexPacker )
193     {
194         this.indexPacker = indexPacker;
195     }
196 }