Upload
nagarajuraginaboyina
View
217
Download
0
Embed Size (px)
Citation preview
7/29/2019 98149998-Java-Beans
1/42
Click to edit Master subtitle style
3/6/13
JAVA BEANS
7/29/2019 98149998-Java-Beans
2/42
3/6/13
Component Framework Component
A reusable software module that is supplied in abinary form and can be used to composeapplications
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
7/29/2019 98149998-Java-Beans
3/42
3/6/13
Component Model Features
Property Management
Event Handling
Persistence and Versioning Portability and Interoperability
Distributed Computing Support
7/29/2019 98149998-Java-Beans
4/42
3/6/13
What is a JavaBean?
7/29/2019 98149998-Java-Beans
5/42
3/6/13
JavaBeans are reusablesoftwarecomponents
a reusable software component that can be visuallymanipulated in builder tools
They are classes written in theJava programming
language conforming to a particular convention
They are used to encapsulate many objects into a
single object (the bean)
So that they can be passed around as a single beanobject instead of as multiple individual objects
http://en.wikipedia.org/wiki/Code_reusehttp://en.wikipedia.org/wiki/Component-based_software_engineeringhttp://en.wikipedia.org/wiki/Component-based_software_engineeringhttp://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Component-based_software_engineeringhttp://en.wikipedia.org/wiki/Component-based_software_engineeringhttp://en.wikipedia.org/wiki/Code_reuse7/29/2019 98149998-Java-Beans
6/42
3/6/13
Serialization
serialization is the process of converting adata structure or object state into a format that canbe stored
"resurrected" later in the same or another computerenvironment
When the resulting series of bits is reread accordingto the serialization format, it can be used to create asemantically identical clone of the original object.
A JavaBean is a Java Object that is serializable, has-
http://en.wikipedia.org/wiki/Data_structurehttp://en.wikipedia.org/wiki/Object_(computer_science)http://en.wikipedia.org/wiki/Serializationhttp://en.wikipedia.org/wiki/Nullary_constructorhttp://en.wikipedia.org/wiki/Nullary_constructorhttp://en.wikipedia.org/wiki/Nullary_constructorhttp://en.wikipedia.org/wiki/Serializationhttp://en.wikipedia.org/wiki/Object_(computer_science)http://en.wikipedia.org/wiki/Data_structure7/29/2019 98149998-Java-Beans
7/42
3/6/13
Advantages A Bean obtains all of the benefits of Java's "write
once, run anywhere" paradigm.
The properties, events, and methods of a Bean that
are exposed to another application can be controlled.
Auxiliary software can be provided to help configurea Bean.
The configuration settings of a Bean can be saved ina persistent storage and can be restored at a latertime.
7/29/2019 98149998-Java-Beans
8/42
3/6/13
Dis-Advantages A class with a nullary constructor is subject to being
instantiated in an invalid state.
If such a class is instantiated manually by a
developer (rather than automatically by some kind offramework), the developer might not realize that hehas instantiated the class in an invalid state.
Having to create a getter for every property and asetter for many, most, or all of them, creates animmense amount ofboilerplate code.
http://en.wikipedia.org/wiki/Nullary_constructorhttp://en.wikipedia.org/wiki/Boilerplate_codehttp://en.wikipedia.org/wiki/Boilerplate_codehttp://en.wikipedia.org/wiki/Nullary_constructor7/29/2019 98149998-Java-Beans
9/42
3/6/13
JavaBean Conventions In order to function as a JavaBean class, an object
class must obey certain conventions about methodnaming, construction, and behaviour. The requiredconventions are as follows:
The class must have a public default constructor(no-argument). This allows easy instantiation within
editing and activation frameworks.
The class properties must be accessibleusing get, set, is (used for boolean propertiesinstead of get) and other methods (so-calledaccessor methods ) following a standard namingconvention.
http://en.wikipedia.org/wiki/Naming_conventions_(programming)http://en.wikipedia.org/wiki/Class_(computer_science)http://en.wikipedia.org/wiki/Default_constructorhttp://en.wikipedia.org/wiki/Property_(programming)http://en.wikipedia.org/wiki/Accessorhttp://en.wikipedia.org/wiki/Naming_conventions_(programming)http://en.wikipedia.org/wiki/Naming_conventions_(programming)http://en.wikipedia.org/wiki/Naming_conventions_(programming)http://en.wikipedia.org/wiki/Naming_conventions_(programming)http://en.wikipedia.org/wiki/Accessorhttp://en.wikipedia.org/wiki/Property_(programming)http://en.wikipedia.org/wiki/Default_constructorhttp://en.wikipedia.org/wiki/Class_(computer_science)7/29/2019 98149998-Java-Beans
10/42
3/6/13
JavaBeans Objectives
Provide a platform neutralcomponent architecture
Simple to develop Leverage existing Java technologies
Provide design-time support for
visual builder tools
7/29/2019 98149998-Java-Beans
11/42
3/6/13
JavaBeans Characteristics
Properties
Event
Persistence Introspection
Customization
7/29/2019 98149998-Java-Beans
12/42
3/6/13
Properties
Discrete, named attributes thatdetermine the appearance andbehavior and state of a component
Accessible programmatically throughaccessor methods
Accessible visually through propertysheets
Exposed as object fields in a scriptingenvironment
7/29/2019 98149998-Java-Beans
13/42
3/6/13
Property Types
Simple Properties
Bound Properties
Constrained Properties
7/29/2019 98149998-Java-Beans
14/42
3/6/13
Simple Properties
Represent a single valueThe accessor methods should follow
standard naming conventions
public get();
public void set( value);
Example:
public String getHostName();
public void setHostName( String hostName );
7/29/2019 98149998-Java-Beans
15/42
3/6/13
Boolean Properties
They are simple properties
The getter methods follow anoptional design pattern
public boolean is();
Example:
public boolean isConnected();
7/29/2019 98149998-Java-Beans
16/42
3/6/13
Indexed Properties
Represent an array of valuespublic get(int index);
public void set(int index, value);
public [] get();
public void set([] values);
Example:
public Color setPalette(int index);
public void setPalette(int index,Colorvalue);
public Color[] getPalette();
public void setPalette(Color[] values);
7/29/2019 98149998-Java-Beans
17/42
3/6/13
Bound Properties
Registered listeners object arenotified when the value of theproperty changes
Listeners must implement thejava.beans.PropertyChangeListenerinterface
propertyChange(PropertyChangeEvent event);
7/29/2019 98149998-Java-Beans
18/42
3/6/13
Bound Properties SupportFramework
TerminalBeanhostNameportNumb
eraddPropertyChangeListener()removePropertyChangeListene
r()
PropertyChangeEvent
source
newValue
propertyNameoldVal
ue
generates
uses
notifies
PropertyChangeListener
propertyChange()
PropertyChangeSupport
addPropertyChangeListener()removePropertyChangeListene
r() firePropertyChange()
7/29/2019 98149998-Java-Beans
19/42
3/6/13
Bound PropertiesNotification Mechanism
:PropertyEditor
:Bean
:PropertyChangeListener
:PropertyChangeSupport
1.
addPropertyChangeListener
2.addPropertyChangeListener
3.SetHostName(hostName)
4.firePropertyChange(event)
5.propertyChange(eve
7/29/2019 98149998-Java-Beans
20/42
3/6/13
Constrained Properties
Allow registered listeners to validatea proposed change
Listeners must implement thejava.beans.VetoablecChangeListenerinterface
vetoableChange( PropertyChangeEvent event )
throws PropertyVetoException;
7/29/2019 98149998-Java-Beans
21/42
3/6/13
Constrained PropertiesSupport Framework
Bean
hostNameportNumb
eraddVetoableChangeListener()removeVetoableChangeListene
r()
PropertyChangeEve
nt source
newValue
propertyNameoldVal
ue
VetoableChangeSuppo
rtaddVetoableChangeListener()removeVetoableChangeListene
r()
generates
uses
notifies
VetoableChangeL
istener
vetoableChange()
7/29/2019 98149998-Java-Beans
22/42
3/6/13
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 listenerspropertyChangeSupport.firePropertyChange("hostName",
oldHostName, newHostName );}
7/29/2019 98149998-Java-Beans
23/42
3/6/13
Demo
Terminal Bean
Bound Properties
connected
connectedColor
disconnectedColor
Constrained Properties hostName
portNumber
7/29/2019 98149998-Java-Beans
24/42
3/6/13
Overview of Event Model
Provides a notification mechanismbetween a source object and one ormore listener objects
Source or listener objects does notneed to be graphical components
Supports introspection
Extensible
7/29/2019 98149998-Java-Beans
25/42
3/6/13
Event Delegation Model
Listener
Event Source
Event
3. Notify
2. Generate
1.Register
7/29/2019 98149998-Java-Beans
26/42
3/6/13
Event Objects
Encapsulate notification properties
Inherit from thejava.util.EventObjectclass
7/29/2019 98149998-Java-Beans
27/42
3/6/13
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;
}
7/29/2019 98149998-Java-Beans
28/42
3/6/13
Event Listeners
Implement an interface that is definedby the source
Register with source using publishedregistration method
Listener interfaces
Inherit from java.util.EventListener
Have an individual method for each eventtype
Related event handling methods are
7/29/2019 98149998-Java-Beans
29/42
3/6/13
Event Listeners - Example
publicinterface CursorListenerextendsjava.util.EventListener
{
public void cursorMoved( CursorEvent cursorEvent);
}
7/29/2019 98149998-Java-Beans
30/42
3/6/13
Event Sources
Provide methods for registering andde-registering event listeners
Singlecast support
public void add( listener)
throws java.util.TooManyListenersException;
public void remove( listener)
Multicast support
7/29/2019 98149998-Java-Beans
31/42
3/6/13
Event Source - Example
publicclass TerminalBean{
public void addCursorListener( CursorListener listener ){}
public void removeCursorListener( CursorListener listener ){}...
}
7/29/2019 98149998-Java-Beans
32/42
3/6/13
Persistence
Java object serialization facilitiesprovide an automatic mechanism tostore out and restore the internal
state of an object to/from a streamJava externalization mechanism allow
a Java object to have complete
control over the format of the stream
7/29/2019 98149998-Java-Beans
33/42
3/6/13
Serialization Example
Saving and Retrieving componentstate:
publicclass TerminalBean implements java.io.Serializable{
private String hostName;privatetransient boolean isConnected;staticfinal long serialVersionUID = -3283293045227786848L;
}
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outStream );outputStream.writeObject( new TerminaBean() );...ObjectInputStream objectInputStream = new ObjectInputStream( inStream );TerminalBean terminal = (TerminalBean) objectInputStream.readObject( );
7/29/2019 98149998-Java-Beans
34/42
3/6/13
Versioning
Java Serialization support:
Reading streams written by orderversion of the same class
Writing stream intended to be read byorder version of the same class
Identify and load streams that match the
exact class used to write the stream
7/29/2019 98149998-Java-Beans
35/42
3/6/13
Introspection
Discovering the properties, events,and method that a componentsupports
Two methods to performintrospection
Use low level reflection mechanism-
Explicit specification using the BeanInfoclass
7/29/2019 98149998-Java-Beans
36/42
3/6/13
Reflection Mechanism
Uses reflection facilities of the JavaAPI to determine the public methods,fields of the Java Bean
Apply design patterns to determineproperties, methods, and events thatthe JavaBean supports
7/29/2019 98149998-Java-Beans
37/42
3/6/13
BeanInfo
Provides explicit information about aJavaBean
Defined by the creator of the bean
Does not need to provide completeinformation
7/29/2019 98149998-Java-Beans
38/42
3/6/13
Customization
Customize the appearance andbehavior of a component in a designtime environment
Customization is supported through:
Property Editors
Property Sheets Customizers
7/29/2019 98149998-Java-Beans
39/42
3/6/13
Property Editors
Visual element for editing a specificJava Type
For standard types editors areprovided
Located using thePropertyEditorManger
Displayed on Property sheets
7/29/2019 98149998-Java-Beans
40/42
3/6/13
Property Sheets
Consist of all editors necessary toedit the properties of a component
Keep track of which editors valueschange
Update bean after each change
7/29/2019 98149998-Java-Beans
41/42
3/6/13
Customizers
More elaborate visual interfaces toedit the properties of a bean in amultiple step process
Act like wizards or experts
Builder tools register with thecustomizers for property changesand update bean after each change
7/29/2019 98149998-Java-Beans
42/42
Packaging and Deployment
JAR (Java ARchive) files are theprimary method of packaging anddistributing JavaBeans
A JAR file contains:
Class files
Serialized prototypes of a bean
Documentation files
Resources