Top Banner
Java Beans
79
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit4wt

Java Beans

Page 2: Unit4wt

JavaBeans

• An introduction to component-based development in general

• Introduction to JavaBeans– Java components– client-side

• Working with the BDK• The beans development life cycle• Writing simple and advanced beans

Page 3: Unit4wt

Java Components

• JavaBeans -- portable, platform-independent component model

• Java components are known as beans• A bean: a reusable software component that

can be manipulated visually in a builder tool

Page 4: Unit4wt

JavaBeans vs. Class Libraries

• Beans are appropriate for software components that can be visually manipulated

• Class libraries are good for providing functionality that is useful to programmers, and doesn’t benefit from visual manipulation

Page 5: Unit4wt

JavaBeans Concepts

• A component is a self-contained reusable software unit

• Components expose their features (public methods and events) to builder tools

• A builder tool maintains Beans in a palette or toolbox.

Page 6: Unit4wt

Concepts...

• You can select a bean from the toolbox, drop it in a form, and modify its appearance and behavior.

• Also, you can define its interaction with other beans

• ALL this without a line of code.

Page 7: Unit4wt

JavaBean Characteristics

• a public class with 0-argument constuctor• it has properties with accessory methods• it has events• it can customized• its state can be saved• it can be analyzed by a builder tool

Page 8: Unit4wt

Key Concepts

• A builder tool discover a bean’s features by a process known as introspection.– Adhering to specific rules (design pattern) when

naming Bean features. – Providing property, method, and event

information with a related Bean Information class.• Properties (bean’s appearance and behavior

characteristics) can be changed at design-time.

Page 9: Unit4wt

Key Concepts….

• Properties can be customized at design-time. Customization can be done:– using property editor– using bean customizers

• Events are used when beans want to intercommunicate

• Persistence: for saving and restoring the state• Bean’s methods are regular Java methods.

Page 10: Unit4wt

Security Issues

• JavaBeans are sbject to the standard Java security model

• The security model has neither extended nor relaxed.

• If a bean runs as an untrusted applet then it will be subject to applet security

• If a bean runs as a stand-alone application then it will be treated as a normal Java application.

Page 11: Unit4wt

Creating jar files

• c-create an archive• C-change the directory during the execution• f-first name in the file names listing is the name of the archive

that has to be created• M-the file name in the listing is a manifest file name which is

present externally• u-Update existing jar file• t-The contents of archieve can be arranged in tabular form• v-It gives the verbose output• M-The manifest file is not created• o-the compression should not be used

Page 12: Unit4wt

Create Sample.java fileclass Sample extends Canvas -to make it visibleCreate manifest file

Page 13: Unit4wt

MyFirstBean

• import java.awt.*;• import java.io.Serializable;• public class FirstBean extends Canvas implements Serializable

{• public FirstBean() {• setSize(50,30);• setBackground(Color.blue);• }• }

Page 14: Unit4wt

First Bean

• Compile: javac FirstBean.java• Create a manifest file:• mani.mft

– Name: FirstBean.class– Java-Bean: True

• Create a jar file:• jar cfm FirstBean.jar mani.mft FirstBean.class

Page 15: Unit4wt

Using Beans in hand-written app

• Use Beans.instantiate• Frame f;• f = new Frame("Testing Beans");• try {• ClassLoader cl = this.getClass().getClassLoader();• fb =(FirstBean)Beans.instantiate(cl,"FirstBean");• } catch(Exception e) {• e.printStackTrace();• } • f.add(fb);

Page 16: Unit4wt

Properties

• Bean’s appearance and behavior -- changeable at design time.

• They are private values• Can be accessed through getter and setter

methods• getter and setter methods must follow some

rules -- design patterns (documenting experience)

Page 17: Unit4wt

Properties

• A builder tool can:– discover a bean’s properties– determine the properties’ read/write attribute– locate an appropriate “property editor” for each

type– display the properties (in a sheet)– alter the properties at design-time

Page 18: Unit4wt

Types of Properties

• Simple• Index: multiple-value properties• Bound: provide event notification when value

changes• Constrained: how proposed changes can be

okayed or vetoed by other object

