ctkServices.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "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 "ctkServiceRegistrationPrivate.h"
  23. #include "ctkQtServiceRegistration_p.h"
  24. #include "ctkLDAPExpr.h"
  25. using namespace QtMobility;
  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(PluginConstants::OBJECTCLASS, classes);
  58. }
  59. props.insert(PluginConstants::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. throw std::invalid_argument
  100. (std::string("Service object is not an instance of ") + cls.toStdString());
  101. }
  102. }
  103. }
  104. ctkServiceRegistration* res = new ctkServiceRegistration(plugin, service,
  105. createServiceProperties(properties, classes));
  106. {
  107. QMutexLocker lock(&mutex);
  108. services.insert(res, classes);
  109. for (QStringListIterator i(classes); i.hasNext(); )
  110. {
  111. QString currClass = i.next();
  112. QList<ctkServiceRegistration*>& s = classServices[currClass];
  113. QList<ctkServiceRegistration*>::iterator ip =
  114. std::lower_bound(s.begin(), s.end(), res, ServiceRegistrationComparator());
  115. s.insert(ip, res);
  116. }
  117. }
  118. //ctkServiceReference* r = res->getReference();
  119. // TODO
  120. //Listeners l = bundle.fwCtx.listeners;
  121. //l.serviceChanged(l.getMatchingServiceListeners(r),
  122. // new ServiceEvent(ServiceEvent.REGISTERED, r),
  123. // null);
  124. return res;
  125. }
  126. void ctkServices::registerService(ctkPluginPrivate* plugin, QByteArray serviceDescription)
  127. {
  128. QMutexLocker lock(&mutex);
  129. QBuffer serviceBuffer(&serviceDescription);
  130. qServiceManager.addService(&serviceBuffer);
  131. QServiceManager::Error error = qServiceManager.error();
  132. if (!(error == QServiceManager::NoError || error == QServiceManager::ServiceAlreadyExists))
  133. {
  134. throw std::invalid_argument(std::string("Registering the service descriptor for plugin ")
  135. + plugin->symbolicName.toStdString() + " failed: " +
  136. getQServiceManagerErrorString(error).toStdString());
  137. }
  138. QString serviceName = plugin->symbolicName + "_" + plugin->version.toString();
  139. QList<QServiceInterfaceDescriptor> descriptors = qServiceManager.findInterfaces(serviceName);
  140. if (descriptors.isEmpty())
  141. {
  142. qDebug().nospace() << "Warning: No interfaces found for service name " << serviceName
  143. << " in plugin " << plugin->symbolicName << " (ctkVersion " << plugin->version.toString() << ")";
  144. }
  145. QListIterator<QServiceInterfaceDescriptor> it(descriptors);
  146. while (it.hasNext())
  147. {
  148. QServiceInterfaceDescriptor descr = it.next();
  149. qDebug() << "Registering:" << descr.interfaceName();
  150. QStringList classes;
  151. ServiceProperties props;
  152. QStringList customKeys = descr.customAttributes();
  153. QStringListIterator keyIt(customKeys);
  154. bool classAttrFound = false;
  155. while (keyIt.hasNext())
  156. {
  157. QString key = keyIt.next();
  158. if (key == PluginConstants::OBJECTCLASS)
  159. {
  160. classAttrFound = true;
  161. classes << descr.customAttribute(key);
  162. }
  163. else
  164. {
  165. props.insert(key, descr.customAttribute(key));
  166. }
  167. }
  168. if (!classAttrFound)
  169. {
  170. throw std::invalid_argument(std::string("The custom attribute \"") +
  171. PluginConstants::OBJECTCLASS.toStdString() +
  172. "\" is missing in the interface description of \"" +
  173. descr.interfaceName().toStdString());
  174. }
  175. ctkServiceRegistration* res = new ctkQtServiceRegistration(plugin,
  176. descr,
  177. createServiceProperties(props, classes));
  178. services.insert(res, classes);
  179. for (QStringListIterator i(classes); i.hasNext(); )
  180. {
  181. QString currClass = i.next();
  182. QList<ctkServiceRegistration*>& s = classServices[currClass];
  183. QList<ctkServiceRegistration*>::iterator ip =
  184. std::lower_bound(s.begin(), s.end(), res, ServiceRegistrationComparator());
  185. s.insert(ip, res);
  186. }
  187. //ctkServiceReference* r = res->getReference();
  188. // TODO
  189. //Listeners l = bundle.fwCtx.listeners;
  190. //l.serviceChanged(l.getMatchingServiceListeners(r),
  191. // new ServiceEvent(ServiceEvent.REGISTERED, r),
  192. // null);
  193. }
  194. }
  195. QString ctkServices::getQServiceManagerErrorString(QServiceManager::Error error)
  196. {
  197. switch (error)
  198. {
  199. case QServiceManager::NoError:
  200. return QString("No error occurred.");
  201. case QServiceManager::StorageAccessError:
  202. return QString("The service data storage is not accessible. This could be because the caller does not have the required permissions.");
  203. case QServiceManager::InvalidServiceLocation:
  204. return QString("The service was not found at its specified location.");
  205. case QServiceManager::InvalidServiceXml:
  206. return QString("The XML defining the service metadata is invalid.");
  207. case QServiceManager::InvalidServiceInterfaceDescriptor:
  208. return QString("The service interface descriptor is invalid, or refers to an interface implementation that cannot be accessed in the current scope.");
  209. case QServiceManager::ServiceAlreadyExists:
  210. return QString("Another service has previously been registered with the same location.");
  211. case QServiceManager::ImplementationAlreadyExists:
  212. return QString("Another service that implements the same interface version has previously been registered.");
  213. case QServiceManager::PluginLoadingFailed:
  214. return QString("The service plugin cannot be loaded.");
  215. case QServiceManager::ComponentNotFound:
  216. return QString("The service or interface implementation has not been registered.");
  217. case QServiceManager::ServiceCapabilityDenied:
  218. return QString("The security session does not allow the service based on its capabilities.");
  219. case QServiceManager::UnknownError:
  220. return QString("An unknown error occurred.");
  221. default:
  222. return QString("Unknown error enum.");
  223. }
  224. }
  225. void ctkServices::updateServiceRegistrationOrder(ctkServiceRegistration* sr,
  226. const QStringList& classes)
  227. {
  228. QMutexLocker lock(&mutex);
  229. for (QStringListIterator i(classes); i.hasNext(); )
  230. {
  231. QList<ctkServiceRegistration*>& s = classServices[i.next()];
  232. s.removeAll(sr);
  233. s.insert(std::lower_bound(s.begin(), s.end(), sr, ServiceRegistrationComparator()), sr);
  234. }
  235. }
  236. bool ctkServices::checkServiceClass(QObject* service, const QString& cls) const
  237. {
  238. return service->inherits(cls.toAscii());
  239. }
  240. QList<ctkServiceRegistration*> ctkServices::get(const QString& clazz) const
  241. {
  242. QMutexLocker lock(&mutex);
  243. return classServices.value(clazz);
  244. }
  245. ctkServiceReference* ctkServices::get(ctkPluginPrivate* plugin, const QString& clazz) const
  246. {
  247. QMutexLocker lock(&mutex);
  248. try {
  249. QList<ctkServiceReference*> srs = get(clazz, QString());
  250. qDebug() << "get service ref" << clazz << "for plugin"
  251. << plugin->location << " = " << srs;
  252. if (!srs.isEmpty()) {
  253. return srs.front();
  254. }
  255. }
  256. catch (const std::invalid_argument& )
  257. { }
  258. return 0;
  259. }
  260. QList<ctkServiceReference*> ctkServices::get(const QString& clazz, const QString& filter) const
  261. {
  262. QMutexLocker lock(&mutex);
  263. QListIterator<ctkServiceRegistration*>* s = 0;
  264. ctkLDAPExpr* ldap = 0;
  265. if (clazz.isEmpty())
  266. {
  267. if (!filter.isEmpty())
  268. {
  269. ldap = new ctkLDAPExpr(filter);
  270. QSet<QString> matched = ldap->getMatchedObjectClasses();
  271. if (!matched.isEmpty())
  272. {
  273. //TODO
  274. // ArrayList v = null;
  275. // boolean vReadOnly = true;;
  276. // for (Iterator i = matched.iterator(); i.hasNext(); ) {
  277. // ArrayList cl = (ArrayList) classServices.get(i.next());
  278. // if (cl != null) {
  279. // if (v == null) {
  280. // v = cl;
  281. // } else {
  282. // if (vReadOnly) {
  283. // v = new ArrayList(v);
  284. // vReadOnly = false;
  285. // }
  286. // v.addAll(cl);
  287. // }
  288. // }
  289. // }
  290. // if (v != null) {
  291. // s = v.iterator();
  292. // } else {
  293. // return null;
  294. // }
  295. }
  296. else
  297. {
  298. s = new QListIterator<ctkServiceRegistration*>(services.keys());
  299. }
  300. }
  301. else
  302. {
  303. s = new QListIterator<ctkServiceRegistration*>(services.keys());
  304. }
  305. }
  306. else
  307. {
  308. QList<ctkServiceRegistration*> v = classServices.value(clazz);
  309. if (!v.isEmpty())
  310. {
  311. s = new QListIterator<ctkServiceRegistration*>(v);
  312. }
  313. else
  314. {
  315. return QList<ctkServiceReference*>();
  316. }
  317. if (!filter.isEmpty())
  318. {
  319. ldap = new ctkLDAPExpr(filter);
  320. }
  321. }
  322. QList<ctkServiceReference*> res;
  323. while (s->hasNext())
  324. {
  325. ctkServiceRegistration* sr = s->next();
  326. ctkServiceReference* sri = sr->getReference();
  327. if (filter.isEmpty() || ldap->evaluate(sr->d_func()->properties, false))
  328. {
  329. res.push_back(sri);
  330. }
  331. }
  332. delete s;
  333. delete ldap;
  334. return res;
  335. }
  336. void ctkServices::removeServiceRegistration(ctkServiceRegistration* sr)
  337. {
  338. QMutexLocker lock(&mutex);
  339. QStringList classes = sr->d_func()->properties.value(PluginConstants::OBJECTCLASS).toStringList();
  340. services.remove(sr);
  341. for (QStringListIterator i(classes); i.hasNext(); )
  342. {
  343. QString currClass = i.next();
  344. QList<ctkServiceRegistration*>& s = classServices[currClass];
  345. if (s.size() > 1)
  346. {
  347. s.removeAll(sr);
  348. }
  349. else
  350. {
  351. classServices.remove(currClass);
  352. }
  353. }
  354. }
  355. QList<ctkServiceRegistration*> ctkServices::getRegisteredByPlugin(ctkPluginPrivate* p) const
  356. {
  357. QMutexLocker lock(&mutex);
  358. QList<ctkServiceRegistration*> res;
  359. for (QHashIterator<ctkServiceRegistration*, QStringList> i(services); i.hasNext(); )
  360. {
  361. ctkServiceRegistration* sr = i.next().key();
  362. if ((sr->d_func()->plugin = p))
  363. {
  364. res.push_back(sr);
  365. }
  366. }
  367. return res;
  368. }
  369. QList<ctkServiceRegistration*> ctkServices::getUsedByPlugin(ctkPlugin* p) const
  370. {
  371. QMutexLocker lock(&mutex);
  372. QList<ctkServiceRegistration*> res;
  373. for (QHashIterator<ctkServiceRegistration*, QStringList> i(services); i.hasNext(); )
  374. {
  375. ctkServiceRegistration* sr = i.next().key();
  376. if (sr->d_func()->isUsedByPlugin(p))
  377. {
  378. res.push_back(sr);
  379. }
  380. }
  381. return res;
  382. }