Enterprise Java Beans II
Transcript of Enterprise Java Beans II
-
8/14/2019 Enterprise Java Beans II
1/25
Enterprise Java Beans II
CS-422
Steflik
-
8/14/2019 Enterprise Java Beans II
2/25
EJB Development
The EJB Specification define six roles that are involved in the
development and deployment of EJB applications
Enterprise Bean Provider
Application Assembler
Deployer
System Administrator
EJB Server Provider
EJB Container Provider
-
8/14/2019 Enterprise Java Beans II
3/25
Enterprise Bean Provider
A programmer type, who codes the EJB
functions done by the Enterprise Bean Provider
write a home interface for the bean
write a component interface for the bean that declares various methods
required by the application
write an implementation class that implements the various business
methods declared in the remote interface
write a deployment descriptor describing the resources required by the
bean and various other parameters Package the interfaces and implementation class along with the
deployment descriptor into an EJB JAR file
-
8/14/2019 Enterprise Java Beans II
4/25
Application Assembler
A person with the domain knowledge and expertise to be able to
gather together the beans written by several Bean Providers (including
vendors) and by modifying the deployment descriptors assemble them
into an applications that will represent a complete business solution. This assembly activity is usually done via GUI based tools provided
by the application server OEM.
The assembler should be able to understand the various interfaces
declared by the Bean Providers
Into pulling together the various Beans the assembler will allso collect
together other components, such as JSP, HTML and graphics files.
-
8/14/2019 Enterprise Java Beans II
5/25
The Deployer
The deployer takes an assembled application and deployes it to a
specific environment
the deployer need to have the detail knowledge required for the
specific EJB server to be used and be familiar with the developmentprocedures for that server
the deployer must map the resources required for the application onto
the current infrastructure (map data sources on the data bases)
the deployer is also responsible for mapping security requirements
onto the infrastructure (creating userids, password, user groups)
-
8/14/2019 Enterprise Java Beans II
6/25
System Administrator
The System Administrator is responsible for the overall operational
well being of the installation
Insure the availability of the application at all times
Configure and administer the entire installation
Support the networking infrastructure at the installation
Create groups and users for the system to be used by the deployer while
deploying the application
Monitor the application performance using tools provided by the server
provider Fine-tune the use of various resources required by the application
-
8/14/2019 Enterprise Java Beans II
7/25
EJB Server Provider
A system level expert with expertise in providing low-level support
services for distributed object applications and distributed transaction
management.
Typically an OS vendor or middleware vendor
can be assumed thast the same party plays the role of the EJB
Container Provider
-
8/14/2019 Enterprise Java Beans II
8/25
EJB Container Provider
A system level expert that implements a secure, transaction enabled,
and scaleable container that can be integrated with an EJB Server.
The container provider is responsible for:
run-time environment for the deployed EJBs
deployment Tool for deploying EJBs on the container
-
8/14/2019 Enterprise Java Beans II
9/25
Developing an Enterprise Bean
At it simplest an EJB consists of four elements:
a home interface
a component interface
a bean implementation class
a deployment descripter
well develop a simple bean that calculates our monthly net
salarybased on annual salary,pension contributions and income tax.
It will be a stateless session bean
-
8/14/2019 Enterprise Java Beans II
10/25
Developing the Home Interface
The Home Interface is responsible for managing the life-cycleoperations upon a bean; creating, removing and locating the bean. The
Home interface is the first point of contact that a client has with a bean
A client will obtain a reference to the the beans Home Interface via
JNDI. You will see that each bean is registered in a JNDI server that
can be used at run-time to get a reference to the beans Home Interface Once a client has a reference to the beans home interface it can
perform operations on the bean through this interface: these operations
are:
create a new instance or find an existing instance (local and remote)
access the EJBMetaData interface (remote only)
get a serializable reference to a bean instance (remote only)
remove the bean instance
execute home business methods (remote entity beans only)
-
8/14/2019 Enterprise Java Beans II
11/25
Basic EJBHomeInterface
package javax.ejb;
import java.rmi.Remote;
import java.rmi.RemoteExecption;
public interface EJBHome extends Remote {
// get a reference to the EJBMetaData object; can later be used to retrieve data about the bean
public abstract EJBMetaData getEJBMetaData( ) throws RemoteExecption;
// get a Handle to the home object; basically a persistent reference to the object
public abstract HomeHandle getHomeHandle( ) throws RemoteExecption;
// remove the bean instance ; in the case of en entity bean this is like deleting a row from the data store
public abstract void remove (Object obj) throws RemoteExecption, RemoveExecption;
public abstract void remove (Handle handle) throws RemoteExecption, RemoveExecption;
}
-
8/14/2019 Enterprise Java Beans II
12/25
Basic EJBLocalHome Interface
package javax.ejb;
public interface EJBLocalHome {
public abstract void remove (Object obj ) throws RemoveException, EJBException;}
-
8/14/2019 Enterprise Java Beans II
13/25
the SalaryHome Interface
package simpleBean;
public interface SalaryHome extends javax.ejb.EJBHome{
Salary create() throws java.rmi.RemoteException, javax.ejb.CreateException;
}
-
8/14/2019 Enterprise Java Beans II
14/25
the Component Interface
package javax.ejb;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EJBObject extends Remote {
// get a referenct to the home object
public abstract EJBHome getEJBHome( ) throws RemoteException;
// get a Handle to the home object
public abstract Handle getHandle( ) throws RemoteException;
// for entity beans: get the beans Primary Keypublic abstract Object getPrimaryKey( ) throws RemoteException;
public abstract boolean isIdentical (EJBObject ejbobject) throws RemoteException;
public abstract void remove( ) throws RemoteException, RemoveException;
}
-
8/14/2019 Enterprise Java Beans II
15/25
the Salary Interface
package simpleBean;
public interface Salary extends javax.ejb.EJBObject {
double calculateSalary ( int annualSalary, int pensionContrib, double bonus)
throws hava.rmi.RemoteException;
}
-
8/14/2019 Enterprise Java Beans II
16/25
SalaryEJB Implementationpackage simpleBean;
import javax.ejb.*;
public class SalaryEJB implements SessionBean {
public void ejbCreate( ) { }
public void ejbRemove( ) { }
public void ejbActivate( ) { }
public void ejbPassivate( ) { }
public void Ct( SessionContext ctx ) { }
private static double taxRate = 28;
public double calculateSalary (int annualSalary, int pensionContrib, double bonus) {
double monthly = 0;
monthly = annualSalary/12;
monthly - monthlu - (monthly * (pensionContrib/100));
monthly = monthly - (monthly * (taxrate/100));
return monthly; } }
-
8/14/2019 Enterprise Java Beans II
17/25
Limitations to the EJB Model
Bean must not use read/write static fields bean cant use threads or the Threading API
bean cant use AWT for input or output
bean cannot act as a network server, by listening,accepting or
multicasting on a socket
bean cannot use java.io package
bean cant load a native driver
bean cant use this as an argument or return value
bean cant modify the class loader, stop the JVM, or change the input,
output or error streams
bean cant access or modify security settings
bean must not attempt to define a class in a package
bean must not use the subclass and object substitution features of the
serialization protocol
EJB C t t
-
8/14/2019 Enterprise Java Beans II
18/25
EJB Context In the SalaryBean implementation we had to invoke setSessionContext
there are similar methods required for other bean types
ex. setEntityContext for Entity Beans
each of these methods accepts a subclass of an EJBContext to provide the bean instance with
information about the container - the beans context
The EJBContext interface:
package javax.ejb;
import java.security.Identity;
import java.security.Principle;
import java.util.Properties;
import javax.transaction,UserTransaction;
public interface EJBContext {
public abstract Identy getCallerIdentity( );
public abstract Principal getCallerPrincipal( );
public abstract EJBHome getEJBHome( );
public abstract Properties getEnvironment( );
public abstract boolean getRollbackOnly( ) throws IllegalStateException
public abstract UserTransaction getUserTransaction( ) throws IllegalStateException;
public abstract boolean isCallInRole( String s);
public abstract boolean isCallerInRole (Identity identity);
public abstract void setRollbackOnly( ) throws IllegalStateException }
-
8/14/2019 Enterprise Java Beans II
19/25
EJBContext Interface
Additionally SessionContext and EntityContext (but not
MessageDrivenContext) define:
public abstract EJBLocalObject getEJBLocalObject( ) throws IllegalStateException;
public abstract EJBObject getEJBObject( ) throws IllegalStateException;
-
8/14/2019 Enterprise Java Beans II
20/25
Deployment Descriptors
In the original EJB Specification the deployment descriptor was written as a Java object, in the
EJB 1.1 Specification that was changed to be an XML Document.
DTD Element Optional Purpose
the root element of the deployment descriptor, all other
elements are children of this
Yes textual description of the parent element, many other
elements can have their own description elements
Yes a name that the deployment tool can use to display the name
Yes relative path to a small (16x16) jpeg or GIF to be used by
deployment tools
Yes relative path to a large (32x32) jpeg or GIF to be used by
deployment tools
The parent element that contains the declarations for allbeans within this application
Yes the parent element that contains application assembly
instructions for the EJB application (includes; transaction
attributes, method permissions, security roles and excluded
methods)
Yes a JAR file that contains the classes necessary for a client to
access the bean
A l
-
8/14/2019 Enterprise Java Beans II
21/25
An EJB Deployment Descriptor
Single stateless bean to calculate Monthly Salary
Simple Bean
Simple Salary Bean
-
8/14/2019 Enterprise Java Beans II
22/25
A Salary Client
import javax.ejb.*;
import simpleBean.*
import javax.naming.InitialContext;
class SalaryClient {
public static void main (String[ ] args) {
try {
InitialContext ctx = new InitialContext( );
Object objRef = ctx.lookup(Salary);
SalaryHome home = (SalaryHome) javax.rmi.PortableRemoteObject.narrow(objRef, SalaryHome.class);
Salary bean = home.create( );
System.out.println ( Monthly net salary = + bean.calculateSalary(28000, 2, 500));
} catch (javax.namingException ne) { System.out.println( naming exception caught) }
catch (javax.ejb.CreateException) { System.out.println( EJC Ceration Exception caught); }
catch (java.rmi.RemoteExecption) { System.out.println(rmi RemoteExceprion caught); }
}
}
-
8/14/2019 Enterprise Java Beans II
23/25
Entity Beans ( ver 1.1)
Entity beans represent data in a datastore as a Java object, this usually
means that the bean represents a row in a relational table.
Since the bean and the database record are synonymous, any change in
the bean must be synchronized with the database record; this is calledpersistence
there are two types of Entity beans, they are differentiated by how they
handle persistence
Container-Managed Persistence (CMP)
the synchronization (persistence) is handled automatically by the container
Bean-Managed Persistence (BMP)
we manage the synchronization (persistence) in code in the bean and tell the
container to keep its hands off
-
8/14/2019 Enterprise Java Beans II
24/25
CMP or BMP
One of the services offered by the EJB container is to have the
container manage the persistence, at deployment time the container
will generate all of the code needed to synchronize the bean with the
database.
This lets the programmer focus on business logic and not have to be
concerned over persistance
by using CMP the bean becomes more independent of the database, thiscan make them easier to re-deploy using another database
With BMP we must provide all of the SQL to manage the persistence
as part of the bean, its more work but some EJB containers are not
fully compliant with the EJB specification as relates to CMP; so, the
only option may be to use BMP. This may be only option when trying to use legacy databases (IBM/IMS)
that are not SQL oriented
to get finer graind control than what CMP provides
-
8/14/2019 Enterprise Java Beans II
25/25
Primary Keys
The identity of an entity bean is its primary key
primary key is represented by a primary key class that is defined by
the developer, this class contains everthing necessary to find the entity
in the datastore
for BMP the primary key class must the primary key can be any legal value type in RMI/IIOP, must be
serializable
must provide suitable implementations of the hashCode( ) and equals( )
methods
must have a unique value within the set of all beans of a particular type
for CMP these additional rules apply
key class must have a no-arguments public constructor
must be able to map the beans state to the primary key and vice-versa