ctkServiceReferencePrivate.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "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 "ctkServiceRegistrationPrivate.h"
  23. #include "ctkPluginFrameworkContext_p.h"
  24. ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
  25. : registration(reg)
  26. {
  27. }
  28. QObject* ctkServiceReferencePrivate::getService(ctkPlugin* plugin)
  29. {
  30. QObject* s = 0;
  31. {
  32. QMutexLocker lock(&registration->propsLock);
  33. if (registration->available)
  34. {
  35. int count = registration->dependents.value(plugin);
  36. if (count == 0)
  37. {
  38. QStringList classes =
  39. registration->properties.value(PluginConstants::OBJECTCLASS).toStringList();
  40. registration->dependents[plugin] = 1;
  41. if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
  42. {
  43. try
  44. {
  45. s = serviceFactory->getService(plugin, registration->q_func());
  46. }
  47. catch (const std::exception& pe)
  48. {
  49. ctkServiceException se("ctkServiceFactory throw an exception",
  50. ctkServiceException::FACTORY_EXCEPTION, pe);
  51. plugin->d_func()->fwCtx->listeners.frameworkError
  52. (registration->plugin->q_func(), se);
  53. return 0;
  54. }
  55. if (s == 0) {
  56. ctkServiceException se("ctkServiceFactory produced null",
  57. ctkServiceException::FACTORY_ERROR);
  58. plugin->d_func()->fwCtx->listeners.frameworkError
  59. (registration->plugin->q_func(), se);
  60. return 0;
  61. }
  62. for (QStringListIterator i(classes); i.hasNext(); )
  63. {
  64. QString cls = i.next();
  65. if (!registration->plugin->fwCtx->services.checkServiceClass(s, cls))
  66. {
  67. ctkServiceException se(QString("ctkServiceFactory produced an object ") +
  68. "that did not implement: " + cls,
  69. ctkServiceException::FACTORY_ERROR);
  70. plugin->d_func()->fwCtx->listeners.frameworkError
  71. (registration->plugin->q_func(), se);
  72. return 0;
  73. }
  74. }
  75. registration->serviceInstances.insert(plugin, s);
  76. }
  77. else
  78. {
  79. s = registration->getService();
  80. }
  81. }
  82. else
  83. {
  84. registration->dependents.insert(plugin, count + 1);
  85. if (qobject_cast<ctkServiceFactory*>(registration->getService()))
  86. {
  87. s = registration->serviceInstances.value(plugin);
  88. }
  89. else
  90. {
  91. s = registration->getService();
  92. }
  93. }
  94. }
  95. }
  96. return s;
  97. }
  98. /**
  99. * Unget the service object.
  100. *
  101. * @param bundle Bundle who wants remove service.
  102. * @param checkRefCounter If true decrement refence counter and remove service
  103. * if we reach zero. If false remove service without
  104. * checking refence counter.
  105. * @return True if service was remove or false if only refence counter was
  106. * decremented.
  107. */
  108. bool ctkServiceReferencePrivate::ungetService(ctkPlugin* plugin, bool checkRefCounter)
  109. {
  110. QMutexLocker lock(&registration->propsLock);
  111. bool hadReferences = false;
  112. if (registration->reference != 0)
  113. {
  114. bool removeService = false;
  115. int count= registration->dependents.value(plugin);
  116. if (count > 0)
  117. {
  118. hadReferences = true;
  119. }
  120. if(checkRefCounter)
  121. {
  122. if (count > 1)
  123. {
  124. registration->dependents[plugin] = count - 1;
  125. }
  126. else if(count == 1)
  127. {
  128. removeService = true;
  129. }
  130. }
  131. else
  132. {
  133. removeService = true;
  134. }
  135. if (removeService)
  136. {
  137. QObject* sfi = registration->serviceInstances[plugin];
  138. registration->serviceInstances.remove(plugin);
  139. if (sfi != 0)
  140. {
  141. try
  142. {
  143. qobject_cast<ctkServiceFactory*>(registration->getService())->ungetService(plugin,
  144. registration->q_func(), sfi);
  145. }
  146. catch (const std::exception& e)
  147. {
  148. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
  149. }
  150. }
  151. registration->dependents.remove(plugin);
  152. }
  153. }
  154. return hadReferences;
  155. }