|
UserGuide
Guide for using the peaberry bundle with OSGi.
Eclipse/PDE Example - Text Scrambling Service
Useful Console Commandsss - list all bundles stop n - stop bundle #n start n - start bundle #n diag n - diagnose bundle Installing the OSGi binding module// provides bindings for the current bundle context + OSGi service registry Injector injector = Guice.createInjector(osgiModule(bundleContext), ...); Fluent API ExamplesThe following static imports were used to simplify the examples import static org.ops4j.peaberry.Peaberry.osgiModule; import static org.ops4j.peaberry.Peaberry.service; import static org.ops4j.peaberry.util.TypeLiterals.export; import static org.ops4j.peaberry.util.TypeLiterals.iterable; import static org.ops4j.peaberry.util.Attributes.names; import static org.ops4j.peaberry.util.Filters.ldap; Injecting a stock quote service: @Inject StockQuote quote; ... bind(StockQuote.class).toProvider(service(StockQuote.class).single()); Injecting many stock quote services: @Inject Iterable<StockQuote> quotes; ... bind(iterable(StockQuote.class)).toProvider(service(StockQuote.class).multiple()); Exporting an implementation as a stock quote service: @Inject // the service can be controlled by the Export handle Export<StockQuote> exportedQuote; ... // the service is exported at injection time bind(export(StockQuote.class)).toProvider(service(myQuoteImpl).export()); Applying a custom filter to find a specific service: service(StockQuote.class).filter(ldap("(Currency=GBP)")).single()Applying custom attributes to an exported service: service(myQuoteImpl).attributes(names("Currency=GBP")).export()(the ldap and names utility methods are from org.ops4j.peaberry.util) You can also decorate services with additional behaviour: service(StockQuote.class).decoratedWith(someDecoratorImpl).single() or ask for them to be injected directly, instead of using a dynamic proxy: service(StockQuote.class).single().direct() similarly, if you don't want to bother with an Export handle when exporting: service(myQuoteImpl).export().direct() You can use your own registry implementation, instead of the default binding: service(StockQuote.class).in(myRegistryImpl).single() And you can now actively watch for changes in the services you inject: bind(StockQuote.class).toProvider(service(StockQuote.class).out(new AbstractWatcher<StockQuote>() {
@Override
protected StockQuote adding(Import<StockQuote> service) {
// the returned object is used in the modified and removed calls
StockQuote instance = service.get();
System.out.println("ADDING:" + instance);
return instance;
}
@Override
protected void modified(StockQuote instance, Map<String, ?> attributes) {
System.out.println("MODIFIED:" + instance);
}
@Override
protected void removed(StockQuote instance) {
System.out.println("REMOVED:" + instance);
}
}).multiple());AbstractWatcher is a utility class to make implementing ServiceWatcher a bit easier. |
Sign in to add a comment



Thanks for spotting that Andreas, glad someone reads these pages!
You mean there's something else to read (other than code/examples)? ;)
Now, really... am I missing something?
It all looks to be corrected, so you're not missing anything for now. Unless you mean some non-code for your reading list, for which I'd recommend the upcoming "Pride and Prejudice and Zombies."
Yep, the code + wiki is all you get at the moment ;) of course if there's anything missing (apart from period dramas involving zombies!) feel free to raise an issue or email the group...
Hi, I'm having some problems using Peaberry to inject services between several eclipse plugins. Briefly, I have 4 plugins: one for an editor, one for service interfaces and two for service implementations. I'd like to inject a chosen implementation to my editor using peaberry. Is there a place where I can expose the problem and ask for help? Thanks.
We have a group for Guice-on-OSGi related questions: http://groups.google.com/group/guice-osgi guice-osgi@googlegroups.com (there's a link on the main page)
Testcases really help when debugging, otherwise please provide as much info as possible upfront as it means less to-and-fro on the email and hopefully a fast resolution ;)
I think it should read "3. open peaberry.target ..."
Fixed, thanks