ctkServiceRegistration.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "ctkServiceRegistration.h"
  16. #include <QMutex>
  17. #include <stdexcept>
  18. namespace ctk {
  19. class ServiceRegistrationPrivate
  20. {
  21. private:
  22. /**
  23. * Lock object for synchronous event delivery.
  24. */
  25. QMutex eventLock;
  26. public:
  27. /**
  28. * Plugin registering this service.
  29. */
  30. Plugin* plugin;
  31. /**
  32. * Service or ServiceFactory object.
  33. */
  34. QObject* service;
  35. /**
  36. * Reference object to this service registration.
  37. */
  38. ServiceReference reference;
  39. /**
  40. * Service properties.
  41. */
  42. PluginContext::ServiceProperties properties;
  43. /**
  44. * Plugins dependent on this service. Integer is used as
  45. * reference counter, counting number of unbalanced getService().
  46. */
  47. QHash<Plugin*,int> dependents;
  48. /**
  49. * Object instances that factory has produced.
  50. */
  51. QHash<Plugin*, QObject*> serviceInstances;
  52. /**
  53. * Is service available. I.e., if <code>true</code> then holders
  54. * of a ServiceReference for the service are allowed to get it.
  55. */
  56. volatile bool available;
  57. /**
  58. * Avoid recursive unregistrations. I.e., if <code>true</code> then
  59. * unregistration of this service has started but is not yet
  60. * finished.
  61. */
  62. volatile bool unregistering;
  63. QMutex propsLock;
  64. ServiceRegistrationPrivate(Plugin* plugin, QObject* service,
  65. const PluginContext::ServiceProperties& props)
  66. : plugin(plugin), service(service), reference(this),
  67. properties(props), available(true), unregistering(false)
  68. {
  69. }
  70. /**
  71. * Check if a plugin uses this service
  72. *
  73. * @param p Plugin to check
  74. * @return true if plugin uses this service
  75. */
  76. bool isUsedByPlugin(Plugin* p)
  77. {
  78. QHash<Plugin*, int> deps = dependents;
  79. return deps.contains(p);
  80. }
  81. };
  82. ServiceReference ServiceRegistration::getReference() const
  83. {
  84. Q_D(const ServiceRegistration);
  85. if (!d->available) throw std::logic_error("Service is unregistered");
  86. return d->reference;
  87. }
  88. void ServiceRegistration::setProperties(const PluginContext::ServiceProperties& properties)
  89. {
  90. // QMutexLocker lock(eventLock);
  91. // Set before;
  92. // // TBD, optimize the locking of services
  93. // synchronized (bundle.fwCtx.services) {
  94. //
  95. // synchronized (properties) {
  96. // if (available) {
  97. // // NYI! Optimize the MODIFIED_ENDMATCH code
  98. // Object old_rank = properties.get(Constants.SERVICE_RANKING);
  99. // before = bundle.fwCtx.listeners.getMatchingServiceListeners(reference);
  100. // String[] classes = (String[])properties.get(Constants.OBJECTCLASS);
  101. // Long sid = (Long)properties.get(Constants.SERVICE_ID);
  102. // properties = new PropertiesDictionary(props, classes, sid);
  103. // Object new_rank = properties.get(Constants.SERVICE_RANKING);
  104. // if (old_rank != new_rank && new_rank instanceof Integer &&
  105. // !((Integer)new_rank).equals(old_rank)) {
  106. // bundle.fwCtx.services.updateServiceRegistrationOrder(this, classes);
  107. // }
  108. // } else {
  109. // throw new IllegalStateException("Service is unregistered");
  110. // }
  111. // }
  112. // }
  113. // bundle.fwCtx.listeners
  114. // .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
  115. // new ServiceEvent(ServiceEvent.MODIFIED, reference),
  116. // before);
  117. // bundle.fwCtx.listeners
  118. // .serviceChanged(before,
  119. // new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, reference),
  120. // null);
  121. }
  122. void ServiceRegistration::unregister() const
  123. {
  124. // Q_D(ServiceRegistration);
  125. //
  126. // if (d->unregistering) return; // Silently ignore redundant unregistration.
  127. //
  128. // {
  129. // QMutexLocker lock(eventLock);
  130. // if (d->unregistering) return;
  131. // d->unregistering = true;
  132. //
  133. // if (d->available)
  134. // {
  135. // if (d->plugin)
  136. // {
  137. // d->plugin->fwCtx.services.removeServiceRegistration(this);
  138. // }
  139. // }
  140. // else
  141. // {
  142. // throw std::logic_error("Service is unregistered");
  143. // }
  144. // }
  145. //
  146. // if (d->plugin)
  147. // {
  148. // bundle.fwCtx.listeners
  149. // .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
  150. // new ServiceEvent(ServiceEvent.UNREGISTERING, reference),
  151. // null);
  152. // }
  153. // synchronized (eventLock) {
  154. // synchronized (properties) {
  155. // available = false;
  156. // if (null!=bundle)
  157. // bundle.fwCtx.perm.callUnregister0(this);
  158. // bundle = null;
  159. // dependents = null;
  160. // reference = null;
  161. // service = null;
  162. // serviceInstances = null;
  163. // unregistering = false;
  164. // }
  165. // }
  166. }
  167. }