Module: OpenHAB::OSGi

Defined in:
lib/openhab/osgi.rb

Overview

OSGi services interface

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bundleorg.osgi.framework.Bundle (readonly)

Returns The OSGi Bundle for ScriptExtension Class.

Returns:

  • (org.osgi.framework.Bundle)

    The OSGi Bundle for ScriptExtension Class



68
69
70
# File 'lib/openhab/osgi.rb', line 68

def bundle
  @bundle ||= org.osgi.framework.FrameworkUtil.getBundle($scriptExtension.java_class)
end

.bundle_contextorg.osgi.framework.BundleContext (readonly)

Returns OSGi bundle context for ScriptExtension Class.

Returns:

  • (org.osgi.framework.BundleContext)

    OSGi bundle context for ScriptExtension Class



62
63
64
# File 'lib/openhab/osgi.rb', line 62

def bundle_context
  @bundle_context ||= bundle.bundle_context
end

Class Method Details

.register_service(instance, *interfaces, bundle: nil, **properties) ⇒ org.osgi.framework.ServiceRegistration

Register a new service instance with OSGi

Parameters:

  • instance (Object)

    The service instance

  • interfaces (Module, String)

    The interfaces to register this service for. If not provided, it will default to all Java interfaces the instance implements.

  • bundle (org.osgi.framework.Bundle, nil) (defaults to: nil)

    The bundle to register the service from. If not provided, it will default to the bundle of the first interface.

  • properties (Hash)

    The service registration properties.

Returns:

  • (org.osgi.framework.ServiceRegistration)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openhab/osgi.rb', line 45

def register_service(instance, *interfaces, bundle: nil, **properties)
  if interfaces.empty?
    interfaces = instance.class.ancestors.select { |k| k.respond_to?(:java_class) && k.java_class&.interface? }
  end

  bundle_class = interfaces.first.is_a?(Module) ? interfaces.first : instance
  bundle ||= org.osgi.framework.FrameworkUtil.get_bundle(bundle_class.java_class)
  interfaces.map! { |i| i.is_a?(String) ? i : i.java_class.name }
  bundle.bundle_context.register_service(
    interfaces.to_java(java.lang.String),
    instance,
    java.util.Hashtable.new(properties)
  )
end

.service(name, filter: nil) ⇒ Object

Parameters:

Returns:

  • (Object)


15
16
17
# File 'lib/openhab/osgi.rb', line 15

def service(name, filter: nil)
  services(name, filter: filter).first
end

.services(name, filter: nil) ⇒ Array<Object>

Returns An array of services.

Parameters:

Returns:

  • (Array<Object>)

    An array of services



25
26
27
28
29
30
# File 'lib/openhab/osgi.rb', line 25

def services(name, filter: nil)
  (bundle_context.get_service_references(name, filter) || []).map do |reference|
    logger.trace { "OSGi service found for '#{name}' using OSGi Service Reference #{reference}" }
    bundle_context.get_service(reference)
  end
end