Chapter 10. The java.beans.beancontext PackageThe java.beans.beancontext package extends the JavaBeans component model to add the notion of a containment hierarchy. It also supports bean containers that provide an execution context for the beans they contain and that may also provide a set of services to those beans. This package is typically used by advanced bean developers and developers of bean-manipulation tools. Application programmers who are simply using beans do not typically use this package. Figure 10-1 shows the class hierarchy. Figure 10-1. The java.beans.beancontext packageBeanContext is the central interface of this package. It is a container for beans and also defines several methods that specify context information for beans. BeanContextServices extends BeanContext to define methods that allow a contained bean to query and request available services. A bean that wishes to be told about its containing BeanContext implements the BeanContextChild interface. BeanContext is itself a BeanContextChild, which means that contexts can be nested within other contexts. See Chapter 6, "JavaBeans", for more information on beans and bean contexts.
This interface defines the methods that must be implemented by any class that wants to act as a logical container for JavaBeans components. Every BeanContext is also a BeanContextChild and can therefore be nested within a higher-level bean context. BeanContext is extended by BeanContextServices; any bean context that wants to provide services to the beans it contains must implement this more specialized interface. The BeanContext interface extends the java.util.Collection interface; the children it contains are accessed using the methods of that interface. In addition, BeanContext defines several important methods of its own. instantiateChild() instantiates a new bean, in the same manner as the standard Beans.instantiate() method, and then makes that new bean a child of the context. Calling this method is typically the same as calling the three-argument version of Beans.instantiate(). getResource() and getResourceAsStream() are the BeanContext versions of the java.lang.Class and java.lang.ClassLoader methods of the same name. Some bean-context implementations may provide special behavior for these methods; others may simply delegate to the Class or ClassLoader of the bean. The remaining two methods allow the registration and deregistration of event listeners that the BeanContext notifies when bean children are added or removed from the context. Implementing a BeanContext is a more specialized task than developing a JavaBeans component. Many bean developers will never have to implement a bean context themselves. If you do implement a bean context, you'll probably find it easier to use BeanContextSupport, either by extending it or using an instance as a proxy.
Hierarchy: (BeanContext(BeanContextChild,java.util.Collection,java.beans.DesignMode,java.beans.Visibility)) Implementations: BeanContextServices, BeanContextSupport Passed To: java.beans.AppletInitializer.initialize(), java.beans.Beans.instantiate(), BeanContextChild.setBeanContext(), BeanContextChildSupport.{setBeanContext(), validatePendingSetBeanContext()}, BeanContextEvent.{BeanContextEvent(), setPropagatedFrom()}, BeanContextMembershipEvent.BeanContextMembershipEvent(), BeanContextSupport.BeanContextSupport() Returned By: BeanContextChild.getBeanContext(), BeanContextChildSupport.getBeanContext(), BeanContextEvent.{getBeanContext(), getPropagatedFrom()}, BeanContextSupport.getBeanContextPeer() Type Of: BeanContextChildSupport.beanContext, BeanContextEvent.propagatedFrom
JavaBeans components that are designed to be nested within a bean context and need to be aware of that context must implement this interface. BeanContextChild implements a single beanContext property that identifies the BeanContext within which the bean is contained. The beanContext property is bound and constrained, which means that it must fire PropertyChangeEvent events when setBeanContext() is called, and any call to setBeanContext() may result in a PropertyVetoException if one of the VetoableChangeListener objects vetoes the change. The setBeanContext() method is not intended for use by beans or by applications. When a bean is instantiated or deserialized, its containing bean context calls this method to introduce itself to the bean. The bean must store a reference to its BeanContext in a transient field so that the context is not serialized along with the bean itself. Implementing a BeanContextChild from scratch can be somewhat tricky because you must correctly handle the VetoableChangeListener protocol and correctly implement important conventions, such as storing the BeanContext reference in a transient field. Therefore, most bean developers do not implement the interface directly, but instead use BeanContextSupport, either by subclassing it or by using an instance as a delegate.
Implementations: BeanContext, BeanContextChildSupport Passed To: BeanContext.{getResource(), getResourceAsStream()}, BeanContextChildSupport.BeanContextChildSupport(), BeanContextServices.{getService(), releaseService()}, BeanContextServicesSupport.{getService(), releaseService()}, BeanContextSupport.{getResource(), getResourceAsStream()} Returned By: BeanContextChildSupport.getBeanContextChildPeer(), BeanContextProxy.getBeanContextProxy(), BeanContextSupport.getChildBeanContextChild() Type Of: BeanContextChildSupport.beanContextChildPeer
If a BeanContextChild is not a Component subclass but has an associated Component object to display its visual representation, it implements this interface to allow access to that component.
This class provides support for implementing the BeanContextChild interface in a way that correctly conforms to the details of the bean context specification. A subclass should implement initializeBeanContextResources() and releaseBeanContextResources() to obtain and release whatever resources the bean context child requires, such as service objects obtained from the containing BeanContext. These methods are called when the containing bean context introduces itself by calling setBeanContext(). Any resources obtained with these methods should be stored in transient fields so that they are not serialized along with the bean. A bean that wants a chance to approve any call to setBeanContext() before that call succeeds can implement validatePendingSetBeanContext(). If this method returns false, the setBeanContext() call that triggered it fails with a PropertyVetoException. Many beans are AWT or Swing components and cannot subclass both Component and BeanContextChildSupport. Therefore, many bean developers find it useful to delegate to an internal instance of BeanContextChildSupport. One way to do this is to have your bean implement the BeanContextProxy interface and simply return an instance of BeanContextChildSupport from the getBeanContextProxy() method. Another technique is to actually implement the BeanContextChild interface in your bean, but provide dummy methods that call the corresponding methods of BeanContextChildSupport. If you do this, you should pass an instance of your bean to the BeanContextChildSupport() constructor. This makes the delegation transparent so events appear to come directly from your bean. In either case, you can instantiate BeanContextChildSupport directly. Often, however, you want to create a custom subclass (perhaps as an inner class) to implement methods such as initializeBeanContextResources().
Hierarchy: Object-->BeanContextChildSupport(BeanContextChild,BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener)),Serializable) Subclasses: BeanContextSupport
This interface is implemented by a BeanContext that has a java.awt.Container associated with it. The getContainer() method allows any interested parties to obtain a reference to the container associated with the bean context. It is a common practice for bean contexts to be associated with containers. Unfortunately, BeanContext implements java.util.Collection, which has method-name conflicts with java.awt.Container, so no Container subclass can implement the BeanContext interface. See also BeanContextProxy, which reverses the direction of the proxy relationship.
This is the abstract superclass of all bean context-related events. getBeanContext() returns the source of the event. If isPropagated() returns true, the event has been propagated through a hierarchy of bean contexts, and getPropagatedFrom() returns the most recent bean context to propagate the event.
Hierarchy: Object-->java.util.EventObject(Serializable)-->BeanContextEvent Subclasses: BeanContextMembershipEvent, BeanContextServiceAvailableEvent, BeanContextServiceRevokedEvent
An event of this type is generated by a BeanContext when beans are added to it or removed from it. The event object contains the list of children that were added or removed and allows access to that list in several ways. The size() method returns the number of affected children. The contains() method checks whether a specified object was one of the affected children. toArray() returns the list of affected children as an array, and iterator() returns the list in the form of a java.util.Iterator.
Hierarchy: Object-->java.util.EventObject(Serializable)-->BeanContextEvent-->BeanContextMembershipEvent Passed To: BeanContextMembershipListener.{childrenAdded(), childrenRemoved()}, BeanContextSupport.{fireChildrenAdded(), fireChildrenRemoved()}
This interface should be implemented by any object that wants to be notified when children are added to or removed from a BeanContext.
Hierarchy: (BeanContextMembershipListener(java.util.EventListener)) Passed To: BeanContext.{addBeanContextMembershipListener(), removeBeanContextMembershipListener()}, BeanContextSupport.{addBeanContextMembershipListener(), removeBeanContextMembershipListener()} Returned By: BeanContextSupport.getChildBeanContextMembershipListener()
This interface is implemented by a JavaBeans component (often, but not always, an AWT Component or Container object) that is not itself a BeanContext or BeanContextChild, but has a BeanContext or BeanContextChild object associated with it. The getBeanContextProxy() method returns the associated object. The return type of this method is BeanContextChild. Depending on the context in which you call this method, however, the returned object may actually be a BeanContext or BeanContextServices object. You should test for this using the instanceof operator before casting the object to these more specific types.
An event of this type is generated to notify interested BeanContextServicesListener objects that a new class of service is available from a BeanContextServices object. getServiceClass() returns the class of the service, and getCurrentServiceSelectors() may return a set of additional arguments that can parameterize the service.
Hierarchy: Object-->java.util.EventObject(Serializable)-->BeanContextEvent-->BeanContextServiceAvailableEvent Passed To: BeanContextChildSupport.serviceAvailable(), BeanContextServicesListener.serviceAvailable(), BeanContextServicesSupport.{fireServiceAdded(), serviceAvailable()}
This interface defines the methods that must be implemented by a factory class that wants to provide service objects to beans. To provide its service, a BeanContextServiceProvider is passed to the addService() method of a BeanContextServices object. This creates a mapping in the BeanContextServices object between a class of service (such as java.awt.print.PrinterJob) and a BeanContextServiceProvider that can return a suitable instance of that class to provide the service. When a BeanContextChild requests a service of a particular class from its BeanContextServices container, the BeanContextServices object finds the appropriate BeanContextServiceProvider object and forwards the request to its getService() method. When the bean relinquishes the service, releaseService() is called. A getService() request may include an arbitrary object as an additional parameter or service selector. Service providers that use the service selector argument and that support a finite set of legal service selector values should implement the getCurrentServiceSelectors() method to allow the list of legal selector values to be queried. Bean developers typically do not have to use or implement this interface. From the point of view of a bean context child, service objects are obtained from a BeanContextServices object. Developers creating BeanContextServices implementations, however, must implement appropriate BeanContextServiceProvider objects to provide the services.
Implementations: BeanContextServicesSupport.BCSSProxyServiceProvider Passed To: BeanContextServices.{addService(), revokeService()}, BeanContextServicesSupport.{addService(), createBCSSServiceProvider(), revokeService()} Returned By: BeanContextServicesSupport.BCSSServiceProvider.getServiceProvider() Type Of: BeanContextServicesSupport.BCSSServiceProvider.serviceProvider
A BeanContextServiceProvider that wishes to provide information to a GUI builder tool about the service or services it offers should implement this BeanInfo subinterface. Following the standard BeanInfo naming conventions, the implementing class should have the same name as the service provider class, with "BeanInfo" appended. This enables a design tool to look for and dynamically load the bean info class when necessary. getServicesBeanInfo() should return an array of BeanInfo objects, one for each class of service offered by the BeanContextServiceProvider. These BeanInfo objects enable a design tool to allow the user to visually configure the service object. This can be quite useful, since service objects may be instances of existing classes that were not designed with the standard JavaBeans naming conventions in mind.
Hierarchy: (BeanContextServiceProviderBeanInfo(java.beans.BeanInfo))
This event class provides details about a service revocation initiated by a BeanContextServices object. getServiceClass() specifies the class of service being revoked. isCurrentServiceInvalidNow() specifies whether the currently owned service object has become invalid. If this method returns true, the bean that receives this event must stop using the service object immediately. If the method returns false, the bean can continue to use the service object, but future requests for services of this class will fail.
Hierarchy: Object-->java.util.EventObject(Serializable)-->BeanContextEvent-->BeanContextServiceRevokedEvent Passed To: BeanContextChildSupport.serviceRevoked(), BeanContextServiceRevokedListener.serviceRevoked(), BeanContextServicesSupport.{fireServiceRevoked(), serviceRevoked()}, BeanContextServicesSupport.BCSSProxyServiceProvider.serviceRevoked()
This interface defines a method that is invoked when a service object returned by a BeanContextServices object is forcibly revoked. Unlike other types of event listeners, the BeanContextServiceRevokedListener is not registered and deregistered with a pair of add and remove methods. Instead, an implementation of this interface must be passed to every getService() call on a BeanContextServices object. If the returned service is ever revoked by the granting BeanContextServiceProvider object before the bean has relinquished the service, the serviceRevoked() method of this interface is called. When a service is revoked, it means that future requests for the service will not succeed. But it may also mean that current service objects have become invalid and must not be used anymore. The serviceRevoked() method should call the isCurrentServiceInvalidNow() method of the supplied event object to determine if this is the case. If so, it must immediately stop using the service object.
Hierarchy: (BeanContextServiceRevokedListener(java.util.EventListener)) Implementations: BeanContextServicesListener, BeanContextServicesSupport.BCSSProxyServiceProvider Passed To: BeanContextServices.getService(), BeanContextServicesSupport.getService()
This interface defines additional methods a bean context class must implement if it wants to provide services to the beans it contains. A bean calls hasService() to determine if a service of a particular type is available from its bean context. It calls getService() to request an instance of the specified service class and then calls releaseService() when it no longer needs the service object. A bean that wants to find the complete list of available services can call getCurrentServiceClasses(). Some services allow (or require) a service selector object to be passed to the getService() method to provide additional information about the service object. If a service defines a fixed set of legal service selectors, getCurrentServiceSelectors() allows a bean to iterate through the set of selector objects. Beans that want to know when new services become available or when existing services are revoked should register a BeanContextServicesListener object with addBeanContextServicesListener(). If a BeanContextServices object does not provide a requested service, but is nested within another BeanContext, it should check whether any of its ancestor bean contexts can provide the service. The BeanContextServices interface extends BeanContextServicesListener. This means that every BeanContextServices object can be listening to the set of services available from its container. The previous methods are the ones beans call to obtain services. BeanContextServices defines a different set of methods service providers use to deliver services. addService() defines a BeanContextServiceProvider for a specified Class of service. revokeService() removes this mapping between service class and service provider, and indicates that the specified service is no longer available. When a service is revoked, the BeanContextServices object must notify any beans that have been granted service objects (and have not released them yet) that the service has been revoked. It does this by notifying the BeanContextServiceRevokedListener objects passed to the getService() method. Bean context developers may find it easier to use the BeanContextServicesSupport class, either by subclassing or by delegation, instead of implementing BeanContextServices from scratch.
Hierarchy: (BeanContextServices(BeanContext(BeanContextChild,java.util.Collection,java.beans.DesignMode,java.beans.Visibility),BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener)))) Implementations: BeanContextServicesSupport Passed To: BeanContextServiceAvailableEvent.BeanContextServiceAvailableEvent(), BeanContextServiceProvider.{getCurrentServiceSelectors(), getService(), releaseService()}, BeanContextServiceRevokedEvent.BeanContextServiceRevokedEvent(), BeanContextServicesSupport.BeanContextServicesSupport(), BeanContextServicesSupport.BCSSProxyServiceProvider.{getCurrentServiceSelectors(), getService(), releaseService()} Returned By: BeanContextServiceAvailableEvent.getSourceAsBeanContextServices(), BeanContextServiceRevokedEvent.getSourceAsBeanContextServices(), BeanContextServicesSupport.getBeanContextServicesPeer()
This interface adds a serviceAvailable() method to the serviceRevoked() method of BeanContextServiceRevokedListener. Listeners of this type can be registered with a BeanContextServices object and are notified when a new class of service becomes available or when an existing class of service is revoked.
Hierarchy: (BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener))) Implementations: BeanContextChildSupport, BeanContextServices Passed To: BeanContextServices.{addBeanContextServicesListener(), removeBeanContextServicesListener()}, BeanContextServicesSupport.{addBeanContextServicesListener(), removeBeanContextServicesListener()} Returned By: BeanContextServicesSupport.getChildBeanContextServicesListener()
This class is a useful implementation of the BeanContextServices interface that correctly conforms to the bean context specifications and conventions. Most bean context implementors find it easier to subclass this class or delegate to an instance of this class rather than implement BeanContextServices from scratch. The most common technique is to implement the BeanContextProxy interface and return an instance of BeanContextServicesSupport from the getBeanContextProxy() method.
Hierarchy: Object-->BeanContextChildSupport(BeanContextChild,BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener)),Serializable)-->BeanContextSupport(BeanContext(BeanContextChild,java.util.Collection,java.beans.DesignMode,java.beans.Visibility),java.beans.PropertyChangeListener(java.util.EventListener),Serializable,java.beans.VetoableChangeListener(java.util.EventListener))-->BeanContextServicesSupport(BeanContextServices(BeanContext(BeanContextChild,java.util.Collection,java.beans.DesignMode,java.beans.Visibility),BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener))))
This class is used internally by BeanContextServicesSupport to associate additional information with each child of the bean context. It has no public or protected method or fields, but may be customized by subclassing.
This inner class is used internally by BeanContextServicesSupport to properly handle delegation to the services provided by containing bean contexts. It implements the BeanContextServiceProvider interface in terms of the methods of a containing BeanContextServices object.
Type Of: BeanContextServicesSupport.proxy
This inner class is a trivial wrapper around a BeanContextServiceProvider object. Subclasses that want to associate additional information with each service provider can subclass this class and override the createBCSSServiceProvider() method of BeanContextServicesSupport.
Returned By: BeanContextServicesSupport.createBCSSServiceProvider()
This class provides a simple, easily customizable implementation of BeanContext. Most bean context implementors find it easier to subclass BeanContextSupport or create a BeanContextSupport delegate object rather than implement the BeanContext interface from scratch. Bean contexts are often AWT or Swing containers and cannot (because of a method-naming conflict) implement BeanContext. Therefore, a context object implements the BeanContextProxy interface and returns a BeanContext object from its getBeanContextProxy() method. A BeanContextSupport object is a suitable object to return from this method. Some bean contexts require customized behavior, however, and BeanContextSupport is designed to be easily customized through subclassing. Protected methods such as childJustAddedHook() and validatePendingAdd() are particularly useful when subclassing.
Hierarchy: Object-->BeanContextChildSupport(BeanContextChild,BeanContextServicesListener(BeanContextServiceRevokedListener(java.util.EventListener)),Serializable)-->BeanContextSupport(BeanContext(BeanContextChild,java.util.Collection,java.beans.DesignMode,java.beans.Visibility),java.beans.PropertyChangeListener(java.util.EventListener),Serializable,java.beans.VetoableChangeListener(java.util.EventListener)) Subclasses: BeanContextServicesSupport
This class is used internally by BeanContextSupport to keep track of additional information about its children. In particular, for children that implement the BeanContextProxy interface, it keeps track of the BeanContextChild object associated with the child. This class does not define any public or protected fields or methods. BeanContextSupport subclasses that want to associate additional information with each child can subclass this class and override the createBCSChild() method to instantiate the new subclass.
Subclasses: BeanContextServicesSupport.BCSSChild Passed To: BeanContextServicesSupport.childJustRemovedHook(), BeanContextSupport.{childDeserializedHook(), childJustAddedHook(), childJustRemovedHook()} Returned By: BeanContextServicesSupport.createBCSChild(), BeanContextSupport.createBCSChild()
This class implements the java.util.Iterator interface. An instance of this class is returned by the iterator() method implemented by BeanContextSupport. The remove() method has an empty implementation and does not actually remove a child of the bean context.
Copyright © 2001 O'Reilly & Associates. All rights reserved. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|