1 / 41

Introduction to JavaBeans™

Introduction to JavaBeans™. Dimitrios Psarros Questra Consulting dpsarros@questra.com (716)381-0260 x225. Outline. Component Software Engineering Event Properties Persistence and Versioning Introspection and Customization Packaging and Deployment Future Directions.

Download Presentation

Introduction to JavaBeans™

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • Component Software Engineering • Event • Properties • Persistence and Versioning • Introspection and Customization • Packaging and Deployment • Future Directions

  3. 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. Component Model Features • Property Management • Event Handling • Persistence and Versioning • Portability and Interoperability • Distributed Computing Support

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

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

  7. JavaBeans Characteristics • Properties • Event • Persistence • Introspection • Customization

  8. 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

  9. Property Types • Simple Properties • Bound Properties • Constrained Properties

  10. Simple Properties • Represent a single value • The accessor methods should follow standard naming conventions public <PropertyType> get<PropertyName>(); public void set<PropertyName>(<PropertyType> value); Example: public String getHostName(); public void setHostName( String hostName );

  11. Boolean Properties • They are simple properties • The getter methods follow an optional design pattern public boolean is<PropertyName>(); Example: public boolean isConnected();

  12. Indexed Properties • Represent an array of values public <PropertyElement> get<PropertyName>(int index); public void set<PropertyName>(int index,<PropertyElement> value); public <PropertyElement>[] get<PropertyName>(); public void set<PropertyName>(<PropertyElement>[] values); Example: public Color setPalette(int index); public void setPalette(int index,Color value); public Color[] getPalette(); public void setPalette(Color[] values);

  13. 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. TerminalBean hostName portNumber addPropertyChangeListener() removePropertyChangeListener() PropertyChangeSupport PropertyChangeListener PropertyChangeEvent <<interface>> source addPropertyChangeListener() propertyName propertyChange() removePropertyChangeListener() oldValue firePropertyChange() newValue Bound Properties Support Framework generates notifies uses

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

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

  17. Bean hostName portNumber addVetoableChangeListener() removeVetoableChangeListener() VetoableChangeSupport PropertyChangeEvent source addVetoableChangeListener() propertyName removeVetoableChangeListener() oldValue newValue Constrained Properties Support Framework uses generates notifies VetoableChangeListener <<interface>> vetoableChange()

  18. 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. Demo • Terminal Bean • Bound Properties • connected • connectedColor • disconnectedColor • Constrained Properties • hostName • portNumber • Methods • connect() • disconnect()

  20. 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. Event Delegation Model Listener 1. Register 3. Notify Event Source 2. Generate Event

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

  23. 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; }

  24. 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. Event Listeners - Example publicinterface CursorListener extends java.util.EventListener { public void cursorMoved( CursorEvent cursorEvent); }

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

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

  28. 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

  29. Serialization Example publicclass TerminalBean implements java.io.Serializable { private String hostName; privatetransient boolean isConnected; staticfinal long serialVersionUID = -3283293045227786848L; } • Saving and Retrieving component state: ObjectOutputStream objectOutputStream=new ObjectOutputStream(outStream ); outputStream.writeObject( new TerminaBean() ); ... ObjectInputStream objectInputStream = new ObjectInputStream( inStream ); TerminalBean terminal = (TerminalBean) objectInputStream.readObject( );

  30. 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

  31. 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 BeanInfo class

  32. 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. BeanInfo • Provides explicit information about a JavaBean • Defined by the creator of the bean • Does not need to provide complete information

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

  35. 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. 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. 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

  38. 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. 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. Future • "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. Resources • 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.

More Related