ctkServiceReferencePrivate.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "ctkServiceReferencePrivate.h"
  16. #include <QObject>
  17. #include <QMutexLocker>
  18. #include "ctkPluginConstants.h"
  19. #include "ctkServiceFactory.h"
  20. #include "ctkServiceException.h"
  21. #include "ctkPluginPrivate_p.h"
  22. #include "ctkServices_p.h"
  23. #include "ctkServiceRegistrationPrivate.h"
  24. #include "ctkPluginFrameworkContext_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 std::exception& pe)
  58. {
  59. ctkServiceException se("ctkServiceFactory throw an exception",
  60. ctkServiceException::FACTORY_EXCEPTION, &pe);
  61. plugin->d_func()->fwCtx->listeners.frameworkError
  62. (registration->plugin->q_func(), se);
  63. return 0;
  64. }
  65. if (s == 0) {
  66. ctkServiceException se("ctkServiceFactory produced null",
  67. ctkServiceException::FACTORY_ERROR);
  68. plugin->d_func()->fwCtx->listeners.frameworkError
  69. (registration->plugin->q_func(), se);
  70. return 0;
  71. }
  72. for (QStringListIterator i(classes); i.hasNext(); )
  73. {
  74. QString cls = i.next();
  75. if (!registration->plugin->fwCtx->services->checkServiceClass(s, cls))
  76. {
  77. ctkServiceException se(QString("ctkServiceFactory produced an object ") +
  78. "that did not implement: " + cls,
  79. ctkServiceException::FACTORY_ERROR);
  80. plugin->d_func()->fwCtx->listeners.frameworkError
  81. (registration->plugin->q_func(), se);
  82. return 0;
  83. }
  84. }
  85. registration->serviceInstances.insert(plugin, s);
  86. }
  87. else
  88. {
  89. s = registration->getService();
  90. }
  91. }
  92. else
  93. {
  94. registration->dependents.insert(plugin, count + 1);
  95. if (qobject_cast<ctkServiceFactory*>(registration->getService()))
  96. {
  97. s = registration->serviceInstances.value(plugin);
  98. }
  99. else
  100. {
  101. s = registration->getService();
  102. }
  103. }
  104. }
  105. }
  106. return s;
  107. }
  108. //----------------------------------------------------------------------------
  109. bool ctkServiceReferencePrivate::ungetService(QSharedPointer<ctkPlugin> plugin, bool checkRefCounter)
  110. {
  111. QMutexLocker lock(&registration->propsLock);
  112. bool hadReferences = false;
  113. bool removeService = false;
  114. int count= registration->dependents.value(plugin);
  115. if (count > 0)
  116. {
  117. hadReferences = true;
  118. }
  119. if(checkRefCounter)
  120. {
  121. if (count > 1)
  122. {
  123. registration->dependents[plugin] = count - 1;
  124. }
  125. else if(count == 1)
  126. {
  127. removeService = true;
  128. }
  129. }
  130. else
  131. {
  132. removeService = true;
  133. }
  134. if (removeService)
  135. {
  136. QObject* sfi = registration->serviceInstances[plugin];
  137. registration->serviceInstances.remove(plugin);
  138. if (sfi != 0)
  139. {
  140. try
  141. {
  142. qobject_cast<ctkServiceFactory*>(
  143. registration->getService())->ungetService(plugin, ctkServiceRegistration(registration), sfi);
  144. }
  145. catch (const std::exception& e)
  146. {
  147. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
  148. }
  149. }
  150. registration->dependents.remove(plugin);
  151. }
  152. return hadReferences;
  153. }
  154. //----------------------------------------------------------------------------
  155. ctkDictionary ctkServiceReferencePrivate::getProperties() const
  156. {
  157. return registration->properties;
  158. }
  159. //----------------------------------------------------------------------------
  160. QVariant ctkServiceReferencePrivate::getProperty(const QString& key, bool lock) const
  161. {
  162. if (lock)
  163. {
  164. QMutexLocker lock(&registration->propsLock);
  165. return registration->properties.value(key);
  166. }
  167. return registration->properties.value(key);
  168. }