1 | |
package org.apache.archiva.consumers.lucene; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.Date; |
27 | |
import java.util.List; |
28 | |
|
29 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
30 | |
import org.apache.maven.archiva.configuration.ConfigurationNames; |
31 | |
import org.apache.maven.archiva.configuration.FileTypes; |
32 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
33 | |
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; |
34 | |
import org.apache.maven.archiva.consumers.ConsumerException; |
35 | |
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; |
36 | |
import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; |
37 | |
import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; |
38 | |
import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; |
39 | |
import org.apache.maven.archiva.scheduled.tasks.TaskCreator; |
40 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
41 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
42 | |
import org.codehaus.plexus.registry.Registry; |
43 | |
import org.codehaus.plexus.registry.RegistryListener; |
44 | |
import org.codehaus.plexus.taskqueue.TaskQueueException; |
45 | |
import org.slf4j.Logger; |
46 | |
import org.slf4j.LoggerFactory; |
47 | |
import org.sonatype.nexus.index.context.IndexingContext; |
48 | |
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class NexusIndexerConsumer |
54 | |
extends AbstractMonitoredConsumer |
55 | |
implements KnownRepositoryContentConsumer, RegistryListener, Initializable |
56 | |
{ |
57 | 0 | private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class ); |
58 | |
|
59 | |
private ArchivaConfiguration configuration; |
60 | |
|
61 | |
private FileTypes filetypes; |
62 | |
|
63 | |
private ManagedDefaultRepositoryContent repositoryContent; |
64 | |
|
65 | |
private File managedRepository; |
66 | |
|
67 | |
private ArchivaTaskScheduler scheduler; |
68 | |
|
69 | |
private IndexingContext context; |
70 | |
|
71 | 0 | private List<String> includes = new ArrayList<String>(); |
72 | |
|
73 | |
private ManagedRepositoryConfiguration repository; |
74 | |
|
75 | |
public NexusIndexerConsumer( ArchivaTaskScheduler scheduler, ArchivaConfiguration configuration, FileTypes filetypes ) |
76 | 0 | { |
77 | 0 | this.configuration = configuration; |
78 | 0 | this.filetypes = filetypes; |
79 | 0 | this.scheduler = scheduler; |
80 | 0 | } |
81 | |
|
82 | |
public String getDescription() |
83 | |
{ |
84 | 0 | return "Indexes the repository to provide search and IDE integration features"; |
85 | |
} |
86 | |
|
87 | |
public String getId() |
88 | |
{ |
89 | 0 | return "index-content"; |
90 | |
} |
91 | |
|
92 | |
public boolean isPermanent() |
93 | |
{ |
94 | 0 | return false; |
95 | |
} |
96 | |
|
97 | |
public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered ) |
98 | |
throws ConsumerException |
99 | |
{ |
100 | 0 | managedRepository = new File( repository.getLocation() ); |
101 | |
|
102 | 0 | repositoryContent = new ManagedDefaultRepositoryContent(); |
103 | 0 | repositoryContent.setRepository( repository ); |
104 | |
|
105 | |
try |
106 | |
{ |
107 | 0 | log.info( "Creating indexing context for repo : " + repository.getId() ); |
108 | 0 | context = TaskCreator.createContext( repository ); |
109 | |
} |
110 | 0 | catch ( IOException e ) |
111 | |
{ |
112 | 0 | throw new ConsumerException( e.getMessage(), e ); |
113 | |
} |
114 | 0 | catch ( UnsupportedExistingLuceneIndexException e ) |
115 | |
{ |
116 | 0 | throw new ConsumerException( e.getMessage(), e ); |
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo ) |
121 | |
throws ConsumerException |
122 | |
{ |
123 | 0 | if( executeOnEntireRepo ) |
124 | |
{ |
125 | 0 | beginScan( repository, whenGathered ); |
126 | |
} |
127 | |
else |
128 | |
{ |
129 | 0 | this.repository = repository; |
130 | 0 | managedRepository = new File( repository.getLocation() ); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
public void processFile( String path ) |
135 | |
throws ConsumerException |
136 | |
{ |
137 | 0 | File artifactFile = new File( managedRepository, path ); |
138 | |
|
139 | 0 | ArtifactIndexingTask task = |
140 | |
TaskCreator.createIndexingTask( repositoryContent.getRepository(), artifactFile, |
141 | |
ArtifactIndexingTask.Action.ADD, context ); |
142 | |
try |
143 | |
{ |
144 | 0 | log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." ); |
145 | 0 | scheduler.queueIndexingTask( task ); |
146 | |
} |
147 | 0 | catch ( TaskQueueException e ) |
148 | |
{ |
149 | 0 | throw new ConsumerException( e.getMessage(), e ); |
150 | 0 | } |
151 | 0 | } |
152 | |
|
153 | |
public void processFile( String path, boolean executeOnEntireRepo ) |
154 | |
throws Exception |
155 | |
{ |
156 | 0 | if( executeOnEntireRepo ) |
157 | |
{ |
158 | 0 | processFile( path ); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 0 | File artifactFile = new File( managedRepository, path ); |
163 | |
|
164 | |
|
165 | 0 | ArtifactIndexingTask task = |
166 | |
new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context, false ); |
167 | |
try |
168 | |
{ |
169 | 0 | log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." ); |
170 | 0 | scheduler.queueIndexingTask( task ); |
171 | |
} |
172 | 0 | catch ( TaskQueueException e ) |
173 | |
{ |
174 | 0 | throw new ConsumerException( e.getMessage(), e ); |
175 | 0 | } |
176 | |
} |
177 | 0 | } |
178 | |
|
179 | |
public void completeScan() |
180 | |
{ |
181 | 0 | ArtifactIndexingTask task = |
182 | |
TaskCreator.createIndexingTask( repositoryContent.getRepository(), null, |
183 | |
ArtifactIndexingTask.Action.FINISH, context ); |
184 | |
try |
185 | |
{ |
186 | 0 | log.debug( "Queueing indexing task + '" + task + "' to finish indexing." ); |
187 | 0 | scheduler.queueIndexingTask( task ); |
188 | |
} |
189 | 0 | catch ( TaskQueueException e ) |
190 | |
{ |
191 | 0 | log.error( "Error queueing task: " + task + ": " + e.getMessage(), e ); |
192 | 0 | } |
193 | 0 | context = null; |
194 | 0 | } |
195 | |
|
196 | |
public void completeScan( boolean executeOnEntireRepo ) |
197 | |
{ |
198 | 0 | if( executeOnEntireRepo ) |
199 | |
{ |
200 | 0 | completeScan(); |
201 | |
} |
202 | |
|
203 | |
|
204 | 0 | } |
205 | |
|
206 | |
public List<String> getExcludes() |
207 | |
{ |
208 | 0 | return Collections.emptyList(); |
209 | |
} |
210 | |
|
211 | |
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
212 | |
{ |
213 | 0 | if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) |
214 | |
{ |
215 | 0 | initIncludes(); |
216 | |
} |
217 | 0 | } |
218 | |
|
219 | |
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
220 | |
{ |
221 | |
|
222 | 0 | } |
223 | |
|
224 | |
private void initIncludes() |
225 | |
{ |
226 | 0 | includes.clear(); |
227 | |
|
228 | 0 | includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) ); |
229 | |
|
230 | 0 | includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) ); |
231 | 0 | } |
232 | |
|
233 | |
public void initialize() |
234 | |
throws InitializationException |
235 | |
{ |
236 | 0 | configuration.addChangeListener( this ); |
237 | |
|
238 | 0 | initIncludes(); |
239 | 0 | } |
240 | |
|
241 | |
public List<String> getIncludes() |
242 | |
{ |
243 | 0 | return includes; |
244 | |
} |
245 | |
} |