fw4spl
ServiceConfig.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwServices/registry/ServiceConfig.hpp"
8 
9 #include <fwRuntime/ConfigurationElement.hpp>
10 #include <fwRuntime/helper.hpp>
11 #include <fwRuntime/Runtime.hpp>
12 
13 namespace fwServices
14 {
15 namespace registry
16 {
17 
18 const std::string ServiceConfig::CONFIG_EXT_POINT = "::fwServices::registry::ServiceConfig";
19 
20 ServiceConfig::sptr ServiceConfig::s_currentServiceConfig = ServiceConfig::New();
21 
22 //-----------------------------------------------------------------------------
23 
24 ServiceConfig::sptr ServiceConfig::getDefault()
25 {
27 }
28 
29 //-----------------------------------------------------------------------------
30 
32 {
33 }
34 
35 //-----------------------------------------------------------------------------
36 
38 {
39  typedef std::shared_ptr< ::fwRuntime::Extension > ExtensionType;
40 
41  std::vector< ExtensionType > extElements;
42  extElements = ::fwRuntime::getAllExtensionsForPoint(CONFIG_EXT_POINT);
43  for(ExtensionType ext : extElements)
44  {
45  // Get id
46  SLM_ASSERT("Missing id element", ext->hasConfigurationElement("id"));
47  std::string id = ext->findConfigurationElement("id")->getValue();
48 
49  // Get service
50  std::string service = "";
51  if ( ext->hasConfigurationElement("service") )
52  {
53  service = ext->findConfigurationElement("service")->getValue();
54  }
55 
56  // Get desc
57  std::string desc = "No description available";
58  if ( ext->hasConfigurationElement("desc") )
59  {
60  desc = ext->findConfigurationElement("desc")->getValue();
61  }
62 
63  // Get config
64  ::fwRuntime::ConfigurationElement::csptr config = ext->findConfigurationElement("config");
65 
66  // Add service config info
67  this->addServiceConfigInfo(id, service, desc, config);
68  }
69 }
70 
71 //-----------------------------------------------------------------------------
72 
74  ( const std::string& configId,
75  const std::string& service,
76  const std::string& desc,
77  ::fwRuntime::ConfigurationElement::csptr config)
78 {
80 
81  OSLM_DEBUG( "New service config registering : "
82  << " configId = " << configId
83  << " service = " << service
84  << " desc = " << desc
85  );
86 
87  SLM_ASSERT("The service config with the id "<< configId <<" already exists.",
88  m_reg.find( configId ) == m_reg.end() );
89 
90  ServiceConfigInfo::sptr info = ServiceConfigInfo::New();
91  info->service = service;
92  info->desc = desc;
93  info->config = config;
94  m_reg[configId] = info;
95 }
96 
97 //-----------------------------------------------------------------------------
98 
100 {
101 }
102 
103 //-----------------------------------------------------------------------------
104 
106 {
108  m_reg.clear();
109 }
110 
111 //-----------------------------------------------------------------------------
112 
113 ::fwRuntime::ConfigurationElement::csptr ServiceConfig::getServiceConfig( const std::string& configId,
114  const std::string& serviceImpl ) const
115 {
117  Registry::const_iterator iter = m_reg.find( configId );
118  SLM_ASSERT("The id " << configId << " is not found in the application configuration registry",
119  iter != m_reg.end());
120  SLM_ASSERT("The id " << configId << " is not allowed for this service " << serviceImpl,
121  serviceImpl.empty() || iter->second->service.empty() || iter->second->service == serviceImpl);
122  return iter->second->config;
123 }
124 
125 //-----------------------------------------------------------------------------
126 
127 const std::string& ServiceConfig::getConfigDesc( const std::string& configId ) const
128 {
130  Registry::const_iterator iter = m_reg.find( configId );
131  SLM_ASSERT("The id " << configId << " is not found in the application configuration registry",
132  iter != m_reg.end());
133  return iter->second->desc;
134 }
135 
136 //-----------------------------------------------------------------------------
137 
138 std::vector< std::string > ServiceConfig::getAllConfigForService( std::string serviceImpl, bool matchingOnly ) const
139 {
141  std::vector< std::string > configs;
142 
143  for(Registry::value_type srvCfg : m_reg)
144  {
145  ServiceConfigInfo::sptr info = srvCfg.second;
146  if ( (info->service.empty() && !matchingOnly ) || info->service == serviceImpl)
147  {
148  configs.push_back(srvCfg.first);
149  }
150  }
151 
152  return configs;
153 }
154 
155 //-----------------------------------------------------------------------------
156 
157 } // namespace registry
158 
159 } // namespace fwServices
Contains fwAtomsFilter::registry details.
FWSERVICES_API::fwRuntime::ConfigurationElement::csptr getServiceConfig(const std::string &configId, const std::string &serviceImpl="") const
Returns the configuration with the given id for the service with the given implementation.
FWSERVICES_API void clearRegistry()
Clear the registry.
FWSERVICES_API void addServiceConfigInfo(const std::string &configId, const std::string &service, const std::string &desc,::fwRuntime::ConfigurationElement::csptr config)
Register a new service configuration.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
mutable::fwCore::mt::ReadWriteMutex m_registryMutex
Used to protect the registry access.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
FWSERVICES_API std::vector< std::string > getAllConfigForService(std::string serviceImpl, bool matchingOnly=false) const
Returns a vector containing the names of the available config for the service with the given implemen...
Registry m_reg
Container of service information <configId, service config information>
FWSERVICES_API void parseBundleInformation()
Parses bundle information to retrieve service declaration.
FWSERVICES_API const std::string & getConfigDesc(const std::string &configId) const
Returns the description of the given configuration name.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWSERVICES_API ServiceConfig()
Constructor.
static ServiceConfig::sptr s_currentServiceConfig
The global instance of the service config.
static FWSERVICES_API ServiceConfig::sptr getDefault()
Return the default global instance of ServiceConfig.
virtual FWSERVICES_API ~ServiceConfig()
Destructor.
#define OSLM_DEBUG(message)
Definition: spyLog.hpp:241