java beans and enterprise java beans n.
Skip this Video
Loading SlideShow in 5 Seconds..
Java Beans and Enterprise Java Beans PowerPoint Presentation
Download Presentation
Java Beans and Enterprise Java Beans

Java Beans and Enterprise Java Beans

163 Vues Download Presentation
Télécharger la présentation

Java Beans and Enterprise Java Beans

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Java Beans andEnterprise Java Beans Kimble Cheron, Prof. Steven A. Demurjian, and Mitch Saba Computer Science & Engr. Dept. (860) 486 - 4818 Paul C. Barr The Mitre Corporation Eatontown NJ

  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 Today’s Environment • Designer’s 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 • We’ll 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 Employee’s 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 i=0;i<l.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 Bean’s 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 Bean’s getHandle() Method • Returns a Handle Object • To Restore • Call the Handle Object’s 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 Doesn’t 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*; • 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.lookup(“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 needn’t 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; 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("order 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. WebLogic IBM Oracle GemStone BEA Borland Netscape Lotus Forte Progress Novell Novera Borland Informix IONA More... Who’s Announced EJB Support?

  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)