41
TM Introduction to JavaBeans™ Introduction to JavaBeans™ Dimitrios Psarros Dimitrios Psarros Questra Consulting Questra Consulting [email protected] [email protected] (716)381-0260 x225 (716)381-0260 x225

TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting [email protected] (716)381-0260 x225

Embed Size (px)

Citation preview

Page 1: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Introduction to JavaBeans™Introduction to JavaBeans™

Dimitrios PsarrosDimitrios PsarrosQuestra ConsultingQuestra [email protected]@questra.com(716)381-0260 x225(716)381-0260 x225

Page 2: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

OutlineOutline

Component Software EngineeringComponent Software EngineeringEventEventPropertiesPropertiesPersistence and VersioningPersistence and VersioningIntrospection and CustomizationIntrospection and CustomizationPackaging and DeploymentPackaging and DeploymentFuture DirectionsFuture Directions

Page 3: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Component FrameworkComponent Framework

ComponentComponent– A reusable software module that is supplied in a A reusable software module that is supplied in a

binary form and can be used to compose binary form and can be used to compose applicationsapplications

ContainerContainer– Provides the context that components can be Provides the context that components can be

assembled and interact with one anotherassembled and interact with one another

Component ModelComponent Model– Defines the architecture of how components can Defines the architecture of how components can

interact in a dynamic environmentinteract in a dynamic environment

Page 4: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Component Model FeaturesComponent Model Features

Property ManagementProperty ManagementEvent HandlingEvent HandlingPersistence and VersioningPersistence and VersioningPortability and InteroperabilityPortability and InteroperabilityDistributed Computing SupportDistributed Computing Support

Page 5: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

What is a JavaBean?What is a JavaBean?

“A JavaBean is a reusable software component that can be manipulated visually

in a builder tool”

Page 6: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

JavaBeans ObjectivesJavaBeans Objectives

Provide a platform neutral component Provide a platform neutral component architecturearchitecture

Simple to developSimple to developLeverage existing Java technologiesLeverage existing Java technologiesProvide design-time support for visual builder Provide design-time support for visual builder toolstools

Page 7: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

JavaBeans CharacteristicsJavaBeans Characteristics

PropertiesPropertiesEventEventPersistencePersistenceIntrospectionIntrospectionCustomizationCustomization

Page 8: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

PropertiesProperties

Discrete, named attributes that determine the Discrete, named attributes that determine the appearance and behavior and state of a appearance and behavior and state of a componentcomponent

Accessible programmatically through Accessible programmatically through accessor methodsaccessor methods

Accessible visually through property sheetsAccessible visually through property sheetsExposed as object fields in a scripting Exposed as object fields in a scripting environmentenvironment

Page 9: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Property TypesProperty Types

Simple PropertiesSimple PropertiesBound PropertiesBound PropertiesConstrained PropertiesConstrained Properties

Page 10: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Simple PropertiesSimple Properties

Represent a single valueRepresent a single valueThe accessor methods should follow The accessor methods should follow standard naming conventionsstandard naming conventions

public <PropertyType> get<PropertyName>();

public void set<PropertyName>(<PropertyType> value);

Example:

public String getHostName();

public void setHostName( String hostName );

Page 11: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Boolean PropertiesBoolean Properties

They are simple propertiesThey are simple propertiesThe getter methods follow an optional design The getter methods follow an optional design patternpattern

public boolean is<PropertyName>();

Example:

public boolean isConnected();

Page 12: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Indexed PropertiesIndexed Properties

Represent an array of valuesRepresent 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);

Page 13: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Bound PropertiesBound Properties

Registered listeners object are notified when Registered listeners object are notified when the value of the property changesthe value of the property changes

Listeners must implement the Listeners must implement the java.beans.PropertyChangeListener java.beans.PropertyChangeListener interfaceinterface

propertyChange(PropertyChangeEvent event);propertyChange(PropertyChangeEvent event);

Page 14: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Bound Properties Support FrameworkBound Properties Support Framework

TerminalBean

hostNameportNumber

addPropertyChangeListener()removePropertyChangeListener()

PropertyChangeEvent

source

newValue

propertyNameoldValue

generatesuses notifies

PropertyChangeListener

propertyChange()

<<interface>>PropertyChangeSupport

addPropertyChangeListener()removePropertyChangeListener()

firePropertyChange()

Page 15: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Bound Properties Notification MechanismBound Properties Notification Mechanism

:PropertyEditor

:Bean

:PropertyChangeListener

:PropertyChangeSupport

