380 likes | 490 Views
J2EE Concepts. Why J2EE ?. Client. Client. Client. Server. Server. Database. Why J2EE ?. Things to consider when building large business systems : Load Balancing Transparent fail-over Back end integration Transactions Threading Middleware Dynamic Deployment Object Life cycle
E N D
Why J2EE ? Client Client Client Server Server Database J2EE Concepts
Why J2EE ? Things to consider when building large business systems : • Load Balancing • Transparent fail-over • Back end integration • Transactions • Threading Middleware • Dynamic Deployment • Object Life cycle • Resource pooling • Caching • Security J2EE Concepts
J2EE Concepts : Definition : The Java 2 Enterprise Edition (J2EE) is a multi-tiered architecture for implementing enterprise-class applications and web based applications Model : It is based on component based application model. In this model such components use services provided by the container which would otherwise typically need to be incorporated in the application code. J2EE Concepts
J2EE Components • Application clients and applets are components that run on the client. • Java Servlet and JavaServer Pages technology components are Web components that run on the web server. (Web Container) • Enterprise JavaBeans components (enterprise beans) are business components that run on the application server. (EJB Container) J2EE Concepts
J2EE Components -- continued J2EE Concepts
J2EE Containers Definition : Containers are the interface between a component and the low-level platform-specific functionality that supports the component. Types : • Enterprise JavaBeans (EJB) container • Web container • Application client container • Applet container J2EE Concepts
J2EE Container -- continued J2EE Concepts
J2EE APIs • Enterprise JavaBeans Technology • JDBC API • Java Servlet Technology • Java Server Pages Technology • Java Message Service (JMS) • Java Naming and Directory Interface (JNDI) • Java Transaction API (JTA) • Java Mail API • Java API for XML Processing (JAXP) • Java API for XML Registries (JAXR) • Java API for XML-Based RPC (JAX-RPC) • SOAP with Attachment API for Java (SAAJ) • Java Authentication and Authorization Service J2EE Concepts
EJB Container Isolates the enterprise bean from direct access by client applications. A container may provide the following services: • Lifecycle management • Transactions • Security • Connection pooling • Instance pooling J2EE Concepts
J2EE Application Packaging A J2EE application is delivered in an Enterprise Archive (EAR) file with an extension “.ear” EAR file is a standard Java Archive file (JAR) with an extension “.ear” It consists of multiple J2EE modules. J2EE Module => one or more J2EE components for the same container type + component deployment descriptor. Deployment Descriptor => an XML document with an .xml extension that describes a component's deployment settings. J2EE Concepts
Enterprise JavaBeans Definition : Enterprise Javabeans (EJBs) standard is the component architecture for deployable server side components in Java. Values : • It is Agreed Upon by the industry • Portability is easier • Rapid application development J2EE Concepts
Roles In EJB • 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 J2EE Concepts
EJB Architecture EJB SERVER EJB Container Enterprise Bean EJB CLIENT J2EE Concepts
Session Beans Interact with Client Model Business logic Do not survive system shutdown Entity Beans Represent persistent data Survive system shutdown Types Of Beans J2EE Concepts
Stateless No Internal state Do not need to be passivated Lifetime is controlled by the container Stateful Possess internal state Need to be passivated/activated Assigned to one client for a lifetime Session Beans J2EE Concepts
Container Managed Container is responsible for saving state. In dd, specify container managed fields Persistence is data store independent. Bean Managed Bean is responsible for saving state Container doesn’t need to generate DB calls Less Adaptable : Persistence is hard coded in the code. Entity Beans J2EE Concepts
What constitutes an Enterprise Bean ? • Enterprise Bean Class – • Implements the methods defined in the following interfaces. • Must implement either javax.ejb.SessionBean or javax.ejb.EntityBean • Home Interface – • Define methods for creating, destroying and finding EJB objects. • Must extend javax.ejb.EJBHome interface J2EE Concepts
What constitutes an Enterprise Bean ? • Remote Interface – • Defines business logic functions that the corresponding bean class implement. • Must extend javax.ejb.EJBObject interface. • Deployment Descriptor – An XML file that specifies information about the bean such as type of the bean, persistence etc. • Utility/Helper Classes – • Vendor Specific files.(jboss.xml & jaws.xml) J2EE Concepts
Writing a Session Bean Home Interface : import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; Cart create(String person, String id) throws RemoteException, CreateException; } J2EE Concepts
Writing a Session Bean – continued Remote Interface : The remote interface, which extends javax.ejb.EJBObject, defines the business methods that a remote client may invoke. import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException; } J2EE Concepts
Writing a Session Bean – continued Bean Class : public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); } J2EE Concepts
Writing a Session Bean – continued public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: "+ id); } contents = new Vector(); } J2EE Concepts
Writing a Session Bean – continued public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + "not in cart."); } } public void addBook(String title) { contents.addElement(title); } public Vector getContents() { return contents; } J2EE Concepts
Writing a Session Bean – continued public CartBean() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} } J2EE Concepts
Writing a Session Bean – continued Helper Classes The CartEJB session bean has two helper classes: BookException and IdVerifier. The BookException is thrown by the removeBook method and the IdVerifier validates the customerId in one of the ejbCreate methods. J2EE Concepts
Writing a Session Bean – continued Deployment Descriptor (ejb-jar.xml) <?xml version="1.0" encoding="Cp1252"?> <ejb-jar> <description>jBoss test application</description> <display-name>Test</display-name> <enterprise-beans> <session> <ejb-name>CartBean</ejb-name> <home>cartHome</home> <remote>Cart</remote> <ejb-class>CartBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> J2EE Concepts
Session Bean Client public static void main(String[] args) { try { InitialContext jndiContext = new InitialContext(); Object ref = jndiContext.lookup(“ejb/CartBean"); // Get a reference from this to the Bean's Home interface CartHome home = (CartHome) PortableRemoteObject.narrow (ref, CartHome .class); Cart cart = home.create(); } catch(Exception e) { System.out.println(e.toString()); } } J2EE Concepts
Writing an Entity Bean(CMP) Let’s write a entity bean CD that’s represents a music CD. It contains attributes like title code id and other properties of a music CD. Remote Interface : public interface CD extends EJBObject { public Integer getId() throws RemoteException; public void setId(Integer id) throws RemoteException; public String getTitle() throws RemoteException; public void setTitle(String title) throws RemoteException; public String getArtist() throws RemoteException; public void setArtist(String artist) throws RemoteException; public String getType() throws RemoteException; public void setType(String type) throws RemoteException; public String getNotes() throws RemoteException; public void setNotes(String type) throws RemoteException; } J2EE Concepts
Writing an Entity Bean -- continued Home Interface : /** * This interface defines the home interface for the CD Bean */ public interface CDHome extends EJBHome { public CD create(Integer id) throws RemoteException, CreateException; public CD findByPrimaryKey (Integer id) throws RemoteException, FinderException; public Collection findByType (String type) throws RemoteException, FinderException; public Collection findAll() throws RemoteException, FinderException; } J2EE Concepts
Writing an Entity Bean-- continued Bean Class : public abstract class CDBean implements EntityBean { public CDBean() {} public void setEntityContext( EntityContext entityContext ) throws EJBException {} public void unsetEntityContext() throws EJBException {} public void ejbRemove() throws RemoveException, EJBException {} public void ejbActivate() throws EJBException {} public void ejbPassivate() throws EJBException{} public void ejbLoad() throws EJBException{} public void ejbStore() throws EJBException{} public abstract Integer getId(); public abstract void setId( Integer id ); public abstract String getTitle(); public abstract void setTitle( String title ); public abstract String getType(); public abstract void setType( String type ); } J2EE Concepts
Writing an Entity Bean-- continued Deployment Descriptor (ejb-jar.xml) <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <display-name></display-name> <enterprise-beans> <entity> <ejb-name>CDEJB</ejb-name> <home>CDHome</home> <remote>CD</remote> <ejb-class>CDBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.Integer</prim-key-class> <reentrant>False</reentrant> <cmp-version>2.x</cmp-version> J2EE Concepts
Writing an Entity Bean-- continued <cmp-field> <field-name>id</field-name> </cmp-field> <cmp-field> <field-name>title</field-name> </cmp-field> <cmp-field> <field-name>type</field-name> </cmp-field> <cmp-field> <field-name>artist</field-name> </cmp-field> <primkey-field>id</primkey-field> </entity> </enterprise-beans> </ejb-jar> J2EE Concepts
Writing an Entity Bean-- continued Jboss.xml <?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-beans> <entity> <ejb-name>CDBean</ejb-name> <jndi-name>cd/CD</jndi-name> </entity> </enterprise-beans> </jboss> J2EE Concepts
Writing an Entity Bean-- continued JAWS – Just Another Web Store • JAWS is the O/R mapper used by JBoss to manage CMP entity beans. • JAWS is configured in a file named standardjaws.xml, located in the conf/config-name directory in the JBoss distribution. This file configures JAWS for all JBoss. What can you do with JAWS.xml ? • Specify a datasource and the type-mappings to use with it • Set a bunch of options concerning jaws behavior • Specify how JAWS should build/use your tables • Define finders to access you entity beans • Define a type mapping J2EE Concepts
JAWS <jaws> <datasource>java:/MySqlDS</datasource> <type-mapping>mySQL</type-mapping> <enterprise-beans> <entity> <ejb-name>CDBean</ejb-name> <table-name>CLASS</table-name> <create-table>false</create-table> <cmp-field> <field-name>id</field-name> <column-name>ID</column-name> </cmp-field> </entity> </enterprise-beans> <jaws> J2EE Concepts
JAWS <jaws> <enterprise-beans> <entity> <ejb-name>ClassBean</ejb-name> ... <cmp-field> <field-name>title</field-name> <column-name>Title</column-name> <jdbc-type>VARCHAR</jdbc-type> <sql-type>VARCHAR(100)</sql-type> </cmp-field> ... </entity> </enterprise-beans> </jaws> J2EE Concepts