Skip to content

szhem/osgi-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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("foo.bar.Class");

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"),
    raw("(&(g=n)(f=n))"),
    like("f", "g", LikeCriterion.MatchMode.ANYWHERE),
    not(anyEq(attrs))
).filter();

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
services.startTracking();

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

// 8. finally you have to release all resources obtained and unregister all the listeners from the OSGi framework
services.stopTracking();

License

The component is distributed under Apache License 2.0.

About

OSGi Utilities

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages