ctkServiceReference_p.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "ctkServiceReference_p.h"
  16. #include <QObject>
  17. #include <QMutexLocker>
  18. #include "ctkPlugin_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkPluginFrameworkContext_p.h"
  21. #include "ctkServiceFactory.h"
  22. #include "ctkServiceException.h"
  23. #include "ctkServices_p.h"
  24. #include "ctkServiceRegistration_p.h"
  25. //----------------------------------------------------------------------------
  26. ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
  27. : ref(1), registration(reg)
  28. {
  29. if (registration) registration->ref.ref();
  30. }
  31. //----------------------------------------------------------------------------
  32. ctkServiceReferencePrivate::~ctkServiceReferencePrivate()
  33. {
  34. if (registration && !registration->ref.deref())
  35. delete registration;
  36. }
  37. //----------------------------------------------------------------------------
  38. QObject* ctkServiceReferencePrivate::getService(QSharedPointer<ctkPlugin> plugin)
  39. {
  40. QObject* s = 0;
  41. {
  42. QMutexLocker lock(&registration->propsLock);
  43. if (registration->available)
  44. {
  45. int count = registration->dependents.value(plugin);
  46. if (count == 0)
  47. {
  48. QStringList classes =
  49. registration->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  50. registration->dependents[plugin] = 1;
  51. if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
  52. {
  53. try
  54. {
  55. s = serviceFactory->getService(plugin, ctkServiceRegistration(registration));
  56. }
  57. catch (const ctkException& pe)
  58. {
  59. ctkServiceException se("ctkServiceFactory throw an exception",
  60. ctkServiceException::FACTORY_EXCEPTION, pe);
  61. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), se);
  62. return 0;
  63. }
  64. if (s == 0)
  65. {
  66. ctkServiceException se("ctkServiceFactory produced null",
  67. ctkServiceException::FACTORY_ERROR);
  68. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), se);
  69. return 0;
  70. }
  71. for (QStringListIterator i(classes); i.hasNext(); )
  72. {
  73. QString cls = i.next();
  74. if (!registration->plugin->fwCtx->services->checkServiceClass(s, cls))
  75. {
  76. ctkServiceException se(QString("ctkServiceFactory produced an object ") +
  77. "that did not implement: " + cls,
  78. ctkServiceException::FACTORY_ERROR);
  79. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), se);
  80. return 0;
  81. }
  82. }
  83. registration->serviceInstances.insert(plugin, s);
  84. }
  85. else
  86. {
  87. s = registration->getService();
  88. }
  89. }
  90. else
  91. {
  92. registration->dependents.insert(plugin, count + 1);
  93. if (qobject_cast<ctkServiceFactory*>(registration->getService()))
  94. {
  95. s = registration->serviceInstances.value(plugin);
  96. }
  97. else
  98. {
  99. s = registration->getService();
  100. }
  101. }
  102. }
  103. }
  104. return s;
  105. }
  106. //----------------------------------------------------------------------------
  107. bool ctkServiceReferencePrivate::ungetService(QSharedPointer<ctkPlugin> plugin, bool checkRefCounter)
  108. {
  109. QMutexLocker lock(&registration->propsLock);
  110. bool hadReferences = false;
  111. bool removeService = false;
  112. int count= registration->dependents.value(plugin);
  113. if (count > 0)
  114. {
  115. hadReferences = true;
  116. }
  117. if(checkRefCounter)
  118. {
  119. if (count > 1)
  120. {
  121. registration->dependents[plugin] = count - 1;
  122. }
  123. else if(count == 1)
  124. {
  125. removeService = true;
  126. }
  127. }
  128. else
  129. {
  130. removeService = true;
  131. }
  132. if (removeService)
  133. {
  134. QObject* sfi = registration->serviceInstances[plugin];
  135. registration->serviceInstances.remove(plugin);
  136. if (sfi != 0)
  137. {
  138. try
  139. {
  140. qobject_cast<ctkServiceFactory*>(
  141. registration->getService())->ungetService(plugin, ctkServiceRegistration(registration), sfi);
  142. }
  143. catch (const ctkException& e)
  144. {
  145. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
  146. }
  147. }
  148. registration->dependents.remove(plugin);
  149. }
  150. return hadReferences;
  151. }
  152. //----------------------------------------------------------------------------
  153. const ctkServiceProperties& ctkServiceReferencePrivate::getProperties() const
  154. {
  155. return registration->properties;
  156. }
  157. //----------------------------------------------------------------------------
  158. QVariant ctkServiceReferencePrivate::getProperty(const QString& key, bool lock) const
  159. {
  160. if (lock)
  161. {
  162. QMutexLocker lock(&registration->propsLock);
  163. return registration->properties.value(key);
  164. }
  165. return registration->properties.value(key);
  166. }