changeset 1434:f9cd40166269

refactoring of OrthancPlugins, improvement in ServeFolders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2015 16:04:05 +0200
parents 461e7554bff7
children 6406f5493d92
files OrthancServer/OrthancRestApi/OrthancRestSystem.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/main.cpp Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 7 files changed, 127 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestSystem.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -132,7 +132,7 @@
     if (OrthancRestApi::GetContext(call).HasPlugins())
     {
       std::list<std::string> plugins;
-      OrthancRestApi::GetContext(call).GetPluginsManager().ListPlugins(plugins);
+      OrthancRestApi::GetContext(call).GetPlugins().GetManager().ListPlugins(plugins);
 
       for (std::list<std::string>::const_iterator 
              it = plugins.begin(); it != plugins.end(); ++it)
@@ -152,7 +152,7 @@
       return;
     }
 
-    const PluginsManager& manager = OrthancRestApi::GetContext(call).GetPluginsManager();
+    const PluginsManager& manager = OrthancRestApi::GetContext(call).GetPlugins().GetManager();
     std::string id = call.GetUriComponent("id", "");
 
     if (manager.HasPlugin(id))
@@ -161,7 +161,7 @@
       v["ID"] = id;
       v["Version"] = manager.GetPluginVersion(id);
 
-      const OrthancPlugins& plugins = OrthancRestApi::GetContext(call).GetOrthancPlugins();
+      const OrthancPlugins& plugins = OrthancRestApi::GetContext(call).GetPlugins();
       const char *c = plugins.GetProperty(id.c_str(), _OrthancPluginProperty_RootUri);
       if (c != NULL)
       {
@@ -188,11 +188,11 @@
 
     if (OrthancRestApi::GetContext(call).HasPlugins())
     {
-      const PluginsManager& manager = OrthancRestApi::GetContext(call).GetPluginsManager();
-      const OrthancPlugins& plugins = OrthancRestApi::GetContext(call).GetOrthancPlugins();
+      const OrthancPlugins& plugins = OrthancRestApi::GetContext(call).GetPlugins();
+      const PluginsManager& manager = plugins.GetManager();
 
       std::list<std::string> lst;
-      OrthancRestApi::GetContext(call).GetPluginsManager().ListPlugins(lst);
+      manager.ListPlugins(lst);
 
       for (std::list<std::string>::const_iterator
              it = lst.begin(); it != lst.end(); ++it)
--- a/OrthancServer/ServerContext.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/OrthancServer/ServerContext.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -75,7 +75,6 @@
     scheduler_(Configuration::GetGlobalIntegerParameter("LimitJobs", 10)),
     lua_(*this),
     plugins_(NULL),
-    pluginsManager_(NULL),
     queryRetrieveArchive_(Configuration::GetGlobalIntegerParameter("QueryRetrieveSize", 10)),
     defaultLocalAet_(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"))
   {
@@ -376,26 +375,34 @@
   }
 
 
-  bool ServerContext::HasPlugins() const
+  void ServerContext::SetPlugins(OrthancPlugins& plugins)
   {
-    return (pluginsManager_ && plugins_);
+    plugins_ = &plugins;
+
+    // TODO REFACTOR THIS
+    listeners_.clear();
+    listeners_.push_back(ServerListener(lua_, "Lua"));
+    listeners_.push_back(ServerListener(plugins, "plugin"));
   }
 
 
-  const PluginsManager& ServerContext::GetPluginsManager() const
+  void ServerContext::ResetPlugins()
   {
-    if (HasPlugins())
-    {
-      return *pluginsManager_;
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_InternalError);
-    }
+    plugins_ = NULL;
+
+    // TODO REFACTOR THIS
+    listeners_.clear();
+    listeners_.push_back(ServerListener(lua_, "Lua"));
   }
 
 
-  const OrthancPlugins& ServerContext::GetOrthancPlugins() const
+  bool ServerContext::HasPlugins() const
+  {
+    return (plugins_ != NULL);
+  }
+
+
+  const OrthancPlugins& ServerContext::GetPlugins() const
   {
     if (HasPlugins())
     {
--- a/OrthancServer/ServerContext.h	Tue Jun 30 15:09:34 2015 +0200
+++ b/OrthancServer/ServerContext.h	Tue Jun 30 16:04:05 2015 +0200
@@ -39,7 +39,6 @@
 #include "../Core/Lua/LuaContext.h"
 #include "../Core/RestApi/RestApiOutput.h"
 #include "../Plugins/Engine/OrthancPlugins.h"
-#include "../Plugins/Engine/PluginsManager.h"
 #include "DicomInstanceToStore.h"
 #include "DicomProtocol/ReusableDicomUserConnection.h"
 #include "IServerListener.h"
@@ -114,7 +113,6 @@
     LuaScripting lua_;
     OrthancPlugins* plugins_;
     ServerListeners listeners_;
-    const PluginsManager* pluginsManager_;
 
     SharedArchive  queryRetrieveArchive_;
     std::string defaultLocalAet_;
@@ -199,36 +197,12 @@
       return scheduler_;
     }
 
-    void SetOrthancPlugins(const PluginsManager& manager,
-                           OrthancPlugins& plugins)
-    {
-      pluginsManager_ = &manager;
-      plugins_ = &plugins;
-      listeners_.clear();
-      listeners_.push_back(ServerListener(lua_, "Lua"));  // TODO REFACTOR THIS
-      listeners_.push_back(ServerListener(plugins, "plugin"));  // TODO REFACTOR THIS
-    }
-
-    void ResetOrthancPlugins()
-    {
-      pluginsManager_ = NULL;
-      plugins_ = NULL;
-      listeners_.clear();
-      listeners_.push_back(ServerListener(lua_, "Lua"));  // TODO REFACTOR THIS
-    }
-
     bool DeleteResource(Json::Value& target,
                         const std::string& uuid,
                         ResourceType expectedType);
 
     void SignalChange(const ServerIndexChange& change);
 
-    bool HasPlugins() const;
-
-    const PluginsManager& GetPluginsManager() const;
-
-    const OrthancPlugins& GetOrthancPlugins() const;
-
     SharedArchive& GetQueryRetrieveArchive()
     {
       return queryRetrieveArchive_;
@@ -243,5 +217,19 @@
     {
       return lua_;
     }
+
+
+    /**
+     * Management of the plugins
+     **/
+
+    void SetPlugins(OrthancPlugins& plugins);
+
+    void ResetPlugins();
+
+    bool HasPlugins() const;
+
+    const OrthancPlugins& GetPlugins() const;
+
   };
 }
--- a/OrthancServer/main.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/OrthancServer/main.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -49,7 +49,6 @@
 #include "OrthancFindRequestHandler.h"
 #include "OrthancMoveRequestHandler.h"
 #include "ServerToolbox.h"
-#include "../Plugins/Engine/PluginsManager.h"
 #include "../Plugins/Engine/OrthancPlugins.h"
 #include "FromDcmtkBridge.h"
 
@@ -369,16 +368,16 @@
 }
 
 
-static void LoadPlugins(PluginsManager& pluginsManager)
+static void LoadPlugins(OrthancPlugins& plugins)
 {
-  std::list<std::string> plugins;
-  Configuration::GetGlobalListOfStringsParameter(plugins, "Plugins");
+  std::list<std::string> path;
+  Configuration::GetGlobalListOfStringsParameter(path, "Plugins");
   for (std::list<std::string>::const_iterator
-         it = plugins.begin(); it != plugins.end(); ++it)
+         it = path.begin(); it != path.end(); ++it)
   {
     std::string path = Configuration::InterpretStringParameterAsPath(*it);
     LOG(WARNING) << "Loading plugin(s) from: " << path;
-    pluginsManager.RegisterPlugin(path);
+    plugins.GetManager().RegisterPlugin(path);
   }  
 }
 
