Method: OpenHAB::RSpec::Mocks::EventAdmin#addingService

Defined in:
lib/openhab/rspec/mocks/event_admin.rb

#addingService(reference) ⇒ Object

rubocop:disable Naming/MethodName



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openhab/rspec/mocks/event_admin.rb', line 98

def addingService(reference) # rubocop:disable Naming/MethodName
  topics = Array(reference.get_property(org.osgi.service.event.EventConstants::EVENT_TOPIC))
  topics = nil if topics.empty? || topics.include?("*")

  service = OSGi.send(:bundle_context).get_service(reference)

  if reference.get_property("component.name") == "org.openhab.core.internal.events.OSGiEventManager"
    # OSGiEventManager will create a ThreadedEventHandler on OSGi activation;
    # we're skipping that, and directly sending to a non-threaded event handler.
    service.class.field_reader :typedEventFactories, :typedEventSubscribers
    service = OSGiEventManager.new(service.typedEventFactories, service.typedEventSubscribers)
  end
  if topics.nil?
    @handlers_matching_all_events << service
  else
    topics.each do |topic|
      @handlers_matching_topics[topic] << service
    end
  end
  service
end