ctkServiceTracker.h 13 KB

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