ctkManagedServiceFactory.h 6.4 KB

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