View Javadoc

1   package org.apache.maven.index.context;
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 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   * A merged indexing context that offers read only "view" on multiple other indexing contexts merged and presented as
43   * one. Usable for searching and publishing, but all write operations are basically noop.
44   * 
45   * @author cstamas
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         // noop
164     }
165 
166     public void updateTimestamp( boolean save )
167         throws IOException
168     {
169         // noop
170     }
171 
172     public void updateTimestamp( boolean save, Date date )
173         throws IOException
174     {
175         // noop
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         // noop?
219         return null;
220         // throw new UnsupportedOperationException( "Merged indexing context is read-only!" );
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         // noop
244     }
245 
246     public void rollback()
247         throws IOException
248     {
249         // noop
250     }
251 
252     public void optimize()
253         throws IOException
254     {
255         // noop
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         // noop
278     }
279 
280     public void purge()
281         throws IOException
282     {
283         // noop
284     }
285 
286     public void merge( Directory directory )
287         throws IOException
288     {
289         // noop
290     }
291 
292     public void merge( Directory directory, DocumentFilter filter )
293         throws IOException
294     {
295         // noop
296     }
297 
298     public void replace( Directory directory )
299         throws IOException
300     {
301         // noop
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         // noop
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         // noop
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         // noop
361     }
362 
363 }