Page 19: Unit4wt

Simple Properties

• When a builder tool introspect your bean it discovers two methods:– public Color getColor()– public void setColor(Color c)

• The builder tool knows that a property named “Color” exists -- of type Color.

• It tries to locate a property editor for that type to display the properties in a sheet.

Page 20: Unit4wt

Simple Properties….

• Adding a Color property– Create and initialize a private instance variable

• private Color color = Color.blue;– Write public getter & setter methods

• public Color getColor() {– return color;

• }• public void setColor(Color c) {

– color = c;– repaint();

• }

Page 21: Unit4wt

Events “Introspection”

• For a bean to be the source of an event, it must implement methods that add and remove listener objects for the type of the event:– public void add<EventListenerType>(<EventListenerType> elt);

– same thing for remove• These methods help a source Bean know

where to fire events.

Page 22: Unit4wt

Events “Introspection”

• Source Bean fires events at the listeners using method of those interfaces.

• Example: if a source Bean register ActionListsener objects, it will fire events at those objects by calling the actionPerformed method on those listeners

Page 23: Unit4wt

Events “using BeanInfo”

• Implementing the BeanInfo interface allows you to explicitly publish the events a Bean fires

Page 24: Unit4wt

BeanInfo interface

• Bean exposes its features in a property sheetusing java.beans.Introspector class (which uses

Core Reflection API)• The discovery process is named

“introspection”• OR you can associate a class that implements

the BeanInfo with your bean

Page 25: Unit4wt

Bean Customization

• The appearance and behavior of a bean can be customized at design time.

• Two ways to customize a bean:– using a property editor

• each bean property has its own editor• a bean’s property is displayed in a property sheet

– using customizers• gives you complete GUI control over bean

customization

Page 26: Unit4wt

Property Editors

• A property editor is a user interface for editing a bean property. The property must have both, read/write accessor methods.

• A property editor must implement the PropertyEditor interface.

• PropertyEditorSupport does that already, so you can extend it.

Page 27: Unit4wt

Property Editors

• If you provide a custom property editor class, then you must refer to this class by calling PropertyDescriptor.setPropertyEditorClass in a BeanInfo class.

• Each bean may have a BeanInfo class which customizes how the bean is to appear. SimpleBeanInfo implements that interface

Page 28: Unit4wt

Enterprise Java Beans

• Introduction– Application Server– Java 2 Enterprise Edition

• What is an Enterprise Bean ?– EJB Properties– EJB Overview– Deployment Phase

– Type of beans

• Client access with interfaces– Remote access– Local Access

Page 29: Unit4wt

Introduction

• Enterprise Java Beans ( EJB ) is – a middleware component model for Java and CORBA– a specification for creating server-side, scalable,

transactional, multi-user and secure enterprise-level applications

– one of several Java APIs in the Java

• Presented by Sun in the 1999, they are easier than other technologies as RMI or Corba

Page 30: Unit4wt

Introduction

• This is the three level structure for Application Server

Page 31: Unit4wt

Applicaton Server

• Presentation– HTML Application – Java Application

• Business Logic• Data Access

Page 32: Unit4wt

Presentation

• HTML– Generated server-

side HTML– Runs on any Web

browser– Less client-side

power

• Java– Required Java virtual

Machine– More client side

power– Runned on a page– Security (Applet)– Launched from a

browser or a standalone application

Page 33: Unit4wt

Business Logic

• Implements the logic of the application defining all the function that may be used from a client– Change Business Rules Easily– Re-use components– Make complex applications manageable– Secure Data hiding

Page 34: Unit4wt

Data Access

• Utility to access external datas such as Database or other Web component

• Access other SOA

Page 35: Unit4wt

J2EE Application Server

• Java 2 Enterprise Edition standardizes interfaces for Application Server components

Page 36: Unit4wt

What is an Enterprise Bean ?

• Is a server side component written in Java Language• Industry standard distribuited component model• Incorporates the business logic of an application ( the

code that implements the purpose of the application)

• Replicates the table model as objects

Page 37: Unit4wt

EJB Overview

Page 38: Unit4wt

Deployment Phase

