420 likes | 608 Views
Introduction to JavaBeans™. Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225. Outline. Component Software Engineering Event Properties Persistence and Versioning Introspection and Customization Packaging and Deployment Future Directions.
E N D
Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225
Outline • Component Software Engineering • Event • Properties • Persistence and Versioning • Introspection and Customization • Packaging and Deployment • Future Directions
Component Framework • Component • A reusable software module that is supplied in a binary form and can be used to compose applications • Container • Provides the context that components can be assembled and interact with one another • Component Model • Defines the architecture of how components can interact in a dynamic environment
Component Model Features • Property Management • Event Handling • Persistence and Versioning • Portability and Interoperability • Distributed Computing Support
What is a JavaBean? “A JavaBean is a reusable software component that can be manipulated visually in a builder tool”
JavaBeans Objectives • Provide a platform neutral component architecture • Simple to develop • Leverage existing Java technologies • Provide design-time support for visual builder tools
JavaBeans Characteristics • Properties • Event • Persistence • Introspection • Customization
Properties • Discrete, named attributes that determine the appearance and behavior and state of a component • Accessible programmatically through accessor methods • Accessible visually through property sheets • Exposed as object fields in a scripting environment
Property Types • Simple Properties • Bound Properties • Constrained Properties
Simple Properties • Represent a single value • The accessor methods should follow standard naming conventions public <PropertyType> get<PropertyName>(); public void set<PropertyName>(<PropertyType> value); Example: public String getHostName(); public void setHostName( String hostName );
Boolean Properties • They are simple properties • The getter methods follow an optional design pattern public boolean is<PropertyName>(); Example: public boolean isConnected();
Indexed Properties • Represent an array of values public <PropertyElement> get<PropertyName>(int index); public void set<PropertyName>(int index,<PropertyElement> value); public <PropertyElement>[] get<PropertyName>(); public void set<PropertyName>(<PropertyElement>[] values); Example: public Color setPalette(int index); public void setPalette(int index,Color value); public Color[] getPalette(); public void setPalette(Color[] values);
Bound Properties • Registered listeners object are notified when the value of the property changes • Listeners must implement the java.beans.PropertyChangeListener interface propertyChange(PropertyChangeEvent event);
TerminalBean hostName portNumber addPropertyChangeListener() removePropertyChangeListener() PropertyChangeSupport PropertyChangeListener PropertyChangeEvent <<interface>> source addPropertyChangeListener() propertyName propertyChange() removePropertyChangeListener() oldValue firePropertyChange() newValue Bound Properties Support Framework generates notifies uses
Bound Properties Notification Mechanism :PropertyEditor :PropertyChangeListener 1. addPropertyChangeListener 3. SetHostName(“hostName”) :Bean 2. addPropertyChangeListener 4. firePropertyChange(event) :PropertyChangeSupport 5. propertyChange(event)
Constrained Properties • Allow registered listeners to validate a proposed change • Listeners must implement the java.beans.VetoablecChangeListener interface vetoableChange( PropertyChangeEvent event ) throws PropertyVetoException;
Bean hostName portNumber addVetoableChangeListener() removeVetoableChangeListener() VetoableChangeSupport PropertyChangeEvent source addVetoableChangeListener() propertyName removeVetoableChangeListener() oldValue newValue Constrained Properties Support Framework uses generates notifies VetoableChangeListener <<interface>> vetoableChange()
Constrained Properties - Example public void setHostName( String newHostName ) throws java.beans.PropertyVetoException { String oldHostName = this.hostName; // First tell the vetoers about the change. If anyone objects, we // don't catch the exception but just let if pass on to our caller. vetoableChangeSupport.fireVetoableChange( "hostName", oldHostName, newHostName ); // change accepted; update state this.hostName = newHostName; // notify property change listeners propertyChangeSupport.firePropertyChange("hostName", oldHostName, newHostName ); }
Demo • Terminal Bean • Bound Properties • connected • connectedColor • disconnectedColor • Constrained Properties • hostName • portNumber • Methods • connect() • disconnect()
Overview of Event Model • Provides a notification mechanism between a source object and one or more listener objects • Source or listener objects does not need to be graphical components • Supports introspection • Extensible
Event Delegation Model Listener 1. Register 3. Notify Event Source 2. Generate Event
Event Objects • Encapsulate notification properties • Inherit from the java.util.EventObject class
Event Object - Example publicclass CursorEvent extends java.util.EventObject { public CursorEvent(Object source,Point location) { super( source ); this.location = location; } Point getLocation() { return location; } private Point location; }
Event Listeners • Implement an interface that is defined by the source • Register with source using published registration method • Listener interfaces • Inherit from java.util.EventListener • Have an individual method for each event type • Related event handling methods are usually grouped in the same interface
Event Listeners - Example publicinterface CursorListener extends java.util.EventListener { public void cursorMoved( CursorEvent cursorEvent); }
Event Sources • Provide methods for registering and de-registering event listeners • Singlecast support public void add<ListenerType>(<ListenerType> listener) throws java.util.TooManyListenersException; public void remove<ListenerType>(<ListenerType> listener) • Multicast support public void add<ListenerType>(<ListenerType> listener) public void remove<ListenerType>(<ListenerType> listener)
Event Source - Example publicclass TerminalBean { … public void addCursorListener( CursorListener listener ) { } public void removeCursorListener( CursorListener listener ) { } ... }
Persistence • Java object serialization facilities provide an automatic mechanism to store out and restore the internal state of an object to/from a stream • Java externalization mechanism allow a Java object to have complete control over the format of the stream
Serialization Example publicclass TerminalBean implements java.io.Serializable { private String hostName; privatetransient boolean isConnected; staticfinal long serialVersionUID = -3283293045227786848L; } • Saving and Retrieving component state: ObjectOutputStream objectOutputStream=new ObjectOutputStream(outStream ); outputStream.writeObject( new TerminaBean() ); ... ObjectInputStream objectInputStream = new ObjectInputStream( inStream ); TerminalBean terminal = (TerminalBean) objectInputStream.readObject( );
Versioning • Java Serialization support: • Reading streams written by order version of the same class • Writing stream intended to be read by order version of the same class • Identify and load streams that match the exact class used to write the stream
Introspection • Discovering the properties, events, and method that a component supports • Two methods to perform introspection • Use low level reflection mechanism- • Explicit specification using the BeanInfo class
Reflection Mechanism • Uses reflection facilities of the Java API to determine the public methods, fields of the Java Bean • Apply design patterns to determine properties, methods, and events that the JavaBean supports
BeanInfo • Provides explicit information about a JavaBean • Defined by the creator of the bean • Does not need to provide complete information
Customization • Customize the appearance and behavior of a component in a design time environment • Customization is supported through: • Property Editors • Property Sheets • Customizers
Property Editors • Visual element for editing a specific Java Type • For standard types editors are provided • Located using the PropertyEditorManger • Displayed on Property sheets
Property Sheets • Consist of all editors necessary to edit the properties of a component • Keep track of which editor’s values change • Update bean after each change
Customizers • More elaborate visual interfaces to edit the properties of a bean in a multiple step process • Act like wizards or experts • Builder tools register with the customizers for property changes and update bean after each change
Packaging and Deployment • JAR (Java ARchive) files are the primary method of packaging and distributing JavaBeans • A JAR file contains: • Class files • Serialized prototypes of a bean • Documentation files • Resources
Jar File Example SRC_DIR=com/questra/beans CLASSFILES= \ $(SRC_DIR)\Terminal.class \ $(SRC_DIR)\CursorEvent.class \ $(SRC_DIR)\CursorListener.class \ $(SRC_DIR)\TerminalBeanInfo.class \ $(SRC_DIR)\Terminal$$VetoOpenConnection.class DATAFILES= $(SRC_DIR)\TerminalBeanIconColor16.gif JARFILE= bdk\jars\terminal.jar all: $(JARFILE) # Create a JAR file with a suitable manifest. $(JARFILE): $(CLASSFILES) $(DATAFILES) jar cfm $(JARFILE) <<manifest.tmp $(SRC_DIR)\*.class $(DATAFILES) Name: com/questra/beans/Terminal.classJava-Bean: True <<. SUFFIXES: .java .class {$(SRC_DIR)}.java{$(SRC_DIR)}.class : javac $< clean: -del $(SRC_DIR)\*.class -del $(JARFILE)
Future • "Glasgow" • Extensible runtime containment and services protocol • Drag and Drop subsystem • JavaBeans Activation framework • Enterprise Java Beans • Build distributed, scalable, transactional OO business applications • Runtime environment • Interoperability with non-Java applications • Compatible with CORBA
Resources • JavaBeans Home Page • http://java.sun.com/beans/ • Glasgow • http://java.sun.com/beans/glasgow/ • Books • Michael Morrison, presenting JavaBeans, Sams.net:Indianapolis, 1997. • Dan Brookshier, JavaBeans Developer’s Reference, News Riders Publishing:Indianapolis, 1997.