ctkManagedServiceFactory.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 CTKMANAGEDSERVICEFACTORY_H
  16. #define CTKMANAGEDSERVICEFACTORY_H
  17. #include <QString>
  18. #include <ctkPluginFramework_global.h>
  19. /**
  20. * Manage multiple service instances.
  21. *
  22. * Plugins registering this interface are giving the Configuration Admin service
  23. * the ability to create and configure a number of instances of a service that
  24. * the implementing plugin can provide. For example, a plugin implementing a
  25. * DHCP server could be instantiated multiple times for different interfaces
  26. * using a factory.
  27. *
  28. * <p>
  29. * Each of these <i>service instances </i> is represented, in the persistent
  30. * storage of the Configuration Admin service, by a factory
  31. * {@code ctkConfiguration} object that has a PID. When such a
  32. * {@code ctkConfiguration} is updated, the Configuration Admin service
  33. * calls the {@code ctkManagedServiceFactory} updated method with the new
  34. * properties. When {@code updated} is called with a new PID, the Managed
  35. * Service Factory should create a new factory instance based on these
  36. * configuration properties. When called with a PID that it has seen before, it
  37. * should update that existing service instance with the new configuration
  38. * information.
  39. *
  40. * <p>
  41. * In general it is expected that the implementation of this interface will
  42. * maintain a data structure that maps PIDs to the factory instances that it has
  43. * created. The semantics of a factory instance are defined by the Managed
  44. * Service Factory. However, if the factory instance is registered as a service
  45. * object with the service registry, its PID should match the PID of the
  46. * corresponding {@code ctkConfiguration} object (but it should <b>not</b>
  47. * be registered as a Managed Service!).
  48. *
  49. * <p>
  50. * An example that demonstrates the use of a factory. It will create serial
  51. * ports under command of the Configuration Admin service.
  52. *
  53. * \code
  54. *
  55. * class SerialPortFactory : public QObject, public ctkManagedServiceFactory
  56. * {
  57. *
  58. * ctkServiceRegistration registration;
  59. * QHash<QString, SerialPort*> ports;
  60. *
  61. * void start(ctkPluginContext* context)
  62. * {
  63. * ctkDictionary properties;
  64. * properties.insert(ctkPluginConstants::SERVICE_PID,
  65. * "com.acme.serialportfactory");
  66. * registration = context->registerService<ctkManagedServiceFactory>(
  67. * this, properties);
  68. * }
  69. *
  70. * public:
  71. *
  72. * void updated(const QString& pid, const ctkDictionary& properties)
  73. * {
  74. * QString portName = properties["port"].toString();
  75. * SerialPort* port = ports[pid];
  76. * if (port == 0)
  77. * {
  78. * port = new SerialPort();
  79. * ports.insert(pid, port);
  80. * port->open();
  81. * }
  82. * if (port->getPortName() == portName)
  83. * return;
  84. * port->setPortName(portName);
  85. * }
  86. *
  87. * void deleted(const QString& pid)
  88. * {
  89. * SerialPort* port = ports[pid];
  90. * port->close();
  91. * ports.remove(pid);
  92. * }
  93. * ...
  94. * };
  95. *
  96. * \endcode
  97. */
  98. struct ctkManagedServiceFactory
  99. {
  100. virtual ~ctkManagedServiceFactory() {}
  101. /**
  102. * Return a descriptive name of this factory.
  103. *
  104. * @return the name for the factory, which might be localized
  105. */
  106. virtual QString getName() = 0;
  107. /**
  108. * Create a new instance, or update the configuration of an existing
  109. * instance.
  110. *
  111. * If the PID of the {@code ctkConfiguration} object is new for the
  112. * Managed Service Factory, then create a new factory instance, using the
  113. * configuration {@code properties} provided. Else, update the
  114. * service instance with the provided {@code properties}.
  115. *
  116. * <p>
  117. * If the factory instance is registered with the Framework, then the
  118. * configuration {@code properties} should be copied to its registry
  119. * properties. This is not mandatory and security sensitive properties
  120. * should obviously not be copied.
  121. *
  122. * <p>
  123. * If this method throws any {@code exception}, the Configuration
  124. * Admin service must catch it and should log it.
  125. *
  126. * <p>
  127. * When the implementation of updated detects any kind of error in the
  128. * configuration properties, it should create a new
  129. * {@link ctkConfigurationException} which describes the problem.
  130. *
  131. * <p>
  132. * The Configuration Admin service must call this method asynchronously.
  133. * This implies that implementors of the {@code ctkManagedServiceFactory}
  134. * class can be assured that the callback will not take place during
  135. * registration when they execute the registration in a synchronized method.
  136. *
  137. * @param pid The PID for this configuration.
  138. * @param properties A copy of the configuration properties. This argument
  139. * must not contain the service.pluginLocation" property. The value
  140. * of this property may be obtained from the
  141. * {@code ctkConfiguration#getPluginLocation} method.
  142. * @throws ctkConfigurationException when the configuration properties are
  143. * invalid.
  144. */
  145. virtual void updated(const QString& pid, const ctkDictionary& properties) = 0;
  146. /**
  147. * Remove a factory instance.
  148. *
  149. * Remove the factory instance associated with the PID. If the instance was
  150. * registered with the service registry, it should be unregistered.
  151. * <p>
  152. * If this method throws any {@code exception}, the Configuration
  153. * Admin service must catch it and should log it.
  154. * <p>
  155. * The Configuration Admin service must call this method asynchronously.
  156. *
  157. * @param pid the PID of the service to be removed
  158. */
  159. virtual void deleted(const QString& pid) = 0;
  160. };
  161. Q_DECLARE_INTERFACE(ctkManagedServiceFactory, "org.commontk.service.cm.ManagedServiceFactory")
  162. #endif // CTKMANAGEDSERVICEFACTORY_H