Page 39: Unit4wt

Deployment Phase

Page 40: Unit4wt

Type of beans

• Session Bean• Entity Bean• Message Driven Bean

Page 41: Unit4wt

Session Bean

• Represents a single client inside the server• The client calls the session bean to invoke methods

of an application on the server • Perform works for its client, hiding the complexity of

interaction with other objects in the server• Is not shared• Is not persistent• When the client stops the session,the bean can be

assigned to another client from the server• Unique to each client

Page 42: Unit4wt

Session Bean

• Stateful session bean

• Stateless session bean

Page 43: Unit4wt

Stateful Session Bean

• Contains the state of a single client session: – Information on the client – On method called – Return values

This state is called conversational state and is not retained when the session ends, also if the client not removes the bean

Page 44: Unit4wt

Stateless Session Bean

• Not maintain a conversational state for a particular client

• Contains values only for the duration of the single invocation

• Except during method invocation, all instances of stateless session bean are equivalent

• Pooled

Page 45: Unit4wt

Entity Bean

• Represents a business object in a persistent storage mechanism such as a relational database

• Usually is a table in the database and each instance of that entity bean is a row in that tableProperties:

• Persistent• Allow shared access• Have primary key • Have relationship with other entity beans. • Auto commit.

Page 46: Unit4wt

Entity Bean persistent

• Bean managed persistence

• Container managed persistence

Page 47: Unit4wt

Bean managed persistence

• Who write the bean’s code must access the database and save his own data

• you will have more control over how the entity bean accesses a database

Page 48: Unit4wt

Container managed persistence

• The container save the data• There is no code in the bean for access the database• The container handles all database access required

for the bean• the EJB container transparently and implicitly

manages the persistent state

Page 49: Unit4wt

Entity bean’s shared access

• Entity beans can be used by different clients• It’s important that they work whithin transactions• The EJB container provides transaction management • The transaction’s attribute are specified in the bean’s

deployment description • Concurrency management

Page 50: Unit4wt

Entity bean’s primary key

• Each entity bean has a unique object identifier like a key in a database table

• Each instance represents as Row in table

Page 51: Unit4wt

Entity bean’s relationship

• Container managed persistent – The container performs all the operation to create

relationship

• Bean managed persistent– The code to perform relations must be written in the bean

Page 52: Unit4wt

Message Driven bean

• Allows applications to process messages asynchronously

• The messages may be sent by :– An application client– Another enterprise bean– A Web component– A JMS Client

Page 53: Unit4wt

Message Driven bean

• Retain no data or conversational state for a specific client

• The instance variables of the message-driven bean e can contain some state across the handling of client messages--for example, a JMS API connection, an open database connection, or an object reference to an ejb.

Page 54: Unit4wt

Message Driven bean

• A client can’t access directly to a message driven bean

• When a message arrive, the container gives it to a message driven bean

• The bean process the message• The onMessage method may call helper methods,

or it may invoke a session or entity bean to process the information in the message or to store it in a database

Page 55: Unit4wt

Client access with interfaces

• A client may access a session or an entity bean only through the methods defined in the bean's interfaces

• They define the client's view of a bean • Public business methods declared in Bean interface’s

can be visible to client, to invoke• Types of access:

– Remote access– Local access

Page 56: Unit4wt

Remote access

• A remote client of an enterprise bean has the following traits:– It may run on a different machine and a different Java

virtual machine than the enterprise bean it accesses (It is not required to run on a different JVM )

– It can be a Web component– It can be another enterprise bean– It can be RMI object

Page 57: Unit4wt

Remote access

• To create an enterprise bean with remote access, you must :– Code a remote interface

• Business methods

– Code a home interface• Finder methods • Home methods• Utility methods (to get home)

Page 58: Unit4wt

Remote access example

Page 59: Unit4wt

Local access

• A local client has these characteristics– It must run in the same JVM as the enterprise

bean it accesses– It may be a Web component or another enterprise

bean– To the local client, the location of the enterprise

bean it accesses is not transparent– It is often an entity bean that has a container-

managed relationship with another entity bean

Page 60: Unit4wt

Local access

