Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (53 sloc) 2.79 KB

OSGi Utilities

  1. What is it?
  2. How to use?
  3. License

What is it?

This project contains rather useful OSGi utilities to deal with OSGi services.

Currently there are the following features:

How to use?

Bundle Delegating ClassLoader

If you need to delegate class or resource loading to the specified bundle you can use the provided BundleDelegatingClassLoader which can fallback to the specified ClassLoader if the provided bundle fails to load required class or resource.

Here is an example:

BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundle, getClass().getClassLoader());
Class<?> clazz = classLoader.loadClass("");

OSGi Services Filter DSL

Here is a simple example to make things clear:

import static com.github.szhem.osgi.util.filter.Filters.*;


Filter filter = and(
    eq("a", "b"),
    ne("b", "d"),
    approx("g", "s"),
    le("y", "z"),
    like("f", "g", LikeCriterion.MatchMode.ANYWHERE),

The example above will produce the following filter string (&(a=b)(!(b=d))(g~=s)(y<=z)(&(g=n)(f=n))(f=*g*)(!(|(a=b)(c=d))))

Collections API to track for OSGi services

To start traking for OSGi services you have to do the following steps:

// 1. get BundleContext somehow
BundleContext bundleContext = ...;

// 2. create filter somehow, for example, using Filters DSL
String serviceFilter = null;

// 3. optionally specify the ClassLoader to fallback to load exported OSGi service interfaces
ClassLoader fallbackClassLoader = getClass().getClassLoader();

// 4. initialize proxy creator to proxy obtained OSGi services as the may come and go
OsgiProxyCreator proxyCreator = new OsgiDefaultProxyCreator();

// 5. create OsgiServiceList or OsgiServiceCollection
List<Service> services = new OsgiServiceList<Service>(bundleContext, serviceFilter, fallbackClassLoader, proxyCreator);

// 6. start tracking for services

// 7. now list with services is updated dynamically even during iterating through it, as services will come and go
for (Service service : services) {

// 8. finally you have to release all resources obtained and unregister all the listeners from the OSGi framework


The component is distributed under Apache License 2.0.

Something went wrong with that request. Please try again.