1 / 30

BMP Entity EJBs J2EE Connector Architecture

Learn how to create a BMP entity bean with remote interface, home interface, primary key class, and exception class. Dive into the deployment descriptor and create a client for the JobsBMP bean in this comprehensive EJB lesson.

mhelmer
Download Presentation

BMP Entity EJBs J2EE Connector Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BMP Entity EJBsJ2EE Connector Architecture

  2. BMP Bean: Example • The example in this lesson creates a BMP entity bean with the following components: • Remote interface: JobsBMP • Home interface: JobsBMPHome • Bean class: JobsBMPBean • Primary key class: JobsBMPPK • Exception class: JobSalException • Deployment descriptor:ejb-jar.xml • Client for the JobsBMP bean:JobsBMPClient

  3. Remote Interface: JobsBMP ... public interface JobsBMP extends EJBObject { void incrMinSal(double amt) throws JobSalException, RemoteException; void incrMaxSal(double amt) throws RemoteException; String getJobTitle()throws RemoteException; void setJobTitle(String title)throws RemoteException; double getMinSal()throws RemoteException; void setMinSal(double amt)throws RemoteException; double getMaxSal()throws RemoteException; void setMaxSal(double amt)throws RemoteException; }

  4. Home Interface: JobsBMPHome ... public interface JobsBMPHome extends EJBHome { JobsBMP create() throws RemoteException, CreateException; JobsBMP create(String id, String title, double minSal, double maxSal) throws CreateException,RemoteException;JobsBMP findByPrimaryKey(JobsBMPPK primKey) throws FinderException, RemoteException;Collection findByMaxSalLimit (double salLimit) throws FinderException, RemoteException;double getAvgMaxSal() throws JobSalException, RemoteException; }

  5. Primary Key Class: JobsBMPPK import java.io.Serializable; public class JobsBMPPK implements Serializable { public String jobId;public JobsBMPPK(String id) { this.jobId = id; }public boolean equals(Object job) {...}public int hashCode() { return super.hashCode(); }public String toString() { return jobId; } }

  6. User-Defined Exception: JobSalException public class JobSalException extends Exception { public JobSalException(){ super(); } public JobSalException(Exception e) { super(e.toString()); } public JobSalException(String s) { super(s); } }

  7. Bean Class: JobsBMPBean ... public class JobsBMPBean implements EntityBean { public String id; public String jobTitle; public double maxSal; public double minSal; private Connection conn = null; private EntityContext context; private PreparedStatement ps = null; private ResultSet rset = null; public JobsBMPBean() { System.out.println("New bean instance created"); } ...

  8. Bean Class: JobsBMPBean ... public JobsBMPPKejbCreate(String id, String title, double minSal, double maxSal) { try { this.id = id; this.jobTitle = title; this.minSal = minSal; this.maxSal = maxSal; conn = getConnection(); ps = conn.prepareStatement("INSERT INTO jobs VALUES(?,?,?,?)"); ps.setString(1, id); ps.setString(2, jobTitle); ps.setDouble(3, minSal); ps.setDouble(4, maxSal); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { closeConnection(); } return new JobsBMPPK(id); } ...

  9. Home object EJBobject create() and ejbCreate() EJB Container 1 2 ejbCreate() create() Entity Beaninstance primary key EJB Object 5 4 3 Client Table in database

  10. Bean Class: JobsBMPBean public void ejbPostCreate(String id, String title, double minSal, double maxSal){ } public JobsBMPPK ejbFindByPrimaryKey(JobsBMPPK primKey) { try { conn = getConnection(); ps = conn.prepareStatement( "SELECT job_id FROM jobs WHERE job_id = ?"); ps.setString(1, primKey.toString()); rset = ps.executeQuery(); if (!rset.next()) { throw new ObjectNotFoundException("no job with job ID " + primKey); } ps.close(); } catch (Exception e) { ... } finally { closeConnection(); } return primKey; }

  11. Bean Class: JobsBMPBean public void ejbActivate() { }public void ejbPassivate() { }public void setEntityContext(EntityContext ctx) { this.context = ctx; }public voidunsetEntityContext() { this.context = null; } public void ejbRemove(){ JobsBMPPK jobId = (JobsBMPPK)context.getPrimaryKey();try { conn = getConnection(); ps = conn.prepareStatement("DELETE FROM jobs WHERE job_id = ?"); ps.setString(1, jobId.toString()); ps.executeUpdate(); } catch (Exception e1) {...}finally { closeConnection(); } }

  12. Bean Class: JobsBMPBean public void ejbLoad() { JobsBMPPK key=(JobsBMPPK)context.getPrimaryKey();this.id = key.jobId;try { conn = getConnection(); ps = conn.prepareStatement( "SELECT job_title,min_salary, max_salary " + "FROM jobs WHERE job_id = ? "); ps.setString(1, id); rset = ps.executeQuery(); rset.next(); jobTitle = rset.getString("job_title"); minSal = rset.getDouble("min_salary"); maxSal = rset.getDouble("max_salary"); } ... }

  13. Bean Class: JobsBMPBean public void ejbStore() { JobsBMPPK key= (JobsBMPPK)context.getPrimaryKey(); String id = key.jobId; try { conn = getConnection(); ps = conn.prepareStatement( "UPDATE jobs SET job_title=?, min_salary=?, max_salary=? WHERE job_id = ?"); ps.setString(1, jobTitle); ps.setDouble(2, minSal); ps.setDouble(3, maxSal); ps.setString(4, id); ps.executeUpdate(); } ... }

  14. Bean Class: JobsBMPBean • public void incrMinSal(double amt) • throws JobSalException { if ((minSal + amt) > maxSal) { throw new JobSalException ("You cannot increase min salary to be more than " + maxSal);} else { minSal += amt; } } public void incrMaxSal(double amt) { maxSal += amt; } • public String getJobTitle() { return jobTitle; } • public void setJobTitle(String title) { this.jobTitle = title; } • public double getMinSal() { return minSal; }

  15. Bean Class: JobsBMPBean public void setMinSal(double amt) { this.minSal = minSal; } public double getMaxSal() { return maxSal; } public void setMaxSal(double amt) { this.maxSal = maxSal; } private Connection getConnection() throws SQLException { DataSource ds=null; try { Context ctx = new InitialContext(); ds=(DataSource)ctx.lookup("java:comp/env/jdbc/hrDS"); } catch (NamingException e) { System.out.println("Could not get connection"); e.printStackTrace(); throw new SQLException(e.getMessage()); } return ds.getConnection(); }

  16. Bean Class: JobsBMPBean private void closeConnection () { try { if (rset != null) rset.close();} catch (Exception e) {...} try { if (ps != null) ps.close();} catch (Exception e) {...} try { if (conn != null) conn.close(); } catch (Exception e) {...} }

  17. Bean Class: JobsBMPBean public double ejbHomeGetAvgMaxSal() throws JobSalException { try { conn = getConnection(); ps = conn.prepareStatement ("SELECT AVG(max_salary) as AvgMax FROM jobs"); rset = ps.executeQuery(); • if (rset.next()) return rset.getDouble("AvgMax"); } • catch (Exception e) { • e.printStackTrace(); • throw new JobSalException(); } • finally { closeConnection(); } • throw new JobSalException ("Error in the method"); • }

  18. Bean Class: JobsBMPBean • public Collection ejbFindByMaxSalLimit(double salLimit) { Vector v = null; try { v = new Vector(); conn = getConnection(); ps = conn.prepareStatement ("SELECT job_id FROM jobs WHERE max_salary > ? "); ps.setDouble(1,salLimit); rset = ps.executeQuery(); while (rset.next()) { String id = rset.getString("job_id"); v.addElement(new JobsBMPPK(id)); } } catch (Exception e) {...} finally { closeConnection(); } return v; } }

  19. Deployment Descriptor ... <ejb-jar> <enterprise-beans> <entity> <ejb-name>JobsBMP</ejb-name> <home> demos.JobsBMPHome</home> <remote>demos.JobsBMP</remote> <ejb-class>demos.JobsBMPBean</ejb-class> <persistence-type>Bean</persistence-type> <prim-key-class>demos.JobsBMPPK </prim-key-class> <reentrant>False</reentrant> <resource-ref> <res-ref-name>jdbc/hrDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Application</res-auth></resource-ref></entity> ...

  20. Deployment Descriptor ... </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>JobsBMP</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>

  21. Creating Data Source data-sources.xml <?xml version="1.0" standalone='yes'?> • ... • <data-sources><data-source • class="com.evermind.sql.DriverManagerDataSource" • name="OracleDS" • location="jdbc/OracleCoreDS" • xa-location="jdbc/xa/OracleXADS" • ejb-location="jdbc/hrDS"connection-driver="oracle.jdbc.driver.OracleDriver" • url="jdbc:oracle:thin:@localhost:1521:orcl" • username="hr" • password="hr" • inactivity-timeout="30" • /> • </data-sources>

  22. Client Class: JobsBMPClient import javax.ejb.*; import java.rmi.RemoteException; import java.sql.*; import java.util.*; import javax.naming.*; public class JobsBMPClient { public static void main(String[] args) { JobsBMP jobs = null; try { Context context = getInitialContext(); JobsBMPHome jobsBMPHome = (JobsBMPHome) PortableRemoteObject.narrow (context.lookup("JobsBMP"),JobsBMPHome.class); JobsBMP jobsBMP; jobsBMP = jobsBMPHome.create("job_dev", "Bean Developer", 3000, 5000); ...

  23. Client Class: JobsBMPClient ... jobsBMP.incrMinSal( 200.0 ); System.out.println ("min_salary after incrementing " + jobsBMP.getMinSal( )); jobsBMP.incrMaxSal( 600.0 ); System.out.println ("max_salary after incrementing " + jobsBMP.getMaxSal( )); System.out.println("printing job_title "+ jobsBMP.getJobTitle( )); Collection col=jobsBMPHome.findByMaxSalLimit(15000); Iterator it = col.iterator(); while (it.hasNext()) {JobsBMP jobSals = (JobsBMP)it.next();System.out.println(jobSals.getMaxSal()); } }catch(Throwable ex) {...} } ...

  24. Overview of J2EE Connector Architecture • The J2EE Connector Architecture (JCA) enables J2EE components to interact with Enterprise Information Systems (EISs) such as: • Enterprise resource planning (ERP) • Mainframe transaction processing • Databases and nonrelational systems, and so on • JCA simplifies the integration of diverse EISs. Adherence to the JCA specification makes an EIS implementation portable across compliant J2EE servers.

  25. OC4J J2EE Connector Architecture • Basic J2EE Connector Architecture: ApplicationContract OC4J ResourceAdapter SystemContracts (Quality ofService) Enterprise Information System J2EE ApplicationComponent

  26. What Is a Resource Adapter? • It is a driver used by a client application to connect to a specific EIS. • OC4J provides support for two J2EE 1.3 types: • A stand-alone resource adapter, which is found in a Resource Adapter Archive (RAR) file for use by all deployed applications • An embedded resource adapter, which is bundled in an EAR file for a specific enterprise application • Examples of resource adapters: • JDBC or SQL for Java (SQLJ) drivers for database connections • ERP adapter to connector a specific ERP

  27. Resource Adapter Deployment Descriptors • OC4J provides the following deployment descriptors for resource adapters: • ra.xml: A standard J2EE deployment descriptor for developing an adapter • oc4j-ra.xml: Contains deployment configurations for deploying an adapter to OC4J • oc4j-connectors.xml • One oc4j-connectors.xml file: Contains a list of stand-alone resource adapters deployed. It is located in the ORACLEAS_HOME/j2ee/home/config directory.

  28. Deploying Stand-Alone Resource Adapters • When deploying stand-alone adapters: • Give the resource adapter a unique name, to simplify future operations such as removal of a deployed resource adapter • Deploy in one of the following two ways: • Using the Admin command-line tool (admin.jar) • Manually through directory manipulation, by expanding the .rar file contents into a user-specified directory name <connector-name> below the ORACLEAS_HOME/j2ee/home/<connector directory> (which defaults to ORACLEAS_HOME/j2ee/home/connectors)

  29. Deploying Embedded Resource Adapters • Embedded resource adapters are packaged and deployed inside an Enterprise Application Archive (EAR) file. • Each application (from an EAR file) has one oc4j-connectors.xml file. • The application oc4j-connectors.xml file lists all the resource adapters deployed in the EAR file. • The oc4j-connectors.xml file is automatically generated if not already included in the EAR file.

  30. Common Client Interface (CCI) API • CCI is defined by the J2EE Connector Architecture specification as: • A set of interfaces and classes to allow a J2EE component to perform data access operations with an EIS • Main interfaces and classes are: • ConnectionFactory • Connection • Interaction • RecordFactory • Record (Use Sun’s Java J2EE Tutorial for more information.)

More Related