ctkServiceRegistration.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include <QMutex>
  16. #include "ctkPluginFrameworkContext_p.h"
  17. #include "ctkPlugin_p.h"
  18. #include "ctkPluginConstants.h"
  19. #include "ctkServiceFactory.h"
  20. #include "ctkServiceRegistration.h"
  21. #include "ctkServiceRegistration_p.h"
  22. #include "ctkServices_p.h"
  23. #include "ctkServiceSlotEntry_p.h"
  24. #include <stdexcept>
  25. //----------------------------------------------------------------------------
  26. ctkServiceRegistration::ctkServiceRegistration()
  27. : d_ptr(0)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. ctkServiceRegistration::ctkServiceRegistration(const ctkServiceRegistration& reg)
  32. : d_ptr(reg.d_ptr)
  33. {
  34. if (d_func()) d_func()->ref.ref();
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkServiceRegistration::ctkServiceRegistration(ctkServiceRegistrationPrivate* registrationPrivate)
  38. : d_ptr(registrationPrivate)
  39. {
  40. if(d_func()) d_func()->ref.ref();
  41. }
  42. //----------------------------------------------------------------------------
  43. ctkServiceRegistration::ctkServiceRegistration(ctkPluginPrivate* plugin, QObject* service,
  44. const ctkDictionary& props)
  45. : d_ptr(new ctkServiceRegistrationPrivate(plugin, service, props))
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. ctkServiceRegistration::operator bool() const
  50. {
  51. return d_func();
  52. }
  53. //----------------------------------------------------------------------------
  54. ctkServiceRegistration& ctkServiceRegistration::operator=(int null)
  55. {
  56. if (null == 0)
  57. {
  58. if (d_func() && !d_func()->ref.deref())
  59. {
  60. delete d_ptr;
  61. }
  62. d_ptr = 0;
  63. }
  64. return *this;
  65. }
  66. //----------------------------------------------------------------------------
  67. ctkServiceRegistration::~ctkServiceRegistration()
  68. {
  69. if (d_func() && !d_func()->ref.deref())
  70. delete d_ptr;
  71. }
  72. //----------------------------------------------------------------------------
  73. ctkServiceReference ctkServiceRegistration::getReference() const
  74. {
  75. Q_D(const ctkServiceRegistration);
  76. if (!d) throw ctkIllegalStateException("ctkServiceRegistration object invalid");
  77. if (!d->available) throw ctkIllegalStateException("Service is unregistered");
  78. return d->reference;
  79. }
  80. //----------------------------------------------------------------------------
  81. void ctkServiceRegistration::setProperties(const ctkDictionary& props)
  82. {
  83. Q_D(ctkServiceRegistration);
  84. if (!d) throw ctkIllegalStateException("ctkServiceRegistration object invalid");
  85. QMutexLocker lock(&d->eventLock);
  86. QSet<ctkServiceSlotEntry> before;
  87. // TBD, optimize the locking of services
  88. {
  89. QMutexLocker lock2(&d->plugin->fwCtx->globalFwLock);
  90. QMutexLocker lock3(&d->propsLock);
  91. if (d->available)
  92. {
  93. // NYI! Optimize the MODIFIED_ENDMATCH code
  94. int old_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  95. before = d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference, false);
  96. QStringList classes = d->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  97. qlonglong sid = d->properties.value(ctkPluginConstants::SERVICE_ID).toLongLong();
  98. d->properties = ctkServices::createServiceProperties(props, classes, sid);
  99. int new_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  100. if (old_rank != new_rank)
  101. {
  102. d->plugin->fwCtx->services->updateServiceRegistrationOrder(*this, classes);
  103. }
  104. }
  105. else
  106. {
  107. throw ctkIllegalStateException("Service is unregistered");
  108. }
  109. }
  110. d->plugin->fwCtx->listeners.serviceChanged(
  111. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  112. ctkServiceEvent(ctkServiceEvent::MODIFIED, d->reference), before);
  113. d->plugin->fwCtx->listeners.serviceChanged(
  114. before,
  115. ctkServiceEvent(ctkServiceEvent::MODIFIED_ENDMATCH, d->reference));
  116. }
  117. //----------------------------------------------------------------------------
  118. void ctkServiceRegistration::unregister()
  119. {
  120. Q_D(ctkServiceRegistration);
  121. if (!d) throw ctkIllegalStateException("ctkServiceRegistration object invalid");
  122. if (d->unregistering) return; // Silently ignore redundant unregistration.
  123. {
  124. QMutexLocker lock(&d->eventLock);
  125. if (d->unregistering) return;
  126. d->unregistering = true;
  127. if (d->available)
  128. {
  129. if (d->plugin)
  130. {
  131. d->plugin->fwCtx->services->removeServiceRegistration(*this);
  132. }
  133. }
  134. else
  135. {
  136. throw ctkIllegalStateException("Service is unregistered");
  137. }
  138. }
  139. if (d->plugin)
  140. {
  141. d->plugin->fwCtx->listeners.serviceChanged(
  142. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  143. ctkServiceEvent(ctkServiceEvent::UNREGISTERING, d->reference));
  144. }
  145. {
  146. QMutexLocker lock(&d->eventLock);
  147. {
  148. QMutexLocker lock2(&d->propsLock);
  149. d->available = false;
  150. if (d->plugin)
  151. {
  152. for (QHashIterator<QSharedPointer<ctkPlugin>, QObject*> i(d->serviceInstances); i.hasNext();)
  153. {
  154. QObject* obj = i.next().value();
  155. try
  156. {
  157. // NYI, don't call inside lock
  158. qobject_cast<ctkServiceFactory*>(d->service)->ungetService(i.key(),
  159. *this,
  160. obj);
  161. }
  162. catch (const ctkException& ue)
  163. {
  164. ctkPluginFrameworkEvent pfwEvent(ctkPluginFrameworkEvent::PLUGIN_ERROR, d->plugin->q_func(), ue);
  165. d->plugin->fwCtx->listeners.emitFrameworkEvent(pfwEvent);
  166. }
  167. }
  168. }
  169. d->plugin = 0;
  170. d->dependents.clear();
  171. d->service = 0;
  172. d->serviceInstances.clear();
  173. d->reference = 0;
  174. d->unregistering = false;
  175. }
  176. }
  177. }
  178. //----------------------------------------------------------------------------
  179. bool ctkServiceRegistration::operator<(const ctkServiceRegistration& o) const
  180. {
  181. Q_D(const ctkServiceRegistration);
  182. if (!d) return true;
  183. return d->reference <(o.d_func()->reference);
  184. }
  185. //----------------------------------------------------------------------------
  186. bool ctkServiceRegistration::operator==(const ctkServiceRegistration& registration) const
  187. {
  188. Q_D(const ctkServiceRegistration);
  189. return d == registration.d_func();
  190. }
  191. //----------------------------------------------------------------------------
  192. ctkServiceRegistration& ctkServiceRegistration::operator=(const ctkServiceRegistration& registration)
  193. {
  194. ctkServiceRegistrationPrivate* curr_d = d_func();
  195. d_ptr = registration.d_ptr;
  196. if (d_ptr) d_ptr->ref.ref();
  197. if (curr_d && !curr_d->ref.deref())
  198. delete curr_d;
  199. return *this;
  200. }
  201. //----------------------------------------------------------------------------
  202. uint qHash(const ctkServiceRegistration& serviceReg)
  203. {
  204. return qHash(serviceReg.d_func());
  205. }