]> source.dussan.org Git - archiva.git/blob
56fdf79944fbd410c8e5519952b7dbf859043d24
[archiva.git] /
1 package org.apache.archiva.consumers.lucene;
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 org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ConfigurationNames;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.archiva.redback.components.registry.Registry;
32 import org.apache.archiva.redback.components.registry.RegistryListener;
33 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
34 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
35 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexCreator;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
43
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Date;
52 import java.util.List;
53
54 /**
55  * Consumer for indexing the repository to provide search and IDE integration features.
56  */
57 @Service( "knownRepositoryContentConsumer#index-content" )
58 @Scope( "prototype" )
59 public class NexusIndexerConsumer
60     extends AbstractMonitoredConsumer
61     implements KnownRepositoryContentConsumer, RegistryListener
62 {
63     private Logger log = LoggerFactory.getLogger( getClass() );
64
65     private ArchivaConfiguration configuration;
66
67     private FileTypes filetypes;
68
69     private Path managedRepository;
70
71     private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
72
73     private IndexingContext indexingContext;
74
75     private NexusIndexer nexusIndexer;
76
77     private List<String> includes = new ArrayList<>( 0 );
78
79     private ManagedRepository repository;
80
81     private List<? extends IndexCreator> allIndexCreators;
82
83     private ManagedRepositoryAdmin managedRepositoryAdmin;
84
85     @Inject
86     public NexusIndexerConsumer(
87         @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
88         @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes,
89         List<IndexCreator> indexCreators, ManagedRepositoryAdmin managedRepositoryAdmin, NexusIndexer nexusIndexer )
90     {
91         this.configuration = configuration;
92         this.filetypes = filetypes;
93         this.scheduler = scheduler;
94         this.nexusIndexer = nexusIndexer;
95         this.allIndexCreators = indexCreators;
96         this.managedRepositoryAdmin = managedRepositoryAdmin;
97     }
98
99     @Override
100     public String getDescription()
101     {
102         return "Indexes the repository to provide search and IDE integration features";
103     }
104
105     @Override
106     public String getId()
107     {
108         return "index-content";
109     }
110
111     @Override
112     public void beginScan( ManagedRepository repository, Date whenGathered )
113         throws ConsumerException
114     {
115         this.repository = repository;
116         managedRepository = Paths.get( repository.getLocation() );
117
118         try
119         {
120             log.info( "Creating indexing context for repo : {}", repository.getId() );
121             indexingContext = managedRepositoryAdmin.createIndexContext( repository );
122         }
123         catch ( RepositoryAdminException e )
124         {
125             throw new ConsumerException( e.getMessage(), e );
126         }
127     }
128
129     @Override
130     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
131         throws ConsumerException
132     {
133         if ( executeOnEntireRepo )
134         {
135             beginScan( repository, whenGathered );
136         }
137         else
138         {
139             this.repository = repository;
140             managedRepository = Paths.get( repository.getLocation() );
141         }
142     }
143
144     @Override
145     public void processFile( String path )
146         throws ConsumerException
147     {
148         Path artifactFile = managedRepository.resolve(path);
149
150         ArtifactIndexingTask task =
151             new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD, getIndexingContext() );
152         try
153         {
154             log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
155             scheduler.queueTask( task );
156         }
157         catch ( TaskQueueException e )
158         {
159             throw new ConsumerException( e.getMessage(), e );
160         }
161     }
162
163     @Override
164     public void processFile( String path, boolean executeOnEntireRepo )
165         throws Exception
166     {
167         if ( executeOnEntireRepo )
168         {
169             processFile( path );
170         }
171         else
172         {
173             Path artifactFile = managedRepository.resolve(path);
174
175             // specify in indexing task that this is not a repo scan request!
176             ArtifactIndexingTask task =
177                 new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD,
178                                           getIndexingContext(), false );
179             // only update index we don't need to scan the full repo here
180             task.setOnlyUpdate( true );
181             try
182             {
183                 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
184                 scheduler.queueTask( task );
185             }
186             catch ( TaskQueueException e )
187             {
188                 throw new ConsumerException( e.getMessage(), e );
189             }
190         }
191     }
192
193     @Override
194     public void completeScan()
195     {
196         IndexingContext context = this.indexingContext;
197         if ( context == null )
198         {
199             try
200             {
201                 context = getIndexingContext();
202             }
203             catch ( ConsumerException e )
204             {
205                 log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
206                 return;
207             }
208         }
209         ArtifactIndexingTask task =
210             new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
211         try
212         {
213             log.debug( "Queueing indexing task '{}' to finish indexing.", task );
214             scheduler.queueTask( task );
215         }
216         catch ( TaskQueueException e )
217         {
218             log.error( "Error queueing task: {}: {}", task, e.getMessage(), e );
219         }
220     }
221
222     @Override
223     public void completeScan( boolean executeOnEntireRepo )
224     {
225         if ( executeOnEntireRepo )
226         {
227             completeScan();
228         }
229
230         // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
231     }
232
233     @Override
234     public List<String> getExcludes()
235     {
236         return Collections.emptyList();
237     }
238
239     @Override
240     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
241     {
242         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
243         {
244             initIncludes();
245         }
246     }
247
248     @Override
249     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
250     {
251         /* do nothing */
252     }
253
254     private void initIncludes()
255     {
256         List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
257         List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
258
259         includes = new ArrayList<>( indexable.size() + artifacts.size() );
260
261         includes.addAll( indexable );
262
263         includes.addAll( artifacts );
264     }
265
266     @PostConstruct
267     public void initialize()
268     {
269         configuration.addChangeListener( this );
270
271         initIncludes();
272     }
273
274     @Override
275     public List<String> getIncludes()
276     {
277         return includes;
278     }
279
280
281     private IndexingContext getIndexingContext()
282         throws ConsumerException
283     {
284
285         if ( this.indexingContext == null )
286         {
287             try
288             {
289                 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
290             }
291             catch ( RepositoryAdminException e )
292             {
293                 throw new ConsumerException( e.getMessage(), e );
294             }
295         }
296         return indexingContext;
297     }
298 }