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.io.InputStream;
25  import java.io.OutputStream;
26  import java.util.Date;
27  
28  import org.apache.lucene.document.Document;
29  import org.apache.lucene.document.Field;
30  import org.apache.lucene.index.IndexReader;
31  import org.apache.lucene.index.IndexWriter;
32  import org.apache.lucene.store.Directory;
33  import org.apache.lucene.store.IndexInput;
34  import org.apache.lucene.store.IndexOutput;
35  import org.apache.maven.index.ArtifactInfo;
36  import org.codehaus.plexus.util.FileUtils;
37  
38  public class IndexUtils
39  {
40      public static final String TIMESTAMP_FILE = "timestamp";
41  
42      private static final int BUFFER_SIZE = 16384;
43  
44      // Directory
45  
46      public static void copyDirectory( Directory source, Directory target )
47          throws IOException
48      {
49          // perform plain copy (but semantic changes between Lucene 2.4 and 2.9 exists, so timestamp file will be not
50          // copied in 2.9)
51          Directory.copy( source, target, false );
52  
53          copyFile( source, target, IndexingContext.INDEX_UPDATER_PROPERTIES_FILE );
54          copyFile( source, target, IndexingContext.INDEX_PACKER_PROPERTIES_FILE );
55  
56          Date ts = getTimestamp( source );
57          updateTimestamp( target, ts );
58      }
59  
60      public static boolean copyFile( Directory source, Directory target, String name )
61          throws IOException
62      {
63          return copyFile( source, target, name, name );
64      }
65  
66      public static boolean copyFile( Directory source, Directory target, String srcName, String targetName )
67          throws IOException
68      {
69          if ( !source.fileExists( srcName ) )
70          {
71              return false;
72          }
73  
74          byte[] buf = new byte[BUFFER_SIZE];
75  
76          IndexInput is = null;
77          IndexOutput os = null;
78  
79          try
80          {
81              is = source.openInput( srcName );
82  
83              os = target.createOutput( targetName );
84  
85              // and copy to dest directory
86              long len = is.length();
87              long readCount = 0;
88              while ( readCount < len )
89              {
90                  int toRead = readCount + BUFFER_SIZE > len ? (int) ( len - readCount ) : BUFFER_SIZE;
91                  is.readBytes( buf, 0, toRead );
92                  os.writeBytes( buf, toRead );
93                  readCount += toRead;
94              }
95  
96              return true;
97          }
98          finally
99          {
100             close( os );
101 
102             close( is );
103         }
104     }
105 
106     // timestamp
107 
108     public static ArtifactInfo constructArtifactInfo( Document doc, IndexingContext context )
109     {
110         // if no UINFO can't create, must be a different type of record
111         if ( doc.get( ArtifactInfo.UINFO ) == null )
112         {
113             return null;
114         }
115 
116         boolean res = false;
117 
118         ArtifactInfo artifactInfo = new ArtifactInfo();
119 
120         for ( IndexCreator ic : context.getIndexCreators() )
121         {
122             res |= ic.updateArtifactInfo( doc, artifactInfo );
123         }
124 
125         return res ? artifactInfo : null;
126     }
127 
128     public static Document updateDocument( Document doc, IndexingContext context )
129     {
130         return updateDocument( doc, context, true );
131     }
132 
133     public static Document updateDocument( Document doc, IndexingContext context, boolean updateLastModified )
134     {
135         ArtifactInfo ai = constructArtifactInfo( doc, context );
136         if ( ai == null )
137         {
138             return doc;
139         }
140 
141         Document document = new Document();
142 
143         // unique key
144         document.add( new Field( ArtifactInfo.UINFO, ai.getUinfo(), Field.Store.YES, Field.Index.NOT_ANALYZED ) );
145 
146         if ( updateLastModified || doc.getField( ArtifactInfo.LAST_MODIFIED ) == null )
147         {
148             document.add( new Field( ArtifactInfo.LAST_MODIFIED, //
149                 Long.toString( System.currentTimeMillis() ), Field.Store.YES, Field.Index.NO ) );
150         }
151         else
152         {
153             document.add( doc.getField( ArtifactInfo.LAST_MODIFIED ) );
154         }
155 
156         for ( IndexCreator ic : context.getIndexCreators() )
157         {
158             ic.updateDocument( ai, document );
159         }
160 
161         return document;
162     }
163 
164     public static void deleteTimestamp( Directory directory )
165         throws IOException
166     {
167         if ( directory.fileExists( TIMESTAMP_FILE ) )
168         {
169             directory.deleteFile( TIMESTAMP_FILE );
170         }
171     }
172 
173     public static void updateTimestamp( Directory directory, Date timestamp )
174         throws IOException
175     {
176         synchronized ( directory )
177         {
178             Date currentTimestamp = getTimestamp( directory );
179 
180             if ( timestamp != null && ( currentTimestamp == null || !currentTimestamp.equals( timestamp ) ) )
181             {
182                 deleteTimestamp( directory );
183 
184                 IndexOutput io = directory.createOutput( TIMESTAMP_FILE );
185 
186                 try
187                 {
188                     io.writeLong( timestamp.getTime() );
189 
190                     io.flush();
191                 }
192                 finally
193                 {
194                     close( io );
195                 }
196             }
197         }
198     }
199 
200     public static Date getTimestamp( Directory directory )
201     {
202         synchronized ( directory )
203         {
204             Date result = null;
205             try
206             {
207                 if ( directory.fileExists( TIMESTAMP_FILE ) )
208                 {
209                     IndexInput ii = null;
210 
211                     try
212                     {
213                         ii = directory.openInput( TIMESTAMP_FILE );
214 
215                         result = new Date( ii.readLong() );
216                     }
217                     finally
218                     {
219                         close( ii );
220                     }
221                 }
222             }
223             catch ( IOException ex )
224             {
225             }
226 
227             return result;
228         }
229     }
230 
231     // close helpers
232 
233     public static void close( OutputStream os )
234     {
235         if ( os != null )
236         {
237             try
238             {
239                 os.close();
240             }
241             catch ( IOException e )
242             {
243                 // ignore
244             }
245         }
246     }
247 
248     public static void close( InputStream is )
249     {
250         if ( is != null )
251         {
252             try
253             {
254                 is.close();
255             }
256             catch ( IOException e )
257             {
258                 // ignore
259             }
260         }
261     }
262 
263     public static void close( IndexOutput io )
264     {
265         if ( io != null )
266         {
267             try
268             {
269                 io.close();
270             }
271             catch ( IOException e )
272             {
273                 // ignore
274             }
275         }
276     }
277 
278     public static void close( IndexInput in )
279     {
280         if ( in != null )
281         {
282             try
283             {
284                 in.close();
285             }
286             catch ( IOException e )
287             {
288                 // ignore
289             }
290         }
291     }
292 
293     public static void close( IndexReader r )
294     {
295         if ( r != null )
296         {
297             try
298             {
299                 r.close();
300             }
301             catch ( IOException e )
302             {
303                 // ignore
304             }
305         }
306     }
307 
308     public static void close( IndexWriter w )
309     {
310         if ( w != null )
311         {
312             try
313             {
314                 w.close();
315             }
316             catch ( IOException e )
317             {
318                 // ignore
319             }
320         }
321     }
322 
323     public static void close( Directory d )
324     {
325         if ( d != null )
326         {
327             try
328             {
329                 d.close();
330             }
331             catch ( IOException e )
332             {
333                 // ignore
334             }
335         }
336     }
337 
338     public static void delete( File indexDir )
339     {
340         try
341         {
342             FileUtils.deleteDirectory( indexDir );
343         }
344         catch ( IOException ex )
345         {
346             // ignore
347         }
348     }
349 
350 }