ctkManagedServiceFactoryTracker.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "ctkManagedServiceFactoryTracker_p.h"
  16. #include <service/cm/ctkConfigurationException.h>
  17. #include <service/log/ctkLogService.h>
  18. #include "ctkConfigurationAdminActivator_p.h"
  19. #include "ctkConfigurationAdminFactory_p.h"
  20. #include "ctkConfigurationStore_p.h"
  21. #include "ctkConfigurationImpl_p.h"
  22. #include <QRunnable>
  23. ctkManagedServiceFactoryTracker::ctkManagedServiceFactoryTracker(
  24. ctkConfigurationAdminFactory* configurationAdminFactory,
  25. ctkConfigurationStore* configurationStore,
  26. ctkPluginContext* context)
  27. : ctkServiceTracker<ctkManagedServiceFactory*>(context),
  28. context(context),
  29. configurationAdminFactory(configurationAdminFactory),
  30. configurationStoreMutex(QMutex::Recursive),
  31. configurationStore(configurationStore),
  32. queue("ctkManagedServiceFactory Update Queue")
  33. {
  34. }
  35. ctkManagedServiceFactory* ctkManagedServiceFactoryTracker::addingService(const ctkServiceReference& reference)
  36. {
  37. QString factoryPid = reference.getProperty(ctkPluginConstants::SERVICE_PID).toString();
  38. if (factoryPid.isEmpty())
  39. return 0;
  40. ctkManagedServiceFactory* service = context->getService<ctkManagedServiceFactory>(reference);
  41. if (service == 0)
  42. return 0;
  43. {
  44. QMutexLocker lock(&configurationStoreMutex);
  45. addManagedServiceFactory(reference, factoryPid, service);
  46. }
  47. return service;
  48. }
  49. void ctkManagedServiceFactoryTracker::modifiedService(const ctkServiceReference& reference,
  50. ctkManagedServiceFactory* service)
  51. {
  52. QString factoryPid = reference.getProperty(ctkPluginConstants::SERVICE_PID).toString();
  53. {
  54. QMutexLocker lock(&configurationStoreMutex);
  55. if (getManagedServiceFactory(factoryPid) == service)
  56. return;
  57. QString previousPid = getPidForManagedServiceFactory(service);
  58. removeManagedServiceFactory(reference, previousPid);
  59. addingService(reference);
  60. }
  61. }
  62. void ctkManagedServiceFactoryTracker::removedService(const ctkServiceReference& reference,
  63. ctkManagedServiceFactory* service)
  64. {
  65. Q_UNUSED(service)
  66. QString factoryPid = reference.getProperty(ctkPluginConstants::SERVICE_PID).toString();
  67. {
  68. QMutexLocker lock(&configurationStoreMutex);
  69. removeManagedServiceFactory(reference, factoryPid);
  70. }
  71. context->ungetService(reference);
  72. }
  73. void ctkManagedServiceFactoryTracker::notifyDeleted(ctkConfigurationImpl* config)
  74. {
  75. config->checkLocked();
  76. QString factoryPid = config->getFactoryPid(false);
  77. ctkServiceReference reference = getManagedServiceFactoryReference(factoryPid);
  78. if (reference && config->bind(reference.getPlugin()))
  79. {
  80. asynchDeleted(getManagedServiceFactory(factoryPid), config->getPid(false));
  81. }
  82. }
  83. void ctkManagedServiceFactoryTracker::notifyUpdated(ctkConfigurationImpl* config)
  84. {
  85. config->checkLocked();
  86. QString factoryPid = config->getFactoryPid();
  87. ctkServiceReference reference = getManagedServiceFactoryReference(factoryPid);
  88. if (reference && config->bind(reference.getPlugin()))
  89. {
  90. ctkDictionary properties = config->getProperties();
  91. configurationAdminFactory->modifyConfiguration(reference, properties);
  92. asynchUpdated(getManagedServiceFactory(factoryPid), config->getPid(), properties);
  93. }
  94. }
  95. void ctkManagedServiceFactoryTracker::addManagedServiceFactory(
  96. const ctkServiceReference& reference, const QString& factoryPid,
  97. ctkManagedServiceFactory* service)
  98. {
  99. QList<ctkConfigurationImplPtr> configs = configurationStore->getFactoryConfigurations(factoryPid);
  100. ctkConfigurationImplLocker lock(configs);
  101. if (trackManagedServiceFactory(factoryPid, reference, service))
  102. {
  103. foreach (ctkConfigurationImplPtr config, configs)
  104. {
  105. if (config->isDeleted())
  106. {
  107. // ignore this config
  108. }
  109. else if (config->bind(reference.getPlugin()))
  110. {
  111. ctkDictionary properties = config->getProperties();
  112. configurationAdminFactory->modifyConfiguration(reference, properties);
  113. asynchUpdated(service, config->getPid(), properties);
  114. }
  115. else
  116. {
  117. CTK_WARN(configurationAdminFactory->getLogService())
  118. << "Configuration for " << ctkPluginConstants::SERVICE_PID << "="
  119. << config->getPid() << " could not be bound to "
  120. << reference.getPlugin()->getLocation();
  121. }
  122. }
  123. }
  124. }
  125. void ctkManagedServiceFactoryTracker::removeManagedServiceFactory(
  126. const ctkServiceReference& reference, const QString& factoryPid)
  127. {
  128. QList<ctkConfigurationImplPtr> configs = configurationStore->getFactoryConfigurations(factoryPid);
  129. ctkConfigurationImplLocker lock(configs);
  130. untrackManagedServiceFactory(factoryPid, reference);
  131. }
  132. bool ctkManagedServiceFactoryTracker::trackManagedServiceFactory(const QString& factoryPid,
  133. const ctkServiceReference& reference,
  134. ctkManagedServiceFactory* service)
  135. {
  136. QMutexLocker lock(&managedServiceFactoryMutex);
  137. if (managedServiceFactoryReferences.contains(factoryPid))
  138. {
  139. CTK_WARN(configurationAdminFactory->getLogService())
  140. << "ctkManagedServiceFactory already registered for "
  141. << ctkPluginConstants::SERVICE_PID << "=" << factoryPid;
  142. return false;
  143. }
  144. managedServiceFactoryReferences.insert(factoryPid, reference);
  145. managedServiceFactories.insert(factoryPid, service);
  146. return true;
  147. }
  148. void ctkManagedServiceFactoryTracker::untrackManagedServiceFactory(const QString& factoryPid,
  149. const ctkServiceReference& reference)
  150. {
  151. Q_UNUSED(reference)
  152. QMutexLocker lock(&managedServiceFactoryMutex);
  153. managedServiceFactoryReferences.remove(factoryPid);
  154. managedServiceFactories.remove(factoryPid);
  155. }
  156. ctkManagedServiceFactory* ctkManagedServiceFactoryTracker::getManagedServiceFactory(const QString& factoryPid) const
  157. {
  158. QMutexLocker lock(&managedServiceFactoryMutex);
  159. return managedServiceFactories.value(factoryPid);
  160. }
  161. ctkServiceReference ctkManagedServiceFactoryTracker::getManagedServiceFactoryReference(const QString& factoryPid) const
  162. {
  163. QMutexLocker lock(&managedServiceFactoryMutex);
  164. return managedServiceFactoryReferences.value(factoryPid);
  165. }
  166. QString ctkManagedServiceFactoryTracker::getPidForManagedServiceFactory(ctkManagedServiceFactory* service) const
  167. {
  168. QMutexLocker lock(&managedServiceFactoryMutex);
  169. QHash<QString, ctkManagedServiceFactory*>::ConstIterator end = managedServiceFactories.end();
  170. QHash<QString, ctkManagedServiceFactory*>::ConstIterator it;
  171. for (it = managedServiceFactories.begin(); it != end; ++it)
  172. {
  173. if (it.value() == service)
  174. return it.key();
  175. }
  176. return QString();
  177. }
  178. class _AsynchDeleteRunnable : public QRunnable
  179. {
  180. public:
  181. _AsynchDeleteRunnable(ctkManagedServiceFactory* service, const QString& pid,
  182. ctkLogService* log)
  183. : service(service), pid(pid), log(log)
  184. {
  185. }
  186. void run()
  187. {
  188. try
  189. {
  190. service->deleted(pid);
  191. }
  192. catch (const std::exception* e)
  193. {
  194. CTK_ERROR_EXC(log, e);
  195. }
  196. }
  197. private:
  198. ctkManagedServiceFactory* const service;
  199. const QString pid;
  200. ctkLogService* const log;
  201. };
  202. void ctkManagedServiceFactoryTracker::asynchDeleted(ctkManagedServiceFactory* service, const QString& pid)
  203. {
  204. queue.put(new _AsynchDeleteRunnable(service, pid, configurationAdminFactory->getLogService()));
  205. }
  206. class _AsynchFactoryUpdateRunnable : public QRunnable
  207. {
  208. public:
  209. _AsynchFactoryUpdateRunnable(ctkManagedServiceFactory* service, const QString& pid,
  210. const ctkDictionary& properties, ctkLogService* log)
  211. : service(service), pid(pid), properties(properties), log(log)
  212. {
  213. }
  214. void run()
  215. {
  216. try
  217. {
  218. service->updated(pid, properties);
  219. }
  220. catch (const ctkConfigurationException* e)
  221. {
  222. // we might consider doing more for ConfigurationExceptions
  223. CTK_ERROR_EXC(log, e);
  224. }
  225. catch (const std::exception* e)
  226. {
  227. CTK_ERROR_EXC(log, e);
  228. }
  229. }
  230. private:
  231. ctkManagedServiceFactory* const service;
  232. const QString pid;
  233. const ctkDictionary properties;
  234. ctkLogService* const log;
  235. };
  236. void ctkManagedServiceFactoryTracker::asynchUpdated(ctkManagedServiceFactory* service, const QString& pid,
  237. const ctkDictionary& properties)
  238. {
  239. queue.put(new _AsynchFactoryUpdateRunnable(service, pid, properties, configurationAdminFactory->getLogService()));
  240. }