7 #include "fwRuntime/Bundle.hpp" 9 #include "fwRuntime/EmptyPlugin.hpp" 10 #include "fwRuntime/ExecutableFactory.hpp" 11 #include "fwRuntime/Extension.hpp" 12 #include "fwRuntime/ExtensionPoint.hpp" 13 #include "fwRuntime/IExecutable.hpp" 14 #include "fwRuntime/io/BundleDescriptorReader.hpp" 15 #include "fwRuntime/profile/Initializer.hpp" 16 #include "fwRuntime/profile/Profile.hpp" 17 #include "fwRuntime/profile/Stopper.hpp" 18 #include "fwRuntime/Runtime.hpp" 19 #include "fwRuntime/utils/GenericExecutableFactory.hpp" 21 #include <fwCore/base.hpp> 33 SPTR( Bundle ) Bundle::m_loadingBundle;
39 return m_loadingBundle;
45 const std::string&
id,
46 const std::string& version ) :
47 Bundle(location, id, version,
"")
54 const std::string&
id,
55 const std::string& version,
56 const std::string& c ) :
57 m_resourcesLocation( location.lexically_normal() ),
63 SLM_ASSERT(
"Invalid bundle location.", m_resourcesLocation.is_complete() == true );
66 std::string strLocation = location.string();
67 const std::string strRCPrefix = BUNDLE_RC_PREFIX;
68 const auto itBundle = strLocation.find(strRCPrefix);
69 if(itBundle != std::string::npos)
71 strLocation.replace(itBundle, strRCPrefix.length(), std::string(BUNDLE_LIB_PREFIX));
73 m_libraryLocation = ::boost::filesystem::path(strLocation);
80 m_executableFactories.insert( factory );
87 return m_executableFactories.begin();
94 return m_executableFactories.end();
101 std::shared_ptr<ExecutableFactory> resExecutableFactory;
102 for(
const ExecutableFactoryContainer::value_type&
factory : m_executableFactories)
106 resExecutableFactory =
factory;
110 return resExecutableFactory;
117 m_extensions.insert( extension );
125 for(
const ExtensionContainer::value_type& extension : m_extensions)
127 if(extension->getIdentifier() == identifier)
140 for(
const ExtensionContainer::value_type& extension : m_extensions)
142 if(extension->getIdentifier() == identifier)
144 extension->setEnable(enable);
154 return m_extensions.begin();
161 return m_extensions.end();
167 m_extensionPoints.insert( extensionPoint );
174 std::shared_ptr<ExtensionPoint> resExtensionPoint;
175 for(
const ExtensionPointContainer::value_type& extensionPoint : m_extensionPoints)
177 if(extensionPoint->getIdentifier() == identifier && extensionPoint->isEnable())
179 resExtensionPoint = extensionPoint;
183 return resExtensionPoint;
191 for(
const ExtensionPointContainer::value_type& extensionPoint : m_extensionPoints)
193 if(extensionPoint->getIdentifier() == identifier)
195 hasExtensionPoint =
true;
206 for(
const ExtensionPointContainer::value_type& extensionPoint : m_extensionPoints)
208 if(extensionPoint->getIdentifier() == identifier)
210 extensionPoint->setEnable(enable);
220 return m_extensionPoints.begin();
227 return m_extensionPoints.end();
235 m_libraries.insert(library);
242 return m_libraries.begin();
249 return m_libraries.end();
256 m_requirements.insert(requirement);
277 return m_libraryLocation;
284 return m_resourcesLocation;
303 void Bundle::loadLibraries()
306 if( m_enable ==
false )
308 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": bundle is not enabled." );
312 SLM_ASSERT(
"Bundle is already loaded", m_loadingBundle == 0 );
317 m_loadingBundle = shared_from_this();
320 for(
const LibraryContainer::value_type& library : m_libraries)
322 if(library->isLoaded() ==
false)
328 catch( std::exception& e )
332 message +=
"Unable to load module ";
333 message += library->getPath().string();
338 m_loadingBundle.reset();
346 m_loadingBundle.reset();
349 assert( m_loadingBundle == 0 );
350 SLM_TRACE(
"Library " + getBundleStr(m_identifier, m_version) +
" loaded");
355 void Bundle::loadRequirements()
360 RequirementContainer::const_iterator iter;
361 for(
const RequirementContainer::value_type& requirement : m_requirements)
368 throw RuntimeException( requirement +
": required bundle not found or not enabled." );
371 if ( !bundle->isStarted() )
377 catch(
const std::exception& e )
381 message +=
"Bundle " + getBundleStr(m_identifier, m_version) +
" was not able to load requirements. ";
391 SLM_ASSERT(
"Bundle " + getBundleStr(m_identifier, m_version) +
" already started.",
393 if( m_enable ==
false )
395 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": bundle is not enabled." );
398 if( m_plugin ==
nullptr )
405 SLM_TRACE(getBundleStr(m_identifier, m_version) +
" Started");
407 catch( std::exception& e )
410 ": start plugin error (after load requirement) :" + e.what() );
417 void Bundle::startPlugin()
419 SLM_ASSERT(
"Bundle " + getBundleStr(m_identifier, m_version) +
" plugin is already started.",
422 const std::string pluginType(
getClass() );
428 if( pluginType.empty() )
437 plugin = std::dynamic_pointer_cast<
IPlugin >( executable );
443 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": unable to create a plugin instance." );
446 SLM_TRACE(
"Starting " + getBundleStr(m_identifier, m_version) +
" Bundle's plugin.");
450 SLM_TRACE(
"Register stopper for " + getBundleStr(m_identifier, m_version) +
" Bundle's plugin.");
451 ::fwRuntime::profile::getCurrentProfile()->add(
455 ::fwRuntime::profile::getCurrentProfile()->add(
459 catch( std::exception& e )
461 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": start plugin error : " + e.what() );
469 SLM_ASSERT(
"Bundle "+ getBundleStr(m_identifier, m_version) +
" not started.", m_started );
470 SLM_ASSERT(getBundleStr(m_identifier, m_version) +
" : m_plugin not an intance.", m_plugin !=
nullptr );
471 SLM_ASSERT(
"Bundle " + getBundleStr(m_identifier, m_version) +
" not uninitialized.", !m_initialized );
473 SLM_TRACE(
"Stopping " + getBundleStr(m_identifier, m_version) +
" Bundle's plugin.");
478 OSLM_TRACE(getBundleStr(m_identifier, m_version) <<
" Stopped");
480 catch( std::exception& e )
482 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": stop plugin error : " + e.what() );
503 SLM_ASSERT(
"Bundle '" + getBundleStr(m_identifier, m_version) +
"' not started.", m_started );
504 SLM_ASSERT(
"Bundle '"+ getBundleStr(m_identifier, m_version) +
"' already initialized.", !m_initialized );
507 m_initialized =
true;
508 SLM_TRACE(
"Initializing " + getBundleStr(m_identifier, m_version) +
" ...");
509 m_plugin->initialize();
510 SLM_TRACE(
" " + getBundleStr(m_identifier, m_version) +
" Initialized");
512 catch( std::exception& e )
514 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": initialize plugin error : " + e.what() );
520 void Bundle::uninitialize()
522 SLM_ASSERT(
"Bundle '"+ getBundleStr(m_identifier, m_version) +
"' has not been started.",
523 m_plugin !=
nullptr);
524 SLM_ASSERT(
"Bundle '"+ getBundleStr(m_identifier, m_version) +
"' not initialized.", m_initialized );
528 m_plugin->uninitialize();
529 m_initialized =
false;
532 catch( std::exception& e )
534 throw RuntimeException( getBundleStr(m_identifier, m_version) +
": initialize plugin error : " + e.what() );
556 m_parameters[identifier] = value;
563 ParameterContainer::const_iterator found = m_parameters.find(identifier);
565 return (found != m_parameters.end()) ? found->second : std::string();
572 return (m_parameters.find(identifier) != m_parameters.end());
577 std::string fwRuntime::Bundle::getBundleStr(
const std::string& identifier,
const fwRuntime::Version& version)
579 return identifier + s_VERSION_DELIMITER + version.
string();
584 void Bundle::operator= (
const Bundle& )
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Defines the runtime class.
FWRUNTIME_API void setEnable(const bool state)
Changes the enable state of the bundle.
void addExtension(std::shared_ptr< Extension > extension)
Adds the specified extension to the bundle.
FWRUNTIME_API void initialize()
Initialize the bundle.
FWRUNTIME_API void addParameter(const std::string &identifier, const std::string &value)
Adds a parameter to the bundle.
FWRUNTIME_API bool isEnable() const
Tells if the bundle is enable.
Defines the extension class.
Defines the abstract executable factory class.
FWRUNTIME_API void start()
Starts the bundle.
FWRUNTIME_API void setEnableExtension(const std::string &identifier, const bool enable)
Search a specific extension in the bundle to enable or disable it.
Defines the runtime exception class.
FWRUNTIME_API std::shared_ptr< Bundle > findEnabledBundle(const std::string &identifier, const Version &version=Version()) const
Retrieves the enabled bundle for the specified idenfier.
FWRUNTIME_API void addExecutableFactory(std::shared_ptr< ExecutableFactory > factory)
Adds an executable factory instance to the bundle.
FWRUNTIME_API ExtensionConstIterator extensionsBegin() const
Retrieves the iterator on the first item in the managed extension collection.
FWRUNTIME_API IExecutable * createExecutableInstance(const std::string &type)
Create an instance of the given executable object type.
FWRUNTIME_API ExecutableFactoryConstIterator executableFactoriesBegin() const
Retrieves the iterator on the first item in the managed executable factory collection.
Defines the extension point class.
Defines the base executable interface.An executable object is an instance created by an extension poi...
FWRUNTIME_API void unregisterBundle(std::shared_ptr< Bundle > bundle)
Unregister a bundle instance to the runtime system.
#define OSLM_TRACE(message)
Contains fwAtomsFilter::factory utilities.
FWRUNTIME_API const Version & getVersion() const
Retrieves the version of the bundle.
FWRUNTIME_API ExecutableFactoryConstIterator executableFactoriesEnd() const
Retrieves the iterator on the ending item in the managed executable factory collection.
FWRUNTIME_API std::shared_ptr< ExecutableFactory > findExecutableFactory(const std::string &type) const
Retrieves the executable factory instance for the specified type name.
FWRUNTIME_API void stop()
Tells if the bundle is enable.
FWRUNTIME_API LibraryConstIterator librariesBegin() const
Retrieves the iterator on the first item in the managed dynamic library collection.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
#define SLM_ERROR(message)
FWRUNTIME_API const std::string getClass() const
Retrieves the class representing the bundle executable part.
FWRUNTIME_API ExtensionPointConstIterator extensionPointsEnd() const
Retrieves the iterator on the ending item in the managed extension point collection.
FWRUNTIME_API bool hasParameter(const std::string &name) const
Tells if a parameter exists.
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier) const
Retrieves the extension point for the given identifier.
LibraryContainer::const_iterator LibraryConstIterator
Defines the dynamic library container.
Implements a default plugin for bundles that don't provide a fwRuntime::IPlugin interface implementat...
FWRUNTIME_API ExtensionConstIterator extensionsEnd() const
Retrieves the iterator on the ending item in the managed extension collection.
Defines the plugin interface.
FWRUNTIME_APIconst::boost::filesystem::path & getResourcesLocation() const
Retrieves the bundle location.
Defines the module class.This class is only a bridge to a native module implementor.
FWRUNTIME_API const std::string & getIdentifier() const
Retrieves the bundle identifier.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
ExtensionContainer::const_iterator ExtensionConstIterator
Defines the extension container.
FWRUNTIME_API void addRequirement(const std::string &requirement)
Adds a requirement to the bundle.
ExecutableFactoryContainer::const_iterator ExecutableFactoryConstIterator
Defines the executable.
FWRUNTIME_API ExtensionPointConstIterator extensionPointsBegin() const
Retrieves the iterator on the first item in the managed extension point collection.
#define SLM_TRACE(message)
FWRUNTIME_API bool hasExtensionPoint(const std::string &identifier) const
Tells if a specific extension point exists.
void setBundle(const ::fwRuntime::Bundle *bundle) noexcept
Sets the bundle the library is attached to.
FWRUNTIME_API void setEnableExtensionPoint(const std::string &identifier, const bool enable)
Search a specific extension point in the bundle to enable or disable it.
Defines the bundle class.
FWRUNTIME_API const std::string getParameterValue(const std::string &identifier) const
Retrieves the value of the given parameter.
FWRUNTIME_API void addLibrary(std::shared_ptr< dl::Library > library)
Adds the specified library to the bundle.
Bundle(const ::boost::filesystem::path &location, const std::string &id, const std::string &version)
Constructor.
Holds version information for libraries and bundles.
ExtensionPointContainer::const_iterator ExtensionPointConstIterator
Defines the extension point.
FWRUNTIME_API const std::string string() const
String converter.
FWRUNTIME_API bool hasExtension(const std::string &identifier) const
Tells if an specific extension exists.
FWRUNTIME_APIconst::boost::filesystem::path & getLibraryLocation() const
Retrieves the bundle location.
void addExtensionPoint(std::shared_ptr< ExtensionPoint > extension)
Adds the specified extension point to the bundle.
FWRUNTIME_API std::shared_ptr< IPlugin > getPlugin() const
Retrieves the plugin instance for the specified bundle identifier.
FWRUNTIME_API LibraryConstIterator librariesEnd() const
Retrieves the iterator on the ending item in the managed dynamic library collection.