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