@@ -389,11 +388,9 @@
 static bool StartOrthanc(int argc, char *argv[])
 {
 #if ENABLE_PLUGINS == 1
-  OrthancPlugins orthancPlugins;
-  orthancPlugins.SetCommandLineArguments(argc, argv);
-  PluginsManager pluginsManager;
-  pluginsManager.RegisterServiceProvider(orthancPlugins);
-  LoadPlugins(pluginsManager);
+  OrthancPlugins plugins;
+  plugins.SetCommandLineArguments(argc, argv);
+  LoadPlugins(plugins);
 #endif
 
   // "storage" and "database" must be declared BEFORE "ServerContext
@@ -402,9 +399,9 @@
   std::auto_ptr<IStorageArea>  storage;
   std::auto_ptr<ServerContext> context;
 
-  if (orthancPlugins.HasDatabase())
+  if (plugins.HasDatabase())
   {
-    context.reset(new ServerContext(orthancPlugins.GetDatabase()));
+    context.reset(new ServerContext(plugins.GetDatabase()));
   }
   else
   {
@@ -483,9 +480,9 @@
 #endif
 
 #if ENABLE_PLUGINS == 1
-    orthancPlugins.SetServerContext(*context);
-    httpServer.RegisterHandler(orthancPlugins);
-    context->SetOrthancPlugins(pluginsManager, orthancPlugins);
+    plugins.SetServerContext(*context);
+    httpServer.RegisterHandler(plugins);
+    context->SetPlugins(plugins);
 #endif
 
     httpServer.RegisterHandler(staticResources);
@@ -494,10 +491,10 @@
 
 #if ENABLE_PLUGINS == 1
     // Prepare the storage area
-    if (orthancPlugins.HasStorageArea())
+    if (plugins.HasStorageArea())
     {
       LOG(WARNING) << "Using a custom storage area from plugins";
-      storage.reset(orthancPlugins.GetStorageArea());
+      storage.reset(plugins.GetStorageArea());
     }
     else
 #endif
@@ -512,7 +509,7 @@
     if (Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
     {
 #if ENABLE_PLUGINS == 1
-      orthancPlugins.SetOrthancRestApi(restApi);
+      plugins.SetOrthancRestApi(restApi);
 #endif
 
       httpServer.Start();
@@ -547,9 +544,9 @@
     LOG(WARNING) << "Orthanc is stopping";
 
 #if ENABLE_PLUGINS == 1
-    context->ResetOrthancPlugins();
-    orthancPlugins.Stop();
-    orthancPlugins.ResetOrthancRestApi();
+    context->ResetPlugins();
+    plugins.Stop();
+    plugins.ResetOrthancRestApi();
     LOG(WARNING) << "    Plugins have stopped";
 #endif
 
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -182,6 +182,7 @@
     typedef std::list<OrthancPluginOnChangeCallback>  OnChangeCallbacks;
     typedef std::map<Property, std::string>  Properties;
 
+    PluginsManager manager_;
     ServerContext* context_;
     RestCallbacks restCallbacks_;
     OrthancRestApi* restApi_;
@@ -252,6 +253,7 @@
   OrthancPlugins::OrthancPlugins()
   {
     pimpl_.reset(new PImpl());
+    pimpl_->manager_.RegisterServiceProvider(*this);
     pimpl_->changeThread_ = boost::thread(PImpl::ChangeThread, pimpl_.get());
   }
 
@@ -1431,4 +1433,16 @@
     pimpl_->argc_ = argc;
     pimpl_->argv_ = argv;
   }
+
+
+  PluginsManager& OrthancPlugins::GetManager()
+  {
+    return pimpl_->manager_;
+  }
+
+
+  const PluginsManager& OrthancPlugins::GetManager() const
+  {
+    return pimpl_->manager_;
+  }
 }
--- a/Plugins/Engine/OrthancPlugins.h	Tue Jun 30 15:09:34 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.h	Tue Jun 30 16:04:05 2015 +0200
@@ -141,5 +141,9 @@
                             _OrthancPluginProperty property) const;
 
     void SetCommandLineArguments(int argc, char* argv[]);
+
+    PluginsManager& GetManager();
+
+    const PluginsManager& GetManager() const;
   };
 }
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Tue Jun 30 15:09:34 2015 +0200
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Tue Jun 30 16:04:05 2015 +0200
@@ -76,6 +76,10 @@
   {
     return "image/jpeg";
   }
