7 #include "fwRuntime/Runtime.hpp" 9 #include "fwRuntime/ConfigurationElement.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/IPlugin.hpp" 17 #include <boost/filesystem/operations.hpp> 28 #include <mach-o/dyld.h> 38 std::shared_ptr<Runtime> Runtime::m_instance;
46 char progPath[MAX_PATH];
47 if( GetModuleFileName(NULL, progPath, MAX_PATH) == 0 )
49 char progPath[PATH_MAX];
50 uint32_t size = PATH_MAX;
51 if( _NSGetExecutablePath(progPath, &size) != 0 )
53 char progPath[PATH_MAX];
54 ssize_t len = readlink(
"/proc/self/exe", progPath, PATH_MAX-1);
63 OSLM_WARN(
"Cannot guess the path of the executable, it's required to set the working directory. " 64 "Current working directory is used");
65 m_workingPath = ::boost::filesystem::current_path();
69 m_workingPath = progPath;
71 m_workingPath = m_workingPath.normalize().parent_path().parent_path();
85 m_bundles.insert( bundle );
86 std::for_each( bundle->extensionsBegin(), bundle->extensionsEnd(),
88 std::for_each( bundle->extensionPointsBegin(), bundle->extensionPointsEnd(),
90 std::for_each( bundle->executableFactoriesBegin(), bundle->executableFactoriesEnd(),
98 FwCoreNotUsedMacro(bundle);
107 using ::fwRuntime::io::BundleDescriptorReader;
108 const BundleDescriptorReader::BundleContainer bundles = BundleDescriptorReader::createBundles( repository );
109 std::for_each( bundles.begin(), bundles.end(), std::bind(&
Runtime::addBundle,
this, std::placeholders::_1) );
111 catch(
const std::exception& exception)
113 throw RuntimeException( std::string(
"Error while adding bundles. ") + exception.what() );
124 SLM_ASSERT(
"Default Bundles location not found: " + location.string(), ::boost::filesystem::exists(location));
135 return m_bundles.begin();
142 return m_bundles.end();
150 const std::string type( factory->getType() );
153 throw RuntimeException(type +
": type already used by an executable factory.");
156 m_executableFactories.insert( factory );
164 const std::string type( factory->getType() );
167 m_executableFactories.erase(factory);
174 std::shared_ptr< ExecutableFactory > resFactory;
175 for(
const ExecutableFactoryContainer::value_type&
factory : m_executableFactories)
191 const std::string identifier(extension->getIdentifier());
192 if( !identifier.empty() && this->
findExtension(identifier) != 0 )
194 throw RuntimeException(identifier +
": identifier already used by a registered extension.");
197 m_extensions.insert( extension );
205 const std::string identifier(extension->getIdentifier());
206 SLM_WARN_IF(
"Extension " + identifier +
" not found.",
207 !identifier.empty() && this->
findExtension(identifier) == 0 );
209 m_extensions.erase( extension );
216 return m_extensions.begin();
223 return m_extensions.end();
231 const std::string identifier(point->getIdentifier());
234 throw RuntimeException(identifier +
": identifier already used by a registered extension point.");
237 m_extensionPoints.insert(point);
245 const std::string identifier(point->getIdentifier());
246 SLM_WARN_IF(
"ExtensionPoint " + identifier +
" not found.",
249 m_extensionPoints.erase(point);
256 std::shared_ptr<Bundle> resBundle;
257 for(
const std::shared_ptr<Bundle>& bundle : m_bundles)
259 if(bundle->getIdentifier() == identifier && bundle->getVersion() == version)
272 std::shared_ptr<Bundle> resBundle;
273 for(
const std::shared_ptr<Bundle>& bundle : m_bundles)
275 if(bundle->getIdentifier() == identifier && bundle->getVersion() == version && bundle->isEnable())
288 if(m_instance.get() == 0)
290 m_instance = std::shared_ptr<Runtime>(
new Runtime());
292 return m_instance.get();
299 std::shared_ptr<Extension> resExtension;
300 for(
const ExtensionContainer::value_type& extension : m_extensions)
302 if(extension->getIdentifier() == identifier && extension->isEnable())
304 resExtension = extension;
315 std::shared_ptr<ExtensionPoint> resExtensionPoint;
316 for(
const ExtensionPointContainer::value_type& extensionPoint : m_extensionPoints)
318 if(extensionPoint->getIdentifier() == identifier && extensionPoint->isEnable())
320 resExtensionPoint = extensionPoint;
324 return resExtensionPoint;
331 std::shared_ptr< ExecutableFactory >
factory;
337 throw RuntimeException( type +
": no executable factory found for that type." );
341 IExecutable* result( factory->createExecutable() );
342 result->setBundle( factory->getBundle() );
351 ConfigurationElement::sptr configurationElement )
353 std::shared_ptr< ExecutableFactory >
factory;
364 configurationElement->getBundle()->start();
371 throw RuntimeException( type +
": no executable factory found for that type." );
378 factory->getBundle()->start();
379 result = factory->createExecutable();
381 result->
setBundle( factory->getBundle() );
384 catch(
const std::exception& e )
386 std::string message(
"Unable to create an executable instance. " );
397 m_workingPath = workingPath;
404 return m_workingPath;
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Defines the runtime class.
virtual void setInitializationData(const std::shared_ptr< ConfigurationElement > configuration) noexcept=0
Initializes the executable instance with the specified configuration element.
ExtensionContainer::iterator ExtensionIterator
Defines the extension container type.
FWRUNTIME_API void addExtension(std::shared_ptr< Extension > extension)
Registers a new extension.
FWRUNTIME_API ExtensionIterator extensionsEnd()
Retrieves the iterator on the end of the extension collection.
Defines the runtime exception class.
FWRUNTIME_API BundleIterator bundlesBegin()
Retrieves the iterator on the begining of the bundle collection.
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 IExecutable * createExecutableInstance(const std::string &type)
Create an instance of the given executable object type.
FWRUNTIME_API void unregisterExecutableFactory(std::shared_ptr< ExecutableFactory > factory)
Unregister a new executable factory instance to the runtime system.
FWRUNTIME_API std::shared_ptr< Extension > findExtension(const std::string &identifier) const
Retrieves the extension instance matching the specified identifier.
FWRUNTIME_API void setWorkingPath(const ::boost::filesystem::path &workingPath)
Set the working path where Bundles and share folder are located.
FWRUNTIME_API ExtensionIterator extensionsBegin()
Retrieves the iterator on the beginning of the extension collection.
FWRUNTIME_API void addBundle(std::shared_ptr< Bundle > bundle)
Adds a new bundle instance to the runtime system.
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.
FWRUNTIME_API BundleIterator bundlesEnd()
Retrieves the iterator on the end of the bundle collection.
Contains fwAtomsFilter::factory utilities.
FWRUNTIME_API std::shared_ptr< Bundle > findBundle(const std::string &identifier, const Version &version=Version()) const
Retrieves the bundle for the specified idenfier.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier) const
Retrieves the extension point instance matching the specified identifier.
FWRUNTIME_API std::shared_ptr< ExecutableFactory > findExecutableFactory(const std::string &type) const
Retrieves the executable factory for the given identifier.
#define OSLM_WARN(message)
~Runtime()
Destructor : does nothing.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWRUNTIME_API void addExecutableFactory(std::shared_ptr< ExecutableFactory > factory)
Adds a new executable factory instance to the runtime system.
virtual void setBundle(std::shared_ptr< Bundle > bundle)=0
Updates the bundle the executable originates from.
FWRUNTIME_API void addBundles(const ::boost::filesystem::path &repository)
Adds all bundle found in the given path.
BundleContainer::iterator BundleIterator
Defines the bundle container iterator type.
Holds version information for libraries and bundles.
FWRUNTIME_API void unregisterExtensionPoint(std::shared_ptr< ExtensionPoint > point)
Unregister a new extension point.
FWRUNTIME_API void addDefaultBundles()
Adds all bundle found at the default location.
FWRUNTIME_API void addExtensionPoint(std::shared_ptr< ExtensionPoint > point)
Registers a new extension point.
#define SLM_WARN_IF(message, cond)
FWRUNTIME_API void unregisterExtension(std::shared_ptr< Extension > extension)
Unregister a new extension.
FWRUNTIME_API::boost::filesystem::path getWorkingPath() const
Get the path where Bundles and share folder are located.