Giter VIP home page Giter VIP logo

osgi-service-helper's Introduction

OSGi Service Helper

This comprises an utility class to safely consume OSGi services using low-level APIs and that is used to safely retrieve target service instances. The release of target service objects can be performed automatically if used with try-with-resources block. Otherwise, close() must be invoked that releases the service objects and associated ServiceTracker. it tries to follow the best practice guidelines inherently that developers don't have to worry at all. In addition, we can also make sure that the source base would be free from resource and memory leak interferences and it could also work in the concurrent programming environment safely.

Usage 1:

 try (final ServiceSupplier<MyService> serviceSupplier = ServiceSupplier.supply(MyService.class)) {
 	   final Stream<MyService> stream = serviceSupplier.get();
 }

Usage 2:

 try (final ServiceSupplier<MyService> serviceSupplier = ServiceSupplier.supply(MyService.class, filter)) {
 	   final Stream<MyService> stream = serviceSupplier.get();
 }

Usage 3:

 final Collection<MyService> refs = ServiceSupplier.references(MyService.class, filter)
 		.collect(toCollection());
 for (final ServiceReference<MyService> ref : refs) {
 	try (final ServiceSupplier serviceSupplier = ServiceSupplier.supply(ref)) {
 		final Stream<MyService> stream = serviceSupplier.get();
 		final Optional<MyService> service = stream.findFirst();
 	}
 }

Usage 4:

 final TrackerSupplier<MyService> trackerSupplier = ServiceSupplier.supplyWithTracker(MyService.class, filter);

 final BiConsumer<ServiceReference<MyService>, MyService> onAddedAction = (MyService::doA).andThen(MyService::doB);
 final BiConsumer<ServiceReference<MyService>, MyService> onRemovalAction = (MyService::removeA).andThen(MyService::removeB);
 final BiConsumer<ServiceReference<MyService>, MyService> onModifiedAction = MyService::update;

 final ServiceCallback<MyService> callback = ServiceCallbackSupplier.create()
 								    .onAdded(onAddedAction)
 								    .onModified(onModifiedAction)
 								    .onRemoved(onRemovalAction)
 								    .get();
 
 final ServiceSupplier<MyService> serviceSupplier = trackerSupplier.shouldWait(false).withCallback(callback).get();
 final Stream<MyService> stream = serviceSupplier.get();

 serviceSupplier.close(); //call it when service tracking is not required at all

Usage 5 (Fluent API):

final ServiceSupplier<MyService> serviceSupplier = ServiceSupplier.supplyWithTracker(MyService.class, null)
							          .shouldWait(true)
   						                  .timeout(5, SECONDS)
   						                  .withCallback(
   							             ServiceCallbackSupplier
   							               .<MyService>create()
   							               .onAdded((r, s) -> s.doA())
   							               .onRemoved(MyService::removeA)
   							               .get())
   						                  .get();

final Stream<MyService> stream = serviceSupplier.get();

serviceSupplier.close(); //call it when service tracking is not required at all

osgi-service-helper's People

Contributors

amitjoy avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.