changeset 1274:b9e2ed59cae4

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 21 Jan 2015 17:32:53 +0100
parents 88010d8e12cf
children 4287285709d1
files NEWS OrthancServer/OrthancInitialization.cpp OrthancServer/OrthancInitialization.h OrthancServer/main.cpp
diffstat 4 files changed, 116 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Mon Jan 19 16:08:58 2015 +0100
+++ b/NEWS	Wed Jan 21 17:32:53 2015 +0100
@@ -8,6 +8,7 @@
 * Instances without PatientID are now allowed
 * Support of Tudor DICOM in Query/Retrieve
 * Fix issue 25 (AET with underscore not allowed)
+* Code refactorings
 
 Plugins
 -------
--- a/OrthancServer/OrthancInitialization.cpp	Mon Jan 19 16:08:58 2015 +0100
+++ b/OrthancServer/OrthancInitialization.cpp	Wed Jan 21 17:32:53 2015 +0100
@@ -45,6 +45,9 @@
 #include <boost/thread.hpp>
 #include <glog/logging.h>
 
+#include "DatabaseWrapper.h"
+#include "../Core/FileStorage/FilesystemStorage.h"
+
 
 #if ORTHANC_JPEG_ENABLED == 1
 #include <dcmtk/dcmjpeg/djdecode.h>
@@ -654,4 +657,108 @@
   {
     return configurationAbsolutePath_;
   }
+
+
+  static IDatabaseWrapper* CreateSQLiteWrapper()
+  {
+    std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
+
+    // Open the database
+    boost::filesystem::path indexDirectory = Configuration::InterpretStringParameterAsPath(
+      Configuration::GetGlobalStringParameter("IndexDirectory", storageDirectoryStr));
+
+    LOG(WARNING) << "SQLite index directory: " << indexDirectory;
+
+    try
+    {
+      boost::filesystem::create_directories(indexDirectory);
+    }
+    catch (boost::filesystem::filesystem_error)
+    {
+    }
+
+    return new DatabaseWrapper(indexDirectory.string() + "/index");
+  }
+
+
+  namespace
+  {
+    // Anonymous namespace to avoid clashes between compilation modules
+
+    class FilesystemStorageWithoutDicom : public IStorageArea
+    {
+    private:
+      FilesystemStorage storage_;
+
+    public:
+      FilesystemStorageWithoutDicom(const std::string& path) : storage_(path)
+      {
+      }
+
+      virtual void Create(const std::string& uuid,
+                          const void* content, 
+                          size_t size,
+                          FileContentType type)
+      {
+        if (type != FileContentType_Dicom)
+        {
+          storage_.Create(uuid, content, size, type);
+        }
+      }
+
+      virtual void Read(std::string& content,
+                        const std::string& uuid,
+                        FileContentType type)
+      {
+        if (type != FileContentType_Dicom)
+        {
+          storage_.Read(content, uuid, type);
+        }
+        else
+        {
+          throw OrthancException(ErrorCode_UnknownResource);
+        }
+      }
+
+      virtual void Remove(const std::string& uuid,
+                          FileContentType type) 
+      {
+        if (type != FileContentType_Dicom)
+        {
+          storage_.Remove(uuid, type);
+        }
+      }
+    };
+  }
+
+
+  static IStorageArea* CreateFilesystemStorage()
+  {
+    std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
+
+    boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr);
+    LOG(WARNING) << "Storage directory: " << storageDirectory;
+
+    if (Configuration::GetGlobalBoolParameter("StoreDicom", true))
+    {
+      return new FilesystemStorage(storageDirectory.string());
+    }
+    else
+    {
+      LOG(WARNING) << "The DICOM files will not be stored, Orthanc running in index-only mode";
+      return new FilesystemStorageWithoutDicom(storageDirectory.string());
+    }
+  }
+
+
+  IDatabaseWrapper* Configuration::CreateDatabaseWrapper()
+  {
+    return CreateSQLiteWrapper();
+  }
+
+
+  IStorageArea* Configuration::CreateStorageArea()
+  {
+    return CreateFilesystemStorage();
+  }  
 }
