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.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
45
46 public static void copyDirectory( Directory source, Directory target )
47 throws IOException
48 {
49
50
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
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
107
108 public static ArtifactInfo constructArtifactInfo( Document doc, IndexingContext context )
109 {
110
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
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
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
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
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
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
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
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
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
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
347 }
348 }
349
350 }