7 #include "fwServices/registry/AppConfig.hpp" 9 #include <fwData/Composite.hpp> 10 #include <fwData/String.hpp> 12 #include <fwRuntime/ConfigurationElement.hpp> 13 #include <fwRuntime/helper.hpp> 14 #include <fwRuntime/Runtime.hpp> 16 #include <boost/regex.hpp> 23 AppConfig::sptr AppConfig::s_currentAppConfig = AppConfig::New();
24 ::fwCore::mt::Mutex AppConfig::s_idMutex;
26 std::string AppConfig::s_mandatoryParameterIdentifier =
"@mandatory@";
28 AppConfig::UidDefinitionType AppConfig::s_uidDefinitionDictionary = { {
"object",
"uid" },
32 {
"connect",
"channel" }, };
33 static const ::boost::regex s_isVariable(
"\\${.*}.*" );
39 return s_currentAppConfig;
52 auto extensions = ::fwRuntime::getAllExtensionsForPoint(
"::fwServices::registry::AppConfig");
53 for(
const auto& ext : extensions )
56 std::string configId = ext->findConfigurationElement(
"id")->getValue();
59 std::string group =
"";
60 if ( ext->hasConfigurationElement(
"group") )
62 group = ext->findConfigurationElement(
"group")->getValue();
65 std::string desc =
"No description available";
66 if ( ext->hasConfigurationElement(
"desc") )
68 desc = ext->findConfigurationElement(
"desc")->getValue();
70 unsigned long syntax = 1;
71 if ( ext->hasConfigurationElement(
"syntax") )
73 syntax = std::stoul( ext->findConfigurationElement(
"syntax")->getValue() );
76 AppInfo::ParametersType parameters;
77 if ( ext->hasConfigurationElement(
"parameters") )
79 ::fwRuntime::ConfigurationElement::csptr parametersConfig = ext->findConfigurationElement(
"parameters");
81 for( ::fwRuntime::ConfigurationElement::sptr paramConfig : elements )
83 std::string name = paramConfig->getExistingAttributeValue(
"name");
85 if(paramConfig->hasAttribute(
"default"))
87 parameters[name] = paramConfig->getAttributeValue(
"default");
91 parameters[name] = s_mandatoryParameterIdentifier;
98 ::fwRuntime::ConfigurationElement::csptr config = ext->findConfigurationElement(
"config");
101 std::shared_ptr< ::fwRuntime::Bundle> bundle = ext->getBundle();
102 std::string bundleId = bundle->getIdentifier();
103 std::string bundleVersion = bundle->getVersion().string();
106 this->
addAppInfo( configId, group, desc, parameters, config, bundleId, bundleVersion );
113 const std::string& group,
114 const std::string& desc,
115 const AppInfo::ParametersType& parameters,
116 const ::fwRuntime::ConfigurationElement::csptr& config,
117 const std::string& bundleId,
118 const std::string& bundleVersion)
122 SLM_DEBUG(
"New app config registering : configId = " + configId );
123 SLM_ASSERT(
"The app config with the id = "<< configId <<
" already exist.",
m_reg.find( configId ) ==
m_reg.end() );
125 AppInfo::sptr info = AppInfo::New();
128 info->config = config;
129 info->parameters = parameters;
130 info->bundleId = bundleId;
131 info->bundleVersion = bundleVersion;
132 m_reg[configId] = info;
152 const std::string& configId,
154 bool autoPrefixId)
const 158 Registry::const_iterator iter =
m_reg.find( configId );
159 SLM_ASSERT(
"The id " << configId <<
" is not found in the application configuration registry",
160 iter !=
m_reg.end());
163 ::fwRuntime::ConfigurationElement::sptr newConfig;
166 AppInfo::ParametersType parameters = iter->second->parameters;
168 for( AppInfo::ParametersType::value_type param : parameters )
170 FieldAdaptorType::const_iterator iter = fieldAdaptors.find( param.first );
171 const std::string key =
"\\$\\{" + param.first +
"\\}";
172 if ( iter != fieldAdaptors.end() )
174 fields[key] = iter->second;
176 else if ( param.second != s_mandatoryParameterIdentifier)
178 fields[key] = param.second;
182 FW_RAISE(
"Parameter : '" << param.first <<
"' is needed by the app configuration id='"<< configId <<
"'.");
186 std::string autoPrefixName;
192 UidParameterReplaceType parameterReplaceAdaptors;
193 this->collectUIDForParameterReplace(iter->second->config, parameterReplaceAdaptors);
194 newConfig = this->adaptConfig(iter->second->config, fields, parameterReplaceAdaptors, autoPrefixName);
202 ::fwData::Composite::csptr replaceFields,
214 Registry::const_iterator iter =
m_reg.find( _configId );
215 SLM_ASSERT(
"The id " << _configId <<
" is not found in the application configuration registry",
216 iter !=
m_reg.end());
218 std::shared_ptr< ::fwRuntime::Bundle > bundle = ::fwRuntime::findBundle(iter->second->bundleId,
219 iter->second->bundleVersion);
229 std::vector< std::string > ids;
230 for(
const Registry::value_type& elem :
m_reg )
232 ids.push_back( elem.first );
242 std::vector< std::string > ids;
243 for(
const Registry::value_type& elem :
m_reg )
245 AppInfo::sptr info = elem.second;
246 if(info->group == group)
248 ids.push_back( elem.first );
256 FieldAdaptorType AppConfig::compositeToFieldAdaptor( ::fwData::Composite::csptr fieldAdaptors )
const 259 for(const ::fwData::Composite::value_type& elem : * fieldAdaptors )
261 fields[elem.first] = ::fwData::String::dynamicCast( elem.second )->value();
271 ::fwCore::mt::ScopedLock lock(s_idMutex);
272 static unsigned int srvCpt = 1;
273 std::stringstream sstr;
275 if ( serviceUid.empty() )
277 sstr <<
"AppConfigManager_" << srvCpt;
281 sstr << serviceUid <<
"_" << srvCpt;
289 void AppConfig::collectUIDForParameterReplace(::fwRuntime::ConfigurationElement::csptr _cfgElem,
290 UidParameterReplaceType& _replaceMap)
292 const auto& name = _cfgElem->getName();
293 for(
const auto& attribute : _cfgElem->getAttributes() )
295 auto range = s_uidDefinitionDictionary.equal_range(name);
297 for (
auto it = range.first; it != range.second; ++it)
299 if(it->second == attribute.first && !::boost::regex_match(attribute.second, s_isVariable ) )
301 _replaceMap.insert(attribute.second);
306 for (
const auto& subElem : _cfgElem->getElements())
308 collectUIDForParameterReplace( subElem, _replaceMap );
314 ::fwRuntime::EConfigurationElement::sptr AppConfig::adaptConfig(::fwRuntime::ConfigurationElement::csptr _cfgElem,
316 const UidParameterReplaceType& _uidParameterReplace,
317 const std::string& _autoPrefixId)
319 ::fwRuntime::EConfigurationElement::sptr result = ::fwRuntime::EConfigurationElement::New( _cfgElem->getName() );
320 result->setValue( adaptField( _cfgElem->getValue(), _fieldAdaptors ) );
322 for(
const auto& attribute : _cfgElem->getAttributes() )
325 if(!_autoPrefixId.empty())
327 if(attribute.first ==
"uid" ||
328 attribute.first ==
"sid" ||
329 attribute.first ==
"wid" ||
330 attribute.first ==
"channel" )
333 if ( !::boost::regex_match( attribute.second, s_isVariable ) )
336 result->setAttributeValue( attribute.first,
337 _autoPrefixId +
"_" + adaptField( attribute.second, _fieldAdaptors ));
342 else if(attribute.first ==
"by")
345 if ( !::boost::regex_match( attribute.second, s_isVariable ) )
348 auto itParam = _uidParameterReplace.find(attribute.second);
349 if(itParam != _uidParameterReplace.end())
351 result->setAttributeValue( attribute.first,
352 _autoPrefixId +
"_" +
353 adaptField( attribute.second, _fieldAdaptors ));
360 result->setAttributeValue( attribute.first, adaptField( attribute.second, _fieldAdaptors ) );
363 for (
const auto& subElem : _cfgElem->getElements())
366 if( !_autoPrefixId.empty() && (subElem->getName() ==
"signal" || subElem->getName() ==
"slot" ) )
369 if ( !::boost::regex_match( subElem->getValue(), s_isVariable ) )
372 auto elt = ::fwRuntime::EConfigurationElement::New( subElem->getName() );
373 elt->setValue( _autoPrefixId +
"_" + subElem->getValue() );
375 for (
const auto& attr : subElem->getAttributes())
377 elt->setAttributeValue(attr.first, attr.second);
380 result->addConfigurationElement( elt );
385 result->addConfigurationElement( adaptConfig( subElem, _fieldAdaptors, _uidParameterReplace, _autoPrefixId ) );
393 std::string AppConfig::adaptField(
const std::string& _str,
const FieldAdaptorType& _variablesMap )
395 std::string newStr = _str;
401 if ( ::boost::regex_search(_str, s_isVariable ) )
404 for(
const auto& fieldAdaptor : _variablesMap )
406 const ::boost::regex varRegex(
"(.*)" + fieldAdaptor.first +
"(.*)" );
407 if ( ::boost::regex_match( _str, varRegex ) )
409 const std::string varReplace(
"\\1" + fieldAdaptor.second +
"\\2");
410 newStr = ::boost::regex_replace( newStr, varRegex, varReplace,
411 ::boost::match_default | ::boost::format_sed );
Contains fwAtomsFilter::registry details.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
std::vector< std::shared_ptr< ConfigurationElement > > Container
Defines the configuration element container type.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
FWSERVICES_API AppConfig()
Constructor.
FWSERVICES_API void addAppInfo(const std::string &configId, const std::string &group, const std::string &desc, const AppInfo::ParametersType ¶meters, const ::fwRuntime::ConfigurationElement::csptr &config, const std::string &bundleId, const std::string &bundleVersion)
Register a new config.
#define SLM_DEBUG(message)
FWSERVICES_API std::vector< std::string > getAllConfigs() const
Return all configurations ( standard and template ) register in the registry.
virtual FWSERVICES_API ~AppConfig()
Destructor.
Registry m_reg
Container of <configId, AppConfig information>
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWSERVICES_API void clearRegistry()
Clear the registry.
FWSERVICES_API std::shared_ptr< ::fwRuntime::Bundle > getBundle(const std::string &_configId)
Retrieves the bunble from the config id.
FWSERVICES_API void parseBundleInformation()
Parses bundle information to retrieve configuration declaration.
static FWSERVICES_API AppConfig::sptr getDefault()
Return an instance of AppConfig.
static FWSERVICES_API std::string getUniqueIdentifier(const std::string &serviceUid="")
Create an unique identifier.
std::map< std::string, std::string > FieldAdaptorType
Associations of <pattern, value>.
FWSERVICES_API std::vector< std::string > getConfigsFromGroup(const std::string &group) const
Return all configurations with specified group.
FWSERVICES_API::fwRuntime::ConfigurationElement::csptr getAdaptedTemplateConfig(const std::string &configId, const FieldAdaptorType replaceFields, bool autoPrefixId) const
Return the adapted config with the identifier configId.