1. addPropertyChangeListener

2. addPropertyChangeListener

3. SetHostName(“hostName”)

4. firePropertyChange(event)

5. propertyChange(event)

Page 16: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Constrained PropertiesConstrained Properties

Allow registered listeners to validate a Allow registered listeners to validate a proposed changeproposed change

Listeners must implement the Listeners must implement the java.beans.VetoablecChangeListenerjava.beans.VetoablecChangeListener interfaceinterface

vetoableChange( PropertyChangeEvent event )vetoableChange( PropertyChangeEvent event )throws PropertyVetoException;throws PropertyVetoException;

Page 17: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Constrained Properties Support Constrained Properties Support FrameworkFramework

Bean

hostNameportNumber

addVetoableChangeListener()removeVetoableChangeListener()

PropertyChangeEvent

source

newValue

propertyNameoldValue

VetoableChangeSupport

addVetoableChangeListener()removeVetoableChangeListener()

generatesuses notifies

VetoableChangeListener

vetoableChange()

<<interface>>

Page 18: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Constrained Properties - ExampleConstrained 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 );}

Page 19: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

DemoDemo

Terminal BeanTerminal Bean– Bound PropertiesBound Properties

• connectedconnected• connectedColorconnectedColor• disconnectedColordisconnectedColor

– Constrained PropertiesConstrained Properties• hostNamehostName• portNumberportNumber

– MethodsMethods• connect()connect()• disconnect()disconnect()

Page 20: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Overview of Event ModelOverview of Event Model

Provides a notification mechanism between a Provides a notification mechanism between a source object and one or more listener source object and one or more listener objectsobjects

Source or listener objects does not need to Source or listener objects does not need to be graphical componentsbe graphical components

Supports introspectionSupports introspectionExtensibleExtensible

Page 21: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event Delegation ModelEvent Delegation Model

Listener

Event Source

Event

3. Notify

2. Generate

1. Register

Page 22: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event ObjectsEvent Objects

Encapsulate notification propertiesEncapsulate notification propertiesInherit from the Inherit from the java.util.EventObjectjava.util.EventObject class class

Page 23: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event Object - ExampleEvent 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;

}

Page 24: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event ListenersEvent Listeners

Implement an interface that is defined by the Implement an interface that is defined by the sourcesource

Register with source using published Register with source using published registration methodregistration method

Listener interfacesListener interfaces– Inherit from java.util.EventListenerInherit from java.util.EventListener– Have an individual method for each event typeHave an individual method for each event type– Related event handling methods are usually grouped Related event handling methods are usually grouped

in the same interfacein the same interface

Page 25: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event Listeners - ExampleEvent Listeners - Example

public interface CursorListener extends java.util.EventListener

{

public void cursorMoved( CursorEvent cursorEvent);

}

Page 26: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event SourcesEvent Sources

Provide methods for registering and de-Provide methods for registering and de-registering event listenersregistering event listeners

– Singlecast supportSinglecast support

public void add<ListenerType>(<ListenerType> listener)public void add<ListenerType>(<ListenerType> listener)throws java.util.TooManyListenersException;throws java.util.TooManyListenersException;

public void remove<ListenerType>(<ListenerType> listener)public void remove<ListenerType>(<ListenerType> listener)

– Multicast supportMulticast support

public void add<ListenerType>(<ListenerType> listener)public void add<ListenerType>(<ListenerType> listener)

public void remove<ListenerType>(<ListenerType> listener)public void remove<ListenerType>(<ListenerType> listener)

Page 27: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Event Source - ExampleEvent Source - Example

public class TerminalBean{ … public void addCursorListener( CursorListener listener ) { } public void removeCursorListener( CursorListener listener ) { } ...}

Page 28: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

PersistencePersistence

Java object serialization facilities provide an Java object serialization facilities provide an automatic mechanism to store out and automatic mechanism to store out and restore the internal state of an object to/from restore the internal state of an object to/from a streama stream

Java externalization mechanism allow a Java Java externalization mechanism allow a Java object to have complete control over the object to have complete control over the format of the streamformat of the stream

Page 29: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Serialization ExampleSerialization Example

public class TerminalBean implements java.io.Serializable{

private String hostName;private transient boolean isConnected;static final long serialVersionUID = -3283293045227786848L;

}

ObjectOutputStream objectOutputStream=new ObjectOutputStream(outStream );outputStream.writeObject( new TerminaBean() );...ObjectInputStream objectInputStream = new ObjectInputStream( inStream );TerminalBean terminal = (TerminalBean) objectInputStream.readObject( );

