bYTEBoss L23JavaBeans

Download Report

Transcript bYTEBoss L23JavaBeans

Java Beans
 Java
offers software component
development through java Beans
 Java Beans are based on a software
component model for java.
 The model is designed to allow third party
vendors to create and sell Java
components that can be integrated into
other software products.
 Java
beans is an architecture and platform
independent set of classes for creating
and using Java software components.
 Beans support the software component
model which focuses on the use of
components and containers.
Advantages
 It
has write once, run anywhere paradigm
 A bean may be designed to operate in
different locations which makes it useful in
global market.
 A bean may register to receive events
from other objects and can generate
events that are sent to other objects.
Application Builder Tools







A utility that enables you to configure a set of Beans, connect them
together and produce a working application.
A palette is provided that lists all the available beans. As additional
beans are developed / purchased they can be added to the palette.
Worksheet allows the designer to lay out beans in GUI.
Special editors and customizers allow a bean to be configured.
Commands allow a designer to inquire about the state and behavior
of a bean.
Capabilities exist to interconnect beans.
When a collection of beans have been configured and connected, it
is possible to save all in a persistent storage area. Later on this
information can be used to restore the state of the application.
BDK
 Bean
Developer Kit
 JDK must be installed for BDK.
 BDK can be downloaded from sun java
site.
 Using BDK
Sample Beans

Blue Button


Orange Button


A grey button with additional font properties.
Explicit Button


Same as Blue button
Our Button


A simple button with background, foreground, label and font
properties.
Same as Blue button
EventMonitor

A text area that is used to view events as they happen.

Jelly Bean


Juggler


A simple visual component that draws a colored oval
“jelly bean”.
Represnts a threaded animated component.
Voter

By default the bean will reject all change requests but
change requests will be accepted if the the vetoAll
propterty is set to false.

ChangeReporter


Molecule


Uses RMI to contact a remote server.
JDBC Select


Similar to juggler and accept mouse input.
QuoteMonitor


A text filed
Uses JDBC API to connect a database.
BridgeTester

Provides set of property types and events that may be
used to test other bean components.
JAR files
 BDK
expect beans to be packaged within
JAR files.
 A JAR file allows to deploy a set of classes
and their associate resources.
 The package java.util.zip contains classes
that read and write JAR files.
Manifest Files
 Indicate
which of the components in a JAR
file are java Beans.
 A manifest file may reference several
.class files.
 If a .class file is a java Bean, its entry must
be immediately followed by the line “javaBean: True”.
Creating a jar file
 Command
jar cf XY.jar *.class *.gif
creates XY jar file that contains all .class
and all gif files of current directory.
Syntax:
jar options files
Options
c
a new archive is to be created
 f first element in file list is the name of
the archive that is to be created.
 u update existing JAR file
 v verbose output
 x files are to be extracted from the
archive.
 0 don’t use compression.
Properties
 It
is a subset of Beans state.
 Value of properties determine behavior
and appearance of component.
 3 types of properties



Simple
Boolean
Indexed
Simple Property
 Has
a single value
 N for name of property
 T for its type


public T getN();
public void setN(T arg);
 A read
/ write have both these
 Read only has only get method.
 A write only has only set method.

The following listing shows a class that has three
read/write simple properties:
public class Box {
private double depth, height, width;
public double getDepth( ) { return depth; }
public void setDepth(double d) { depth = d; }
public double getHeight( ) { return height; }
public void setHeight(double h) { height = h; }
public double getWidth( ) { return width; }
public void setWidth(double w) { width = w; }
}
Boolean Properties

Has value true or false
public boolean isN();
 public boolean getN();
 public void setN(boolean value);
public class Line
{
private boolean dotted = false;
public boolean isDotted( ) { return dotted; }
public void setDotted(boolean dotted)
{ this.dotted = dotted; }
}

Indexed property
 Consists
of multiple values.
 Public T getN(int index);
 Public T[ ] getN();
 Public void setN(int index, T value);
 Public void setN(T values[ ]);
