ctkServiceRegistration.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "ctkServiceRegistration.h"
  16. #include "ctkServiceRegistrationPrivate.h"
  17. #include "ctkPluginFrameworkContext_p.h"
  18. #include "ctkPluginPrivate_p.h"
  19. #include "ctkServiceFactory.h"
  20. #include <QMutex>
  21. #include <stdexcept>
  22. ctkServiceRegistration::ctkServiceRegistration(ctkPluginPrivate* plugin, QObject* service,
  23. const ServiceProperties& props)
  24. : d_ptr(new ctkServiceRegistrationPrivate(this, plugin, service, props))
  25. {
  26. }
  27. ctkServiceRegistration::ctkServiceRegistration(ctkServiceRegistrationPrivate& dd)
  28. : d_ptr(&dd)
  29. {
  30. }
  31. ctkServiceReference* ctkServiceRegistration::getReference()
  32. {
  33. Q_D(ctkServiceRegistration);
  34. if (!d->available) throw std::logic_error("Service is unregistered");
  35. return d->reference;
  36. }
  37. void ctkServiceRegistration::setProperties(const ServiceProperties& properties)
  38. {
  39. Q_UNUSED(properties)
  40. // QMutexLocker lock(eventLock);
  41. // Set before;
  42. // // TBD, optimize the locking of services
  43. // synchronized (bundle.fwCtx.services) {
  44. //
  45. // synchronized (properties) {
  46. // if (available) {
  47. // // NYI! Optimize the MODIFIED_ENDMATCH code
  48. // Object old_rank = properties.get(Constants.SERVICE_RANKING);
  49. // before = bundle.fwCtx.listeners.getMatchingServiceListeners(reference);
  50. // String[] classes = (String[])properties.get(Constants.OBJECTCLASS);
  51. // Long sid = (Long)properties.get(Constants.SERVICE_ID);
  52. // properties = new PropertiesDictionary(props, classes, sid);
  53. // Object new_rank = properties.get(Constants.SERVICE_RANKING);
  54. // if (old_rank != new_rank && new_rank instanceof Integer &&
  55. // !((Integer)new_rank).equals(old_rank)) {
  56. // bundle.fwCtx.services.updateServiceRegistrationOrder(this, classes);
  57. // }
  58. // } else {
  59. // throw new IllegalStateException("Service is unregistered");
  60. // }
  61. // }
  62. // }
  63. // bundle.fwCtx.listeners
  64. // .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
  65. // new ServiceEvent(ServiceEvent.MODIFIED, reference),
  66. // before);
  67. // bundle.fwCtx.listeners
  68. // .serviceChanged(before,
  69. // new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, reference),
  70. // null);
  71. }
  72. void ctkServiceRegistration::unregister()
  73. {
  74. Q_D(ctkServiceRegistration);
  75. if (d->unregistering) return; // Silently ignore redundant unregistration.
  76. {
  77. QMutexLocker lock(&d->eventLock);
  78. if (d->unregistering) return;
  79. d->unregistering = true;
  80. if (d->available)
  81. {
  82. if (d->plugin)
  83. {
  84. d->plugin->fwCtx->services.removeServiceRegistration(this);
  85. }
  86. }
  87. else
  88. {
  89. throw std::logic_error("Service is unregistered");
  90. }
  91. }
  92. if (d->plugin)
  93. {
  94. //TODO
  95. // bundle.fwCtx.listeners
  96. // .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
  97. // new ServiceEvent(ServiceEvent.UNREGISTERING, reference),
  98. // null);
  99. }
  100. {
  101. QMutexLocker lock(&d->eventLock);
  102. {
  103. QMutexLocker lock2(&d->propsLock);
  104. d->available = false;
  105. if (d->plugin)
  106. {
  107. for (QHashIterator<ctkPlugin*, QObject*> i(d->serviceInstances); i.hasNext();)
  108. {
  109. QObject* obj = i.next().value();
  110. try
  111. {
  112. // NYI, don't call inside lock
  113. qobject_cast<ctkServiceFactory*>(d->service)->ungetService(i.key(),
  114. this,
  115. obj);
  116. }
  117. catch (const std::exception& ue)
  118. {
  119. ctkPluginFrameworkEvent pfwEvent(ctkPluginFrameworkEvent::ERROR, d->plugin->q_func(), ue);
  120. d->plugin->fwCtx->listeners
  121. .emitFrameworkEvent(pfwEvent);
  122. }
  123. }
  124. }
  125. d->plugin = 0;
  126. d->dependents.clear();
  127. d->service = 0;
  128. d->serviceInstances.clear();;
  129. d->unregistering = false;
  130. }
  131. }
  132. }
  133. bool ctkServiceRegistration::operator<(const ctkServiceRegistration& o) const
  134. {
  135. Q_D(const ctkServiceRegistration);
  136. return d->reference->operator <(*(o.d_func()->reference));
  137. }