70
70
import org .elasticsearch .script .ScriptService ;
71
71
import org .elasticsearch .threadpool .ThreadPool ;
72
72
73
- import java .io .File ;
74
- import java .io .FileInputStream ;
73
+ import java .io .BufferedReader ;
74
+ import java .io .IOException ;
75
75
import java .io .InputStreamReader ;
76
76
import java .io .UnsupportedEncodingException ;
77
+ import java .nio .file .DirectoryStream ;
78
+ import java .nio .file .Files ;
79
+ import java .nio .file .Path ;
77
80
import java .util .*;
78
81
import java .util .concurrent .Semaphore ;
79
82
import java .util .concurrent .TimeUnit ;
@@ -303,17 +306,17 @@ public ClusterState execute(ClusterState currentState) throws Exception {
303
306
}
304
307
305
308
// now add config level mappings
306
- File mappingsDir = new File ( environment .configFile (), "mappings" );
307
- if (mappingsDir . exists () && mappingsDir . isDirectory ()) {
309
+ Path mappingsDir = environment .configFile (). resolve ( "mappings" );
310
+ if (Files . isDirectory (mappingsDir )) {
308
311
// first index level
309
- File indexMappingsDir = new File ( mappingsDir , request .index ());
310
- if (indexMappingsDir . exists () && indexMappingsDir . isDirectory ()) {
312
+ Path indexMappingsDir = mappingsDir . resolve ( request .index ());
313
+ if (Files . isDirectory (indexMappingsDir )) {
311
314
addMappings (mappings , indexMappingsDir );
312
315
}
313
316
314
317
// second is the _default mapping
315
- File defaultMappingsDir = new File ( mappingsDir , "_default" );
316
- if (defaultMappingsDir . exists () && defaultMappingsDir . isDirectory ()) {
318
+ Path defaultMappingsDir = mappingsDir . resolve ( "_default" );
319
+ if (Files . isDirectory (defaultMappingsDir )) {
317
320
addMappings (mappings , defaultMappingsDir );
318
321
}
319
322
}
@@ -485,28 +488,30 @@ private Map<String, Object> parseMapping(String mappingSource) throws Exception
485
488
return XContentFactory .xContent (mappingSource ).createParser (mappingSource ).mapAndClose ();
486
489
}
487
490
488
- private void addMappings (Map <String , Map <String , Object >> mappings , File mappingsDir ) {
489
- File [] mappingsFiles = mappingsDir .listFiles ();
490
- for (File mappingFile : mappingsFiles ) {
491
- if (mappingFile .isHidden ()) {
492
- continue ;
493
- }
494
- int lastDotIndex = mappingFile .getName ().lastIndexOf ('.' );
495
- String mappingType = lastDotIndex != -1 ? mappingFile .getName ().substring (0 , lastDotIndex ) : mappingFile .getName ();
496
- try {
497
- String mappingSource = Streams .copyToString (new InputStreamReader (new FileInputStream (mappingFile ), Charsets .UTF_8 ));
498
- if (mappings .containsKey (mappingType )) {
499
- XContentHelper .mergeDefaults (mappings .get (mappingType ), parseMapping (mappingSource ));
500
- } else {
501
- mappings .put (mappingType , parseMapping (mappingSource ));
491
+ private void addMappings (Map <String , Map <String , Object >> mappings , Path mappingsDir ) throws IOException {
492
+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (mappingsDir )) {
493
+ for (Path mappingFile : stream ) {
494
+ final String fileName = mappingFile .getFileName ().toString ();
495
+ if (Files .isHidden (mappingFile )) {
496
+ continue ;
497
+ }
498
+ int lastDotIndex = fileName .lastIndexOf ('.' );
499
+ String mappingType = lastDotIndex != -1 ? mappingFile .getFileName ().toString ().substring (0 , lastDotIndex ) : mappingFile .getFileName ().toString ();
500
+ try (BufferedReader reader = Files .newBufferedReader (mappingFile , Charsets .UTF_8 )) {
501
+ String mappingSource = Streams .copyToString (reader );
502
+ if (mappings .containsKey (mappingType )) {
503
+ XContentHelper .mergeDefaults (mappings .get (mappingType ), parseMapping (mappingSource ));
504
+ } else {
505
+ mappings .put (mappingType , parseMapping (mappingSource ));
506
+ }
507
+ } catch (Exception e ) {
508
+ logger .warn ("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring..." , e );
502
509
}
503
- } catch (Exception e ) {
504
- logger .warn ("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring..." , e );
505
510
}
506
511
}
507
512
}
508
513
509
- private List <IndexTemplateMetaData > findTemplates (CreateIndexClusterStateUpdateRequest request , ClusterState state , IndexTemplateFilter indexTemplateFilter ) {
514
+ private List <IndexTemplateMetaData > findTemplates (CreateIndexClusterStateUpdateRequest request , ClusterState state , IndexTemplateFilter indexTemplateFilter ) throws IOException {
510
515
List <IndexTemplateMetaData > templates = Lists .newArrayList ();
511
516
for (ObjectCursor <IndexTemplateMetaData > cursor : state .metaData ().templates ().values ()) {
512
517
IndexTemplateMetaData template = cursor .value ;
@@ -516,22 +521,21 @@ private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateR
516
521
}
517
522
518
523
// see if we have templates defined under config
519
- File templatesDir = new File (environment .configFile (), "templates" );
520
- if (templatesDir .exists () && templatesDir .isDirectory ()) {
521
- File [] templatesFiles = templatesDir .listFiles ();
522
- if (templatesFiles != null ) {
523
- for (File templatesFile : templatesFiles ) {
524
- if (templatesFile .isFile ()) {
524
+ final Path templatesDir = environment .configFile ().resolve ("templates" );
525
+ if (Files .exists (templatesDir ) && Files .isDirectory (templatesDir )) {
526
+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (templatesDir )) {
527
+ for (Path templatesFile : stream ) {
528
+ if (Files .isRegularFile (templatesFile )) {
525
529
XContentParser parser = null ;
526
530
try {
527
- byte [] templatesData = Streams . copyToByteArray (templatesFile );
531
+ final byte [] templatesData = Files . readAllBytes (templatesFile );
528
532
parser = XContentHelper .createParser (templatesData , 0 , templatesData .length );
529
- IndexTemplateMetaData template = IndexTemplateMetaData .Builder .fromXContent (parser , templatesFile .getName ());
533
+ IndexTemplateMetaData template = IndexTemplateMetaData .Builder .fromXContent (parser , templatesFile .getFileName (). toString ());
530
534
if (indexTemplateFilter .apply (request , template )) {
531
535
templates .add (template );
532
536
}
533
537
} catch (Exception e ) {
534
- logger .warn ("[{}] failed to read template [{}] from config" , e , request .index (), templatesFile .getAbsolutePath ());
538
+ logger .warn ("[{}] failed to read template [{}] from config" , e , request .index (), templatesFile .toAbsolutePath ());
535
539
} finally {
536
540
Releasables .closeWhileHandlingException (parser );
537
541
}
0 commit comments