public class PieChart
{
private double data[ ];
public double getData(int index)
{ return data[index]; }
public void setData(int index, double value)
{ data[index] = value; }
public double[ ] getData( )
{ return data; }
public void setData(double[ ] values)
{
data = new double[values.length];
System.arraycopy(values, 0, data, 0, values.length);
}
}
Events
 public

void addTListener(TListener el);
For multiple listeners
 public
void addTListener(TListener el)
throws TooManyListeners;

For one listener
 public
void removeTListener(TListener el);

These methods are used by event listeners to
register an interest in events of a specific type.
 The first pattern indicates that a Bean can
multicast an event to multiple listeners.
 The second pattern indicates that a Bean can
unicast an event to only one listener.
 The third pattern is used by a listener when it no
longer wishes to receive a specific type of event
notification from a Bean.
Reflection





It is the ability to obtain information about the fields,
constructors and methods of any class at run time.
Introspection uses reflection to obtain information about
a bean.
JVM creates an instance of the class “Class” for each
type including classes, interfaces, arrays and simple
types.
Class provides various instance methods to get
information about the type.
“Member” is an interface in java.lang.reflect that defines
methods that are common to the fields, constructors and
methods of a class.
 “Field”
class in java.lang.reflect package
contains methods to read and write the
value of a field within another object.
 This class implements Member interface.
 The class Constructor in java.lang.reflect
contains methods to get information about
a constructor. It implements Member
interface.
 The
class Method in java.lang.reflect is
used to get information about a method. It
implements Member interface.
 The class Array in java.lang.reflect is used
to built an array at run time.
Introspection

It enables to find out information about the
structure and functionality of the bean.
 Introspection services are provided by
JavaBeans API are divided in Low-level API



Used by application builder tools.
Reflection work at low-level.
High-level API



Used by developers.
Limited access of a bean’s internal.
Introspection works at high-level.
Introspector

This class in java.beans provides static methods to
obtain information about the properties, events and
methods of a bean.
 When a bean named Abc is encountered, the
introspection looks for a class AbcBeanInfo. If it exists, it
is used to provide information about the properties,
events and methods of component. If it doesn’t exist, a
BeanInfo object is constructed by default.
 getBeanInfo() encapsulates all information about a bean
and wraps it up into a single BeanInfo object.
 static BeanInfo getBeanInfo(Class beanClass)
 static BeanInfo getBeanInfo(Class beanClass, Class
ignoreClass)
Developing a simple bean

Create a directory for new bean
c:\\bdk\\demo\\sunw\\demo\\colors
 Create java source file Colors.java
 Compile java file
 Create a Manifest File




switch to the c:\\bdk\\demo directory.
This is the directory in which the manifest files for the BDK demos are located.
Put the source code for your manifest file in the file colors.mft.
It is shown here:
Name: sunw/demo/colors/Colors.class
Java-Bean: True
 This file indicates that there is one .class file in the JAR file and that it is a
Java Bean.
 Colors.class file is in the package sunw.demo.colors and in the
subdirectory sunw\\demo\\colors relative to the current directory.

Generate JAR file
 Beans are included in the ToolBox window of the BDK
only if they are in JAR files in the directory c:\\bdk\\jars.
 These files are generated with the jar utility. Enter the
following:
 jar cfm colors.jar colors.mft sunw\\demo\\colors\\*.class
 This command creates the file colors.jar and places it in
the directory c:\\bdk\\jars.
 Start BDK
 Test.
Example
C:\beans\demo\sunw\demo\my\> javac
My.java
C:\beans\demo\> jar cfm ..\jars\my.jar My.mf
sunw\demo\my\*.class
BeanInfo interface
 PropertyDescriptor[]
getPropertyDescriptors()
 EventSetDescriptor[]
getEventSetDescriptors()
 MethodDescriptor[]
getMethodDescriptors()
Constrained properties
 A bean
has constrained property
generates an event when an attempt is
made to change its value.
 The event is of type
PropertyChangeEvent.