Introduction to JavaBeans™
Download
Report
Transcript Introduction to JavaBeans™
Introduction to JavaBeans™
Dimitrios Psarros
Questra Consulting
[email protected]
(716)381-0260 x225
TM
Outline
Component Software Engineering
Event
Properties
Persistence and Versioning
Introspection and Customization
Packaging and Deployment
Future Directions
TM
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
TM
Component Model Features
Property Management
Event Handling
Persistence and Versioning
Portability and Interoperability
Distributed Computing Support
TM
What is a JavaBean?
“A JavaBean is a reusable software
component that can be manipulated visually in
a builder tool”
TM
JavaBeans Objectives
Provide a platform neutral component
architecture
Simple to develop
Leverage existing Java technologies
Provide design-time support for visual builder
tools
TM
JavaBeans Characteristics
Properties
Event
Persistence
Introspection
Customization
TM
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
TM
Property Types
Simple Properties
Bound Properties
Constrained Properties
TM
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 );
TM
Boolean Properties
They are simple properties
The getter methods follow an optional design
pattern
public boolean is<PropertyName>();
Example:
public boolean isConnected();
TM
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);
TM
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);
TM
Bound Properties Support Framework
TerminalBean
hostName
portNumber
addPropertyChangeListener()
removePropertyChangeListener()
uses
generates
PropertyChangeSupport
PropertyChangeEvent
addPropertyChangeListener()
removePropertyChangeListener()
firePropertyChange()
source
propertyName
oldValue
newValue
notifies
PropertyChangeListener
<<interface>>
propertyChange()
TM
Bound Properties Notification Mechanism
:PropertyEditor
:PropertyChangeListener
1. addPropertyChangeListener
3. SetHostName(“hostName”)
:Bean
2. addPropertyChangeListener
4. firePropertyChange(event)
:PropertyChangeSupport
5. propertyChange(event)
TM
Constrained Properties
Allow registered listeners to validate a
proposed change
Listeners must implement the
java.beans.VetoablecChangeListener
interface
vetoableChange( PropertyChangeEvent event )
throws PropertyVetoException;
TM
Constrained Properties Support
Framework
Bean
hostName
portNumber
addVetoableChangeListener()
removeVetoableChangeListener()
uses
generates
VetoableChangeSupport
PropertyChangeEvent
addVetoableChangeListener()
removeVetoableChangeListener()
source
propertyName
oldValue
newValue
notifies
VetoableChangeListener
<<interface>>
vetoableChange()
TM
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 );
}
TM
Demo
Terminal Bean
– Bound Properties
• connected
• connectedColor
• disconnectedColor
– Constrained Properties
• hostName
• portNumber
– Methods
• connect()
• disconnect()
TM
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
TM
Event Delegation Model
Listener
1. Register
3. Notify
Event Source
2. Generate
Event
TM
Event Objects
Encapsulate notification properties
Inherit from the java.util.EventObject class
TM
Event Object - Example
public class CursorEvent extends java.util.EventObject
{
public CursorEvent(Object source,Point location)
{
super( source );
this.location = location;
}
Point getLocation() { return location; }
private Point location;
}
TM
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
TM
Event Listeners - Example
public interface CursorListener extends java.util.EventListener
{
public void cursorMoved( CursorEvent cursorEvent);
}
TM
Event Sources
Provide methods for registering and deregistering 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)
TM
Event Source - Example
public class TerminalBean
{
…
public void addCursorListener( CursorListener listener )
{
}
public void removeCursorListener( CursorListener listener )
{
}
...
}
TM
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
TM
Serialization Example
public class TerminalBean implements java.io.Serializable
{
private String hostName;
private transient boolean isConnected;
static final 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( );
TM
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
TM
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
TM
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
TM
BeanInfo
Provides explicit information about a
JavaBean
Defined by the creator of the bean
Does not need to provide complete
information
TM
Customization
Customize the appearance and behavior of a
component in a design time environment
Customization is supported through:
– Property Editors
– Property Sheets
– Customizers
TM
Property Editors
Visual element for editing a specific Java
Type
For standard types editors are provided
Located using the PropertyEditorManger
Displayed on Property sheets
TM
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
TM
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
TM
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
TM
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)
TM
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
TM
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.
TM