ctkPluginContext.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKPLUGINCONTEXT_H_
  16. #define CTKPLUGINCONTEXT_H_
  17. #include <QHash>
  18. #include <QString>
  19. #include <QVariant>
  20. #include <QUrl>
  21. #include "ctkPluginFramework_global.h"
  22. #include "ctkPluginEvent.h"
  23. #include "CTKPluginFrameworkExport.h"
  24. // CTK class forward declarations
  25. class ctkPlugin;
  26. class ctkPluginPrivate;
  27. class ctkServiceRegistration;
  28. class ctkServiceReference;
  29. class ctkPluginContextPrivate;
  30. /**
  31. * A plugin's execution context within the Framework. The context is used to
  32. * grant access to other methods so that this plugin can interact with the
  33. * Framework.
  34. *
  35. * <p>
  36. * <code>ctkPluginContext</code> methods allow a plugin to:
  37. * <ul>
  38. * <li>Subscribe to events published by the Framework.
  39. * <li>Register service objects with the Framework service registry.
  40. * <li>Retrieve <code>ServiceReferences</code> from the Framework service
  41. * registry.
  42. * <li>Get and release service objects for a referenced service.
  43. * <li>Install new plugins in the Framework.
  44. * <li>Get the list of plugins installed in the Framework.
  45. * <li>Get the {@link ctkPlugin} object for a plugin.
  46. * <li>Create <code>QFile</code> objects for files in a persistent storage
  47. * area provided for the plugin by the Framework.
  48. * </ul>
  49. *
  50. * <p>
  51. * A <code>ctkPluginContext</code> object will be created and provided to the
  52. * plugin associated with this context when it is started using the
  53. * {@link ctkPluginActivator::start} method. The same <code>ctkPluginContext</code>
  54. * object will be passed to the plugin associated with this context when it is
  55. * stopped using the {@link ctkPluginActivator::stop} method. A
  56. * <code>ctkPluginContext</code> object is generally for the private use of its
  57. * associated plugin and is not meant to be shared with other plugins in the
  58. * plugin environment.
  59. *
  60. * <p>
  61. * The <code>ctkPlugin</code> object associated with a <code>ctkPluginContext</code>
  62. * object is called the <em>context plugin</em>.
  63. *
  64. * <p>
  65. * The <code>ctkPluginContext</code> object is only valid during the execution of
  66. * its context plugin; that is, during the period from when the context plugin
  67. * is in the <code>STARTING</code>, <code>STOPPING</code>, and
  68. * <code>ACTIVE</code> plugin states. If the <code>ctkPluginContext</code>
  69. * object is used subsequently, a <code>std::logic_error</code> must be
  70. * thrown. The <code>ctkPluginContext</code> object must never be reused after
  71. * its context plugin is stopped.
  72. *
  73. * <p>
  74. * The Framework is the only entity that can create <code>ctkPluginContext</code>
  75. * objects and they are only valid within the Framework that created them.
  76. *
  77. * @threadsafe
  78. */
  79. class CTK_PLUGINFW_EXPORT ctkPluginContext
  80. {
  81. Q_DECLARE_PRIVATE(ctkPluginContext)
  82. public:
  83. ~ctkPluginContext();
  84. /**
  85. * Returns the value of the specified property. If the key is not found in
  86. * the Framework properties, the system properties are then searched. The
  87. * method returns an invalid QVariant if the property is not found.
  88. *
  89. * @param key The name of the requested property.
  90. * @return The value of the requested property, or an invalid QVariant if
  91. * the property is undefined.
  92. */
  93. QVariant getProperty(const QString& key) const;
  94. /**
  95. * Returns the <code>ctkPlugin</code> object associated with this
  96. * <code>ctkPluginContext</code>. This plugin is called the context plugin.
  97. *
  98. * @return The <code>ctkPlugin</code> object associated with this
  99. * <code>ctkPluginContext</code>.
  100. * @throws std::logic_error If this ctkPluginContext is no
  101. * longer valid.
  102. */
  103. ctkPlugin* getPlugin() const;
  104. /**
  105. * Returns the plugin with the specified identifier.
  106. *
  107. * @param id The identifier of the plugin to retrieve.
  108. * @return A <code>ctkPlugin</code> object or <code>0</code> if the
  109. * identifier does not match any installed plugin.
  110. */
  111. ctkPlugin* getPlugin(long id) const;
  112. /**
  113. * Returns a list of all installed plugins.
  114. * <p>
  115. * This method returns a list of all plugins installed in the plugin
  116. * environment at the time of the call to this method. However, since the
  117. * Framework is a very dynamic environment, plugins can be installed or
  118. * uninstalled at anytime.
  119. *
  120. * @return A QList of <code>ctkPlugin</code> objects, one object per
  121. * installed plugin.
  122. */
  123. QList<ctkPlugin*> getPlugins() const;
  124. /**
  125. * Registers the specified service object with the specified properties
  126. * under the specified class names into the Framework. A
  127. * <code>ctkServiceRegistration</code> object is returned. The
  128. * <code>ctkServiceRegistration</code> object is for the private use of the
  129. * plugin registering the service and should not be shared with other
  130. * plugins. The registering plugin is defined to be the context plugin.
  131. * Other plugins can locate the service by using either the
  132. * {@link #getServiceReferences} or {@link #getServiceReference} method.
  133. *
  134. * <p>
  135. * A plugin can register a service object that implements the
  136. * {@link ctkServiceFactory} interface to have more flexibility in providing
  137. * service objects to other plugins.
  138. *
  139. * <p>
  140. * The following steps are required to register a service:
  141. * <ol>
  142. * <li>If <code>service</code> is not a <code>ctkServiceFactory</code>, an
  143. * <code>std::invalid_argument</code> is thrown if <code>service</code>
  144. * is not an instance of all the specified class names.
  145. * <li>The Framework adds the following service properties to the service
  146. * properties from the specified <code>ServiceProperties</code> (which may be
  147. * omitted): <br/>
  148. * A property named {@link ctkPluginConstants#SERVICE_ID} identifying the
  149. * registration number of the service <br/>
  150. * A property named {@link ctkPluginConstants#OBJECTCLASS} containing all the
  151. * specified classes. <br/>
  152. * Properties with these names in the specified <code>ServiceProperties</code> will
  153. * be ignored.
  154. * <li>The service is added to the Framework service registry and may now be
  155. * used by other plugins.
  156. * <li>A service event of type {@link ServiceEvent#REGISTERED} is fired.
  157. * <li>A <code>ctkServiceRegistration</code> object for this registration is
  158. * returned.
  159. * </ol>
  160. *
  161. * @param clazzes The class names under which the service can be located.
  162. * The class names will be stored in the service's
  163. * properties under the key {@link ctkPluginConstants#OBJECTCLASS}.
  164. * @param service The service object or a <code>ctkServiceFactory</code>
  165. * object.
  166. * @param properties The properties for this service. The keys in the
  167. * properties object must all be <code>QString</code> objects. See
  168. * {@link ctkPluginConstants} for a list of standard service property keys.
  169. * Changes should not be made to this object after calling this
  170. * method. To update the service's properties the
  171. * {@link ctkServiceRegistration::setProperties} method must be called.
  172. * The set of properties may be omitted if the service has
  173. * no properties.
  174. * @return A <code>ctkServiceRegistration</code> object for use by the plugin
  175. * registering the service to update the service's properties or to
  176. * unregister the service.
  177. * @throws std::invalid_argument If one of the following is true:
  178. * <ul>
  179. * <li><code>service</code> is <code>0</code>. <li><code>service
  180. * </code> is not a <code>ctkServiceFactory</code> object and is not an
  181. * instance of all the named classes in <code>clazzes</code>. <li>
  182. * <code>properties</code> contains case variants of the same key
  183. * name.
  184. * </ul>
  185. * @throws std::logic_error If this ctkPluginContext is no longer valid.
  186. * @see ctkServiceRegistration
  187. * @see ctkServiceFactory
  188. */
  189. ctkServiceRegistration* registerService(const QStringList& clazzes, QObject* service, const ServiceProperties& properties = ServiceProperties());
  190. /**
  191. * Returns a list of <code>ctkServiceReference</code> objects. The returned
  192. * list contains services that
  193. * were registered under the specified class and match the specified filter
  194. * expression.
  195. *
  196. * <p>
  197. * The list is valid at the time of the call to this method. However since
  198. * the Framework is a very dynamic environment, services can be modified or
  199. * unregistered at any time.
  200. *
  201. * <p>
  202. * The specified <code>filter</code> expression is used to select the
  203. * registered services whose service properties contain keys and values
  204. * which satisfy the filter expression. See {@link Filter} for a description
  205. * of the filter syntax. If the specified <code>filter</code> is
  206. * empty, all registered services are considered to match the
  207. * filter. If the specified <code>filter</code> expression cannot be parsed,
  208. * an {@link std::invalid_argument} will be thrown with a human readable
  209. * message where the filter became unparsable.
  210. *
  211. * <p>
  212. * The result is a list of <code>ctkServiceReference</code> objects for all
  213. * services that meet all of the following conditions:
  214. * <ul>
  215. * <li>If the specified class name, <code>clazz</code>, is not
  216. * empty, the service must have been registered with the
  217. * specified class name. The complete list of class names with which a
  218. * service was registered is available from the service's
  219. * {@link PlugincConstants::OBJECTCLASS objectClass} property.
  220. * <li>If the specified <code>filter</code> is not empty, the
  221. * filter expression must match the service.
  222. * </ul>
  223. *
  224. * @param clazz The class name with which the service was registered or
  225. * an empty string for all services.
  226. * @param filter The filter expression or empty for all
  227. * services.
  228. * @return A list of <code>ctkServiceReference</code> objects or
  229. * an empty list if no services are registered which satisfy the
  230. * search.
  231. * @throws std::invalid_argument If the specified <code>filter</code>
  232. * contains an invalid filter expression that cannot be parsed.
  233. * @throws std::logic_error If this ctkPluginContext is no longer valid.
  234. */
  235. QList<ctkServiceReference> getServiceReferences(const QString& clazz, const QString& filter = QString());
  236. /**
  237. * Returns a <code>ctkServiceReference</code> object for a service that
  238. * implements and was registered under the specified class.
  239. *
  240. * <p>
  241. * The returned <code>ctkServiceReference</code> object is valid at the time of
  242. * the call to this method. However as the Framework is a very dynamic
  243. * environment, services can be modified or unregistered at any time.
  244. *
  245. * <p>
  246. * This method is the same as calling
  247. * {@link ctkPluginContext::getServiceReferences(const QString&, const QString&)} with an
  248. * empty filter expression. It is provided as a convenience for
  249. * when the caller is interested in any service that implements the
  250. * specified class.
  251. * <p>
  252. * If multiple such services exist, the service with the highest ranking (as
  253. * specified in its {@link ctkPluginConstants::SERVICE_RANKING} property) is returned.
  254. * <p>
  255. * If there is a tie in ranking, the service with the lowest service ID (as
  256. * specified in its {@link ctkPluginConstants::SERVICE_ID} property); that is, the
  257. * service that was registered first is returned.
  258. *
  259. * @param clazz The class name with which the service was registered.
  260. * @return A <code>ctkServiceReference</code> object, or <code>0</code> if
  261. * no services are registered which implement the named class.
  262. * @throws std::logic_error If this ctkPluginContext is no longer valid.
  263. * @throws ctkServiceException It no service was registered under the given class name.
  264. * @see #getServiceReferences(const QString&, const QString&)
  265. */
  266. ctkServiceReference getServiceReference(const QString& clazz);
  267. /**
  268. * Returns the service object referenced by the specified
  269. * <code>ctkServiceReference</code> object.
  270. * <p>
  271. * A plugin's use of a service is tracked by the plugin's use count of that
  272. * service. Each time a service's service object is returned by
  273. * {@link #getService(ctkServiceReference*)} the context plugin's use count for
  274. * that service is incremented by one. Each time the service is released by
  275. * {@link #ungetService(ctkServiceReference*)} the context plugin's use count
  276. * for that service is decremented by one.
  277. * <p>
  278. * When a plugin's use count for a service drops to zero, the plugin should
  279. * no longer use that service.
  280. *
  281. * <p>
  282. * This method will always return <code>0</code> when the service
  283. * associated with this <code>reference</code> has been unregistered.
  284. *
  285. * <p>
  286. * The following steps are required to get the service object:
  287. * <ol>
  288. * <li>If the service has been unregistered, <code>0</code> is returned.
  289. * <li>The context plugin's use count for this service is incremented by
  290. * one.
  291. * <li>If the context plugin's use count for the service is currently one
  292. * and the service was registered with an object implementing the
  293. * <code>ctkServiceFactory</code> interface, the
  294. * {@link ctkServiceFactory::getService(ctkPlugin*, ctkServiceRegistration*)} method is
  295. * called to create a service object for the context plugin. This service
  296. * object is cached by the Framework. While the context plugin's use count
  297. * for the service is greater than zero, subsequent calls to get the
  298. * services's service object for the context plugin will return the cached
  299. * service object. <br>
  300. * If the service object returned by the <code>ctkServiceFactory</code> object
  301. * is not an instance of all the classes named when the service
  302. * was registered or the <code>ctkServiceFactory</code> object throws an
  303. * exception, <code>0</code> is returned and a Framework event of type
  304. * {@link ctkPluginFrameworkEvent::ERROR} containing a {@link ctkServiceException}
  305. * describing the error is fired.
  306. * <li>The service object for the service is returned.
  307. * </ol>
  308. *
  309. * @param reference A reference to the service.
  310. * @return A service object for the service associated with
  311. * <code>reference</code> or <code>0</code> if the service is not
  312. * registered, the service object returned by a
  313. * <code>ctkServiceFactory</code> does not implement the classes under
  314. * which it was registered or the <code>ctkServiceFactory</code> threw
  315. * an exception.
  316. * @throws std::logic_error If this ctkPluginContext is no
  317. * longer valid.
  318. * @throws std::invalid_argument If the specified
  319. * <code>ctkServiceReference</code> was not created by the same
  320. * framework instance as this <code>ctkPluginContext</code>.
  321. * @see #ungetService(ctkServiceReference*)
  322. * @see ctkServiceFactory
  323. */
  324. QObject* getService(ctkServiceReference reference);
  325. /**
  326. * Releases the service object referenced by the specified
  327. * <code>ctkServiceReference</code> object. If the context plugin's use count
  328. * for the service is zero, this method returns <code>false</code>.
  329. * Otherwise, the context plugins's use count for the service is decremented
  330. * by one.
  331. *
  332. * <p>
  333. * The service's service object should no longer be used and all references
  334. * to it should be destroyed when a bundle's use count for the service drops
  335. * to zero.
  336. *
  337. * <p>
  338. * The following steps are required to unget the service object:
  339. * <ol>
  340. * <li>If the context plugin's use count for the service is zero or the
  341. * service has been unregistered, <code>false</code> is returned.
  342. * <li>The context plugin's use count for this service is decremented by
  343. * one.
  344. * <li>If the context plugin's use count for the service is currently zero
  345. * and the service was registered with a <code>ctkServiceFactory</code> object,
  346. * the
  347. * {@link ctkServiceFactory#ungetService(ctkPlugin*, ctkServiceRegistration*, QObject*)}
  348. * method is called to release the service object for the context plugin.
  349. * <li><code>true</code> is returned.
  350. * </ol>
  351. *
  352. * @param reference A reference to the service to be released.
  353. * @return <code>false</code> if the context plugin's use count for the
  354. * service is zero or if the service has been unregistered;
  355. * <code>true</code> otherwise.
  356. * @throws std::logic_error If this ctkPluginContext is no
  357. * longer valid.
  358. * @throws std::invalid_argument If the specified
  359. * <code>ctkServiceReference</code> was not created by the same
  360. * framework instance as this <code>ctkPluginContext</code>.
  361. * @see #getService
  362. * @see ctkServiceFactory
  363. */
  364. bool ungetService(const ctkServiceReference& reference);
  365. /**
  366. * Installs a plugin from the specified <code>QIODevice</code> object.
  367. *
  368. * <p>
  369. * If the specified <code>QIODevice</code> is <code>null</code>, the
  370. * Framework must create the <code>QIODevice</code> from which to read the
  371. * plugin by interpreting, in an implementation dependent manner, the
  372. * specified <code>location</code>.
  373. *
  374. * <p>
  375. * The specified <code>location</code> identifier will be used as the
  376. * identity of the plugin. Every installed plugin is uniquely identified by
  377. * its location identifier which is typically in the form of a URL.
  378. *
  379. * <p>
  380. * The following steps are required to install a plugin:
  381. * <ol>
  382. * <li>If a plugin containing the same location identifier is already
  383. * installed, the <code>ctkPlugin</code> object for that plugin is returned.
  384. *
  385. * <li>The plugin's content is read from the input stream. If this fails, a
  386. * {@link ctkPluginException} is thrown.
  387. *
  388. * <li>The plugin's associated resources are allocated. The associated
  389. * resources minimally consist of a unique identifier and a persistent
  390. * storage area. If this step fails, a <code>ctkPluginException</code>
  391. * is thrown.
  392. *
  393. * <li>The plugin's state is set to <code>INSTALLED</code>.
  394. *
  395. * <li>A plugin event of type {@link ctkPluginEvent#INSTALLED} is fired.
  396. *
  397. * <li>The <code>ctkPlugin</code> object for the newly or previously installed
  398. * plugin is returned.
  399. * </ol>
  400. *
  401. * <b>Postconditions, no exceptions thrown </b>
  402. * <ul>
  403. * <li><code>getState()</code> in &#x007B; <code>INSTALLED</code>,
  404. * <code>RESOLVED</code> &#x007D;.
  405. * <li>Plugin has a unique ID.
  406. * </ul>
  407. * <b>Postconditions, when an exception is thrown </b>
  408. * <ul>
  409. * <li>Plugin is not installed and no trace of the plugin exists.
  410. * </ul>
  411. *
  412. * @param location The location identifier of the plugin to install.
  413. * @param input The <code>QIODevice</code> object from which this plugin
  414. * will be read or <code>null</code> to indicate the Framework must
  415. * create the I/O device from the specified location identifier.
  416. * The I/O device must always be closed when this method completes,
  417. * even if an exception is thrown.
  418. * @return The <code>ctkPlugin</code> object of the installed plugin.
  419. * @throws ctkPluginException If the I/O device cannot be read or the
  420. * installation failed.
  421. * @throws std::logic_error If this ctkPluginContext is no longer valid.
  422. */
  423. ctkPlugin* installPlugin(const QUrl& location, QIODevice* in = 0);
  424. /**
  425. * Connects the specified <code>slot</code> to the context
  426. * plugins's signal which is emitted when a plugin has
  427. * a lifecycle state change. The signature of the slot
  428. * must be "slotName(ctkPluginEvent)".
  429. *
  430. * @param receiver The object to connect to.
  431. * @param slot The slot to be connected.
  432. * @param type The Qt connection type.
  433. * @throws std::logic_error If this ctkPluginContext is no
  434. * longer valid.
  435. * @see ctkPluginEvent
  436. * @see ctkEventBus
  437. */
  438. bool connectPluginListener(const QObject* receiver, const char* slot, Qt::ConnectionType type = Qt::QueuedConnection);
  439. /**
  440. * Connects the specified <code>slot</code> to the context
  441. * plugin's signal which emits general Framework events. The signature
  442. * of the slot must be "slotName(ctkPluginFrameworkEvent)".
  443. *
  444. * @param receiver The object to connect to.
  445. * @param slot The slot to be connected.
  446. * @param type The Qt connection type.
  447. * @throws std::logic_error If this ctkPluginContext is no
  448. * longer valid.
  449. * @see ctkPluginFrameworkEvent
  450. * @see ctkEventBus
  451. */
  452. bool connectFrameworkListener(const QObject* receiver, const char* slot, Qt::ConnectionType type = Qt::QueuedConnection);
  453. /**
  454. * Connects the specified <code>slot</code> with the
  455. * specified <code>filter</code> to the context plugins's signal emitting
  456. * service events when a service has a lifecycle state change. The signature
  457. * of the slot must be "slotName(const ctkServiceEvent&)", but only the name
  458. * of the slot must be provided as the argument.
  459. * See {@link ctkLDAPSearchFilter} for a description of
  460. * the filter syntax.
  461. *
  462. * <p>
  463. * If the context plugin's list of listeners already contains a the same
  464. * slot for the given receiver, then this
  465. * method replaces that slot's filter (which may be <code>null</code>)
  466. * with the specified one (which may be <code>null</code>).
  467. *
  468. * <p>
  469. * The slot is called if the filter criteria is met. To filter based
  470. * upon the class of the service, the filter should reference the
  471. * {@link ctkPluginConstants#OBJECTCLASS} property. If <code>filter</code> is
  472. * <code>null</code>, all services are considered to match the filter.
  473. *
  474. * <p>
  475. * When using a <code>filter</code>, it is possible that the
  476. * <code>ctkServiceEvent</code>s for the complete lifecycle of a service
  477. * will not be delivered to the slot. For example, if the
  478. * <code>filter</code> only matches when the property <code>x</code> has
  479. * the value <code>1</code>, the listener will not be called if the
  480. * service is registered with the property <code>x</code> not set to the
  481. * value <code>1</code>. Subsequently, when the service is modified
  482. * setting property <code>x</code> to the value <code>1</code>, the
  483. * filter will match and the slot will be called with a
  484. * <code>ServiceEvent</code> of type <code>MODIFIED</code>. Thus, the
  485. * slot will not be called with a <code>ServiceEvent</code> of type
  486. * <code>REGISTERED</code>.
  487. *
  488. * @param receiver The object to connect to.
  489. * @param slot The name of the slot to be connected.
  490. * @param filter The filter criteria.
  491. * @param type The Qt connection type.
  492. * @throws std::invalid_argument If <code>filter</code> contains an
  493. * invalid filter string that cannot be parsed.
  494. * @throws std::logic_error If this ctkPluginContext is no
  495. * longer valid.
  496. * @see ctkServiceEvent
  497. * @see disconnectServiceListener()
  498. * @see ctkEventBus
  499. */
  500. void connectServiceListener(QObject* receiver, const char* slot,
  501. const QString& filter = QString());
  502. /**
  503. * Disconnects a slot which has been previously connected
  504. * with a call to connectServiceListener().
  505. *
  506. * @param receiver The object containing the slot.
  507. * @param slot The slot to be disconnected.
  508. * @see connectServiceListener()
  509. */
  510. void disconnectServiceListener(QObject* receiver, const char* slot);
  511. protected:
  512. friend class ctkPluginFrameworkPrivate;
  513. friend class ctkPlugin;
  514. friend class ctkPluginPrivate;
  515. ctkPluginContext(ctkPluginPrivate* plugin);
  516. ctkPluginContextPrivate * const d_ptr;
  517. };
  518. #endif /* CTKPLUGINCONTEXT_H_ */