fw4spl
operations.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 "fwRuntime/operations.hpp"
8 
9 #include "fwRuntime/Bundle.hpp"
10 #include "fwRuntime/ConfigurationElement.hpp"
11 #include "fwRuntime/io/ProfileReader.hpp"
12 #include "fwRuntime/profile/Profile.hpp"
13 #include "fwRuntime/Runtime.hpp"
14 
15 #include <algorithm>
16 #include <vector>
17 
18 namespace fwRuntime
19 {
20 
21 namespace
22 {
23 
24 //------------------------------------------------------------------------------
25 
32 struct ConfigurationElementIdentifierPredicate
33 {
34  ConfigurationElementIdentifierPredicate( const std::string& identifier ) :
35  m_identifier( identifier )
36  {
37  }
38 
39  //------------------------------------------------------------------------------
40 
41  bool operator() ( std::shared_ptr< ConfigurationElement > element )
42  {
43  return element->getAttributeValue("id") == m_identifier;
44  }
45 
46  private:
47 
48  std::string m_identifier;
49 
50 };
51 
52 }
53 
54 //------------------------------------------------------------------------------
55 
56 ConfigurationElement::sptr findConfigurationElement( const std::string& identifier,
57  const std::string& pointIdentifier )
58 {
59  typedef std::vector< ConfigurationElement::sptr > ElementContainer;
60  ConfigurationElement::sptr resultConfig;
61  ElementContainer elements =
62  getAllConfigurationElementsForPoint< ElementContainer >( pointIdentifier );
63  ElementContainer::iterator foundElement = ::std::find_if( elements.begin(),
64  elements.end(),
65  ConfigurationElementIdentifierPredicate(identifier) );
66  if(foundElement != elements.end())
67  {
68  resultConfig = *foundElement;
69  }
70  return resultConfig;
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 std::shared_ptr< Extension > findExtension( const std::string& identifier )
76 {
78  return rntm->findExtension( identifier );
79 }
80 
81 //------------------------------------------------------------------------------
82 
83 std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string& identifier)
84 {
86  return rntm->findExtensionPoint( identifier );
87 }
88 
89 //------------------------------------------------------------------------------
90 
91 ::boost::filesystem::path getBundleResourcePath(const std::string& bundleIdentifier) noexcept
92 {
93  Runtime* rntm = Runtime::getDefault();
94  std::shared_ptr<Bundle> bundle = rntm->findBundle( bundleIdentifier );
95 
96  if(bundle == nullptr)
97  {
98  SLM_ERROR("Could not find bundle " + bundleIdentifier + "'");
99  return ::boost::filesystem::path();
100  }
101  return bundle->getResourcesLocation();
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 ::boost::filesystem::path getBundleResourceFilePath(const std::string& bundleIdentifier,
107  const ::boost::filesystem::path& path) noexcept
108 {
109  Runtime* rntm = Runtime::getDefault();
110  std::shared_ptr<Bundle> bundle = rntm->findBundle( bundleIdentifier );
111 
112  if(bundle == nullptr)
113  {
114  SLM_ERROR("Could not find bundle '" + bundleIdentifier + "'");
115  return ::boost::filesystem::path();
116  }
117  return getBundleResourcePath(bundle, path);
118 }
119 
120 //------------------------------------------------------------------------------
121 
122 ::boost::filesystem::path getBundleResourceFilePath(const ::boost::filesystem::path& path) noexcept
123 {
124  SLM_ASSERT("Path should be relative", path.is_relative());
125  const std::string bundleIdentifierAndVersion = path.begin()->string();
126 
127  // TEMP_FB: Change _ into - when version refactor is made
128  auto itVersionDelimiter = bundleIdentifierAndVersion.find(Bundle::s_VERSION_DELIMITER);
129  auto bundleIdentifier = bundleIdentifierAndVersion.substr(0, itVersionDelimiter);
130  auto bundleVersion = bundleIdentifierAndVersion.substr(itVersionDelimiter + 1);
131 
132  // Strip the bundle name
133  ::boost::filesystem::path pathWithoutBundle;
134  for(auto itPath = ++path.begin(); itPath != path.end(); itPath++)
135  {
136  pathWithoutBundle /= *itPath;
137  }
138 
139  try
140  {
141  Runtime* rntm = Runtime::getDefault();
142  Version version(bundleVersion);
143  std::shared_ptr<Bundle> bundle = rntm->findBundle( bundleIdentifier, version );
144 
145  if(bundle == nullptr)
146  {
147  SLM_ERROR("Could not find bundle '" + bundleIdentifier + "' with version '" + version.string() + "'");
148  return ::boost::filesystem::path();
149  }
150  return getBundleResourcePath(bundle, pathWithoutBundle );
151  }
152  catch(...)
153  {
154  SLM_ERROR("Error looking for bundle '" + bundleIdentifier + "' with version '" + bundleVersion + "'");
155  return ::boost::filesystem::path();
156  }
157 }
158 
159 //------------------------------------------------------------------------------
160 
161 ::boost::filesystem::path getLibraryResourceFilePath(const ::boost::filesystem::path& path) noexcept
162 {
163  // Currently the library resources are at the same location than bundles
164  // This might change in the future
165  Runtime* rntm = Runtime::getDefault();
166  return rntm->getWorkingPath() / BUNDLE_RC_PREFIX / path;
167 }
168 
169 //------------------------------------------------------------------------------
170 
171 ::boost::filesystem::path getResourceFilePath(const ::boost::filesystem::path& path) noexcept
172 {
173  auto file = ::fwRuntime::getBundleResourceFilePath(path);
174  if(file.empty())
175  {
176  // If not found in a bundle, look into libraries
177  file = ::fwRuntime::getLibraryResourceFilePath(path);
178  SLM_ERROR_IF("Resource '" + path.string() + "' has not been found in any bundle or library", file.empty());
179  }
180  return file;
181 }
182 
183 //------------------------------------------------------------------------------
184 
185 ::boost::filesystem::path getBundleResourcePath( std::shared_ptr<Bundle> bundle,
186  const ::boost::filesystem::path& path) noexcept
187 {
188  return bundle->getResourcesLocation() / path;
189 }
190 
191 //------------------------------------------------------------------------------
192 
193 ::boost::filesystem::path getBundleResourcePath( ConfigurationElement::sptr element,
194  const ::boost::filesystem::path& path) noexcept
195 {
196  return getBundleResourcePath(element->getBundle(), path);
197 }
198 
199 //------------------------------------------------------------------------------
200 
201 ::boost::filesystem::path getBundleResourcePath(const IExecutable* executable,
202  const ::boost::filesystem::path& path) noexcept
203 {
204  return getBundleResourcePath(executable->getBundle(), path);
205 }
206 
207 //------------------------------------------------------------------------------
208 
209 void addBundles( const ::boost::filesystem::path& directory)
210 {
211  Runtime* rntm = Runtime::getDefault();
212  rntm->addBundles( directory );
213 }
214 
215 //------------------------------------------------------------------------------
216 
217 ::fwRuntime::profile::Profile::sptr startProfile( const ::boost::filesystem::path& path )
218 {
219  try
220  {
221  ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::io::ProfileReader::createProfile(path);
222  profile->start();
223  return profile;
224  }
225  catch( const std::exception& exception )
226  {
227  throw RuntimeException( std::string(path.string() + ": invalid profile file. ") + exception.what() );
228  }
229 }
230 
231 //------------------------------------------------------------------------------
232 
233 std::shared_ptr< Bundle > findBundle( const std::string& identifier, const Version& version )
234 {
235  return Runtime::getDefault()->findBundle( identifier, version );
236 }
237 
238 //------------------------------------------------------------------------------
239 
240 void startBundle(const std::string& identifier)
241 {
242  Runtime* rntm = Runtime::getDefault();
243 
244  // Retrieves the specified bundle.
245  std::shared_ptr<Bundle> bundle = rntm->findBundle( identifier );
246  if( bundle == 0 )
247  {
248  throw RuntimeException(identifier + ": bundle not found.");
249  }
250  // Starts the found bundle.
251  bundle->start();
252 }
253 
254 //------------------------------------------------------------------------------
255 
256 } // namespace fwRuntime
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Definition: Runtime.cpp:286
Defines the runtime class.
Definition: Runtime.hpp:37
FWRUNTIME_API::boost::filesystem::path getLibraryResourceFilePath(const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a resource path whose first element is a library identifier...
Definition: operations.cpp:161
FWRUNTIME_API::boost::filesystem::path getResourceFilePath(const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a resource path whose first element is a library or a bundle ide...
Definition: operations.cpp:171
FWRUNTIME_API std::shared_ptr< ConfigurationElement > findConfigurationElement(const std::string &identifier, const std::string &pointIdentifier)
Retrieve the configuation element with the given identifier for the given extension point...
Definition: operations.cpp:56
Defines the runtime exception class.
FWRUNTIME_API std::shared_ptr< Extension > findExtension(const std::string &identifier) const
Retrieves the extension instance matching the specified identifier.
Definition: Runtime.cpp:297
Defines the base executable interface.An executable object is an instance created by an extension poi...
Definition: IExecutable.hpp:41
FWRUNTIME_API std::shared_ptr< Extension > findExtension(const std::string &identifier)
Retrieve the extension having the specified identifier.
Definition: operations.cpp:75
FWRUNTIME_API std::shared_ptr< Bundle > findBundle(const std::string &identifier, const Version &version=Version()) const
Retrieves the bundle for the specified idenfier.
Definition: Runtime.cpp:254
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.
Definition: Runtime.cpp:313
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier)
Retrieves the extension point having the specified identifier.
Definition: operations.cpp:83
FWRUNTIME_API void addBundles(const boost::filesystem::path &directory)
Loads all bundles that can be found in the specified directory.
FWRUNTIME_API void startBundle(const std::string &identifier)
Starts the bundle specified by the given identifier.
Definition: operations.cpp:240
#define SLM_ERROR_IF(message, cond)
Definition: spyLog.hpp:276
FWRUNTIME_API std::shared_ptr< ::fwRuntime::profile::Profile > startProfile(const boost::filesystem::path &path)
Starts the given bundle set profile.
#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
FWRUNTIME_API std::shared_ptr< Bundle > findBundle(const std::string &identifier, const Version &version=Version())
Retrieves the bundle with the given identifier and version.
Definition: operations.cpp:233
FWRUNTIME_API void addBundles(const ::boost::filesystem::path &repository)
Adds all bundle found in the given path.
Definition: Runtime.cpp:103
static FWRUNTIME_API std::shared_ptr< ::fwRuntime::profile::Profile > createProfile(const boost::filesystem::path &path)
Creates a profile from an xml file located at the given path.
FWRUNTIME_API::boost::filesystem::path getBundleResourceFilePath(const std::string &bundleIdentifier, const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a path relative to the bundle having the specified identifier...
Definition: operations.cpp:106
FWRUNTIME_API::boost::filesystem::path getBundleResourcePath(const std::string &bundleIdentifier) noexcept
Retrieve the filesystem valid path of resources of a bundle.
Definition: operations.cpp:91
Holds version information for libraries and bundles.
FWRUNTIME_API const std::string string() const
String converter.
FWRUNTIME_API::boost::filesystem::path getWorkingPath() const
Get the path where Bundles and share folder are located.
Definition: Runtime.cpp:402