Presentation is loading. Please wait.

Presentation is loading. Please wait.

TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716)381-0260 x225.

Similar presentations


Presentation on theme: "TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716)381-0260 x225."— Presentation transcript:

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

2 TMOutline  Component Software Engineering  Event  Properties  Persistence and Versioning  Introspection and Customization  Packaging and Deployment  Future Directions

3 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

4 TM Component Model Features  Property Management  Event Handling  Persistence and Versioning  Portability and Interoperability  Distributed Computing Support

5 TM What is a JavaBean? “A JavaBean is a reusable software component that can be manipulated visually in a builder tool”

6 TM JavaBeans Objectives  Provide a platform neutral component architecture  Simple to develop  Leverage existing Java technologies  Provide design-time support for visual builder tools

7 TM JavaBeans Characteristics  Properties  Event  Persistence  Introspection  Customization

8 TMProperties  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

9 TM Property Types  Simple Properties  Bound Properties  Constrained Properties

10 TM Simple Properties  Represent a single value  The accessor methods should follow standard naming conventions public get (); public void set ( value); Example: public String getHostName(); public void setHostName( String hostName );

11 TM Boolean Properties  They are simple properties  The getter methods follow an optional design pattern public boolean is (); Example: public boolean isConnected();

12 TM Indexed Properties  Represent an array of values public 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,Color value); public Color[] getPalette(); public void setPalette(Color[] values);

13 TM Bound Properties  Registered listeners object are notified when the value of the property changes  Listeners must implement the java.beans.PropertyChangeListener interface propertyChange(PropertyChangeEvent event);

14 TM Bound Properties Support Framework TerminalBean hostName portNumber addPropertyChangeListener() removePropertyChangeListener() PropertyChangeEvent source newValue propertyName oldValue generates uses notifies PropertyChangeListener propertyChange() > PropertyChangeSupport addPropertyC hangeListener() removePropertyChangeListener() firePropertyChange()

15 TM Bound Properties Notification Mechanism :PropertyEditor :Bean :PropertyChangeListener :PropertyChangeSupport 1. addPropertyChangeListener 2. addPropertyChangeListener 3. SetHostName(“hostName”) 4. firePropertyChange(event) 5. propertyChange(event)

16 TM Constrained Properties  Allow registered listeners to validate a proposed change  Listeners must implement the java.beans.VetoablecChangeListener interface vetoableChange( PropertyChangeEvent event ) throws PropertyVetoException;

17 TM Constrained Properties Support Framework Bean hostName portNumber addVetoableChangeListener() removeVetoableChangeListener() PropertyChangeEvent source newValue propertyName oldValue VetoableChangeSupport addVetoableC hangeListener() removeVetoableChangeListener() generatesusesnotifies VetoableChangeListener vetoableChange() >

18 TM 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 listeners propertyChangeSupport.firePropertyChange("hostName", oldHostName, newHostName ); }

19 TMDemo  Terminal Bean –Bound Properties connectedconnected connectedColorconnectedColor disconnectedColordisconnectedColor –Constrained Properties hostNamehostName portNumberportNumber –Methods connect()connect() disconnect()disconnect()

20 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

21 TM Event Delegation Model Listener Event Source Event 3. Notify 2. Generate 1. Register

22 TM Event Objects  Encapsulate notification properties  Inherit from the java.util.EventObject class

23 TM Event 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; }

24 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.EventListener –Have an individual method for each event type –Related event handling methods are usually grouped in the same interface

25 TM Event Listeners - Example public interface CursorListener extends java.util.EventListener { public void cursorMoved( CursorEvent cursorEvent); }

26 TM Event Sources  Provide methods for registering and de- registering event listeners –Singlecast support public void add ( listener) throws java.util.TooManyListenersException; public void remove ( listener) –Multicast support public void add ( listener) public void remove ( listener)

27 TM Event Source - Example public class TerminalBean { … public void addCursorListener( CursorListener listener ) { } public void removeCursorListener( CursorListener listener ) { }... }

28 TMPersistence  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

29 TM Serialization 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:

30 TMVersioning  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

31 TMIntrospection  Discovering the properties, events, and method that a component supports  Two methods to perform introspection –Use low level reflection mechanism- –Explicit specification using the BeanInfo class

32 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 JavaBean supports

33 TMBeanInfo  Provides explicit information about a JavaBean  Defined by the creator of the bean  Does not need to provide complete information

34 TMCustomization  Customize the appearance and behavior of a component in a design time environment  Customization is supported through: –Property Editors –Property Sheets –Customizers

35 TM Property Editors  Visual element for editing a specific Java Type  For standard types editors are provided  Located using the PropertyEditorManger  Displayed on Property sheets

36 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

37 TMCustomizers  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

38 TM Packaging and Deployment  JAR (Java ARchive) files are the primary method of packaging and distributing JavaBeans  A JAR file contains: –Class files –Serialized prototypes of a bean –Documentation files –Resources

39 TM Jar File Example SRC_DIR=com/questra/beans CLASSFILES= \ $(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.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.classJava-Bean: True <<. SUFFIXES:.java.class {$(SRC_DIR)}.java{$(SRC_DIR)}.class : javac $< clean: -del $(SRC_DIR)\*.class -del $(JARFILE)

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

41 TMResources  JavaBeans Home Page –http://java.sun.com/beans/  Glasgow –http://java.sun.com/beans/glasgow/  Books –Michael Morrison, presenting JavaBeans, Sams.net:Indianapolis, 1997. –Dan Brookshier, JavaBeans Developer’s Reference, News Riders Publishing:Indianapolis, 1997.


Download ppt "TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716)381-0260 x225."

Similar presentations


Ads by Google