• To create an enterprise bean with local access, you must :– Code the local interface

• Bean's business methods

– Code the local home interface• Life cycle• Finder methods • Utility methods

Page 61: Unit4wt

Local interfaces

• If an entity bean is the target of a container managed relationship it MUST have local interfaces

• An EJB can use local client view only if it is really guaranteed that other enterprise beans or clients will only address the bean within a single JVM

Page 62: Unit4wt

Contents of an Enterprise Bean

• Deployment descriptor– Persistence type – Transaction attribute

• Enterprise bean class• Interfaces• Helper classes

– Exception – Utility classes

Page 63: Unit4wt

EJB Example

• The OnLine BankWe will take a not completed system to give an idea to how choose if a component is an entity, session or message driven bean.

Page 64: Unit4wt

A few EJB implementations

• WebLogic • Bluestone • Novera• Persistence • Oracle AS • Oracle8i

Page 65: Unit4wt

The EJB architecture

• Consists of:– An EJB server – EJB containers that run within the server – Home objects– Remote EJBObjects – Enterprise Beans– EJB clients – Auxiliary systems like

• Java Naming and Directory Interface (JNDI)• Java Transaction Service (JTS) • Security services• Threading• Pooling

Page 66: Unit4wt

Implements

Invokes

Creates / uses

EJB Architecture

Client

Server

Home Interface(Factory)

EJB Object(Wrapper)

EnterpriseJava Bean(Biz Logic)

RemoteInterface

Container

RMI

RMI

Naming Service

Page 67: Unit4wt

Stateful session bean’s life cycle

• The client invoke the create method• The EJB container :

– Instantiates the bean – Invokes the setSessionContext – Invokes ejbCreate

• The bean is ready• Business methods ready to be called

Page 68: Unit4wt

Stateful session bean’s life cycle

• While in the ready state– EJB container may passivate the bean moving it from

memory to secondary storage – A client may invoke a business method – EJB container may activate a bean,moving it back to the

ready stage, and then calls the bean's ejbActivate method– A client may invoke the remove method and the container

calls the bean's ejbRemove method – Client cannot invoke passivate

Page 69: Unit4wt

Stateful session bean’s life cycle

Page 70: Unit4wt

Stateless session bean’s life cycle

• The client invoke the create method• The EJB container :

– Instantiates the bean – Invokes the setSessionContext – Invokes ejbCreate

• The bean is ready

Page 71: Unit4wt

Stateless session bean’s life cycle

• While in the ready state– A client may invoke a business method– A client may invoke the remove method and the container

calls the bean's ejbRemove method– It’s never passivate– It’s can be pooled

Page 72: Unit4wt

Stateless session bean’s life cycle

Page 73: Unit4wt

Entity bean’s life cycle

• The EJB container :– Creates the instance– Calls the setEntityContext

• The entity bean moves to a pool of available instances

Page 74: Unit4wt

Entity bean’s life cycle

• While in the pool :– Instance is not associated with any particular object

identity – All instances in the pool are identical – EJB container may assign an identity to an instance when

moving it to the ready stage invoking the ejbActivate method

– A client may invoke the create method• EJB container calls ejbCreate and ejbPostCreate

– EJB container may remove the instance invoking unsetEntityContext

– Same bean instance (row) shared by all client

Page 75: Unit4wt

Entity bean’s life cycle

• While in the ready state : – A client may invoke entity bean's business methods– A client may invoke the remove method

• EJB container calls the ejbRemove method

– EJB container may invoke the ejbPassivate method

Page 76: Unit4wt

Entity bean’s life cycle

Page 77: Unit4wt

Message driven bean’s life cycle

• EJB container creates a pool of message-driven bean instances

• For each instance, the EJB container instantiates the bean :– It calls the setMessageDrivenContext – It calls the instance's ejbCreate

• Like a stateless session bean,it’s never passivated, It has only two states: – Nonexistent – Ready to receive messages. – is only a bean class – no interfaces

Page 78: Unit4wt

Message driven bean’s life cycle

• While in the ready state : – EJB container may call onMessage – EJB container may call the ejbRemove

Page 79: Unit4wt

Message driven bean’s life cycle