LS.3. JavaBeans Components and Design Patterns

LS.3.1. Allowing Display and Modification of Attributes

We use JavaBeans components to provide a layer of abstraction on top of the individual classes that implement the net.jini.core.entry.Entry interface. This provides us with several benefits:

  • This approach uses an existing standard and thus reduces the amount of unfamiliar material for programmers.

  • JavaBeans components provide mechanisms for localized display of attribute values and descriptions.

  • Modification of attributes is also handled, via property editors.

LS.3.1.1. Using JavaBeans Components with Entry Classes

Many, if not most, entry classes should have a bean class associated with them. Our use of JavaBeans components provides a familiar mechanism for authors of browse/search tools to represent information about a service’s attributes, such as its icons and appropriately localized descriptions of the meanings and values of its attributes. JavaBeans components also play a role in permitting administrators of a service to modify some of its attributes, as they can manipulate the values of its attributes using standard JavaBeans component mechanisms.

For example, obtaining a java.beans.BeanDescriptor for a JavaBeans component that is linked to a “location” entry object for a particular service allows a programmer to obtain an icon that gives a visual indication of what that entry class is for, along with a short textual description of the class and the values of the individual attributes in the location object. It also permits an administrative tool to view and change certain fields in the location, such as the floor number.

LS.3.2. Associating JavaBeans Components with Entry Classes

The pattern for establishing a link between an entry object and an instance of its JavaBeans component is simple enough, as this example illustrates:

package org.example.foo; 

import java.io.Serializable; 
import net.jini.lookup.entry.EntryBean; 
import net.jini.entry.AbstractEntry; 

public class Size {
    public int value; 
} 

public class Cavenewt extends AbstractEntry {
    public Cavenewt() {
    } 
    public Cavenewt(Size anvilSize) {
        this.anvilSize = anvilSize; 
    } 
    public Size anvilSize; 
} 

public class CavenewtBean implements EntryBean, Serializable {
    protected Cavenewt assoc; 
    public CavenewtBean() {
        super(); 
        assoc = new Cavenewt(); 
    } 
    public void setAnvilSize(Size x) {
        assoc.anvilSize = x; 
    } 
    public Size getAnvilSize() {
        return assoc.anvilSize; 
    } 
    public void makeLink(Entry obj) {
         assoc = (Cavenewt) obj; 
    } 
    public Entry followLink() {
        return assoc; 
    } 
} 

From the above, the pattern should be relatively clear:

  • The name of a JavaBeans component is derived by taking the fully qualified entry class name and appending the string Bean; for example, the name of the JavaBeans component associated with the entry class foo.bar.Baz is foo.bar.BazBean. This implies that an entry class and its associated JavaBeans component must reside in the same package.

  • The class has both a public no-arg constructor and a public constructor that takes each public object field of the class and its superclasses as parameter. The former constructs an empty instance of the class, and the latter initializes each field of the new instance to the given parameter.

  • The class implements the net.jini.core.entry.Entry interface, preferably by extending the net.jini.entry.AbstractEntry class, and the JavaBeans component implements the net.jini.lookup.entry.EntryBean interface.

  • There is a one-to-one link between a JavaBeans component and a particular entry object. The makeLink method establishes this link and will throw an exception if the association is with an entry class of the wrong type. The followLink method returns the entry object associated with a particular JavaBeans component.

  • The no-arg public constructor for a JavaBeans component creates and makes a link to an empty entry object.

  • For each public object field foo in an entry class, there exist both a setFoo and a getFoo method in the associated JavaBeans component. The setFoo method takes a single argument of the same type as the foo field in the associated entry and sets the value of that field to its argument. The getFoo method returns the value of that field.

LS.3.3. Supporting Interfaces and Classes

The following classes and interfaces provide facilities for handling entry classes and their associated JavaBeans components.

package net.jini.lookup.entry; 

public class EntryBeans {
    public static EntryBean createBean(Entry e) 
        throws ClassNotFoundException, java.io.IOException {...} 

    public static Class getBeanClass(Class c) 
        throws ClassNotFoundException {...} 
} 

public interface EntryBean {
    void makeLink(Entry e); 
    Entry followLink(); 
} 

The EntryBeans class cannot be instantiated. Its sole method, createBean, creates and initializes a new JavaBeans component and links it to the entry object it is passed. If a problem occurs creating the JavaBeans component, the method throws either java.io.IOException or ClassNotFoundException.

The createBean method uses the same mechanism for instantiating a JavaBeans component as the java.beans.Beans.instantiate method. It will initially try to instantiate the JavaBeans component using the same class loader as the entry it is passed. If that fails, it will fall back to using the default class loader.

The getBeanClass method returns the class of the JavaBeans component associated with the given attribute class. If the class passed in does not implement the net.jini.core.entry.Entry interface, an IllegalArgumentException is thrown. If the given attribute class cannot be found, a ClassNotFoundException is thrown.

The EntryBean interface must be implemented by all JavaBeans components that are intended to be linked to entry objects. The makeLink method establishes a link between a JavaBeans component object and an entry object, and the followLink method returns the entry object linked to by a particular JavaBeans component. Note that objects that implement the EntryBean interface should not be assumed to perform any internal synchronization in their implementations of the makeLink or followLink methods, or in the setFoo or getFoo patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67