This project has retired. For details please refer to its
Attic page.
ArchivaIndexingTaskExecutor xref
1 package org.apache.archiva.scheduler.indexing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.PlexusSisuBridge;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
28 import org.apache.archiva.redback.components.taskqueue.Task;
29 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
30 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.maven.index.ArtifactContext;
34 import org.apache.maven.index.ArtifactContextProducer;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.NexusIndexer;
39 import org.apache.maven.index.context.IndexingContext;
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.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
46
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
49 import java.io.File;
50 import java.io.IOException;
51
52
53
54
55
56
57 @Service ( "taskExecutor#indexing" )
58 public class ArchivaIndexingTaskExecutor
59 implements TaskExecutor
60 {
61 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
62
63 private IndexPacker indexPacker;
64
65 private ArtifactContextProducer artifactContextProducer;
66
67 @Inject
68 private PlexusSisuBridge plexusSisuBridge;
69
70 @Inject
71 private ManagedRepositoryAdmin managedRepositoryAdmin;
72
73 private NexusIndexer nexusIndexer;
74
75 @PostConstruct
76 public void initialize()
77 throws PlexusSisuBridgeException
78 {
79 log.info( "Initialized {}", this.getClass().getName() );
80
81 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
82
83 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
84
85 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
86
87 }
88
89
90
91
92
93
94
95
96
97 @Override
98 public void executeTask( Task task )
99 throws TaskExecutionException
100 {
101 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
102
103 ManagedRepository repository = indexingTask.getRepository();
104 IndexingContext context = indexingTask.getContext();
105
106 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
107 && indexingTask.isExecuteOnEntireRepo() )
108 {
109 try
110 {
111 long start = System.currentTimeMillis();
112 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
113 long end = System.currentTimeMillis();
114 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(),
115 indexingTask.isOnlyUpdate(), ( end - start ) );
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
127 if ( !indexingTask.isExecuteOnEntireRepo() )
128 {
129 try
130 {
131 log.debug( "Creating indexing context on resource: {}", ( indexingTask.getResourceFile() == null
132 ? "none"
133 : indexingTask.getResourceFile().getPath() ) );
134 context = managedRepositoryAdmin.createIndexContext( repository );
135 }
136 catch ( RepositoryAdminException e )
137 {
138 log.error( "Error occurred while creating context: " + e.getMessage() );
139 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(), e );
140 }
141 }
142
143 if ( context == null || context.getIndexDirectory() == null )
144 {
145 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
146 }
147
148 try
149 {
150 File artifactFile = indexingTask.getResourceFile();
151 if ( artifactFile == null )
152 {
153 log.debug( "no artifact pass in indexing task so skip it" );
154 }
155 else
156 {
157 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
158
159 if ( ac != null )
160 {
161
162
163 if ( artifactFile.getPath().endsWith( ".pom" ) )
164 {
165 ac.getArtifactInfo().fextension = "pom";
166 ac.getArtifactInfo().packaging = "pom";
167 ac.getArtifactInfo().classifier = "pom";
168 }
169 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
170 {
171
172
173
174
175 BooleanQuery q = new BooleanQuery();
176 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
177 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
178 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
179 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
180 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
181 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
182 if ( ac.getArtifactInfo().classifier != null )
183 {
184 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
185 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
186 }
187 if ( ac.getArtifactInfo().packaging != null )
188 {
189 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
190 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
191 }
192 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
193 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
194 if ( flatSearchResponse.getResults().isEmpty() )
195 {
196 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
197 nexusIndexer.addArtifactToIndex( ac, context );
198 }
199 else
200 {
201 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
202
203 nexusIndexer.deleteArtifactFromIndex( ac, context );
204 nexusIndexer.addArtifactToIndex( ac, context );
205 }
206
207 context.updateTimestamp();
208 context.commit();
209
210
211 }
212 else
213 {
214 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
215 nexusIndexer.deleteArtifactFromIndex( ac, context );
216 }
217 }
218 }
219
220 if ( !indexingTask.isExecuteOnEntireRepo() )
221 {
222 log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null
223 ? indexingTask.getResourceFile().getPath()
224 : " none " );
225 finishIndexingTask( indexingTask, repository, context );
226 }
227 }
228 catch ( IOException e )
229 {
230 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
231 e );
232 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
233 e );
234 }
235 }
236
237 }
238
239 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
240 IndexingContext context )
241 throws TaskExecutionException
242 {
243 try
244 {
245
246 context.optimize();
247
248 if ( !repository.isSkipPackedIndexCreation() )
249 {
250
251 IndexPackingRequest request = new IndexPackingRequest( context, context.getIndexDirectoryFile() );
252 indexPacker.packIndex( request );
253 context.updateTimestamp( true );
254
255 log.debug( "Index file packaged at '{}'.", context.getIndexDirectoryFile() );
256 }
257 else
258 {
259 log.debug( "skip packed index creation" );
260 }
261 }
262 catch ( IOException e )
263 {
264 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
265 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
266 e );
267 }
268 }
269
270 public void setIndexPacker( IndexPacker indexPacker )
271 {
272 this.indexPacker = indexPacker;
273 }
274
275 public PlexusSisuBridge getPlexusSisuBridge()
276 {
277 return plexusSisuBridge;
278 }
279
280 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
281 {
282 this.plexusSisuBridge = plexusSisuBridge;
283 }
284 }