ctkServices.cpp 8.4 KB

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