ctkManagedService.h 5.5 KB

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