FileDocCategorySizeDatePackage
DocumentBean.javaAPI DocExample2235Tue Dec 12 18:58:22 GMT 2000None

DocumentBean

public final class DocumentBean extends BeanContextChildSupport
A JavaBean that encapsulates a text file. When added to a bean context, this bean listens for a WordCount service to become available. When the service does become available, the DocumentBean requests an instance of the service. The service then counts the number of words in the file, and prints a report to standard output.

Fields Summary
private File
document
private BeanContextServices
context
Constructors Summary
public DocumentBean(String fileName)
Creates a new DocumentBean given the name of the file to read from.

param
fileName the name of the file to read from

        document = new File(fileName);
    
Methods Summary
public voidserviceAvailable(java.beans.beancontext.BeanContextServiceAvailableEvent bcsae)
Called when this bean detects that a new service has been registered with its context.

param
bcsae the BeanContextServiceAvailableEvent

        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) { }
        }        
    
public voidserviceRevoked(java.beans.beancontext.BeanContextServiceRevokedEvent bcsre)
Called when this bean detects that a service has been revoked from the context.

param
bcsre the BeanContextServiceRevokedEvent

        System.out.println("[Detected a service being revoked from the context]");