ctkServices.cpp 8.2 KB

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