1
+ /*
2
+ * Copyright (C) 2008 Jive Software, 2024 Ignite Realtime Foundation. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
package org .jivesoftware .openfire .index ;
2
17
3
18
import org .apache .commons .io .FileUtils ;
@@ -39,7 +54,7 @@ public abstract class LuceneIndexer
39
54
private final int schemaVersion ;
40
55
protected TaskEngine taskEngine ;
41
56
protected RebuildFuture rebuildFuture ;
42
- private File searchDir ;
57
+ private Path searchDir ;
43
58
private XMLProperties indexProperties ;
44
59
private Directory directory ;
45
60
private IndexSearcher searcher ;
@@ -55,7 +70,7 @@ public abstract class LuceneIndexer
55
70
.setPlugin (MonitoringConstants .PLUGIN_NAME )
56
71
.build ();
57
72
58
- public LuceneIndexer (TaskEngine taskEngine , File searchDir , String logName , int schemaVersion )
73
+ public LuceneIndexer (TaskEngine taskEngine , Path searchDir , String logName , int schemaVersion )
59
74
{
60
75
this .taskEngine = taskEngine ;
61
76
this .searchDir = searchDir ;
@@ -66,19 +81,20 @@ public LuceneIndexer(TaskEngine taskEngine, File searchDir, String logName, int
66
81
public void start ()
67
82
{
68
83
Log .debug ("Starting..." );
69
- if ( ! searchDir .exists () )
84
+ if (! Files .exists (searchDir ) )
70
85
{
71
- if ( !searchDir .mkdirs () )
72
- {
73
- Log .warn ("Lucene index directory '{}' does not exist, but cannot be created!" , searchDir );
86
+ try {
87
+ Files .createDirectories (searchDir );
88
+ } catch (IOException e ) {
89
+ Log .warn ("Lucene index directory '{}' does not exist, but cannot be created!" , searchDir , e );
74
90
}
75
91
}
76
92
77
93
boolean indexCreated = false ;
78
94
try
79
95
{
80
96
indexProperties = loadPropertiesFile (searchDir );
81
- directory = FSDirectory .open (searchDir . toPath () );
97
+ directory = FSDirectory .open (searchDir );
82
98
if ( !DirectoryReader .indexExists (directory ) )
83
99
{
84
100
Log .info ("Unable to find a Lucene index in {}. rebuilding." , directory );
@@ -94,7 +110,7 @@ public void start()
94
110
{
95
111
// See if the data on disk is of the schema version that we expect to be able to use.
96
112
// TODO make this optional through configuration.
97
- final Path schemaVersionFile = searchDir .toPath (). resolve ("openfire-schema.version" );
113
+ final Path schemaVersionFile = searchDir .resolve ("openfire-schema.version" );
98
114
final Scanner scanner = new Scanner (schemaVersionFile );
99
115
int detectedSchemaVersion = -1 ;
100
116
if (scanner .hasNextInt ())
@@ -169,17 +185,22 @@ public void run()
169
185
}
170
186
};
171
187
final Duration updateInterval = UPDATE_INTERVAL .getValue ();
172
- taskEngine .schedule (indexUpdater , Duration .ofMinutes (1 ). toMillis () , updateInterval . toMillis () );
188
+ taskEngine .schedule (indexUpdater , Duration .ofMinutes (1 ), updateInterval );
173
189
}
174
190
175
191
private void removeAndRebuildSearchDir () throws IOException {
176
192
directory .close ();
177
- FileUtils .deleteDirectory (searchDir );
178
- if ( ! searchDir . mkdirs () )
193
+ FileUtils .deleteDirectory (searchDir . toFile () );
194
+ if (! Files . exists ( searchDir ) )
179
195
{
180
- Log .warn ("Lucene index directory '{}' cannot be recreated!" , searchDir );
196
+ try {
197
+ Files .createDirectories (searchDir );
198
+ } catch (IOException e ) {
199
+ Log .warn ("Lucene index directory '{}' cannot be recreated!" , searchDir , e );
200
+ }
181
201
}
182
- directory = FSDirectory .open (searchDir .toPath ());
202
+
203
+ directory = FSDirectory .open (searchDir );
183
204
}
184
205
185
206
protected synchronized Instant getLastModified ()
@@ -244,7 +265,7 @@ public void stop()
244
265
*/
245
266
public long getIndexSize ()
246
267
{
247
- File [] files = searchDir .listFiles (( dir , name ) -> {
268
+ File [] files = searchDir .toFile (). listFiles (( dir , name ) -> {
248
269
// Ignore the index properties file since it's not part of the index.
249
270
return !name .equals ("indexprops.xml" );
250
271
});
@@ -331,11 +352,11 @@ public synchronized Future<Integer> rebuildIndex() {
331
352
332
353
Log .debug ("Removing old data from directory: {}" , searchDir );
333
354
try {
334
- FileUtils .cleanDirectory (searchDir );
335
- Files .write (searchDir .toPath (). resolve ("openfire-schema.version" ), String .valueOf (this .schemaVersion ).getBytes ());
355
+ FileUtils .cleanDirectory (searchDir . toFile () );
356
+ Files .write (searchDir .resolve ("openfire-schema.version" ), String .valueOf (this .schemaVersion ).getBytes ());
336
357
indexProperties = loadPropertiesFile (searchDir );
337
358
} catch ( IOException e ) {
338
- Log .warn ("An exception occurred while trying to clean directory '{}' as part of a rebuild of the Lucene index that's in it." , searchDir );
359
+ Log .warn ("An exception occurred while trying to clean directory '{}' as part of a rebuild of the Lucene index that's in it." , searchDir , e );
339
360
}
340
361
341
362
final Analyzer analyzer = new StandardAnalyzer ();
@@ -431,9 +452,9 @@ public synchronized IndexSearcher getSearcher() throws IOException
431
452
* loaded. If an XML file for the search properties isn't already
432
453
* created, it will attempt to make a file with default values.
433
454
*/
434
- private XMLProperties loadPropertiesFile ( File searchDir ) throws IOException
455
+ private XMLProperties loadPropertiesFile ( Path searchDir ) throws IOException
435
456
{
436
- File indexPropertiesFile = new File (searchDir , "indexprops.xml" );
457
+ File indexPropertiesFile = new File (searchDir . toFile () , "indexprops.xml" );
437
458
438
459
// Make sure the file actually exists. If it doesn't, a new file
439
460
// will be created.
0 commit comments