+  else if (extension == ".woff")
+  {
+    return "application/x-font-woff";
+  }
   else
   {
     std::string s = "Unknown MIME type for extension: " + extension;
@@ -154,7 +158,6 @@
   Json::Reader reader;
   if (reader.parse(s, configuration))
   {
-    std::cout << configuration.toStyledString();
     return true;
   }
   else
@@ -217,15 +220,25 @@
     return 0;
   }
 
-  std::string s = "<html><body><h1>Additional folders served by Orthanc</h1><ul>\n";
+  std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n";
 
-  for (std::map<std::string, std::string>::const_iterator
-         it = folders_.begin(); it != folders_.end(); ++it)
+  if (folders_.empty())
+  {
+    s += "<p>Empty section <tt>ServeFolders</tt> in your configuration file: No additional folder is served.</p>\n";
+  }
+  else
   {
-    s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n";
+    s += "<ul>\n";
+    for (std::map<std::string, std::string>::const_iterator
+           it = folders_.begin(); it != folders_.end(); ++it)
+    {
+      s += "<li><a href=\"" + it->first + "/index.html\">" + it->first + "</li>\n";
+    }
+    
+    s += "</ul>\n";
   }
 
-  s += "</ul></body></html>";
+  s += "</body></html>\n";
 
   OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
 
