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