Java Beans
JavaBeans• An introduction to component-based  development in general• Introduction to JavaBeans  – Java components  – cli...
Java Components• JavaBeans -- portable, platform-independent  component model• Java components are known as beans• A bean:...
JavaBeans vs. Class Libraries• Beans are appropriate for software  components that can be visually manipulated• Class libr...
JavaBeans Concepts• A component is a self-contained reusable  software unit• Components expose their features (public  met...
Concepts...• You can select a bean from the toolbox, drop it  in a form, and modify its appearance and  behavior.• Also, y...
JavaBean Characteristics•   a public class with 0-argument constuctor•   it has properties with accessory methods•   it ha...
Key Concepts• A builder tool discover a bean’s features by a  process known as introspection.  – Adhering to specific rule...
Key Concepts….• Properties can be customized at design-time.  Customization can be done:  – using property editor  – using...
Security Issues• JavaBeans are sbject to the standard Java  security model• The security model has neither extended nor  r...
Creating jar files• c-create an archive• C-change the directory during the execution• f-first name in the file names listi...
Create Sample.java fileclass Sample extends Canvas -to make it visibleCreate manifest file
MyFirstBean• import java.awt.*;• import java.io.Serializable;• public class FirstBean extends Canvas implements Serializab...
First Bean• Compile: javac FirstBean.java• Create a manifest file:• mani.mft  – Name: FirstBean.class  – Java-Bean: True• ...
Using Beans in hand-written app• Use Beans.instantiate•     Frame f;•     f = new Frame("Testing Beans");•     try {•     ...
Properties• Bean’s appearance and behavior -- changeable  at design time.• They are private values• Can be accessed throug...
Properties• A builder tool can:  – discover a bean’s properties  – determine the properties’ read/write attribute  – locat...
Types of Properties• Simple• Index: multiple-value properties• Bound: provide event notification when value  changes• Cons...
Simple Properties• When a builder tool introspect your bean it  discovers two methods:  – public Color getColor()  – publi...
Simple Properties….• Adding a Color property  – Create and initialize a private instance variable     • private Color colo...
Events “Introspection”• For a bean to be the source of an event, it  must implement methods that add and  remove listener ...
Events “Introspection”• Source Bean fires events at the listeners using  method of those interfaces.• Example: if a source...
Events “using BeanInfo”• Implementing the BeanInfo interface allows  you to explicitly publish the events a Bean  fires
BeanInfo interface• Bean exposes its features in a property sheetusing java.beans.Introspector class (which uses  Core Ref...
Bean Customization• The appearance and behavior of a bean can  be customized at design time.• Two ways to customize a bean...
Property Editors• A property editor is a user interface for editing  a bean property. The property must have  both, read/w...
Property Editors• If you provide a custom property editor class,  then you must refer to this class by calling  PropertyDe...
Enterprise Java Beans• Introduction   – Application Server   – Java 2 Enterprise Edition• What is an Enterprise Bean ?   –...
Introduction• Enterprise Java Beans ( EJB ) is   – a middleware component model for Java and CORBA   – a specification for...
Introduction• This is the three level structure for Application Server
Applicaton Server• Presentation  – HTML Application  – Java Application• Business Logic• Data Access
Presentation• HTML                       • Java  – Generated server-side      – Required Java virtual    HTML             ...
Business Logic• Implements the logic of the application defining all  the function that may be used from a client   –   Ch...
Data Access• Utility to access external datas such as Database or  other Web component• Access other SOA
J2EE Application Server• Java 2 Enterprise Edition standardizes interfaces for  Application Server components
What is an Enterprise Bean ?• Is a server side component written in Java Language• Industry standard distribuited componen...
EJB Overview
Deployment Phase
Deployment Phase
Type of beans•   Session Bean•   Entity Bean•   Message Driven Bean
Session Bean• Represents a single client inside the server• The client calls the session bean to invoke methods  of an app...
Session Bean•   Stateful session bean•   Stateless session bean
Stateful Session Bean• Contains the state of a single client session:   – Information on the client   – On method called  ...
Stateless Session Bean• Not maintain a conversational state for a particular  client• Contains values only for the duratio...
Entity Bean•   Represents a business object in a persistent storage    mechanism such as a relational database•   Usually ...
Entity Bean persistent•   Bean managed persistence•   Container managed persistence
Bean managed                   persistence• Who write the bean’s code must access the database  and save his own data• you...
Container managed persistence• The container save the data• There is no code in the bean for access the database• The cont...
Entity bean’s shared access•   Entity beans can be used by different clients•   It’s important that they work whithin tran...
Entity bean’s primary key• Each entity bean has a unique object identifier like a  key in a database table• Each instance ...
Entity bean’s relationship• Container managed persistent  – The container performs all the operation to create    relation...
Message Driven bean• Allows applications to process messages  asynchronously• The messages may be sent by :   – An applica...
Message Driven bean• Retain no data or conversational state for a specific  client• The instance variables of the message-...
Message Driven bean• A client can’t access directly to a message driven  bean• When a message arrive, the container gives ...
Client access with interfaces• A client may access a session or an entity bean only  through the methods defined in the be...
Remote access• A remote client of an enterprise bean has the  following traits:   – It may run on a different machine and ...
Remote access• To create an enterprise bean with remote access, you  must :   – Code a remote interface      • Business me...
Remote access example
Local access• A local client has these characteristics  – It must run in the same JVM as the enterprise    bean it accesse...
Local access• To create an enterprise bean with local access, you  must :   – Code the local interface      • Beans busine...
Local interfaces• If an entity bean is the target of a container managed  relationship it MUST have local interfaces• An E...
Contents of an Enterprise Bean• Deployment descriptor   – Persistence type   – Transaction attribute• Enterprise bean clas...
EJB Example• The OnLine Bank  We will take a not completed system to give an idea to how  choose if a component is an enti...
A few EJB implementations•   WebLogic•   Bluestone•   Novera•   Persistence•   Oracle AS•   Oracle8i
The EJB architecture• Consists of:   –   An EJB server   –   EJB containers that run within the server   –   Home objects ...
EJB ArchitectureNaming Service                      Server                       Home Interface         Container         ...
Stateful session bean’s                       life cycle• The client invoke the create method• The EJB container :   – Ins...
Stateful session bean’s                       life cycle• While in the ready state   – EJB container may passivate the bea...
Stateful session bean’s       life cycle
Stateless session bean’s                       life cycle• The client invoke the create method• The EJB container :   – In...
Stateless session bean’s                       life cycle• While in the ready state   – A client may invoke a business met...
Stateless session bean’s        life cycle
Entity bean’s life cycle• The EJB container :   – Creates the instance   – Calls the setEntityContext• The entity bean mov...
Entity bean’s life cycle• While in the pool :   – Instance is not associated with any particular object     identity   – A...
Entity bean’s life cycle• While in the ready state :   – A client may invoke entity beans business methods   – A client ma...
Entity bean’s life cycle
Message driven bean’s                      life cycle• EJB container creates a pool of message-driven bean  instances• For...
Message driven bean’s                      life cycle• While in the ready state :   – EJB container may call onMessage   –...
Message driven bean’s      life cycle
Upcoming SlideShare
Loading in …5
×

Unit4wt

238 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
238
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit4wt

  1. 1. Java Beans
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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.
  6. 6. 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.
  7. 7. 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
  8. 8. 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.
  9. 9. 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.
  10. 10. 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.
  11. 11. 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
  12. 12. Create Sample.java fileclass Sample extends Canvas -to make it visibleCreate manifest file
  13. 13. MyFirstBean• import java.awt.*;• import java.io.Serializable;• public class FirstBean extends Canvas implements Serializable {• public FirstBean() {• setSize(50,30);• setBackground(Color.blue);• }• }
  14. 14. 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
  15. 15. 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);
  16. 16. 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)
  17. 17. 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
  18. 18. 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
  19. 19. 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.
  20. 20. 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(); •}
  21. 21. 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.
  22. 22. 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
  23. 23. Events “using BeanInfo”• Implementing the BeanInfo interface allows you to explicitly publish the events a Bean fires
  24. 24. 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
  25. 25. 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
  26. 26. 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.
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. Introduction• This is the three level structure for Application Server
  31. 31. Applicaton Server• Presentation – HTML Application – Java Application• Business Logic• Data Access
  32. 32. Presentation• HTML • Java – Generated server-side – Required Java virtual HTML Machine – Runs on any Web – More client side power browser – Runned on a page – Less client-side power – Security (Applet) – Launched from a browser or a standalone application
  33. 33. 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
  34. 34. Data Access• Utility to access external datas such as Database or other Web component• Access other SOA
  35. 35. J2EE Application Server• Java 2 Enterprise Edition standardizes interfaces for Application Server components
  36. 36. 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
  37. 37. EJB Overview
  38. 38. Deployment Phase
  39. 39. Deployment Phase
  40. 40. Type of beans• Session Bean• Entity Bean• Message Driven Bean
  41. 41. 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
  42. 42. Session Bean• Stateful session bean• Stateless session bean
  43. 43. 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
  44. 44. 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
  45. 45. 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 table Properties: • Persistent • Allow shared access • Have primary key • Have relationship with other entity beans. • Auto commit.
  46. 46. Entity Bean persistent• Bean managed persistence• Container managed persistence
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. 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
  51. 51. 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
  52. 52. 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
  53. 53. 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.
  54. 54. 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
  55. 55. Client access with interfaces• A client may access a session or an entity bean only through the methods defined in the beans interfaces• They define the clients 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
  56. 56. 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
  57. 57. 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)
  58. 58. Remote access example
  59. 59. 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
  60. 60. Local access• To create an enterprise bean with local access, you must : – Code the local interface • Beans business methods – Code the local home interface • Life cycle • Finder methods • Utility methods
  61. 61. 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
  62. 62. Contents of an Enterprise Bean• Deployment descriptor – Persistence type – Transaction attribute• Enterprise bean class• Interfaces• Helper classes – Exception – Utility classes
  63. 63. EJB Example• The OnLine Bank We will take a not completed system to give an idea to how choose if a component is an entity, session or message driven bean.
  64. 64. A few EJB implementations• WebLogic• Bluestone• Novera• Persistence• Oracle AS• Oracle8i
  65. 65. 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
  66. 66. EJB ArchitectureNaming Service Server Home Interface Container (Factory) RMI Client RMI Remote EJB Object Interface (Wrapper) Enterprise Java Bean Implements (Biz Logic) Invokes Creates / uses
  67. 67. 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
  68. 68. 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 beans ejbActivate method – A client may invoke the remove method and the container calls the beans ejbRemove method – Client cannot invoke passivate
  69. 69. Stateful session bean’s life cycle
  70. 70. 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
  71. 71. 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 beans ejbRemove method – It’s never passivate – It’s can be pooled
  72. 72. Stateless session bean’s life cycle
  73. 73. Entity bean’s life cycle• The EJB container : – Creates the instance – Calls the setEntityContext• The entity bean moves to a pool of available instances
  74. 74. 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
  75. 75. Entity bean’s life cycle• While in the ready state : – A client may invoke entity beans business methods – A client may invoke the remove method • EJB container calls the ejbRemove method – EJB container may invoke the ejbPassivate method
  76. 76. Entity bean’s life cycle
  77. 77. 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 instances 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
  78. 78. Message driven bean’s life cycle• While in the ready state : – EJB container may call onMessage – EJB container may call the ejbRemove
  79. 79. Message driven bean’s life cycle

×