Example usage for java.beans.beancontext BeanContextServices getService

List of usage examples for java.beans.beancontext BeanContextServices getService

Introduction

In this page you can find the example usage for java.beans.beancontext BeanContextServices getService.

Prototype

Object getService(BeanContextChild child, Object requestor, Class<?> serviceClass, Object serviceSelector,
        BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException;

Source Link

Document

A BeanContextChild , or any arbitrary object associated with a BeanContextChild , may obtain a reference to a currently registered service from its nesting BeanContextServices via invocation of this method.

Usage

From source file:DocumentTester.java

/**
 * Called when this bean detects that a new service has been registered with
 * its context.//from  ww  w. j  a v a2s  .  com
 * 
 * @param bcsae
 *            the BeanContextServiceAvailableEvent
 */
public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) {
    System.out.println("[Detected a service being added to the context]");

    // Get a reference to the context
    BeanContextServices context = bcsae.getSourceAsBeanContextServices();
    System.out.println("Is the context offering a WordCount service? " + context.hasService(WordCount.class));

    // Use the service, if it's available
    if (context.hasService(WordCount.class)) {
        System.out.println("Attempting to use the service...");
        try {
            WordCount service = (WordCount) context.getService(this, this, WordCount.class, document, this);
            System.out.println("Got the service!");
            service.countWords();
        } catch (Exception e) {
        }
    }
}