ctkManagedService.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef CTKMANAGEDSERVICE_H
  16. #define CTKMANAGEDSERVICE_H
  17. #include <QObject>
  18. /**
  19. * A service that can receive configuration data from a Configuration Admin
  20. * service.
  21. *
  22. * <p>
  23. * A Managed Service is a service that needs configuration data. Such an object
  24. * should be registered with the Framework registry with the
  25. * {@code service.pid} property set to some unique identifier called a
  26. * PID.
  27. *
  28. * <p>
  29. * If the Configuration Admin service has a {@code ctkConfiguration} object
  30. * corresponding to this PID, it will callback the {@code updated()}
  31. * method of the {@code ctkManagedService} object, passing the properties of
  32. * that {@code ctkConfiguration} object.
  33. *
  34. * <p>
  35. * If it has no such {@code ctkConfiguration} object, then it calls back
  36. * with an empty properties argument. Registering a Managed Service
  37. * will always result in a callback to the {@code updated()} method
  38. * provided the Configuration Admin service is, or becomes active. This callback
  39. * must always be done asynchronously.
  40. *
  41. * <p>
  42. * Else, every time that either of the {@code updated()} methods is
  43. * called on that {@code ctkConfiguration} object, the
  44. * {@code ctkManagedService#updated()} method with the new properties is
  45. * called. If the {@code remove()} method is called on that
  46. * {@code ctkConfiguration} object, {@code ctkManagedService#updated()}
  47. * is called with an empty map for the properties parameter. All these
  48. * callbacks must be done asynchronously.
  49. *
  50. * <p>
  51. * The following example shows the code of a serial port that will create a port
  52. * depending on configuration information.
  53. *
  54. * \code
  55. *
  56. * class SerialPort : public QObject, public ctkManagedService
  57. * {
  58. *
  59. * QMutex mutex;
  60. * ctkServiceRegistration registration;
  61. * Hashtable configuration;
  62. * CommPortIdentifier id;
  63. *
  64. * void open(CommPortIdentifier id, ctkPluginContext* context)
  65. * {
  66. * QMutexLocker lock(&mutex);
  67. * this->id = id;
  68. * registration = context->registerService<ctkManagedService>(
  69. * this, getDefaults());
  70. * }
  71. *
  72. * ServiceProperties getDefaults()
  73. * {
  74. * ServiceProperties defaults;
  75. * defaults.insert("port", id.getName());
  76. * defaults.insert("product", "unknown");
  77. * defaults.insert("baud", 9600);
  78. * defaults.insert(ctkPluginConstants::SERVICE_PID,
  79. * QString("com.acme.serialport.") + id.getName());
  80. * return defaults;
  81. * }
  82. *
  83. * public:
  84. *
  85. * void updated(const ctkDictionary& configuration)
  86. * {
  87. * QMutexLocker lock(&mutex);
  88. * if (configuration.isEmpty())
  89. * {
  90. * registration.setProperties(getDefaults());
  91. * }
  92. * else
  93. * {
  94. * setSpeed(configuration["baud"].toInt());
  95. * registration.setProperties(configuration);
  96. * }
  97. * }
  98. * ...
  99. * };
  100. *
  101. * \endcode
  102. *
  103. * <p>
  104. * As a convention, it is recommended that when a Managed Service is updated, it
  105. * should copy all the properties it does not recognize into the service
  106. * registration properties. This will allow the Configuration Admin service to
  107. * set properties on services which can then be used by other applications.
  108. */
  109. struct ctkManagedService
  110. {
  111. virtual ~ctkManagedService() {}
  112. /**
  113. * Update the configuration for a Managed Service.
  114. *
  115. * <p>
  116. * When the implementation of {@code updated(const ctkDictionary&)} detects any
  117. * kind of error in the configuration properties, it should create a new
  118. * {@code ctkConfigurationException} which describes the problem. This
  119. * can allow a management system to provide useful information to a human
  120. * administrator.
  121. *
  122. * <p>
  123. * If this method throws any other {@code exception}, the
  124. * Configuration Admin service must catch it and should log it.
  125. * <p>
  126. * The Configuration Admin service must call this method asynchronously
  127. * which initiated the callback. This implies that implementors of Managed
  128. * Service can be assured that the callback will not take place during
  129. * registration when they execute the registration in a synchronized method.
  130. *
  131. * @param properties A copy of the ctkConfiguration properties.
  132. * This argument must not contain the
  133. * "service.pluginLocation" property. The value of this property may
  134. * be obtained from the {@code ctkConfiguration#getPluginLocation()}
  135. * method.
  136. * @throws ctkConfigurationException when the update fails
  137. */
  138. virtual void updated(const ctkDictionary& properties) = 0;
  139. };
  140. Q_DECLARE_INTERFACE(ctkManagedService, "org.commontk.service.cm.ManagedService")
  141. #endif // CTKMANAGEDSERVICE_H