Saving and Retrieving component state:Saving and Retrieving component state:

Page 30: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

VersioningVersioning

Java Serialization support:Java Serialization support:– Reading streams written by order version of the Reading streams written by order version of the

same classsame class– Writing stream intended to be read by order Writing stream intended to be read by order

version of the same classversion of the same class– Identify and load streams that match the exact Identify and load streams that match the exact

class used to write the streamclass used to write the stream

Page 31: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

IntrospectionIntrospection

Discovering the properties, events, and Discovering the properties, events, and method that a component supportsmethod that a component supports

Two methods to perform introspectionTwo methods to perform introspection– Use low level reflection mechanism-Use low level reflection mechanism-– Explicit specification using the Explicit specification using the BeanInfoBeanInfo class class

Page 32: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Reflection MechanismReflection Mechanism

Uses reflection facilities of the Java API to Uses reflection facilities of the Java API to determine the public methods, fields of the determine the public methods, fields of the Java BeanJava Bean

Apply design patterns to determine Apply design patterns to determine properties, methods, and events that the properties, methods, and events that the JavaBean supportsJavaBean supports

Page 33: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

BeanInfoBeanInfo

Provides explicit information about a Provides explicit information about a JavaBeanJavaBean

Defined by the creator of the beanDefined by the creator of the beanDoes not need to provide complete Does not need to provide complete informationinformation

Page 34: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

CustomizationCustomization

Customize the appearance and behavior of a Customize the appearance and behavior of a component in a design time environmentcomponent in a design time environment

Customization is supported through:Customization is supported through:– Property EditorsProperty Editors– Property SheetsProperty Sheets– CustomizersCustomizers

Page 35: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Property EditorsProperty Editors

Visual element for editing a specific Java Visual element for editing a specific Java TypeType

For standard types editors are providedFor standard types editors are providedLocated using the Located using the PropertyEditorMangerPropertyEditorMangerDisplayed on Property sheetsDisplayed on Property sheets

Page 36: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Property SheetsProperty Sheets

Consist of all editors necessary to edit the Consist of all editors necessary to edit the properties of a componentproperties of a component

Keep track of which editor’s values changeKeep track of which editor’s values changeUpdate bean after each changeUpdate bean after each change

Page 37: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

CustomizersCustomizers

More elaborate visual interfaces to edit the More elaborate visual interfaces to edit the properties of a bean in a multiple step properties of a bean in a multiple step processprocess

Act like wizards or expertsAct like wizards or expertsBuilder tools register with the customizers for Builder tools register with the customizers for property changes and update bean after each property changes and update bean after each changechange

Page 38: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Packaging and DeploymentPackaging and Deployment

JAR (Java ARchive) files are the primary JAR (Java ARchive) files are the primary method of packaging and distributing method of packaging and distributing JavaBeansJavaBeans

A JAR file contains:A JAR file contains:– Class filesClass files– Serialized prototypes of a beanSerialized prototypes of a bean– Documentation filesDocumentation files– ResourcesResources

Page 39: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

Jar File ExampleJar File ExampleSRC_DIR=com/questra/beansCLASSFILES= \

$(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.jarall: $(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)

Page 40: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

FutureFuture

"Glasgow"Glasgow" – Extensible runtime containment and services Extensible runtime containment and services

protocolprotocol– Drag and Drop subsystemDrag and Drop subsystem– JavaBeans Activation frameworkJavaBeans Activation framework

Enterprise Java BeansEnterprise Java Beans– Build distributed, scalable, transactional OO Build distributed, scalable, transactional OO

business applicationsbusiness applications– Runtime environment Runtime environment – Interoperability with non-Java applicationsInteroperability with non-Java applications– Compatible with CORBACompatible with CORBA

Page 41: TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225

TM

ResourcesResources

JavaBeans Home PageJavaBeans Home Page– http://java.sun.com/beans/http://java.sun.com/beans/

GlasgowGlasgow– http://java.sun.com/beans/glasgow/http://java.sun.com/beans/glasgow/

BooksBooks– Michael Morrison, Michael Morrison, presenting JavaBeans,presenting JavaBeans,

Sams.net:Indianapolis, 1997.Sams.net:Indianapolis, 1997.– Dan Brookshier, Dan Brookshier, JavaBeans Developer’s JavaBeans Developer’s

ReferenceReference, News Riders Publishing:Indianapolis, , News Riders Publishing:Indianapolis, 1997.1997.