Skip to content
Fetching latest commit…
Cannot retrieve the latest commit at this time.
..
Failed to load latest commit information.
.settings
src
.classpath
.gitignore
.project
LICENSE
README.textile
TODO.txt
build.gradle

README.textile

Groovy OSGi modules

About

The groovyx.osgi module contains helper classes to make OSGi programming easier. This package is inspired by ScalaModules.

Features:

  • simplifies working with OSGi service registry
  • safe access to OSGi services, without fear for OSGi dynamics
  • Service finder DSL
  • OSGi service filter DSL. The filter support is derived from the filter4osgi project and was contributed by Hamlet D’Arcy.

Usage

(Almost) all of groovyx.osgi is implemented using the classes groovyx.osgi.ServcieFinder and groovyx.osgi.ServiceWrapper. groovyx.osgi.OsgiCategory provides some glue code to allow easy access of the functionality provided by ServiceFinder and ServiceWrapper from BundleContext, ServiceReference, and ServiceReference[].

Simple example

In this example, all services of type MyService are looked up in the OSGi service registry.


import grooxy.osgi.OsgiCategory

List results
use(OsgiCategory) {
	results = bundleContext.findServices(MyService).withEachService() { srv ->
		srv.doSomething()
	}
}

results.each { result -> println result }

The above sample is equivalent to:


import grooxy.osgi.ServiceFinder
import grooxy.osgi.ServiceWrapper

ServiceWrapper services = new ServiceFinder(bundleContext)
				.setClassName(MyService)
				.find()
List results = services.withEachService() { srv -> srv.doSomething() }
results.each { result -> println result }

The equivalent Java code looks like this:


import java.util.ArrayList;
import java.util.List
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

List<String> results = new ArrayList<String>();
ServiceReference[] references = context.getServiceReferences(MyService.class.getName(), null);
for (int r = 0; r < references.length; r++) {
	ServiceReference reference = references[r];
	if (reference == null) {
		continue;
	}
	try {
		MyService service = (MyService) context.getService(reference);
		if (service != null) {
			String result = service.doSomething();
			results.add(result);
		}
	}
	finally {
		context.ungetService(reference);
	}
}

Reference

All examples can be run in a fashion similar to the simple example above. For brevity,
only the configuration closure is included in the following snippets.

Find services by class name


import grooxy.osgi.OsgiCategory
import grooxy.osgi.ServiceWrapper

use(OsgiCategory) {
	ServiceWrapper services = bundleContext.findServices(MyService)
}

import grooxy.osgi.ServiceFinder
import grooxy.osgi.ServiceWrapper

ServiceWrapper services = new ServiceFinder(bundleContext)
								.single()
								.setClassName(MyService.class.getName())
								.find()

Roadmap

  • simplified service registration MyObject.registerService (Map, Closure)
  • simplified use of ServiceTracker, ManagedService, and ManagedServiceFactory
  • improve docs
Something went wrong with that request. Please try again.