--- a/OrthancServer/OrthancInitialization.h	Mon Jan 19 16:08:58 2015 +0100
+++ b/OrthancServer/OrthancInitialization.h	Wed Jan 21 17:32:53 2015 +0100
@@ -40,6 +40,8 @@
 #include "DicomProtocol/RemoteModalityParameters.h"
 #include "ServerEnumerations.h"
 #include "OrthancPeerParameters.h"
+#include "IDatabaseWrapper.h"
+#include "../Core/FileStorage/IStorageArea.h"
 
 namespace Orthanc
 {
@@ -102,5 +104,9 @@
     static void RemovePeer(const std::string& symbolicName);
 
     static const std::string& GetConfigurationAbsolutePath();
+
+    static IDatabaseWrapper* CreateDatabaseWrapper();
+
+    static IStorageArea* CreateStorageArea();
   };
 }
--- a/OrthancServer/main.cpp	Mon Jan 19 16:08:58 2015 +0100
+++ b/OrthancServer/main.cpp	Wed Jan 21 17:32:53 2015 +0100
@@ -38,7 +38,6 @@
 #include <boost/algorithm/string/predicate.hpp>
 
 #include "../Core/Uuid.h"
-#include "../Core/FileStorage/FilesystemStorage.h"
 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h"
 #include "../Core/HttpServer/FilesystemHttpHandler.h"
 #include "../Core/Lua/LuaFunctionCall.h"
@@ -50,7 +49,6 @@
 #include "OrthancFindRequestHandler.h"
 #include "OrthancMoveRequestHandler.h"
 #include "ServerToolbox.h"
-#include "DatabaseWrapper.h"
 #include "../Plugins/Engine/PluginsManager.h"
 #include "../Plugins/Engine/OrthancPlugins.h"
 
@@ -383,71 +381,10 @@
 
 
 
-class FilesystemStorageWithoutDicom : public IStorageArea
-{
-private:
-  FilesystemStorage storage_;
-
-public:
-  FilesystemStorageWithoutDicom(const std::string& path) : storage_(path)
-  {
-  }
-
-  virtual void Create(const std::string& uuid,
-                      const void* content, 
-                      size_t size,
-                      FileContentType type)
-  {
-    if (type != FileContentType_Dicom)
-    {
-      storage_.Create(uuid, content, size, type);
-    }
-  }
-
-  virtual void Read(std::string& content,
-                    const std::string& uuid,
-                    FileContentType type)
-  {
-    if (type != FileContentType_Dicom)
-    {
-      storage_.Read(content, uuid, type);
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-  }
-
-  virtual void Remove(const std::string& uuid,
-                      FileContentType type) 
-  {
-    if (type != FileContentType_Dicom)
-    {
-      storage_.Remove(uuid, type);
-    }
-  }
-};
-
-
 static bool StartOrthanc()
 {
-  std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
-
-
-  // Open the database
-  boost::filesystem::path indexDirectory = Configuration::InterpretStringParameterAsPath(
-    Configuration::GetGlobalStringParameter("IndexDirectory", storageDirectoryStr));
-
   std::auto_ptr<IDatabaseWrapper> database;
-  try
-  {
-    boost::filesystem::create_directories(indexDirectory);
-  }
-  catch (boost::filesystem::filesystem_error)
-  {
-  }
-
-  database.reset(new DatabaseWrapper(indexDirectory.string() + "/index"));
+  database.reset(Configuration::CreateDatabaseWrapper());
 
 
   // "storage" must be declared BEFORE "ServerContext context", to
@@ -456,8 +393,6 @@
 
   ServerContext context(*database);
 
-  LOG(WARNING) << "Index directory: " << indexDirectory;
-
   context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false));
   context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true));
 
@@ -553,17 +488,7 @@
     else
 #endif
     {
-      boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr);
-      LOG(WARNING) << "Storage directory: " << storageDirectory;
-      if (Configuration::GetGlobalBoolParameter("StoreDicom", true))
-      {
-        storage.reset(new FilesystemStorage(storageDirectory.string()));
-      }
-      else
-      {
-        LOG(WARNING) << "The DICOM files will not be stored, Orthanc running in index-only mode";
-        storage.reset(new FilesystemStorageWithoutDicom(storageDirectory.string()));
-      }
+      storage.reset(Configuration::CreateStorageArea());
     }
     
     context.SetStorageArea(*storage);