ctkManagedServiceFactory.h 6.4 KB

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