Persistence - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Persistence

Description:

Persistence & Bean Properties Properties in Beans Accessors & Mutators Indexed & Bound properties Constrained properties Examples Persistence in JavaBeans – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 17
Provided by: MuraliK
Category:

less

Transcript and Presenter's Notes

Title: Persistence


1
Persistence Bean Properties
  • Properties in Beans
  • Accessors Mutators
  • Indexed Bound properties
  • Constrained properties
  • Examples
  • Persistence in JavaBeans
  • Java Object Serialization
  • The java.io.Serializable Interface
  • Versioning
  • Validating Objects
  • The java.io.Externalizable Interface

2
What is a Property?
  • Named attributes / characteristics that define
    the behavior state of the object (bean)
  • How is it different from a variable?

Bean
Bean
Properties
Bean actions tied to properties
3
Design Patterns (coding convention)
  • Why?
  • Accessors
  • function to get value of a variable, no params.
  • ltproperty typegt ltproperty namegt
  • ltproperty typegt getltproperty namegt( )
  • Example
  • public class Temperature
  • protected double currentTemperature 25
  • public double getCurrentTemperature()
  • return currentTemperature
  • public boolean isltPropertyNamegt( )

4
Mutators
  • Functions that change the value of a property
  • Takes parameters
  • Design pattern
  • public void setltproperty namegt(ltproperty
    typegt value)
  • Example
  • public class Temperature
  • protected double currentTemperature 25
  • public double setCurrentTemperature(double
    cTemp)
  • currentTemperature cTemp

5
Indexed Bound properties
  • Design pattern indexed property
  • public ltPropertyTypegt getltPropertyNamegt( )
  • public void setltPropertyNamegt (
    ltPropertyTypegt value )
  • public ltPropertyTypegt getltPropertyNamegt (int
    index)
  • public void setltPropertyNamegt (int index,
    ltPropertyTypegt value)
  • (Hint java.lang.ArrayIndexOutOfBoundsException
    should be thrown !)
  • Bound properties support change notifications
  • PropertyChangeListener PropertyChangeEvent

6
Bound properties
Notifying a listener about a change in property
addPropertyChangeListener removePropertyChangeList
ener
Bean 1
Property Owner
(producer)
Property 1 Changed
Property 2 Changed
Property n Changed

PropertyChangeEvent
PropertyChangeEvent
PropertyChangeEvent
propertyChange( )
PropertyChangeListener
Bean 2
(consumer)
7
java.beans.PropertyChangeEvent
  • Constructor
  • public PropertyChangeEvent (Object sourceBean,
    String propertyName, Object oldValue, Object
    newValue )
  • Methods
  • public Object getNewValue ( )
  • public Object getOldValue ( )
  • public String getPropertyName( )
  • public Object getPropagationId ( )
  • public Object setPropagationId ( Object newId )

8
java.beans.PropertyChangeSupport utility class
Listener
Bean
Properties
Listener
Property Support
Listener
  • public PropertyChangeSupport ( Object sourceBean
    )
  • public synchronized void addPropertyChangeListener
    (PropertyChangeListener newListener )
  • public synchronized void addPropertyChangeListener
    (PropertyChangeListener existingListener )
  • public void firePropertyChange ( String
    propertyName, Object oldValue, Object newValue )

9
Constrained Properties
Change rejected
setProperty( )
PropertyVetoException
vetoableChange( )
Change rejected
VetoableChangeListeners
  • VetoableChangeListener
  • Public abstract void vetoableChange(
    PropertyChangeEvent vetoablePropertyEvent )
  • throws PropertyVetoException
  • PropertyVetoException
  • public PropertyVetoException (String
    reasonForVeto, PropertyChangeEvent eventVetoed )
  • VetoableChangeSupport

10
Persistence!
Save
Restore
  • Object Serialization java.io package
  • java.io.FileInputStream java.io.FileOutputStream
  • Java.io.ObjectInputStream java.io.ObjectOutputSt
    ream

Object Stream source and target may vary
11
An example
  • //save a String and double to the stream
  • String str Sample
  • double d 3.14
  • FileOutputStream fos new FileOutputStream
    (Beans.ser)
  • ObjectOutputStream oos new ObjectOutputStream
    (fos)
  • oos.writeObject (str)
  • oos.writeObject (d)
  • oos.flush()
  • // restore the String and double
  • FileInputStream fis new
    FileInputStream (Beans.ser)
  • ObjectInputStream ois new
    ObjectInputStream (fis)
  • String str (String) ois.readObject()
  • double d s.readDouble()

12
java.io.Serializable interface
  • No methods
  • Just a marker, indicating class is serializable
  • Object / Class tree traversed to serialize
  • java.io.NotSerializableException thrown, if
    unserializable member found
  • Static and Transient members not serialized
  • import java.io. Saver.java
  • import java.awt.
  • public class Saver
  • Saver()
  • Button b new Button("Bean Button")
  • b.setFont(new Font("System", Font.BOLD, 20))
  • try
  • FileOutputStream f new FileOutputStream
    ("Saver.ser")
  • ObjectOutputStream o new ObjectOutputStream
    (f)
  • o.writeObject(b)
  • o.flush()
  • catch (Exception e) System.out.println(e)

13
Reader.java
//ltapplet code"Reader" height100
width200gtlt/appletgt import java.applet. import
java.io. import java.awt. public class
Reader extends Applet public void init()
try FileInputStream f new
FileInputStream ("Saver.ser") ObjectInputStream
o new ObjectInputStream (f) Button b
(Button)o.readObject() add(b)
catch (Exception e) System.out.println(e)

14
Versioning
  • inevitable tendency for an object to evolve over
    time and gain new functionality
  • New version (vs.) old version conflict
  • Compatible changes
  • Do not render serialized data unusable by older
    class
  • ? Adding new methods
  • ? Changing the base class
  • java.io.InvalidClassException thrown
  • serialver ltclass namegt - generates a SUID
    (Stream Unique ID )
  • E.g., gt serialver Reader
  • Reader static final long serialVersionUID
    463333450398306235L

15
java.io.Externalizable interface
  • How is it different from Serializable interface?
  • writeExternal ( ) and readExternal ( )
  • Class hierarchy not traversed during
    serialization
  • Versioning responsibility of the object
  • Instantiating Serialized Objects
  • the instantiate( ) method in java.beans.Beans
    (static)
  • 2 parameters class loader, beans fully
    qualified name
  • If 1st parameter null -gt uses default class
    loader
  • Instantiate() looks for a pickled bean, if
    not found uses new to create an instance
  • Requires that the bean have constructor with no
    params.

16
References
  • Developing JavaBeans Robert Englander
  • ( OReilly publications )
  • JavaBeans Developers Resource Prashant
    Sridharan ( Prentice Hall )
  • Presenting JavaBeans Michael Morrison
  • (MCP , Sams publishing )
  • JavaBeans Developer's Guide Piroz Mohseni
  • - ( IDG Books worldwide )
  • http//java.sun.com/products/javabeans
  • Questions ?!!
Write a Comment
User Comments (0)
About PowerShow.com