Post on 27-Mar-2015
DCopyright © 2005, Oracle. All rights reserved.
BMP Entity EJBsJ2EE Connector Architecture
D-2 Copyright © 2005, Oracle. All rights reserved.
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
D-3 Copyright © 2005, Oracle. All rights reserved.
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; }
D-4 Copyright © 2005, Oracle. All rights reserved.
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;
}
D-6 Copyright © 2005, Oracle. All rights reserved.
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; }
}
D-7 Copyright © 2005, Oracle. All rights reserved.
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); }
}
D-8 Copyright © 2005, Oracle. All rights reserved.
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"); }
...
D-9 Copyright © 2005, Oracle. All rights reserved.
Bean Class: JobsBMPBean
...public JobsBMPPK ejbCreate(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);
}...
D-10 Copyright © 2005, Oracle. All rights reserved.
create() and ejbCreate()
Client
Home object
EJBobject
Entity Beaninstance
Table in database
create()
EJB Object
ejbCreate()
primary key
EJB Container
1 2
345
D-11 Copyright © 2005, Oracle. All rights reserved.
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; }
D-12 Copyright © 2005, Oracle. All rights reserved.
Bean Class: JobsBMPBean
public void ejbActivate() { }public void ejbPassivate() { }public void setEntityContext(EntityContext ctx)
{ this.context = ctx; }public void unsetEntityContext()
{ 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(); } }
D-13 Copyright © 2005, Oracle. All rights reserved.
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"); } ...
}
D-14 Copyright © 2005, Oracle. All rights reserved.
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();
} ... }
D-15 Copyright © 2005, Oracle. All rights reserved.
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; }
D-16 Copyright © 2005, Oracle. All rights reserved.
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(); }
D-17 Copyright © 2005, Oracle. All rights reserved.
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) {...} }
D-18 Copyright © 2005, Oracle. All rights reserved.
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"); }
D-19 Copyright © 2005, Oracle. All rights reserved.
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; }}
D-20 Copyright © 2005, Oracle. All rights reserved.
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> ...
D-21 Copyright © 2005, Oracle. All rights reserved.
... </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>
Deployment Descriptor
D-22 Copyright © 2005, Oracle. All rights reserved.
<?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>
Creating Data Source data-sources.xml
D-24 Copyright © 2005, Oracle. All rights reserved.
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);
...
D-25 Copyright © 2005, Oracle. All rights reserved.
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) {...} }
...
D-26 Copyright © 2005, Oracle. All rights reserved.
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.
D-27 Copyright © 2005, Oracle. All rights reserved.
OC4J J2EE Connector Architecture
Basic J2EE Connector Architecture:
OC4J
J2EE ApplicationComponent
ResourceAdapter
Enterprise Information
System
ApplicationContract
SystemContracts
(Quality ofService)
D-28 Copyright © 2005, Oracle. All rights reserved.
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
D-29 Copyright © 2005, Oracle. All rights reserved.
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.
D-30 Copyright © 2005, Oracle. All rights reserved.
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)
D-31 Copyright © 2005, Oracle. All rights reserved.
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.
D-32 Copyright © 2005, Oracle. All rights reserved.
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.)