ctkServices.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "ctkServices_p.h"
  16. #include <QStringListIterator>
  17. #include <QMutexLocker>
  18. #include <QBuffer>
  19. #include <algorithm>
  20. #include "ctkServiceFactory.h"
  21. #include "ctkPluginConstants.h"
  22. #include "ctkPluginFrameworkContext_p.h"
  23. #include "ctkServiceException.h"
  24. #include "ctkServiceRegistrationPrivate.h"
  25. #include "ctkLDAPExpr_p.h"
  26. struct ServiceRegistrationComparator
  27. {
  28. bool operator()(const ctkServiceRegistration& a, const ctkServiceRegistration& b) const
  29. {
  30. return a < b;
  31. }
  32. };
  33. ctkDictionary ctkServices::createServiceProperties(const ctkDictionary& in,
  34. const QStringList& classes,
  35. long sid)
  36. {
  37. static qlonglong nextServiceID = 1;
  38. ctkDictionary props = in;
  39. if (!classes.isEmpty())
  40. {
  41. props.insert(ctkPluginConstants::OBJECTCLASS, classes);
  42. }
  43. props.insert(ctkPluginConstants::SERVICE_ID, sid != -1 ? sid : nextServiceID++);
  44. return props;
  45. }
  46. ctkServices::ctkServices(ctkPluginFrameworkContext* fwCtx)
  47. : mutex(QMutex::Recursive), framework(fwCtx)
  48. {
  49. }
  50. ctkServices::~ctkServices()
  51. {
  52. clear();
  53. }
  54. void ctkServices::clear()
  55. {
  56. services.clear();
  57. classServices.clear();
  58. framework = 0;
  59. }
  60. ctkServiceRegistration ctkServices::registerService(ctkPluginPrivate* plugin,
  61. const QStringList& classes,
  62. QObject* service,
  63. const ctkDictionary& properties)
  64. {
  65. if (service == 0)
  66. {
  67. throw std::invalid_argument("Can't register 0 as a service");
  68. }
  69. // Check if service implements claimed classes and that they exist.
  70. for (QStringListIterator i(classes); i.hasNext();)
  71. {
  72. QString cls = i.next();
  73. if (cls.isEmpty())
  74. {
  75. throw std::invalid_argument("Can't register as null class");
  76. }
  77. if (!(qobject_cast<ctkServiceFactory*>(service)))
  78. {
  79. if (!checkServiceClass(service, cls))
  80. {
  81. QString msg = QString("Service class %1 is not an instance of %2. Maybe you forgot the Q_INTERFACES macro in the service class.")
  82. .arg(service->metaObject()->className()).arg(cls);
  83. throw std::invalid_argument(msg.toStdString());
  84. }
  85. }
  86. }
  87. ctkServiceRegistration res(plugin, service,
  88. createServiceProperties(properties, classes));
  89. {
  90. QMutexLocker lock(&mutex);
  91. services.insert(res, classes);
  92. for (QStringListIterator i(classes); i.hasNext(); )
  93. {
  94. QString currClass = i.next();
  95. QList<ctkServiceRegistration>& s = classServices[currClass];
  96. QList<ctkServiceRegistration>::iterator ip =
  97. std::lower_bound(s.begin(), s.end(), res, ServiceRegistrationComparator());
  98. s.insert(ip, res);
  99. }
  100. }
  101. ctkServiceReference r = res.getReference();
  102. plugin->fwCtx->listeners.serviceChanged(
  103. plugin->fwCtx->listeners.getMatchingServiceSlots(r),
  104. ctkServiceEvent(ctkServiceEvent::REGISTERED, r));
  105. return res;
  106. }
  107. void ctkServices::updateServiceRegistrationOrder(const ctkServiceRegistration& sr,
  108. const QStringList& classes)
  109. {
  110. QMutexLocker lock(&mutex);
  111. for (QStringListIterator i(classes); i.hasNext(); )
  112. {
  113. QList<ctkServiceRegistration>& s = classServices[i.next()];
  114. s.removeAll(sr);
  115. s.insert(std::lower_bound(s.begin(), s.end(), sr, ServiceRegistrationComparator()), sr);
  116. }
  117. }
  118. bool ctkServices::checkServiceClass(QObject* service, const QString& cls) const
  119. {
  120. return service->inherits(cls.toAscii());
  121. }
  122. QList<ctkServiceRegistration> ctkServices::get(const QString& clazz) const
  123. {
  124. QMutexLocker lock(&mutex);
  125. return classServices.value(clazz);
  126. }
  127. ctkServiceReference ctkServices::get(ctkPluginPrivate* plugin, const QString& clazz) const
  128. {
  129. QMutexLocker lock(&mutex);
  130. try {
  131. QList<ctkServiceReference> srs = get(clazz, QString(), plugin);
  132. qDebug() << "get service ref" << clazz << "for plugin"
  133. << plugin->location << " = " << srs.size() << "refs";
  134. if (!srs.isEmpty()) {
  135. return srs.front();
  136. }
  137. }
  138. catch (const std::invalid_argument& )
  139. { }
  140. return ctkServiceReference();
  141. }
  142. QList<ctkServiceReference> ctkServices::get(const QString& clazz, const QString& filter,
  143. ctkPluginPrivate* plugin) const
  144. {
  145. Q_UNUSED(plugin)
  146. QMutexLocker lock(&mutex);
  147. QListIterator<ctkServiceRegistration>* s = 0;
  148. QList<ctkServiceRegistration> v;
  149. ctkLDAPExpr ldap;
  150. if (clazz.isEmpty())
  151. {
  152. if (!filter.isEmpty())
  153. {
  154. ldap = ctkLDAPExpr(filter);
  155. QSet<QString> matched = ldap.getMatchedObjectClasses();
  156. if (!matched.isEmpty())
  157. {
  158. v.clear();
  159. foreach (QString className, matched)
  160. {
  161. const QList<ctkServiceRegistration>& cl = classServices[className];
  162. v += cl;
  163. }
  164. if (!v.isEmpty())
  165. {
  166. s = new QListIterator<ctkServiceRegistration>(v);
  167. }
  168. else
  169. {
  170. return QList<ctkServiceReference>();
  171. }
  172. }
  173. else
  174. {
  175. s = new QListIterator<ctkServiceRegistration>(services.keys());
  176. }
  177. }
  178. else
  179. {
  180. s = new QListIterator<ctkServiceRegistration>(services.keys());
  181. }
  182. }
  183. else
  184. {
  185. QList<ctkServiceRegistration> v = classServices.value(clazz);
  186. if (!v.isEmpty())
  187. {
  188. s = new QListIterator<ctkServiceRegistration>(v);
  189. }
  190. else
  191. {
  192. return QList<ctkServiceReference>();
  193. }
  194. if (!filter.isEmpty())
  195. {
  196. ldap = ctkLDAPExpr(filter);
  197. }
  198. }
  199. QList<ctkServiceReference> res;
  200. while (s->hasNext())
  201. {
  202. ctkServiceRegistration sr = s->next();
  203. ctkServiceReference sri = sr.getReference();
  204. if (filter.isEmpty() || ldap.evaluate(sr.d_func()->properties, false))
  205. {
  206. res.push_back(sri);
  207. }
  208. }
  209. delete s;
  210. return res;
  211. }
  212. void ctkServices::removeServiceRegistration(const ctkServiceRegistration& sr)
  213. {
  214. QMutexLocker lock(&mutex);
  215. QStringList classes = sr.d_func()->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  216. services.remove(sr);
  217. for (QStringListIterator i(classes); i.hasNext(); )
  218. {
  219. QString currClass = i.next();
  220. QList<ctkServiceRegistration>& s = classServices[currClass];
  221. if (s.size() > 1)
  222. {
  223. s.removeAll(sr);
  224. }
  225. else
  226. {
  227. classServices.remove(currClass);
  228. }
  229. }
  230. }
  231. QList<ctkServiceRegistration> ctkServices::getRegisteredByPlugin(ctkPluginPrivate* p) const
  232. {
  233. QMutexLocker lock(&mutex);
  234. QList<ctkServiceRegistration> res;
  235. for (QHashIterator<ctkServiceRegistration, QStringList> i(services); i.hasNext(); )
  236. {
  237. ctkServiceRegistration sr = i.next().key();
  238. if ((sr.d_func()->plugin == p))
  239. {
  240. res.push_back(sr);
  241. }
  242. }
  243. return res;
  244. }
  245. QList<ctkServiceRegistration> ctkServices::getUsedByPlugin(QSharedPointer<ctkPlugin> p) const
  246. {
  247. QMutexLocker lock(&mutex);
  248. QList<ctkServiceRegistration> res;
  249. for (QHashIterator<ctkServiceRegistration, QStringList> i(services); i.hasNext(); )
  250. {
  251. ctkServiceRegistration sr = i.next().key();
  252. if (sr.d_func()->isUsedByPlugin(p))
  253. {
  254. res.push_back(sr);
  255. }
  256. }
  257. return res;
  258. }