ctkServiceRegistration.cpp 5.3 KB

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