comparison OrthancServer/OrthancInitialization.cpp @ 1892:0001f8cd7849

Warn about badly formatted modality/peer definitions in configuration file at startup
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 Dec 2015 10:10:50 +0100
parents 53e045b5a8ec
children b1291df2f780
comparison
equal deleted inserted replaced
1891:b465506eb028 1892:0001f8cd7849
69 #endif 69 #endif
70 70
71 71
72 namespace Orthanc 72 namespace Orthanc
73 { 73 {
74 static boost::mutex globalMutex_; 74 static boost::recursive_mutex globalMutex_;
75 static Json::Value configuration_; 75 static Json::Value configuration_;
76 static boost::filesystem::path defaultDirectory_; 76 static boost::filesystem::path defaultDirectory_;
77 static std::string configurationAbsolutePath_; 77 static std::string configurationAbsolutePath_;
78 static FontRegistry fontRegistry_; 78 static FontRegistry fontRegistry_;
79 79
237 p /= "Configuration.json"; 237 p /= "Configuration.json";
238 configurationAbsolutePath_ = boost::filesystem::absolute(p).string(); 238 configurationAbsolutePath_ = boost::filesystem::absolute(p).string();
239 239
240 AddFileToConfiguration(p); 240 AddFileToConfiguration(p);
241 #endif 241 #endif
242 }
243 }
244
245
246 static void ValidateGlobalConfiguration()
247 {
248 std::set<std::string> ids;
249
250 Configuration::GetListOfOrthancPeers(ids);
251 for (std::set<std::string>::const_iterator it = ids.begin(); it != ids.end(); ++it)
252 {
253 OrthancPeerParameters peer;
254 Configuration::GetOrthancPeer(peer, *it);
255 }
256
257 Configuration::GetListOfDicomModalities(ids);
258 for (std::set<std::string>::const_iterator it = ids.begin(); it != ids.end(); ++it)
259 {
260 RemoteModalityParameters modality;
261 Configuration::GetDicomModalityUsingSymbolicName(modality, *it);
242 } 262 }
243 } 263 }
244 264
245 265
246 static void RegisterUserMetadata() 266 static void RegisterUserMetadata()
365 385
366 386
367 387
368 void OrthancInitialize(const char* configurationFile) 388 void OrthancInitialize(const char* configurationFile)
369 { 389 {
370 boost::mutex::scoped_lock lock(globalMutex_); 390 boost::recursive_mutex::scoped_lock lock(globalMutex_);
371 391
372 #if ORTHANC_SSL_ENABLED == 1 392 #if ORTHANC_SSL_ENABLED == 1
373 // https://wiki.openssl.org/index.php/Library_Initialization 393 // https://wiki.openssl.org/index.php/Library_Initialization
374 SSL_library_init(); 394 SSL_library_init();
375 SSL_load_error_strings(); 395 SSL_load_error_strings();
383 403
384 InitializeServerEnumerations(); 404 InitializeServerEnumerations();
385 405
386 // Read the user-provided configuration 406 // Read the user-provided configuration
387 ReadGlobalConfiguration(configurationFile); 407 ReadGlobalConfiguration(configurationFile);
408 ValidateGlobalConfiguration();
388 409
389 HttpClient::GlobalInitialize(GetGlobalBoolParameterInternal("HttpsVerifyPeers", true), 410 HttpClient::GlobalInitialize(GetGlobalBoolParameterInternal("HttpsVerifyPeers", true),
390 GetGlobalStringParameterInternal("HttpsCACertificates", "")); 411 GetGlobalStringParameterInternal("HttpsCACertificates", ""));
391 412
392 RegisterUserMetadata(); 413 RegisterUserMetadata();
410 431
411 432
412 433
413 void OrthancFinalize() 434 void OrthancFinalize()
414 { 435 {
415 boost::mutex::scoped_lock lock(globalMutex_); 436 boost::recursive_mutex::scoped_lock lock(globalMutex_);
416 HttpClient::GlobalFinalize(); 437 HttpClient::GlobalFinalize();
417 438
418 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 439 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
419 // Unregister JPEG-LS codecs 440 // Unregister JPEG-LS codecs
420 DJLSDecoderRegistration::cleanup(); 441 DJLSDecoderRegistration::cleanup();
442 463
443 464
444 std::string Configuration::GetGlobalStringParameter(const std::string& parameter, 465 std::string Configuration::GetGlobalStringParameter(const std::string& parameter,
445 const std::string& defaultValue) 466 const std::string& defaultValue)
446 { 467 {
447 boost::mutex::scoped_lock lock(globalMutex_); 468 boost::recursive_mutex::scoped_lock lock(globalMutex_);
448 return GetGlobalStringParameterInternal(parameter, defaultValue); 469 return GetGlobalStringParameterInternal(parameter, defaultValue);
449 } 470 }
450 471
451 472
452 int Configuration::GetGlobalIntegerParameter(const std::string& parameter, 473 int Configuration::GetGlobalIntegerParameter(const std::string& parameter,
453 int defaultValue) 474 int defaultValue)
454 { 475 {
455 boost::mutex::scoped_lock lock(globalMutex_); 476 boost::recursive_mutex::scoped_lock lock(globalMutex_);
456 477
457 if (configuration_.isMember(parameter)) 478 if (configuration_.isMember(parameter))
458 { 479 {
459 if (configuration_[parameter].type() != Json::intValue) 480 if (configuration_[parameter].type() != Json::intValue)
460 { 481 {
474 495
475 496
476 bool Configuration::GetGlobalBoolParameter(const std::string& parameter, 497 bool Configuration::GetGlobalBoolParameter(const std::string& parameter,
477 bool defaultValue) 498 bool defaultValue)
478 { 499 {
479 boost::mutex::scoped_lock lock(globalMutex_); 500 boost::recursive_mutex::scoped_lock lock(globalMutex_);
480 return GetGlobalBoolParameterInternal(parameter, defaultValue); 501 return GetGlobalBoolParameterInternal(parameter, defaultValue);
481 } 502 }
482 503
483 504
484 void Configuration::GetDicomModalityUsingSymbolicName(RemoteModalityParameters& modality, 505 void Configuration::GetDicomModalityUsingSymbolicName(RemoteModalityParameters& modality,
485 const std::string& name) 506 const std::string& name)
486 { 507 {
487 boost::mutex::scoped_lock lock(globalMutex_); 508 boost::recursive_mutex::scoped_lock lock(globalMutex_);
488 509
489 if (!configuration_.isMember("DicomModalities")) 510 if (!configuration_.isMember("DicomModalities"))
490 { 511 {
491 LOG(ERROR) << "No modality with symbolic name: " << name; 512 LOG(ERROR) << "No modality with symbolic name: " << name;
492 throw OrthancException(ErrorCode_InexistentItem); 513 throw OrthancException(ErrorCode_InexistentItem);
515 536
516 537
517 void Configuration::GetOrthancPeer(OrthancPeerParameters& peer, 538 void Configuration::GetOrthancPeer(OrthancPeerParameters& peer,
518 const std::string& name) 539 const std::string& name)
519 { 540 {
520 boost::mutex::scoped_lock lock(globalMutex_); 541 boost::recursive_mutex::scoped_lock lock(globalMutex_);
521 542
522 if (!configuration_.isMember("OrthancPeers")) 543 if (!configuration_.isMember("OrthancPeers"))
523 { 544 {
524 LOG(ERROR) << "No peer with symbolic name: " << name; 545 LOG(ERROR) << "No peer with symbolic name: " << name;
525 throw OrthancException(ErrorCode_InexistentItem); 546 throw OrthancException(ErrorCode_InexistentItem);
548 569
549 static bool ReadKeys(std::set<std::string>& target, 570 static bool ReadKeys(std::set<std::string>& target,
550 const char* parameter, 571 const char* parameter,
551 bool onlyAlphanumeric) 572 bool onlyAlphanumeric)
552 { 573 {
553 boost::mutex::scoped_lock lock(globalMutex_); 574 boost::recursive_mutex::scoped_lock lock(globalMutex_);
554 575
555 target.clear(); 576 target.clear();
556 577
557 if (!configuration_.isMember(parameter)) 578 if (!configuration_.isMember(parameter))
558 { 579 {
587 } 608 }
588 609
589 610
590 void Configuration::GetListOfDicomModalities(std::set<std::string>& target) 611 void Configuration::GetListOfDicomModalities(std::set<std::string>& target)
591 { 612 {
613 target.clear();
614
592 if (!ReadKeys(target, "DicomModalities", true)) 615 if (!ReadKeys(target, "DicomModalities", true))
593 { 616 {
594 LOG(ERROR) << "Only alphanumeric and dash characters are allowed in the names of the modalities"; 617 LOG(ERROR) << "Only alphanumeric and dash characters are allowed in the names of the modalities";
595 throw OrthancException(ErrorCode_BadFileFormat); 618 throw OrthancException(ErrorCode_BadFileFormat);
596 } 619 }
597 } 620 }
598 621
599 622
600 void Configuration::GetListOfOrthancPeers(std::set<std::string>& target) 623 void Configuration::GetListOfOrthancPeers(std::set<std::string>& target)
601 { 624 {
625 target.clear();
626
602 if (!ReadKeys(target, "OrthancPeers", true)) 627 if (!ReadKeys(target, "OrthancPeers", true))
603 { 628 {
604 LOG(ERROR) << "Only alphanumeric and dash characters are allowed in the names of Orthanc peers"; 629 LOG(ERROR) << "Only alphanumeric and dash characters are allowed in the names of Orthanc peers";
605 throw OrthancException(ErrorCode_BadFileFormat); 630 throw OrthancException(ErrorCode_BadFileFormat);
606 } 631 }
608 633
609 634
610 635
611 void Configuration::SetupRegisteredUsers(MongooseServer& httpServer) 636 void Configuration::SetupRegisteredUsers(MongooseServer& httpServer)
612 { 637 {
613 boost::mutex::scoped_lock lock(globalMutex_); 638 boost::recursive_mutex::scoped_lock lock(globalMutex_);
614 639
615 httpServer.ClearUsers(); 640 httpServer.ClearUsers();
616 641
617 if (!configuration_.isMember("RegisteredUsers")) 642 if (!configuration_.isMember("RegisteredUsers"))
618 { 643 {
662 } 687 }
663 } 688 }
664 689
665 std::string Configuration::InterpretStringParameterAsPath(const std::string& parameter) 690 std::string Configuration::InterpretStringParameterAsPath(const std::string& parameter)
666 { 691 {
667 boost::mutex::scoped_lock lock(globalMutex_); 692 boost::recursive_mutex::scoped_lock lock(globalMutex_);
668 return InterpretRelativePath(defaultDirectory_.string(), parameter); 693 return InterpretRelativePath(defaultDirectory_.string(), parameter);
669 } 694 }
670 695
671 696
672 void Configuration::GetGlobalListOfStringsParameter(std::list<std::string>& target, 697 void Configuration::GetGlobalListOfStringsParameter(std::list<std::string>& target,
673 const std::string& key) 698 const std::string& key)
674 { 699 {
675 boost::mutex::scoped_lock lock(globalMutex_); 700 boost::recursive_mutex::scoped_lock lock(globalMutex_);
676 701
677 target.clear(); 702 target.clear();
678 703
679 if (!configuration_.isMember(key)) 704 if (!configuration_.isMember(key))
680 { 705 {
775 800
776 801
777 void Configuration::UpdateModality(const std::string& symbolicName, 802 void Configuration::UpdateModality(const std::string& symbolicName,
778 const RemoteModalityParameters& modality) 803 const RemoteModalityParameters& modality)
779 { 804 {
780 boost::mutex::scoped_lock lock(globalMutex_); 805 boost::recursive_mutex::scoped_lock lock(globalMutex_);
781 806
782 if (!configuration_.isMember("DicomModalities")) 807 if (!configuration_.isMember("DicomModalities"))
783 { 808 {
784 configuration_["DicomModalities"] = Json::objectValue; 809 configuration_["DicomModalities"] = Json::objectValue;
785 } 810 }
799 } 824 }
800 825
801 826
802 void Configuration::RemoveModality(const std::string& symbolicName) 827 void Configuration::RemoveModality(const std::string& symbolicName)
803 { 828 {
804 boost::mutex::scoped_lock lock(globalMutex_); 829 boost::recursive_mutex::scoped_lock lock(globalMutex_);
805 830
806 if (!configuration_.isMember("DicomModalities")) 831 if (!configuration_.isMember("DicomModalities"))
807 { 832 {
808 LOG(ERROR) << "No modality with symbolic name: " << symbolicName; 833 LOG(ERROR) << "No modality with symbolic name: " << symbolicName;
809 throw OrthancException(ErrorCode_BadFileFormat); 834 throw OrthancException(ErrorCode_BadFileFormat);
821 846
822 847
823 void Configuration::UpdatePeer(const std::string& symbolicName, 848 void Configuration::UpdatePeer(const std::string& symbolicName,
824 const OrthancPeerParameters& peer) 849 const OrthancPeerParameters& peer)
825 { 850 {
826 boost::mutex::scoped_lock lock(globalMutex_); 851 boost::recursive_mutex::scoped_lock lock(globalMutex_);
827 852
828 if (!configuration_.isMember("OrthancPeers")) 853 if (!configuration_.isMember("OrthancPeers"))
829 { 854 {
830 LOG(ERROR) << "No peer with symbolic name: " << symbolicName; 855 LOG(ERROR) << "No peer with symbolic name: " << symbolicName;
831 configuration_["OrthancPeers"] = Json::objectValue; 856 configuration_["OrthancPeers"] = Json::objectValue;
846 } 871 }
847 872
848 873
849 void Configuration::RemovePeer(const std::string& symbolicName) 874 void Configuration::RemovePeer(const std::string& symbolicName)
850 { 875 {
851 boost::mutex::scoped_lock lock(globalMutex_); 876 boost::recursive_mutex::scoped_lock lock(globalMutex_);
852 877
853 if (!configuration_.isMember("OrthancPeers")) 878 if (!configuration_.isMember("OrthancPeers"))
854 { 879 {
855 LOG(ERROR) << "No peer with symbolic name: " << symbolicName; 880 LOG(ERROR) << "No peer with symbolic name: " << symbolicName;
856 throw OrthancException(ErrorCode_BadFileFormat); 881 throw OrthancException(ErrorCode_BadFileFormat);
978 } 1003 }
979 1004
980 1005
981 void Configuration::GetConfiguration(Json::Value& result) 1006 void Configuration::GetConfiguration(Json::Value& result)
982 { 1007 {
983 boost::mutex::scoped_lock lock(globalMutex_); 1008 boost::recursive_mutex::scoped_lock lock(globalMutex_);
984 result = configuration_; 1009 result = configuration_;
985 } 1010 }
986 1011
987 1012
988 void Configuration::FormatConfiguration(std::string& result) 1013 void Configuration::FormatConfiguration(std::string& result)