ctkEAConfiguration_p.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 CTKEACONFIGURATION_P_H
  16. #define CTKEACONFIGURATION_P_H
  17. #include <QString>
  18. #include "dispatch/ctkEADefaultThreadPool_p.h"
  19. #include "ctkEventAdminService_p.h"
  20. #include <service/cm/ctkManagedService.h>
  21. class ctkPluginContext;
  22. class ctkEAAbstractAdapter;
  23. /**
  24. * The <code>ctkEAConfiguration</code> class encapsules the
  25. * configuration for the event admin.
  26. *
  27. * The service knows about the following properties which are read at plugin startup:
  28. * <p>
  29. * <p>
  30. * <tt>org.commontk.eventadmin.CacheSize</tt> - The size of various internal
  31. * caches.
  32. * </p>
  33. * The default value is 30. Increase in case of a large number (more then 100) of
  34. * <tt>ctkEventHandler</tt> services. A value less then 10 triggers the default value.
  35. * </p>
  36. * <p>
  37. * <p>
  38. * <tt>org.commontk.eventadmin.ThreadPoolSize</tt> - The size of the thread
  39. * pool.
  40. * </p>
  41. * The default value is 10. Increase in case of a large amount of synchronous events
  42. * where the <tt>ctkEventHandler</tt> services in turn send new synchronous events in
  43. * the event dispatching thread or a lot of timeouts are to be expected. A value of
  44. * less then 2 triggers the default value. A value of 2 effectively disables thread
  45. * pooling.
  46. * </p>
  47. * <p>
  48. * <p>
  49. * <tt>org.commontk.eventadmin.Timeout</tt> - The black-listing timeout in
  50. * milliseconds
  51. * </p>
  52. * The default value is 5000. Increase or decrease at own discretion. A value of less
  53. * then 100 turns timeouts off. Any other value is the time in milliseconds granted
  54. * to each <tt>ctkEventHandler</tt> before it gets blacklisted.
  55. * </p>
  56. * <p>
  57. * <p>
  58. * <tt>org.commontk.eventadmin.RequireTopic</tt> - Are <tt>ctkEventHandler</tt>
  59. * required to be registered with a topic?
  60. * </p>
  61. * The default is <tt>true</tt>. The specification says that <tt>ctkEventHandler</tt>
  62. * must register with a list of topics they are interested in. Setting this value to
  63. * <tt>false</tt> will enable that handlers without a topic are receiving all events
  64. * (i.e., they are treated the same as with a topic=*).
  65. * </p>
  66. * <p>
  67. * <p>
  68. * <tt>org.commontk.eventadmin.IgnoreTimeout</tt> - Configure
  69. * <tt>ctkEventHandler</tt>s to be called without a timeout.
  70. * </p>
  71. * If a timeout is configured by default all event handlers are called using the timeout.
  72. * For performance optimization it is possible to configure event handlers where the
  73. * timeout handling is not used - this reduces the thread usage from the thread pools
  74. * as the timout handling requires an additional thread to call the event handler.
  75. * However, the application should work without this configuration property. It is a
  76. * pure optimization!
  77. * The value is a list of strings (separated by comma) which is assumed to define
  78. * exact class names.
  79. *
  80. * These properties are read at startup and serve as a default configuration.
  81. * If a configuration admin is configured, the event admin can be configured
  82. * through the config admin.
  83. */
  84. class ctkEAConfiguration : public QObject, public ctkManagedService
  85. {
  86. Q_OBJECT
  87. Q_INTERFACES(ctkManagedService)
  88. public:
  89. /** The PID for the event admin. */
  90. static const QString PID; // = "org.commontk.eventadmin.impl.EventAdmin"
  91. static const QString PROP_CACHE_SIZE; // = "org.commontk.eventadmin.CacheSize"
  92. static const QString PROP_THREAD_POOL_SIZE; // = "org.commontk.eventadmin.ThreadPoolSize"
  93. static const QString PROP_TIMEOUT; // = "org.commontk.eventadmin.Timeout"
  94. static const QString PROP_REQUIRE_TOPIC; // = "org.commontk.eventadmin.RequireTopic"
  95. static const QString PROP_IGNORE_TIMEOUT; // = "org.commontk.eventadmin.IgnoreTimeout"
  96. static const QString PROP_LOG_LEVEL; // = "org.commontk.eventadmin.LogLevel"
  97. private:
  98. QMutex mutex;
  99. /** The plugin context. */
  100. ctkPluginContext* pluginContext;
  101. int cacheSize;
  102. int threadPoolSize;
  103. int timeout;
  104. bool requireTopic;
  105. QStringList ignoreTimeout;
  106. int logLevel;
  107. // The thread pool used - this is a member because we need to close it on stop
  108. ctkEADefaultThreadPool* sync_pool;
  109. ctkEADefaultThreadPool* async_pool;
  110. // The actual implementation of the service - this is a member because we need to
  111. // close it on stop. Note, security is not part of this implementation but is
  112. // added via a decorator in the start method (this is the wrapped object without
  113. // the wrapper).
  114. ctkEventAdminService* admin;
  115. QScopedPointer<QObject> metaTypeService;
  116. // The registration of the security decorator factory (i.e., the service)
  117. ctkServiceRegistration registration;
  118. // all adapters
  119. QList<ctkEAAbstractAdapter*> adapters;
  120. ctkServiceRegistration managedServiceReg;
  121. public:
  122. ctkEAConfiguration(ctkPluginContext* pluginContext);
  123. void updated(const ctkDictionary& properties);
  124. void updateFromConfigAdmin(const ctkDictionary& config);
  125. /**
  126. * Configures this instance.
  127. */
  128. void configure(const ctkDictionary& config);
  129. /**
  130. * Called upon stopping the plugin. This will block until all pending events are
  131. * delivered. An std::logic_error will be thrown on new events starting with
  132. * the begin of this method. However, it might take some time until we settle
  133. * down which is somewhat cumbersome given that the spec asks for return in
  134. * a timely manner.
  135. */
  136. void destroy();
  137. private:
  138. void startOrUpdate();
  139. /**
  140. * Init the adapters in org.commontk.eventadmin.impl.adapter
  141. */
  142. void adaptEvents(ctkEventAdmin* admin);
  143. QObject* tryToCreateMetaTypeProvider(ctkManagedService* managedService);
  144. /**
  145. * Returns either the parsed int from the value of the property if it is set and
  146. * not less then the min value or the default. Additionally, a warning is
  147. * generated in case the value is erroneous (i.e., can not be parsed as an int or
  148. * is less then the min value).
  149. */
  150. int getIntProperty(const QString& key, const QVariant& value,
  151. int defaultValue, int min);
  152. /**
  153. * Returns true if the value of the property is set and is either 1, true, or yes
  154. * Returns false if the value of the property is set and is either 0, false, or no
  155. * Returns the defaultValue otherwise
  156. */
  157. bool getBoolProperty(const QVariant& obj, bool defaultValue);
  158. };
  159. #endif // CTKEACONFIGURATION_P_H