ctkServiceTracker.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 CTKSERVICETRACKER_H
  16. #define CTKSERVICETRACKER_H
  17. #include <QScopedPointer>
  18. #include "CTKPluginFrameworkExport.h"
  19. #include "ctkServiceReference.h"
  20. #include "ctkServiceTrackerCustomizer.h"
  21. #include "ctkLDAPSearchFilter.h"
  22. class ctkPluginContext;
  23. class ctkServiceTrackerPrivate;
  24. /**
  25. * The <code>ctkServiceTracker</code> class simplifies using services from the
  26. * Framework's service registry.
  27. * <p>
  28. * A <code>ctkServiceTracker</code> object is constructed with search criteria and
  29. * a <code>ctkServiceTrackerCustomizer</code> object. A <code>ctkServiceTracker</code>
  30. * can use a <code>ctkServiceTrackerCustomizer</code> to customize the service
  31. * objects to be tracked. The <code>ctkServiceTracker</code> can then be opened to
  32. * begin tracking all services in the Framework's service registry that match
  33. * the specified search criteria. The <code>ctkServiceTracker</code> correctly
  34. * handles all of the details of listening to <code>ctkServiceEvent</code>s and
  35. * getting and ungetting services.
  36. * <p>
  37. * The <code>getServiceReferences</code> method can be called to get references
  38. * to the services being tracked. The <code>getService</code> and
  39. * <code>getServices</code> methods can be called to get the service objects for
  40. * the tracked service.
  41. * <p>
  42. * The <code>ctkServiceTracker</code> class is thread-safe. It does not call a
  43. * <code>ctkServiceTrackerCustomizer</code> while holding any locks.
  44. * <code>ctkServiceTrackerCustomizer</code> implementations must also be
  45. * thread-safe.
  46. *
  47. * @ThreadSafe
  48. */
  49. class CTK_PLUGINFW_EXPORT ctkServiceTracker : protected ctkServiceTrackerCustomizer
  50. {
  51. public:
  52. ~ctkServiceTracker();
  53. /**
  54. * Create a <code>ctkServiceTracker</code> on the specified
  55. * <code>ctkServiceReference</code>.
  56. *
  57. * <p>
  58. * The service referenced by the specified <code>ctkServiceReference</code>
  59. * will be tracked by this <code>ctkServiceTracker</code>.
  60. *
  61. * @param context The <code>ctkPluginContext</code> against which the tracking
  62. * is done.
  63. * @param reference The <code>ctkServiceReference</code> for the service to be
  64. * tracked.
  65. * @param customizer The customizer object to call when services are added,
  66. * modified, or removed in this <code>ctkServiceTracker</code>. If
  67. * customizer is <code>null</code>, then this
  68. * <code>ctkServiceTracker</code> will be used as the
  69. * <code>ctkServiceTrackerCustomizer</code> and this
  70. * <code>ctkServiceTracker</code> will call the
  71. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  72. */
  73. ctkServiceTracker(ctkPluginContext* context,
  74. const ctkServiceReference& reference,
  75. ctkServiceTrackerCustomizer* customizer = 0);
  76. /**
  77. * Create a <code>ctkServiceTracker</code> on the specified class name.
  78. *
  79. * <p>
  80. * Services registered under the specified class name will be tracked by
  81. * this <code>ctkServiceTracker</code>.
  82. *
  83. * @param context The <code>ctkPluginContext</code> against which the tracking
  84. * is done.
  85. * @param clazz The class name of the services to be tracked.
  86. * @param customizer The customizer object to call when services are added,
  87. * modified, or removed in this <code>ctkServiceTracker</code>. If
  88. * customizer is <code>null</code>, then this
  89. * <code>ctkServiceTracker</code> will be used as the
  90. * <code>ctkServiceTrackerCustomizer</code> and this
  91. * <code>ctkServiceTracker</code> will call the
  92. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  93. */
  94. ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
  95. ctkServiceTrackerCustomizer* customizer = 0);
  96. /**
  97. * Create a <code>ctkServiceTracker</code> on the specified
  98. * <code>ctkLDAPSearchFilter</code> object.
  99. *
  100. * <p>
  101. * Services which match the specified <code>ctkLDAPSearchFilter</code> object will be
  102. * tracked by this <code>ctkServiceTracker</code>.
  103. *
  104. * @param context The <code>ctkPluginContext</code> against which the tracking
  105. * is done.
  106. * @param filter The <code>ctkLDAPSearchFilter</code> to select the services to be
  107. * tracked.
  108. * @param customizer The customizer object to call when services are added,
  109. * modified, or removed in this <code>ctkServiceTracker</code>. If
  110. * customizer is null, then this <code>ctkServiceTracker</code> will be
  111. * used as the <code>ctkServiceTrackerCustomizer</code> and this
  112. * <code>ctkServiceTracker</code> will call the
  113. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  114. */
  115. ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
  116. ctkServiceTrackerCustomizer* customizer = 0);
  117. /**
  118. * Open this <code>ctkServiceTracker</code> and begin tracking services.
  119. *
  120. * <p>
  121. * Services which match the search criteria specified when this
  122. * <code>ctkServiceTracker</code> was created are now tracked by this
  123. * <code>ctkServiceTracker</code>.
  124. *
  125. * @throws std::logic_error If the <code>ctkPluginContext</code>
  126. * with which this <code>ctkServiceTracker</code> was created is no
  127. * longer valid.
  128. */
  129. void open();
  130. /**
  131. * Close this <code>ctkServiceTracker</code>.
  132. *
  133. * <p>
  134. * This method should be called when this <code>ctkServiceTracker</code> should
  135. * end the tracking of services.
  136. *
  137. * <p>
  138. * This implementation calls getServiceReferences() to get the list
  139. * of tracked services to remove.
  140. */
  141. void close();
  142. /**
  143. * Wait for at least one service to be tracked by this
  144. * <code>ctkServiceTracker</code>. This method will also return when this
  145. * <code>ctkServiceTracker</code> is closed.
  146. *
  147. * <p>
  148. * It is strongly recommended that <code>waitForService</code> is not used
  149. * during the calling of the <code>ctkPluginActivator</code> methods.
  150. * <code>ctkPluginActivator</code> methods are expected to complete in a short
  151. * period of time.
  152. *
  153. * <p>
  154. * This implementation calls getService() to determine if a service
  155. * is being tracked.
  156. *
  157. * @param timeout The time interval in milliseconds to wait. If zero, the
  158. * method will wait indefinitely.
  159. * @return Returns the result of getService().
  160. */
  161. QObject* waitForService(unsigned long timeout);
  162. /**
  163. * Return a list of <code>ctkServiceReference</code>s for all services being
  164. * tracked by this <code>ctkServiceTracker</code>.
  165. *
  166. * @return List of <code>ctkServiceReference</code>s.
  167. */
  168. QList<ctkServiceReference> getServiceReferences() const;
  169. /**
  170. * Returns a <code>ctkServiceReference</code> for one of the services being
  171. * tracked by this <code>ctkServiceTracker</code>.
  172. *
  173. * <p>
  174. * If multiple services are being tracked, the service with the highest
  175. * ranking (as specified in its <code>service.ranking</code> property) is
  176. * returned. If there is a tie in ranking, the service with the lowest
  177. * service ID (as specified in its <code>service.id</code> property); that
  178. * is, the service that was registered first is returned. This is the same
  179. * algorithm used by <code>ctkPluginContext::getServiceReference()</code>.
  180. *
  181. * <p>
  182. * This implementation calls getServiceReferences() to get the list
  183. * of references for the tracked services.
  184. *
  185. * @return A <code>ctkServiceReference</code> for a tracked service.
  186. * @throws ctkServiceException if no services are being tracked.
  187. */
  188. ctkServiceReference getServiceReference() const;
  189. /**
  190. * Returns the service object for the specified
  191. * <code>ctkServiceReference</code> if the specified referenced service is
  192. * being tracked by this <code>ctkServiceTracker</code>.
  193. *
  194. * @param reference The reference to the desired service.
  195. * @return A service object or <code>null</code> if the service referenced
  196. * by the specified <code>ctkServiceReference</code> is not being
  197. * tracked.
  198. */
  199. QObject* getService(const ctkServiceReference& reference) const;
  200. /**
  201. * Return a list of service objects for all services being tracked by this
  202. * <code>ctkServiceTracker</code>.
  203. *
  204. * <p>
  205. * This implementation calls getServiceReferences() to get the list
  206. * of references for the tracked services and then calls
  207. * getService(const ctkServiceReference&) for each reference to get the
  208. * tracked service object.
  209. *
  210. * @return A list of service objects or an empty list if no services
  211. * are being tracked.
  212. */
  213. QList<QObject*> getServices() const;
  214. /**
  215. * Returns a service object for one of the services being tracked by this
  216. * <code>ctkServiceTracker</code>.
  217. *
  218. * <p>
  219. * If any services are being tracked, this implementation returns the result
  220. * of calling <code>getService(getServiceReference())</code>.
  221. *
  222. * @return A service object or <code>null</code> if no services are being
  223. * tracked.
  224. */
  225. QObject* getService() const;
  226. /**
  227. * Remove a service from this <code>ctkServiceTracker</code>.
  228. *
  229. * The specified service will be removed from this
  230. * <code>ctkServiceTracker</code>. If the specified service was being tracked
  231. * then the <code>ctkServiceTrackerCustomizer::removedService</code> method will
  232. * be called for that service.
  233. *
  234. * @param reference The reference to the service to be removed.
  235. */
  236. void remove(const ctkServiceReference& reference);
  237. /**
  238. * Return the number of services being tracked by this
  239. * <code>ctkServiceTracker</code>.
  240. *
  241. * @return The number of services being tracked.
  242. */
  243. int size() const;
  244. /**
  245. * Returns the tracking count for this <code>ctkServiceTracker</code>.
  246. *
  247. * The tracking count is initialized to 0 when this
  248. * <code>ctkServiceTracker</code> is opened. Every time a service is added,
  249. * modified or removed from this <code>ctkServiceTracker</code>, the tracking
  250. * count is incremented.
  251. *
  252. * <p>
  253. * The tracking count can be used to determine if this
  254. * <code>ctkServiceTracker</code> has added, modified or removed a service by
  255. * comparing a tracking count value previously collected with the current
  256. * tracking count value. If the value has not changed, then no service has
  257. * been added, modified or removed from this <code>ctkServiceTracker</code>
  258. * since the previous tracking count was collected.
  259. *
  260. * @return The tracking count for this <code>ctkServiceTracker</code> or -1 if
  261. * this <code>ctkServiceTracker</code> is not open.
  262. */
  263. int getTrackingCount() const;
  264. protected:
  265. /**
  266. * Default implementation of the
  267. * <code>ctkServiceTrackerCustomizer::addingService</code> method.
  268. *
  269. * <p>
  270. * This method is only called when this <code>ctkServiceTracker</code> has been
  271. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  272. *
  273. * <p>
  274. * This implementation returns the result of calling <code>getService</code>
  275. * on the <code>ctkPluginContext</code> with which this
  276. * <code>ctkServiceTracker</code> was created passing the specified
  277. * <code>ctkServiceReference</code>.
  278. * <p>
  279. * This method can be overridden in a subclass to customize the service
  280. * object to be tracked for the service being added. In that case, take care
  281. * not to rely on the default implementation of
  282. * \link removedService(const ctkServiceReference&, QObject*) removedService\endlink
  283. * to unget the service.
  284. *
  285. * @param reference The reference to the service being added to this
  286. * <code>ctkServiceTracker</code>.
  287. * @return The service object to be tracked for the service added to this
  288. * <code>ctlServiceTracker</code>.
  289. * @see ctkServiceTrackerCustomizer::addingService(const ctkServiceReference&)
  290. */
  291. QObject* addingService(const ctkServiceReference& reference);
  292. /**
  293. * Default implementation of the
  294. * <code>ctkServiceTrackerCustomizer::modifiedService</code> method.
  295. *
  296. * <p>
  297. * This method is only called when this <code>ctkServiceTracker</code> has been
  298. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  299. *
  300. * <p>
  301. * This implementation does nothing.
  302. *
  303. * @param reference The reference to modified service.
  304. * @param service The service object for the modified service.
  305. * @see ctkServiceTrackerCustomizer::modifiedService(const ctkServiceReference&, QObject*)
  306. */
  307. void modifiedService(const ctkServiceReference& reference, QObject* service);
  308. /**
  309. * Default implementation of the
  310. * <code>ctkServiceTrackerCustomizer::removedService</code> method.
  311. *
  312. * <p>
  313. * This method is only called when this <code>ctkServiceTracker</code> has been
  314. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  315. *
  316. * <p>
  317. * This implementation calls <code>ungetService</code>, on the
  318. * <code>ctkPluginContext</code> with which this <code>ctkServiceTracker</code>
  319. * was created, passing the specified <code>ctkServiceReference</code>.
  320. * <p>
  321. * This method can be overridden in a subclass. If the default
  322. * implementation of \link addingService(const ctkServiceReference&) addingService\endlink
  323. * method was used, this method must unget the service.
  324. *
  325. * @param reference The reference to removed service.
  326. * @param service The service object for the removed service.
  327. * @see ctkServiceTrackerCustomizer::removedService(const ServiceReference&, QObject*)
  328. */
  329. void removedService(const ctkServiceReference& reference, QObject* service);
  330. private:
  331. friend class ctkTrackedService;
  332. Q_DECLARE_PRIVATE(ctkServiceTracker)
  333. const QScopedPointer<ctkServiceTrackerPrivate> d_ptr;
  334. };
  335. #endif // CTKSERVICETRACKER_H