ctkEABlacklistingHandlerTasks.tpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. template<class BlackList, class TopicHandlerFilters, class Filters>
  16. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  17. ctkEABlacklistingHandlerTasks(ctkPluginContext* context,
  18. ctkEABlackList<BlackList>* blackList,
  19. ctkEATopicHandlerFilters<TopicHandlerFilters>* topicHandlerFilters,
  20. ctkEAFilters<Filters>* filters)
  21. : blackList(blackList), context(context),
  22. topicHandlerFilters(topicHandlerFilters), filters(filters)
  23. {
  24. checkNull(context, "Context");
  25. checkNull(blackList, "BlackList");
  26. checkNull(topicHandlerFilters, "TopicHandlerFilters");
  27. checkNull(filters, "Filters");
  28. }
  29. template<class BlackList, class TopicHandlerFilters, class Filters>
  30. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  31. ~ctkEABlacklistingHandlerTasks()
  32. {
  33. delete filters;
  34. delete topicHandlerFilters;
  35. delete blackList;
  36. }
  37. template<class BlackList, class TopicHandlerFilters, class Filters>
  38. QList<ctkEAHandlerTask<ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters> > >
  39. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  40. createHandlerTasks(const ctkEvent& event)
  41. {
  42. QList<ctkEAHandlerTask<Self> > result;
  43. QList<ctkServiceReference> handlerRefs;
  44. try
  45. {
  46. handlerRefs = context->getServiceReferences<ctkEventHandler>(
  47. topicHandlerFilters->createFilterForTopic(event.getTopic()));
  48. }
  49. catch (const ctkInvalidArgumentException& e)
  50. {
  51. CTK_WARN_EXC(ctkEventAdminActivator::getLogService(), &e)
  52. << "Invalid EVENT_TOPIC [" << event.getTopic() << "]";
  53. }
  54. for (int i = 0; i < handlerRefs.size(); ++i)
  55. {
  56. const ctkServiceReference& ref = handlerRefs.at(i);
  57. if (!blackList->contains(ref)
  58. //TODO security
  59. //&& ref.getPlugin()->hasPermission(
  60. // PermissionsUtil.createSubscribePermission(event.getTopic()))
  61. )
  62. {
  63. try
  64. {
  65. if (event.matches(filters->createFilter(
  66. ref.getProperty(ctkEventConstants::EVENT_FILTER).toString())))
  67. {
  68. result.push_back(ctkEAHandlerTask<Self>(ref, event, this));
  69. }
  70. }
  71. catch (const ctkInvalidArgumentException& e)
  72. {
  73. CTK_WARN_SR_EXC(ctkEventAdminActivator::getLogService(), ref, &e)
  74. << "Invalid EVENT_FILTER - Blacklisting ServiceReference ["
  75. << ref << " | Plugin(" << ref.getPlugin() << ")]";
  76. blackList->add(ref);
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. template<class BlackList, class TopicHandlerFilters, class Filters>
  83. void
  84. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  85. blackListRef(const ctkServiceReference& handlerRef)
  86. {
  87. blackList->add(handlerRef);
  88. CTK_WARN(ctkEventAdminActivator::getLogService())
  89. << "Blacklisting ServiceReference [" << handlerRef << " | Plugin("
  90. << handlerRef.getPlugin() << ")] due to timeout!";
  91. }
  92. template<class BlackList, class TopicHandlerFilters, class Filters>
  93. ctkEventHandler*
  94. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  95. getEventHandler(const ctkServiceReference& handlerRef)
  96. {
  97. ctkEventHandler* result = (blackList->contains(handlerRef)) ? 0
  98. : context->getService<ctkEventHandler>(handlerRef);
  99. return (result ? result : &nullEventHandler);
  100. }
  101. template<class BlackList, class TopicHandlerFilters, class Filters>
  102. void
  103. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  104. ungetEventHandler(ctkEventHandler* handler,
  105. const ctkServiceReference& handlerRef)
  106. {
  107. if(&nullEventHandler != handler)
  108. {
  109. // Is the handler not unregistered or blacklisted?
  110. if(!blackList->contains(handlerRef) &&
  111. (handlerRef.getPlugin()))
  112. {
  113. context->ungetService(handlerRef);
  114. }
  115. }
  116. }
  117. template<class BlackList, class TopicHandlerFilters, class Filters>
  118. void
  119. ctkEABlacklistingHandlerTasks<BlackList, TopicHandlerFilters, Filters>::
  120. checkNull(void* object, const QString& name)
  121. {
  122. if(object == 0)
  123. {
  124. throw ctkInvalidArgumentException(qPrintable(name + " may not be null"));
  125. }
  126. }