1 package org.apache.maven.index.context;
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.Collection;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.apache.lucene.analysis.Analyzer;
32 import org.apache.lucene.index.IndexReader;
33 import org.apache.lucene.index.IndexWriter;
34 import org.apache.lucene.index.MultiReader;
35 import org.apache.lucene.search.IndexSearcher;
36 import org.apache.lucene.store.Directory;
37 import org.apache.lucene.store.FSDirectory;
38 import org.apache.maven.index.artifact.GavCalculator;
39 import org.apache.maven.index.artifact.M2GavCalculator;
40
41
42
43
44
45
46
47 public class MergedIndexingContext
48 extends AbstractIndexingContext
49 {
50 private final String id;
51
52 private final String repositoryId;
53
54 private final File repository;
55
56 private final ContextMemberProvider membersProvider;
57
58 private final GavCalculator gavCalculator;
59
60 private final Directory directory;
61
62 private File directoryFile;
63
64 private boolean searchable;
65
66 private MergedIndexingContext( ContextMemberProvider membersProvider, String id, String repositoryId,
67 File repository, Directory indexDirectory, boolean searchable )
68 throws IOException
69 {
70 this.id = id;
71 this.repositoryId = repositoryId;
72 this.repository = repository;
73 this.membersProvider = membersProvider;
74 this.gavCalculator = new M2GavCalculator();
75 this.directory = indexDirectory;
76 this.searchable = searchable;
77 }
78
79 public MergedIndexingContext( String id, String repositoryId, File repository, File indexDirectoryFile,
80 boolean searchable, ContextMemberProvider membersProvider )
81 throws IOException
82 {
83 this( membersProvider, id, repositoryId, repository, FSDirectory.open( indexDirectoryFile ), searchable );
84
85 this.directoryFile = indexDirectoryFile;
86 }
87
88 public MergedIndexingContext( String id, String repositoryId, File repository, Directory indexDirectory,
89 boolean searchable, ContextMemberProvider membersProvider )
90 throws IOException
91 {
92 this( membersProvider, id, repositoryId, repository, indexDirectory, searchable );
93
94 if ( indexDirectory instanceof FSDirectory )
95 {
96 this.directoryFile = ( (FSDirectory) indexDirectory ).getFile();
97 }
98 }
99
100 public Collection<IndexingContext> getMembers()
101 {
102 return membersProvider.getMembers();
103 }
104
105 public String getId()
106 {
107 return id;
108 }
109
110 public String getRepositoryId()
111 {
112 return repositoryId;
113 }
114
115 public File getRepository()
116 {
117 return repository;
118 }
119
120 public String getRepositoryUrl()
121 {
122 return null;
123 }
124
125 public String getIndexUpdateUrl()
126 {
127 return null;
128 }
129
130 public boolean isSearchable()
131 {
132 return searchable;
133 }
134
135 public void setSearchable( boolean searchable )
136 {
137 this.searchable = searchable;
138 }
139
140 public Date getTimestamp()
141 {
142 Date ts = null;
143
144 for ( IndexingContext ctx : getMembers() )
145 {
146 Date cts = ctx.getTimestamp();
147
148 if ( cts != null )
149 {
150 if ( ts == null || cts.after( ts ) )
151 {
152 ts = cts;
153 }
154 }
155 }
156
157 return ts;
158 }
159
160 public void updateTimestamp()
161 throws IOException
162 {
163
164 }
165
166 public void updateTimestamp( boolean save )
167 throws IOException
168 {
169
170 }
171
172 public void updateTimestamp( boolean save, Date date )
173 throws IOException
174 {
175
176 }
177
178 public int getSize()
179 throws IOException
180 {
181 int size = 0;
182
183 for ( IndexingContext ctx : getMembers() )
184 {
185 size += ctx.getSize();
186 }
187
188 return size;
189 }
190
191 public IndexReader getIndexReader()
192 throws IOException
193 {
194 Collection<IndexingContext> members = getMembers();
195
196 ArrayList<IndexReader> contextsToSearch = new ArrayList<IndexReader>( members.size() );
197
198 for ( IndexingContext ctx : members )
199 {
200 contextsToSearch.add( ctx.getIndexReader() );
201 }
202
203 MultiReader multiReader =
204 new MultiReader( contextsToSearch.toArray( new IndexReader[contextsToSearch.size()] ) );
205
206 return multiReader;
207 }
208
209 public IndexSearcher getIndexSearcher()
210 throws IOException
211 {
212 return new NexusIndexSearcher( getIndexReader() );
213 }
214
215 public IndexWriter getIndexWriter()
216 throws IOException
217 {
218
219 return null;
220
221 }
222
223 public List<IndexCreator> getIndexCreators()
224 {
225 HashSet<IndexCreator> creators = new HashSet<IndexCreator>();
226
227 for ( IndexingContext ctx : getMembers() )
228 {
229 creators.addAll( ctx.getIndexCreators() );
230 }
231
232 return new ArrayList<IndexCreator>( creators );
233 }
234
235 public Analyzer getAnalyzer()
236 {
237 return new NexusAnalyzer();
238 }
239
240 public void commit()
241 throws IOException
242 {
243
244 }
245
246 public void rollback()
247 throws IOException
248 {
249
250 }
251
252 public void optimize()
253 throws IOException
254 {
255
256 }
257
258 public void lock()
259 {
260 for ( IndexingContext ctx : getMembers() )
261 {
262 ctx.lock();
263 }
264 }
265
266 public void unlock()
267 {
268 for ( IndexingContext ctx : getMembers() )
269 {
270 ctx.unlock();
271 }
272 }
273
274 public void close( boolean deleteFiles )
275 throws IOException
276 {
277
278 }
279
280 public void purge()
281 throws IOException
282 {
283
284 }
285
286 public void merge( Directory directory )
287 throws IOException
288 {
289
290 }
291
292 public void merge( Directory directory, DocumentFilter filter )
293 throws IOException
294 {
295
296 }
297
298 public void replace( Directory directory )
299 throws IOException
300 {
301
302 }
303
304 public Directory getIndexDirectory()
305 {
306 return directory;
307 }
308
309 public File getIndexDirectoryFile()
310 {
311 return directoryFile;
312 }
313
314 public GavCalculator getGavCalculator()
315 {
316 return gavCalculator;
317 }
318
319 public void setAllGroups( Collection<String> groups )
320 throws IOException
321 {
322
323 }
324
325 public Set<String> getAllGroups()
326 throws IOException
327 {
328 HashSet<String> result = new HashSet<String>();
329
330 for ( IndexingContext ctx : getMembers() )
331 {
332 result.addAll( ctx.getAllGroups() );
333 }
334
335 return result;
336 }
337
338 public void setRootGroups( Collection<String> groups )
339 throws IOException
340 {
341
342 }
343
344 public Set<String> getRootGroups()
345 throws IOException
346 {
347 HashSet<String> result = new HashSet<String>();
348
349 for ( IndexingContext ctx : getMembers() )
350 {
351 result.addAll( ctx.getRootGroups() );
352 }
353
354 return result;
355 }
356
357 public void rebuildGroups()
358 throws IOException
359 {
360
361 }
362
363 }