Persistence & Bean Properties

Slides:



Advertisements
Similar presentations
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.
Advertisements

Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Class Hierarchy II Discussion E. Hierarchy A mail order business sells catalog merchandise all over the country. The rules for taxation on the merchandise.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
1 org.eclipse.ui.texteditor-api l Provides a framework for text editors obeying to the desktop rules. org.eclipse.ui.texteditor.
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CS-434: Object-Oriented Programming Using Java Week 2
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Singleton, Memento, Flyweight.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Components Components are specialized self contained Software entities that can be replicated, Customized and inserted into applications. Components come.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Introduction to Java Beans From Anders Børjesson.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Java Beans - Basics CIS 421 Web-based Java Programming.
Files and Serialization. Files Used to transfer data to and from secondary storage.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Chapter 32 JavaBeans and Bean Events
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 36 JavaBeans and Bean Events
Javabeans for dummies.
JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine
RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
Applets In Java Visit for more Learning Resources 1.
Memento Design Pattern
Chapter 16 Iterators.
Accessing Files in Java
JavaBeans and JSP CS-422.
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
CS 116 Object Oriented Programming II
Chapter 7 Iterators.
null, true, and false are also reserved.
The Building Blocks Classes: Java class library, over 1,800 classes:
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Command Invocation Protocol
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Podcast Ch23f Title: Serialization
Pre-assessment Questions
Java Programming: Advanced Topics
The command invocation protocol
Presentation transcript:

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 The topics of Persistence and Properties in JavaBeans is considered . The subtopics in each of them is listed. November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 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 November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

Design Patterns (coding convention) Why? Accessors function to get value of a variable, no params. <property type> <property name>; <property type> get<property name>( ) ; Example: public class Temperature { protected double currentTemperature = 25; public double getCurrentTemperature() { return currentTemperature; } public boolean is<PropertyName>( ); November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 Mutators: Functions that change the value of a property Takes parameters Design pattern: public void set<property name>(<property type> value) ; Example: public class Temperature { protected double currentTemperature = 25; public double setCurrentTemperature(double cTemp) { currentTemperature = cTemp; } November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

Indexed & Bound properties: Design pattern – indexed property: * public <PropertyType>[] get<PropertyName>( ); public void set<PropertyName> ( <PropertyType>[] value ); * public <PropertyType> get<PropertyName> (int index); public void set<PropertyName> (int index, <PropertyType> value); (Hint: java.lang.ArrayIndexOutOfBoundsException should be thrown !) Bound properties – support change notifications PropertyChangeListener & PropertyChangeEvent November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

Bound properties… Property Owner propertyChange( ) Notifying a listener about a change in property: addPropertyChangeListener removePropertyChangeListener Bean 1 Property Owner (producer) Property 1 Changed Property 2 Changed … Property n Changed PropertyChangeEvent PropertyChangeEvent PropertyChangeEvent propertyChange( ) Bean 2 PropertyChangeListener (consumer) November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

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 ); November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

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 ) November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

Constrained Properties: Change rejected setProperty( ) Bean Constrained property PropertyVetoException vetoableChange( ) Change rejected VetoableChangeListeners VetoableChangeListener Public abstract void vetoableChange( PropertyChangeEvent vetoablePropertyEvent ) throws PropertyVetoException PropertyVetoException public PropertyVetoException (String reasonForVeto, PropertyChangeEvent eventVetoed ) VetoableChangeSupport November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 Application Persistent State Persistent Storage Persistence! Save Restore Object Serialization – java.io package java.io.FileInputStream & java.io.FileOutputStream Java.io.ObjectInputStream & java.io.ObjectOutputStream Socket File Object Stream Serialized State Object Stream source and target may vary… November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 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(); November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

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); } } public static void main (String a[ ]) { new Saver(); November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 Reader.java //<applet code="Reader" height=100 width=200></applet> 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); } November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 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 <class name> - generates a SUID (Stream Unique ID ) E.g., > serialver Reader Reader: static final long serialVersionUID = 463333450398306235L; November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

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, bean’s fully qualified name If 1st parameter = null -> 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. November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002

CS6704: Design Patterns & Component Frameworks, Spring 2002 References: Developing JavaBeans – Robert Englander ( O’Reilly publications ) JavaBeans Developer’s 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 ?!! November 20, 2018November 20, 2018 CS6704: Design Patterns & Component Frameworks, Spring 2002