Java Beans and Enterprise Java Beans - PowerPoint PPT Presentation

About This Presentation
Title:

Java Beans and Enterprise Java Beans

Description:

GUI Button with Property to Allow Button Name to be Set ... Visual Age, JBuilder, PowerJ, Visual Caf ... One Method Present the Property is Read-Only (Set ... – PowerPoint PPT presentation

Number of Views:1415
Avg rating:3.0/5.0
Slides: 75
Provided by: Publicatio57
Category:
Tags: beans | enterprise | java

less

Transcript and Presenter's Notes

Title: Java Beans and Enterprise Java Beans


1
Java Beans andEnterprise Java Beans
  • Paul C. Barr
  • The Mitre Corporation
  • Eatontown NJ
  • poobarr_at_mitre.org

Kimble Cheron, Prof. Steven A. Demurjian, and
Mitch Saba Computer Science Engr. Dept.
steve_at_engr.uconn.edu http//www.engr.uconn.edu/st
eve (860) 486 - 4818
2
Changing Perspectives
  • Computers are Guilty of Creating More Disorder
    then Simplicity
  • Current Software Systems are Relics
  • Built Largely By and For Technical Computer Users
  • Never Intended to Operate in Todays Environment
  • Designers Need to Break Out of Old Mold
  • Consider Total Design Space
  • Design Should Help Manage Complexity, Not Add to
    It
  • What is Available to Assist in Changing Times?

3
Components
  • Reusable Software Building Blocks
  • Pre-Constructed from Encapsulated Application
    Code
  • Easily Combined with Other Components
  • Seamlessly Merged with Custom Code
  • Rapid Prototyping of Complete Applicaiton
  • What is a Component?
  • GUI Widget (Button, Window, etc.)
  • Combination of Components (Window with Elevator
    Bars and Pull-Down Menus)
  • Application Service (Account Management Function)

4
Overview
  • A First Look at Java Beans
  • What are They?
  • How are They Utilized?
  • Enterprise Java Beans
  • Not Just for Clients Anymore!
  • Capabilities and Usage
  • Relationship to New and Old Technologies
  • Component-Based Client/Server Model
  • Multi-Tiered Architecture and EJB
  • Tiered Development for Extensibility
  • Comparisons of Approaches
  • Java Blend for Database Interoperability

5
Java Beans
  • Extends "Write Once, Run Anywhere (WORA)TM" to
    Include "Reuse Everywhere
  • What is a Bean?
  • Independent Reusable Software Component
  • Visually Manipulated in Builder Tools
  • Can Be Visible Object
  • AWT Components
  • or Invisible Objects
  • Queues and Stacks
  • or Composed Objects
  • Calculator Ops Keys Display

6
Java Beans
  • Bean vs. Component
  • Source Code Inaccessible
  • Customizable to Suit Application Needs via
    External Properties
  • Powerful Means of Reuse
  • Examples
  • GUI Button with Property to Allow Button Name to
    be Set
  • Account Management Component that Allows Account
    Database Location to be Set
  • Properties Can be Very Complex and Offer
    Significant Power to Application Builders

7
Java Beans Fundamentals
  • Supported within IDEs
  • Visual Age, JBuilder, PowerJ, Visual Café
  • Construct JavaBeans Component as Specialized Java
    Classes with Enhanced Capabilities
  • Three-Part Architecture
  • Events
  • Notifies Others When Something Has Occurred
  • Delegation-Event Model of AWT
  • Properties
  • Define the Characteristics of the Bean
  • Methods
  • Utilized to Define a Property

8
Architecture Events
  • Events
  • Notifies Others When Something Has Occurred
  • Delegation-Event Model of AWT
  • Event Elements
  • Eventobjects
  • Components Sends to Listener
  • Eventlisteners
  • In Order to Register an Eventlistener With a
    Component, the Component Must Understand the
    Event Set
  • Event Sources
  • Well Discuss Shortly and Revisit in Detail with
    Java RMI

9
Architecture Properties
  • Example AWT Textfield
  • User Will See Properties for the Caret Position,
    Current Text, and the Echo Character, etc.
  • Methods Used to Define a Property
  • public void setPropertyName(PropertyType value)
  • public PropertyType getPropertyName()
  • The Name of the Property is Propertyname
  • The Datatype is Propertytype
  • Only One Method Present the Property is Read-Only
    (Set Missing)
  • Or the Property is Write-only (Get Missing)

10
Architecture Methods
  • Methods
  • Public
  • Available for Anyone to Call
  • Beaninfo and Getmethoddescriptors Method
  • Restricts Which Methods Are Visible to the
    Beanbuilder/Integration Tool
  • The Getmethoddescriptors Method Reports All the
    Methods of a Bean
  • Supports Introspection
  • Beaninfoclass
  • Customize a Bean's Appearance to an Integration
    Tool

11
Abilitiy
  • Persistence
  • Ability of an Object to Store its State
  • Object Serialization Saves All Non-static and
    Non-transient Instance Variables of an Object
  • Objectinput and Objectoutput Interfaces
  • The Basis for Serialization Within Java.
  • Objectinputstream and Objectoutputstream Classes
  • Implement the Objectinput and Objectoutput
    Interfaces

12
Five Defining Features
  • Introspection
  • Allow a Builder Tool to Analyze How a Bean Works
  • Customization
  • User is Allowed to Alter the Appearance and
    Behavior of a Bean
  • Events
  • Firing of Events
  • Inform Builder Tools About Both the Events They
    Can Fire and Handle

13
Five Defining Features
  • Properties
  • Beans Can Be Manipulated Programmatically
  • Support the Customization as Mentioned
  • Persistence
  • Customized Beans Can Have Their State Saved and
    Restored
  • Work in a Constructed Application Can Be Restored
    by an Application Builder's Save and Load Menu
    Commands
  • Beans Are Used Primarily With Builder Tools
  • Programmatic Interfaces Allow Beans to Be
    Manually Manipulated by Text Tools

14
Differences Between Beans and Classes
  • Introspection
  • Process of Determining the Supported Properties,
    Methods, and Events of a Bean
  • Introspector Class
  • Provides Access to the Beaninfo for the Bean
    Component Via Its getBeanInfo Method
  • Code
  • TextField tf new TextField ()
  • BeanInfo bi Introspector.getBeanInfo
    (tf.getClass())
  • Alternative to Introspector Class
  • Provides Access Directly Through the Use of the
    Reflection API

15
Introspection Tools
  • Differentiates Beans From Typical Java Classes
  • Recognize Predefined Patterns in Class
    Definitions and Method Signatures
  • Able to "Look Inside" a Bean to Determine Its
    Properties and Behavior
  • Require That Method Signatures Within Beans Must
    Follow a Certain Pattern
  • Recognize How Beans Can Be Manipulated at Design
    and Run Time
  • Pattern Signatures are Designed to Be Recognized
    by Human Readers and Builder Tools

16
Bean vs. Class
  • A Bean's State Can Be Manipulated at Design Time
    in Contrast to Classes at Run Time
  • Beans Attributes and Behaviors
  • Published by Special Method Signature Patterns
  • Recognized by Beans-Aware Application
    Construction Tools
  • What to Use for Software Modules?
  • Beans are Better for Software Components Visually
    Manipulated Within Builder Tools
  • Classes are Better for Functionality Through a
    Programmatic (Textual) Interface
  • An SQL API Would Be Better Packaged Through a
    Class Library

17
Event Model
  • Three Elements
  • Eventobject
  • Eventlistener
  • Eventsource (the Bean)
  • Eventobject
  • Basis of All Beans Events
  • Java.Util.Eventobject Class
  • Require Programmer to Subclass an Eventobject to
    Have a Specific Event Type

18
Java 1.1 Delegation Event Model
19
Example Event for Employees Hire Date
  • public class HireEvent extends EventObject
  • private long hireDate
  • public HireEvent (Object source)
  • super (source)
  • hireDate System.currentTimeMillis()
  • public HireEvent (Object source, long hired)
  • super (source)
  • hireDate hired
  • public long getHireDate ()
  • return hireDate

20
EventListener
  • Definition
  • Entity That Desires Notification When an Event
    Happens
  • Receives the Specific Eventobject Subclass As a
    Parameter

21
EventListener
  • Eventlistener Interface
  • Empty Interface
  • Acts as a Tagging Interface That All Event
    Listeners Must Extend
  • Eventtypelistener Name of Listener Interface
  • The Name for the New Hireevent
    Listener Would Be Hirelistener
  • Method Names Should Describe the Event Happening
  • Code
  • public interface HireListener
  • extends java.util.EventListener
  • public abstract void hired (HireEvent e)

22
EventSource
  • Eventsource
  • Defines When and Where an Event Will Happen
  • Hireevent and Hirelistener Are Required for an
    Event Source to Function
  • Sends Notification to Registered Classes When
    the Event Happens

23
EventSource Methods
  • Registration Process Method Patterns
  • public synchronized void
  • addListenerType(ListenerType l)
  • public synchronized void
  • removeListenerType(ListenerType l)
  • Maintaining Eventsource Code
  • private Vector hireListeners new Vector()
  • public synchronized void
  • addHireListener(HireListener l)
  • hireListeners.addElement (l)
  • public synchronized void
  • removeHireListener (HireListener l)
  • hireListeners.removeElement (l)

24
EventSource Hiring Example
  • protected void notifyHired ()
  • Vector l
  • // Create Event
  • HireEvent h new HireEvent (this)
  • // Copy listener vector so it won't change while
    firing
  • synchronized (this)
  • l (Vector)hireListeners.clone()
  • for (int i0iltl.size()i)
  • HireListener hl (HireListener)l.elementAt (i)
  • hl.hired(h)

25
What is Enterprise Java Beans ?
  • Expansion of Java Beans (Client-side) to Support
    Server Side Reusable Components
  • Server Components Run on Application Server
  • EJB Integral Part of Java Technology
  • Component Architecture for Distributed Systems
  • Multi-Tier Distributed Architecture
  • Movement of Application Logic from Client to
    Server Side
  • Creation of Thin, Easier to Maintain Clients
  • Framework for Creating Middle Ware
  • Integration of New and Old Technologies
  • RMI, IIOP, CORBA, RPC, Active X, etc.

26
Designer and Developer Roles in Enterprise Java
Beans (EJB)
  • Towards Highly Scalable, Highly Available,
    Highly Reliable, Highly Secure, Transaction
    Distributed Applications
  • Enterprise Bean Provider
  • Creates and Sells EJBs
  • Application Assembler
  • Uses EJBs to Build an Application
  • EJB Server Provider
  • Creates and Sells EJB Server
  • EJB Container Provider
  • Creates and Sells EJB Containers
  • Server Provider Will Likely Provide Containers

27
EJB Roles Deployment
28
Utilizing EJB Technology
29
The EJB Architecture
  • EJB Servers Analogous to CORBA ORB
  • Server Software
  • Provides Naming and Transaction Services
  • Makes Containers Visible
  • EJB Containers Interface Between EJB Bean and
    Outside World
  • Client Never Accesses Bean Directly
  • Access via Container-Generated Methods
  • These Methods Then Call the Beans Methods
  • EJB Clients
  • Locate EJB Containers Via JNDI
  • Make Use of EJB Beans
  • Enterprise Java Beans - Discussed Shortly

30
Recall CORBA
31
EJB Container
32
Enterprise Java APIs
33
Enterprise Java Beans Session Beans
  • Associated With a Particular Client
  • Performs Operations on Behalf of Client
  • Accessing a Database
  • Performing Calculations
  • Created and Destroyed by a Client
  • Can be Transactional - But, Do Not Survive System
    Shutdown
  • Can be Stateless or Maintain Conventional State
    Across Methods and Transactions
  • Must Manage Own Persistent Data

34
Enterprise Java Beans Entity Beans
  • Object Representation of Persistent Data
    Maintained in Permanent Store (Database
  • Identifiable by Primary Key
  • Shared by Multiple Clients
  • Persist Across Multiple Invocations
  • Survive System Shutdown
  • Created by
  • Inserting Data into Database
  • Creating an Object Instance

35
Model for PersistencePassivation/Activation
  • Programmatic Model for Managing Persistent
    Objects
  • EJB Server has the Right to Manage its Working
    Set
  • Passivation
  • Saves State of a Bean to Persistent Storage
  • Then Swaps Bean Out
  • Activation
  • Restores State of a Bean From Persistent
  • Storage,Then Swaps Bean in
  • Applies to Both Session and Entity Beans

36
Stateless vs. Stateful Session Beans
  • Stateless
  • No Internal State
  • Do Not Need to Be "Pass-ivated"
  • Can Be Pooled to Service Multiple Clients
  • Stateful
  • Possess Internal State
  • Need to Handle Passivation/Activation
  • One Per Client

37
Persistent Session Beans
  • Session Beans Can Be Saved and Restored Across
    Client Sessions
  • To Save
  • Call the Session Beans getHandle() Method
  • Returns a Handle Object
  • To Restore
  • Call the Handle Objects getEJBObject() Method

38
Entity Bean Persistence
  • Container-Managed
  • Container is Responsible for Saving State
  • In Deployment Descriptor, Specify
    Container-Managed Fields
  • Persistence Independent of Data Source
  • Bean-Managed
  • Bean is Responsible for Saving its Own State
  • Container Doesnt Need to Generate DB Calls
  • Less Adaptable Persistence is Hard-Coded

39
Writing an EJB Client
  • Locate the Bean Container
  • Allocate a Bean, If Needed
  • Use the Bean
  • Dispose of the Bean

//An idealized EJB client import
paul.ejb.restaurant. public class EJBClient
public static void main(String argv) //get
JNDI naming context javax.naming.Context
initialContext new javax.naming.InitialContext()
//use context to look up EJB home
interface RestaurantHome rh initialContext.look
up(RestaurantHome) //use home interface to
create a session object Restaurant r
rh.Create(Burger Heaven) //invoke business
methods r.order(cheeseburger) //remove
session object r.remove()
40
Writing a Session Bean
  • Create Remote Interface
  • must extend javax.ejb.EJBObject interface
  • must give prototypes for business methods
  • class neednt say implements this is handled
    by the container
  • Create Home Interface
  • must extend javax.ejb.EJBHome interface
  • create() methods, remove() methods
  • Implement Create Methods
  • called by container at creation time
  • Implement the SessionBean Interface
  • ejbActivate() - called when bean is activated
  • ejbPassivate() - called when bean is passivated
  • ejbRemove() - called when bean is destroyed
  • setSessionContext(SessionContext ctx) - called by
    container to give bean a context

41
Session Bean Example
package paul.ejb.restaurant.server public class
OrderBean implements SessionBean private
transient SessionContext ctx private
String order //can have many create
methods public void ejbCreate () throws
Exception //initialization of class
variables here //business method
public boolean order(String order)
this.order order System.out.println("orde
r received for " order) return true
//these methods are required by the
SessionBean interface public void
ejbActivate() throws Exception
public void ejbDestroy() throws Exception
public void ejbPassivate() throws
Exception public void
setSessionContext(SessionContext ctx) throws
Exception this.ctx ctx
42
Writing an Entity Bean
  • Implement the EntityBean Interface
  • ejbActivate() // called on activation
  • ejbPassivate() // called on passivation
  • ejbLoad() // tells bean to load state from
    database
  • ejbStore() // tells bean to store state in
    database
  • ejbRemove() // called when client calls remove()
  • setEntityContext() // called by container when
    instance has been created
  • unsetEntityContext() // called by container
    before removing the instance
  • must also implement ejbFind() // allows client to
    look up EJB objects
  • Optionally Implement create() Methods
  • Create Remote Interface
  • must extend javax.ejb.EJBObject interface
  • Create Home Interface

43
Entity Bean Example
package paul.ejb.entity.server public
class ExampleBean implements EntityBean
private transient EntityContext ctx
//notice no finder method -- generated at
deployment time by container provider //can
have multiple create methods public void
ejbCreate () throws Exception
//business method public boolean
doSomething () //required methods
for EntityBean interface public void
ejbActivate() throws Exception
public void ejbDestroy() throws Exception
public void ejbPassivate() throws
Exception public void ejbLoad()
throws Exception public void
ejbStore() throws Exception
public void setEntityContext (EntityContext ctx)
throws Exception this.ctx ctx

44
Deploying EJBs
  • EJBs Deployed As .SER Files Serialized Instance
  • Manifest File Used to List EJBs
  • Must Also Provide a Deployment Descriptor
  • Sample Entry
  • Name paul.RestaurantDeployment.ser
  • Enterprise-Bean True
  • Name Line
  • Describes a Serialized Deployment Descriptor
  • Enterprise-Bean Line
  • Indicates Whether the Entry Should Be Treated as
    an EJB
  • (Not All Entries Need to Be EJBs)

45
Whos Announced EJB Support?
  • WebLogic
  • IBM
  • Oracle
  • GemStone
  • BEA
  • Borland
  • Netscape
  • Lotus
  • Forte
  • Progress
  • Novell
  • Novera
  • Borland
  • Informix
  • IONA
  • More...

46
Typical Development and Deployment Scenario
  • EJB Server Provider
  • Creates and Sells an EJB Server
  • Provides EJB Containers That Will Run on These
    Servers
  • EJB Providers
  • Individuals Responsible for Developing the EJBs
  • Provide Tools and Components for Down-Stream
    Usage
  • Application Assemblers
  • Individuals that Utilize Pre-Built EJBs to
    Construct Their Domain-Specific Applications
  • Utilize State-of-Art-Tools for EJB and JB

47
EJB Component-Based Architecture
EJB Server
Invoke EJB Methods
Invoke EJB Container Methods
Enterprise Java Bean
EJB Client
EJB Container
48
Client-Server Component Model
49
Client-Server Component Relationship
50
Client-Server Component Relationship (Concluded)
51
EJB Server Example
52
EJB Server Example (continued)
53
EJB Server Example (Concluded)
54
Three-Tier Architecture Concept
55
Two-Tier, Three-Tier, Four-Tier Example
Architectures
From http//java.sun.com/javaone/javaone98/sessio
ns/T400/index.html
56
Wombat Securities
  • Web Access to Brokerage Accounts
  • Only HTML Browser Required on Front End
  • "Brokerbean" EJB Provides Business Logic
  • Login, Query, Trade Servlets Call Brokerbean
  • Use JNDI to Find EJBs, RMI to Invoke Them
  • Order and History Records from Java Blend Product
  • Records Mapped to Oracle Tables, JDBC Calls

57
Four-Tier Architecture Example
58
Initialization Code
class LoginServlet extends HttpServlet
private Broker createBroker(String account,
String password) / Use JNDI
to find Broker EJB home, then return new EJB /
Context initialContext new
InitialContext() BrokerHome
brokerHome (BrokerHome)
initialContext.lookup("wombat/Broker")
return brokerHome.create(account,
password)
public doPost
(HttpServletRequest req, HttpServletResponse
res) throws ServletException,
IOException / Get account and
password from HTML and create EJB/
String account req.getParameter("Account")
String password
req.getParameter("Password")
Broker brk createBroker(account, password)
... ...
59
User Interface Code
class TradeServlet extends HttpServlet
... public
void doPost(HttpServletRequest req,
HttpServletResponse res) throws
ServletException / Get
parameters from HTML and session object /
HttpSession session
req.getSession(true)
String ticker req.getParameter("Symbol") ...
Broker brk (Broker)
session.getValue("LoginServlet.Brok
erRef") / Place order
and print confirmation as HTML /
String orderNum brk.submitOrder(txType,
ticker, shares)
printOrderSubmitted(out, account, orderNum)
...
60
Business Logic Code
class BrokerBean implements SessionBean
public void ejbCreate (String account, String
password) ... public SecurityRecord
getAcctPositions ( ) ... public
OrderRecord getOrderStatus ( ) ...
public String getQuote(String ticker) ...
public String submitOrder(String txType,
String ticker...) ... public
HistoryRecord getAcctHistory ( ) ...
private boolean login (...) ...
... etc ...
61
Database Access Code
  • SQL
  • CREATE TABLE ORDER(
  • ORDERNUM INTEGER NOT NULL,
  • CUST INTEGER NOT NULL,
  • TICKER VARCHAR(5), ...
  • PRIMARY KEY (ORDERNUM),
  • FOREIGN KEY (CUST) REFERENCES CUSTOMER)
  • Java
  • class Order
  • int orderNum
  • Customer cust
  • String ticker
  • ...
  • Java Blend Automatically Maps Classes to/from
    Relational Tables at Compile-Time and Run-Time

62
Nocturnal Aviation, Inc.
  • Passenger Check-in for Regional Airline
  • Local Database for Seating on Today's Flights
  • Clients Invoke EJBs at Local Site Through RMI
  • EJBs Update Database and Queue Updates
  • JMS Queues Updates to Legacy System
  • DBC API Used to Access Local Database
  • JTS Synchs Remote Queue With Local Updates

63
Three-Tier Example
64
Santa Cruz Widgets
  • Small Manufacturer Previously on C
  • New Order Entry, Inventory, and Invoicing
    Applications in Java Programming Language
  • Existing Customer and Order Database
  • Most of Business Logic in Stored Procedures
  • Tool-generated GUI Forms for Java Objects
  • Located Company on Web Using Widgets and Tcl, but
    Not Widgets and Java

65
Santa Cruz Widgets (2-tier)
66
Architecture Comparisons
  • Two-tier Through JDBC API Simplest
  • Multi-tier Separate Business Logic, Protect
    Database Integrity, More Scaleable
  • JMS Queues Vs Synchronous (RMI or IDL)
  • Availability, Response Time, Decoupling
  • JMS Publish Subscribe Off-line Notification
    RMI IIOP Vs JRMP Vs Java IDL
  • Standard Cross-language Calls or Full Java
    Functionality
  • JTS Distributed Integrity, Lockstep Actions

67
Further API Comparisons
  • Servlets Simplifies HTML, Connections, Web
    Front-Ending Legacy Systems
  • EJBs Simplifies Components, Scalability,
    Transactions, Multi-threading, Security, State
  • JDBC Vs ODMG Vs SQLJ API
  • Programming Simplicity, Portability, SQL
    Knowledge
  • JNDI Standardized Name Service Access
  • Enterprise Java APIs vs. Proprietary
  • Multi-Platform, Multiple Providers

68
Summary Key Messages
  • Enterprise Java APIs EJB, JNDI, Java IDL, RMI,
    JDBC, ODMG, SQLJ, JMS, JTS, JMAPI, Servlets
  • Wide Variety of Architectural Alternatives
  • Synchronous/Asynchronous, Multi-Tier,
    Transactional, HTTP/JRMP/IIOP
  • Benefits
  • WORA Portability
  • Multiple Vendors
  • Legacy Connectivity
  • Java Programming Language Productivity

69
EJB Roadmap
70
What Is the Java Blend Product?
  • Product That Integrates Java Programming Language
    Objects With Enterprise Data
  • Provides a Single Object Model Based on Java
    Programming Language Classes
  • Automatic Persistent Storage for Java Application
    Objects
  • Easy, Automatic Access to Existing Relational
    Databases From Java Applications
  • Result of Joint Development by the Javasoft
    Division, Baan and Tech_at_spree

71
JavaBlend Applications
  • JavaBlend Software Provides Mapping Capability
    Between Database Tables and Java Application
    Classes
  • Programmer Deals Only With Java Programming
    Language Objects, and Does Not Need to Know SQL
    or Database Representation

72
Java Blend Components
  • Flexible Development Tool for Automatic
    Bi-Directional Mapping
  • Objects to Relational
  • Relational to Objects
  • Powerful Runtime Environment
  • Transaction Management System
  • Query Processor
  • Cache Management System

73
Java Blend Runtime Architecture
74
Java Blend Software and Enterprise
JavaBeans Technology
  • EJB Technology Provides Scaleable Component
    Architecture for Business Applications
  • Java Blend Product Provides Transparent
    Persistence for Enterprise Javabeans Technology
  • Beans Implementing Business Logic Use Java Blend
    for Database Access
  • Java Blend Product Works With the Enterprise
    Javabeans API Transaction Mechanisms
Write a Comment
User Comments (0)
About PowerShow.com