ctkManagedService.h 5.3 KB

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