Title: JavaBeans
1JavaBeans
2JavaBeans Features
- Discrete -- small and combinable
- Reusable
- Configured visually with IDE
- Changeable properties
- Communicate with other component models such as
Active X
3JavaBeans Services
- Property Management
- Introspection
- Event Handling
- Persistence
- Application Builder Support
4Property Management
- Properties are the data part of the Bean
- Changed via
- Accessor methods at run time -- setters and
getters - Visually in an IDE
- Through persistence (storage and retrieval)
- Via scripts such as VBScript or JavaScript
5Properties
- Simple -- single value (color, location, etc.)
- Indexed -- array of values
- Bound -- notifies interested parties when the
property value has changed - Interested party -- application, applet, another
Bean - Constrained -- interested party validates a new
value and can veto that value
6Simple Property
import java.awt. public class Junk1 extends
Canvas String myString "Junker"
public Junk1 () setBackground(Color.red)
setForeground(Color.blue)
public void setName(String newValue)
myString newValue public String
getName() return myString
public Dimension getMinimumSize() return
new Dimension(50,50)
7Indexed Properties
- Represents an array of values (primitive or
object) - getters and setters take an integer index value
- May also support getting and setting the entire
array with overloaded getters and setters
8Bound Properties
import java.awt. import java.beans. public
class Junk1 extends Canvas String myString
"Junker" private PropertyChangeSupport
changes new PropertyChangeSupport(this)
public Junk1 () setBackground(Color.re
d) setForeground(Color.blue)
public void setName(String newValue)
String oldString newValue myString
newValue changes.firePropertyChange("name"
,oldString,newValue)
9Bound Properties
public void addPropertyChangeListener
(PropertyChangeListener listener)
changes.addPropertyChangeListener(listener)
public void removePropertyChangeListener
(PropertyChangeListener listener)
changes.removePropertyChangeListener(listener)
10Constrained Properties
public class Junk1 extends Canvas String
myString "Junker" private
PropertyChangeSupport changes new
PropertyChangeSupport(this) private
VetoableChangeSupport vetos new
VetoableChangeSupport(this)
11Constrained Properties
public void setName(String newValue)
throws PropertyVetoException String
oldString newValue vetos.fireVetoableCha
nge("name",oldString,newValue) myString
newValue changes.firePropertyChange("name"
,oldString,newValue)
12Constrained Properties
public void addVetoableChangeListener
(VetoableChangeListener listener)
vetos.addVetoableChangeListener(listener)
public void removeVetoableChangeListener
(VetoableChangeListener listener)
vetos.removeVetoableChangeListener(listener)
13Introspection
- Ability to make public data, methods, and
properties visible - Uses the Reflection API and the Serialization API
- Uses low level design patterns (e.g. set)
- Intended primarily for IDE use
14Bean Design Patterns
- Simple Properties
- public ltPropertyTypegt getltPropertyNamegt()
- public void setltPropertyNamegt(ltPropertyTypegt t)
- Example
- public Color getHighlightColor()
- public void setHighlightColor(Color c)
- Boolean properties
- public boolean isltPropertyNamegt()
- e.g. public boolean isPlant()
15Bean Design Patterns
- Indexed Properties
- public ltPropertyElementgt getltPropertyNamegt(int
a) - public void setltPropertyNamegt(int
a,ltPropertyElementgt b) - Examples
- public Member getMember(int a)
- public void setMember(int a, Member b)
- Events
- public void addltEventListenerTypegt(ltEventListenerT
ype a) - public void removeltEventListenerTypegt(ltEventListen
erType a) - e.g. public void addTheListener(TheListener l)
- public void removeTheListener(TheListener l)
16BeanInfo
- ltBeanNamegtBeanInfo class implements the BeanInfo
interface - Provides methods for learning about the Beans
events, properties, and methods. - BeanInfo class can choose what to expose
explicitly and what to leave to introspection
(java.beans.Introspector)
17Event Handling
- Event Source -- generates events
- Event Listener -- responds to events
- Event Listeners are registered with the event
sources - When the event occurs in the source, a specific
method (with appropriate state variables) is
called in all registered listeners
18Event State Objects
- Subclass of java.util.EventObject
- End with the word Event
Public class MouseMovedEvent extends
java.util.EventObject MoveMovedEvent(java.awt.C
omponent source, Point location)
super(source) x location.x y
location.y public Point getLocation()
return new Point(x , y)
19Event Handling methods
- EvenListener interfaces
- Inherit from java.util.EventListener
- End with the word listener
interface MouseMovedListener extends
java.util.EventListener // defines listener
methods that any event listeners for //
MouseMoved events must support void
mouseMoved(MouseMovedEvent mme) class AnyClass
implements MouseMovedListener public void
mouseMoved(MouseMovedEvent mme)
20Event Listener Registration
- Event source classes provide methods for
registering and de-registering listeners
Interface MouseMovedListener extends
java.util.EventListener void
mouseMoved(MouseMovedEvent mme) public class
Junk private Vector listeners new
Vector() public synchronized void
addJunkMouseListener(MouseMovedListener mml)
listeners.addElement(mml) public
synchronized void removeJunkMouseListener(MouseMov
edListener mml) listeners.removeElement(mml)
21Event Listener Registration
protected void notifyMouseMoved()
Vector l EventObject e new
EventObject(this) synchronized(this) l
(Vector)listeners.clone() for (int ii
0 ii lt l.size() ii)
((MouseMovedListener)l.elementAt(ii).mouseMoved(e)
22Event Handling
- Unicast event source -- generates event for
single listener - Multicast event source -- can have multiple
listeners - Event adapters -- objects that go between the
source and listener and provide additional
functionality
23JAR Files
- File format based on the ZIP file format
- One JAR file downloaded in a single HTTP
transaction rather than each item individually - Compressed items
- Items can be digitally signed
- Cross platform standard format
24JAR use with Applets
- HTML code for JAR files
- ltapplet code class_file.class
- archive class_file.jar
- width 460 height 200gt
- lt/appletgt
- Can name multiple JAR files, separated by comma
25Java Archive Tool
- Command
- jar options manifest destination input-files
- Options
- c - create a new or empty archive file
- t - list the table of contents in a JAR file
- x file - extracts all files or just the named
files - f - the second argument specifies a jar file to
process (create, xtract, or table) - v - generates verbose output on stderr
26Java Archive Tool
- Options (continued)
- m - includes manifest information from a
specified manifest file - o - store only, without using ZIP compression
- M - do not create a manifest file for entries
27Java Archive Tool Examples
jar cvf all_stuff.jar . jar cvf
selected_stuff.jar .class .au .jpg .gif jar cvf
subdirectory_stuff.jar sub1 sub2 sub3 jar tf
whats_in_it.jar jar tvf really_whats_in_it.jar NO
TE You can also build JAR files in Visual Café
starting with the Project menu
28Application Builder Support
- Support for third party vendor IDE
- Allows building of Beans with little or no
programming effort - Visual Café has such functionality
29Beans at UST
- Find directories for Beans stuff at
- Network Neighborhood, Entire Network, CSLAB_B,
Nt431, Public, Komar OR - Start, Run, \\Nt431\Public\Komar
- Jars directory contains JAR files for some
sample Beans - Demo directory is top directory for source code
directories
30Using the Example Beans
- Copy the jars directory to your diskette
- In Visual Café, do the following
- Open or create a new project
- Choose the Insert menu and choose Component into
Library - A file open dialogue box appears and you then
need to open the appropriate .jar file on your
diskette (Jelly.jar or juggler.jar or one of the
button Beans)
31Using Example Beans
- Once added to the Component Library, you can open
a view of the Component Library through the View
menu - Drag the bean to the form from the Component
Library as you would any other component - When you add interaction with the Bean, you will
see specific Bean actions at the bottom of the
list
32Beans Assignment
- Study the ExplicitButtonBeanInfo.java file under
directory Demo, Buttons. You will need to be
able to explain what this code does - Copy the Transitional.java file from Demo,
transitional and add explanatory comments to
indicate what the code is doing at each stage --
turn this in one week from today
33Beans Assignment
- Use the JellyBean Bean and create an Applet that
does the following - Has the JellyBean Bean, three buttons, and a text
field - The first button when pressed changes the
JellyBean to red, adds three cents to the
priceInCents, and displays the price in the text
field - The second button changes the color to blue and
reports the current price in the text field - The third button changes the color to green,
subtracts three cents from the price and reports
the new price - Turn in the program one week from today
34Beans Assignment Extra Credit
- Use Visual Café to create a simple, visual Bean
of your choice - Prepare a short demo for the class on what youve
created and how you went about it
35Enterprise JavaBeans
- The Bean concept on the server side
- customizable through properties and customization
methods - can be assembled with other Beans and custom code
to form an application - Sun has provided a detailed specification for how
the Enterprise JavaBeans will interact with an
enterprise bean container system
36Enterprise JavaBeans
- Application execution systems can provide EJB
services - TP Monitors (CICS)
- Component Transaction Servers (Sybase Jaguar)
- CORBA platforms
- Data base management systems (Oracle, Sybase,
etc.) - Web Servers (Netscape Enterprise Server, etc.)
37Enterprise JavaBeans
- EJB Server -- provides access to distributed
transaction management services - EJB Container -- manages a specific class of
objects - life-cycle management
- implicit transaction control
- persistence management
- transparent distribution services
- security
38Enterprise JavaBeans