Introduction to Java Beans Dimitrios Psarros Questra Consulting

Introduction to Java. Beans™ Dimitrios Psarros Questra Consulting dpsarros@questra. com (716)381 -0260 x 225 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 Java. Bean? “A Java. Bean is a reusable software component that can be manipulated visually in a builder tool” TM

Java. Beans Objectives §Provide a platform neutral component architecture §Simple to develop §Leverage existing Java technologies §Provide design-time support for visual builder tools TM

Java. Beans 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 <Property. Type> get<Property. Name>(); public void set<Property. Name>(<Property. Type> value); Example: public String get. Host. Name(); public void set. Host. Name( String host. Name ); TM

Boolean Properties §They are simple properties §The getter methods follow an optional design pattern public boolean is<Property. Name>(); Example: public boolean is. Connected(); TM

Indexed Properties §Represent an array of values public <Property. Element> get<Property. Name>(int index); public void set<Property. Name>(int index, <Property. Element> value); public <Property. Element>[] get<Property. Name>(); public void set<Property. Name>(<Property. Element>[] values); Example: public Color set. Palette(int index); public void set. Palette(int index, Color value); public Color[] get. Palette(); public void set. Palette(Color[] values); TM

Bound Properties §Registered listeners object are notified when the value of the property changes §Listeners must implement the java. beans. Property. Change. Listener interface property. Change(Property. Change. Event event); TM

Bound Properties Support Framework Terminal. Bean host. Name port. Number add. Property. Change. Listener() remove. Property. Change. Listener() uses generates Property. Change. Support Property. Change. Event add. Property. Change. Listener() remove. Property. Change. Listener() fire. Property. Change() source property. Name old. Value new. Value notifies Property. Change. Listener <<interface>> property. Change() TM

Bound Properties Notification Mechanism : Property. Editor : Property. Change. Listener 1. add. Property. Change. Listener 3. Set. Host. Name(“host. Name”) : Bean 2. add. Property. Change. Listener 4. fire. Property. Change(event) : Property. Change. Support 5. property. Change(event) TM

Constrained Properties §Allow registered listeners to validate a proposed change §Listeners must implement the java. beans. Vetoablec. Change. Listener interface vetoable. Change( Property. Change. Event event ) throws Property. Veto. Exception; TM

Constrained Properties Support Framework Bean host. Name port. Number add. Vetoable. Change. Listener() remove. Vetoable. Change. Listener() uses generates Vetoable. Change. Support Property. Change. Event add. Vetoable. Change. Listener() remove. Vetoable. Change. Listener() source property. Name old. Value new. Value notifies Vetoable. Change. Listener <<interface>> vetoable. Change() TM

Constrained Properties - Example public void set. Host. Name( String new. Host. Name ) throws java. beans. Property. Veto. Exception { String old. Host. Name = this. host. Name; // 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. vetoable. Change. Support. fire. Vetoable. Change( "host. Name", old. Host. Name, new. Host. Name ); // change accepted; update state this. host. Name = new. Host. Name; // notify property change listeners property. Change. Support. fire. Property. Change("host. Name", old. Host. Name, new. Host. Name ); } TM

Demo §Terminal Bean – Bound Properties • connected. Color • disconnected. Color – Constrained Properties • host. Name • port. Number – 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. Event. Object class TM

Event Object - Example public class Cursor. Event extends java. util. Event. Object { public Cursor. Event(Object source, Point location) { super( source ); this. location = location; } Point get. Location() { 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. Event. Listener – 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 Cursor. Listener extends java. util. Event. Listener { public void cursor. Moved( Cursor. Event cursor. Event); } TM

Event Sources §Provide methods for registering and deregistering event listeners – Singlecast support public void add<Listener. Type>(<Listener. Type> listener) throws java. util. Too. Many. Listeners. Exception; public void remove<Listener. Type>(<Listener. Type> listener) – Multicast support public void add<Listener. Type>(<Listener. Type> listener) public void remove<Listener. Type>(<Listener. Type> listener) TM

Event Source - Example public class Terminal. Bean { … public void add. Cursor. Listener( Cursor. Listener listener ) { } public void remove. Cursor. Listener( Cursor. Listener 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 Terminal. Bean implements java. io. Serializable { private String host. Name; private transient boolean is. Connected; static final long serial. Version. UID = -3283293045227786848 L; } §Saving and Retrieving component state: Object. Output. Stream object. Output. Stream=new Object. Output. Stream(out. Stream ); output. Stream. write. Object( new Termina. Bean() ); . . . Object. Input. Stream object. Input. Stream = new Object. Input. Stream( in. Stream ); Terminal. Bean terminal = (Terminal. Bean) object. Input. Stream. read. Object( ); 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 Bean. Info 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 Java. Bean supports TM

Bean. Info §Provides explicit information about a Java. Bean §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 Property. Editor. Manger §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 Java. Beans §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)Cursor. Event. class $(SRC_DIR)Cursor. Listener. class $(SRC_DIR)Terminal. Bean. Info. class $(SRC_DIR)Terminal$$Veto. Open. Connection. class DATAFILES= $(SRC_DIR)Terminal. Bean. Icon. Color 16. gif JARFILE= bdkjarsterminal. 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. class. Java-Bean: True <<. SUFFIXES: . java. class {$(SRC_DIR)}. java{$(SRC_DIR)}. class : javac $< clean: -del $(SRC_DIR)*. class -del $(JARFILE) TM

Future §"Glasgow" "Glasgow – Extensible runtime containment and services protocol – Drag and Drop subsystem – Java. Beans Activation framework §Enterprise Java Beans – Build distributed, scalable, transactional OO business applications – Runtime environment – Interoperability with non-Java applications – Compatible with CORBA TM

Resources §Java. Beans Home Page – http: //java. sun. com/beans/ §Glasgow – http: //java. sun. com/beans/glasgow/ §Books – Michael Morrison, presenting Java. Beans, Sams. net: Indianapolis, 1997. – Dan Brookshier, Java. Beans Developer’s Reference, News Riders Publishing: Indianapolis, 1997. TM
- Slides: 41