ctkQtMobilityServiceRuntime.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "ctkQtMobilityServiceRuntime_p.h"
  16. #include "ctkQtMobilityServiceConstants_p.h"
  17. #include "ctkQtMobilityServiceActivator_p.h"
  18. #include "ctkQtMobilityServiceFactory_p.h"
  19. #include <ctkPluginContext.h>
  20. #include <ctkPlugin.h>
  21. #include <service/log/ctkLogService.h>
  22. #include <ctkPluginConstants.h>
  23. #include <ctkServiceRegistration.h>
  24. #include <QBuffer>
  25. ctkQtMobilityServiceRuntime::ctkQtMobilityServiceRuntime(ctkPluginContext* pc)
  26. : pc(pc)
  27. {
  28. }
  29. void ctkQtMobilityServiceRuntime::start()
  30. {
  31. pc->connectPluginListener(this, SLOT(pluginChanged(ctkPluginEvent)), Qt::DirectConnection);
  32. QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins();
  33. foreach(QSharedPointer<ctkPlugin> plugin, plugins)
  34. {
  35. if ((plugin->getState() & (ctkPlugin::ACTIVE | ctkPlugin::STARTING)) != 0)
  36. {
  37. processPlugin(plugin);
  38. }
  39. }
  40. }
  41. void ctkQtMobilityServiceRuntime::stop()
  42. {
  43. this->disconnect(this, SLOT(pluginChanged(ctkPluginEvent)));
  44. // Bundle [] b = (Bundle [])bundleComponents.keySet().toArray(new Bundle[bundleComponents.size()]);
  45. // for (int i = 0; i < b.length; i++) {
  46. // removeBundle(b[i], ComponentConstants.DEACTIVATION_REASON_DISABLED);
  47. // }
  48. }
  49. QServiceManager& ctkQtMobilityServiceRuntime::getQServiceManager()
  50. {
  51. return qServiceManager;
  52. }
  53. void ctkQtMobilityServiceRuntime::pluginChanged(const ctkPluginEvent& pe)
  54. {
  55. QSharedPointer<ctkPlugin> plugin = pe.getPlugin();
  56. ctkPluginEvent::Type eventType = pe.getType();
  57. if (eventType == ctkPluginEvent::LAZY_ACTIVATION)
  58. {
  59. lazy.insert(plugin);
  60. processPlugin(plugin);
  61. }
  62. else if (eventType == ctkPluginEvent::STARTED)
  63. {
  64. if (!lazy.remove(plugin))
  65. {
  66. processPlugin(plugin);
  67. }
  68. }
  69. else if (eventType == ctkPluginEvent::STOPPING)
  70. {
  71. lazy.remove(plugin);
  72. removePlugin(plugin);
  73. }
  74. }
  75. void ctkQtMobilityServiceRuntime::processPlugin(QSharedPointer<ctkPlugin> plugin)
  76. {
  77. QHash<QString, QString> headers = plugin->getHeaders();
  78. QHash<QString, QString>::const_iterator it = headers.find(ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR);
  79. ctkLogService* log = ctkQtMobilityServiceActivator::getLogService();
  80. CTK_DEBUG(log)
  81. << "Process header " << ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR
  82. << " for plugin #" << plugin->getPluginId() << ": " << (it != headers.end() ? it.value() : "[missing]");
  83. if (it != headers.end())
  84. {
  85. QString sd = it.value();
  86. if (sd.isEmpty())
  87. {
  88. QString msg = QString("Header ") + ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR + " empty.";
  89. ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
  90. return;
  91. }
  92. QByteArray serviceDescription = plugin->getResource(sd);
  93. QBuffer serviceBuffer(&serviceDescription);
  94. qServiceManager.addService(&serviceBuffer);
  95. QServiceManager::Error error = qServiceManager.error();
  96. if (!(error == QServiceManager::NoError || error == QServiceManager::ServiceAlreadyExists))
  97. {
  98. QString msg = QString("Registering the QtMobility service descriptor failed: ") +
  99. getQServiceManagerErrorString(error);
  100. ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
  101. return;
  102. }
  103. QString serviceName = plugin->getSymbolicName() + "_" + plugin->getVersion().toString();
  104. QList<QServiceInterfaceDescriptor> descriptors = qServiceManager.findInterfaces(serviceName);
  105. if (descriptors.isEmpty())
  106. {
  107. QString msg = QString("No interfaces found for service name ") + serviceName;
  108. ctkQtMobilityServiceActivator::logWarning(plugin->getPluginContext(), msg);
  109. return;
  110. }
  111. QListIterator<QServiceInterfaceDescriptor> it(descriptors);
  112. while (it.hasNext())
  113. {
  114. QServiceInterfaceDescriptor descr = it.next();
  115. CTK_DEBUG(ctkQtMobilityServiceActivator::getLogService()) << "Registering:" << descr.interfaceName();
  116. QStringList classes;
  117. ctkDictionary props;
  118. QStringList customKeys = descr.customAttributes();
  119. QStringListIterator keyIt(customKeys);
  120. bool classAttrFound = false;
  121. while (keyIt.hasNext())
  122. {
  123. QString key = keyIt.next();
  124. if (key == ctkPluginConstants::OBJECTCLASS)
  125. {
  126. classAttrFound = true;
  127. classes << descr.customAttribute(key);
  128. }
  129. else
  130. {
  131. props.insert(key, descr.customAttribute(key));
  132. }
  133. }
  134. if (!classAttrFound)
  135. {
  136. QString msg = QString("The custom attribute \"") + ctkPluginConstants::OBJECTCLASS
  137. + "\" is missing in the interface description of \"" + descr.interfaceName();
  138. ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
  139. continue;
  140. }
  141. ctkQtMobilityServiceFactory* serviceObject = new ctkQtMobilityServiceFactory(descr, this, plugin);
  142. ctkServiceRegistration serviceReg = plugin->getPluginContext()->registerService(classes, serviceObject, props);
  143. if (serviceReg)
  144. {
  145. mapPluginToServiceFactory.insert(plugin, serviceObject);
  146. mapPluginToServiceRegistration.insert(plugin, serviceReg);
  147. }
  148. else
  149. {
  150. QString msg = QString("Could not register QtMobility service ") + descr.serviceName() + " "
  151. + descr.interfaceName();
  152. ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
  153. continue;
  154. }
  155. }
  156. }
  157. }
  158. void ctkQtMobilityServiceRuntime::removePlugin(QSharedPointer<ctkPlugin> plugin)
  159. {
  160. CTK_DEBUG(ctkQtMobilityServiceActivator::getLogService())
  161. << "Remove " << plugin->getSymbolicName() << " from QtMobSR";
  162. QList<ctkQtMobilityServiceFactory*> serviceFactories = mapPluginToServiceFactory.values(plugin);
  163. QList<ctkServiceRegistration> serviceRegs = mapPluginToServiceRegistration.values(plugin);
  164. foreach(ctkServiceRegistration serviceReg, serviceRegs)
  165. {
  166. serviceReg.unregister();
  167. }
  168. mapPluginToServiceRegistration.remove(plugin);
  169. mapPluginToServiceFactory.remove(plugin);
  170. qDeleteAll(serviceFactories);
  171. }
  172. QString ctkQtMobilityServiceRuntime::getQServiceManagerErrorString(QServiceManager::Error error)
  173. {
  174. switch (error)
  175. {
  176. case QServiceManager::NoError:
  177. return QString("No error occurred.");
  178. case QServiceManager::StorageAccessError:
  179. return QString("The service data storage is not accessible. This could be because the caller does not have the required permissions.");
  180. case QServiceManager::InvalidServiceLocation:
  181. return QString("The service was not found at its specified location.");
  182. case QServiceManager::InvalidServiceXml:
  183. return QString("The XML defining the service metadata is invalid.");
  184. case QServiceManager::InvalidServiceInterfaceDescriptor:
  185. return QString("The service interface descriptor is invalid, or refers to an interface implementation that cannot be accessed in the current scope.");
  186. case QServiceManager::ServiceAlreadyExists:
  187. return QString("Another service has previously been registered with the same location.");
  188. case QServiceManager::ImplementationAlreadyExists:
  189. return QString("Another service that implements the same interface version has previously been registered.");
  190. case QServiceManager::PluginLoadingFailed:
  191. return QString("The service plugin cannot be loaded.");
  192. case QServiceManager::ComponentNotFound:
  193. return QString("The service or interface implementation has not been registered.");
  194. case QServiceManager::ServiceCapabilityDenied:
  195. return QString("The security session does not allow the service based on its capabilities.");
  196. case QServiceManager::UnknownError:
  197. return QString("An unknown error occurred.");
  198. default:
  199. return QString("Unknown error enum.");
  200. }
  201. }