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