org.ops4j.peaberry
Class Peaberry

java.lang.Object
  extended by org.ops4j.peaberry.Peaberry

public final class Peaberry
extends Object

Guice extension that supports injection and outjection of dynamic services.

For example, 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()
Outjection is a way of actively monitoring for services instead of polling:
 service(StockQuote.class).out(myWatcherImpl).multiple()
the given watcher is notified when matching services appear and disappear.
(org.ops4j.peaberry.util has abstract watcher and decorator classes)

See the online User Guide for the latest examples and suggested best practice.

Author:
mcculls@gmail.com (Stuart McCulloch)

Field Summary
static String CACHE_GENERATIONS_HINT
          Name of system property ("org.ops4j.peaberry.cache.generations") suggests the number of flushes before an unused service is released.
static String CACHE_INTERVAL_HINT
          Name of system property ("org.ops4j.peaberry.cache.interval") suggests the time in milliseconds between flushes of the service cache.
static String NATIVE_FILTER_HINT
          Name of system property ("org.ops4j.peaberry.filter.native") suggests using AttributeFilter.toString() as native filter.
 
Method Summary
static com.google.inject.Module osgiModule(org.osgi.framework.BundleContext bundleContext)
          Create a new Guice binding Module for the given bundle context.
static com.google.inject.Module osgiModule(org.osgi.framework.BundleContext bundleContext, ServiceRegistry... registries)
          Create a new Guice binding Module for the given bundle context along with a list of additional ServiceRegistrys to query when injecting or watching for services.
static com.google.inject.Module osgiModule(ServiceRegistry... registries)
          Create a new Guice binding Module that uses an externally bound bundle context along with a list of additional ServiceRegistrys to query when injecting or watching for services.
static
<T> DecoratedServiceBuilder<T>
service(Class<T> clazz)
          Start building a dynamic service provider for the given class.
static
<T> DecoratedServiceBuilder<T>
service(com.google.inject.Key<T> key)
          Start building a dynamic service provider for the given key.
static
<T> DecoratedServiceBuilder<T>
service(T instance)
          Start building a dynamic service provider for the given instance.
static
<T> DecoratedServiceBuilder<T>
service(com.google.inject.TypeLiteral<T> type)
          Start building a dynamic service provider for the given type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHE_INTERVAL_HINT

public static final String CACHE_INTERVAL_HINT
Name of system property ("org.ops4j.peaberry.cache.interval") suggests the time in milliseconds between flushes of the service cache.

See Also:
Constant Field Values

CACHE_GENERATIONS_HINT

public static final String CACHE_GENERATIONS_HINT
Name of system property ("org.ops4j.peaberry.cache.generations") suggests the number of flushes before an unused service is released.

See Also:
Constant Field Values

NATIVE_FILTER_HINT

public static final String NATIVE_FILTER_HINT
Name of system property ("org.ops4j.peaberry.filter.native") suggests using AttributeFilter.toString() as native filter.

See Also:
Constant Field Values
Method Detail

service

public static <T> DecoratedServiceBuilder<T> service(com.google.inject.Key<T> key)
Start building a dynamic service provider for the given key.

Parameters:
key - binding key
Returns:
dynamic service builder

service

public static <T> DecoratedServiceBuilder<T> service(com.google.inject.TypeLiteral<T> type)
Start building a dynamic service provider for the given type.

Parameters:
type - binding type
Returns:
dynamic service builder

service

public static <T> DecoratedServiceBuilder<T> service(Class<T> clazz)
Start building a dynamic service provider for the given class.

Parameters:
clazz - binding class
Returns:
dynamic service builder

service

public static <T> DecoratedServiceBuilder<T> service(T instance)
Start building a dynamic service provider for the given instance.

Parameters:
instance - service instance
Returns:
dynamic service builder

osgiModule

public static com.google.inject.Module osgiModule(org.osgi.framework.BundleContext bundleContext)
Create a new Guice binding Module for the given bundle context.

Parameters:
bundleContext - current bundle context
Returns:
OSGi specific Guice bindings

osgiModule

public static com.google.inject.Module osgiModule(org.osgi.framework.BundleContext bundleContext,
                                                  ServiceRegistry... registries)
Create a new Guice binding Module for the given bundle context along with a list of additional ServiceRegistrys to query when injecting or watching for services.

Parameters:
bundleContext - current bundle context
registries - extra service registries
Returns:
OSGi specific Guice bindings
Since:
1.1

osgiModule

public static com.google.inject.Module osgiModule(ServiceRegistry... registries)
Create a new Guice binding Module that uses an externally bound bundle context along with a list of additional ServiceRegistrys to query when injecting or watching for services.

Parameters:
registries - extra service registries
Returns:
OSGi specific Guice bindings
Since:
1.3


Copyright © 2006-2012 OPS4J - Open Participation Software for Java. All Rights Reserved.