@@ -261,40 +274,47 @@
       return -1;
     }
 
-    if (configuration.isMember("ServeFolders") &&
-        configuration["ServeFolders"].type() == Json::objectValue)
+    if (configuration.isMember("ServeFolders"))
     {
-      Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
-
-      // Register the callback for each base URI
-      for (Json::Value::Members::const_iterator 
-             it = members.begin(); it != members.end(); ++it)
+      if (configuration["ServeFolders"].type() != Json::objectValue)
       {
-        const std::string& baseUri = *it;
-        const std::string path = configuration["ServeFolders"][*it].asString();
-        const std::string regex = "(" + baseUri + ")/(.*)";
-
-        if (baseUri.empty() ||
-            *baseUri.rbegin() == '/')
-        {
-          std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it;
-          OrthancPluginLogWarning(context_, message.c_str());
-          return -1;
-        }
-
-        OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
-        folders_[baseUri] = path;
+        OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)");
+        return -1;
       }
-
-      OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback);
-      OrthancPluginSetRootUri(context, INDEX_URI);
     }
     else
     {
       OrthancPluginLogWarning(context_, "No section \"ServeFolders\" in your configuration file: "
                               "No additional folder will be served!");
+      configuration["ServeFolders"] = Json::objectValue;
     }
 
+
+    Json::Value::Members members = configuration["ServeFolders"].getMemberNames();
+
+    // Register the callback for each base URI
+    for (Json::Value::Members::const_iterator 
+           it = members.begin(); it != members.end(); ++it)
+    {
+      const std::string& baseUri = *it;
+      const std::string path = configuration["ServeFolders"][*it].asString();
+      const std::string regex = "(" + baseUri + ")/(.*)";
+
+      if (baseUri.empty() ||
+          *baseUri.rbegin() == '/')
+      {
+        std::string message = "The URI of a folder to be server cannot be empty or end with a '/': " + *it;
+        OrthancPluginLogWarning(context_, message.c_str());
+        return -1;
+      }
+
+      OrthancPluginRegisterRestCallback(context, regex.c_str(), FolderCallback);
+      folders_[baseUri] = path;
+    }
+
+    OrthancPluginRegisterRestCallback(context, INDEX_URI, IndexCallback);
+    OrthancPluginSetRootUri(context, INDEX_URI);
+
     return 0;
   }