ctkServiceReferencePrivate.cpp 5.4 KB

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