Chapter 22: Development and Deployment Roles

Download Report

Transcript Chapter 22: Development and Deployment Roles

Development and Deployment Roles
Venugopal Pakanati
• What is EJB technology ?
• What is an EJ bean ?
• Types of Beans
• Entity Bean
• Session Bean
EJB Architecture :
EJB Server
EJB Container
Locate, Create, Remove
instances of EJB
EJB
Client
Invoke business methods
of EJB
Home
Interface
Remote
Interface
EJB
Databases
5 Roles in the application development and deployment life cycle
1. Enterprise Java Bean providers
2. Application Assemblers
3. Deployers
4. System Adminstrators
5. Application Server Vendors
Development and Deployment Scenario
EJB Provider
Application
Assembler
Deployer
Application
Server Vendor
System
Administrator
1. The Enterprise Java Bean Provider
• referred to as an ‘application domain expert’.
• needs to be an expert in the business logic.
• needs to be able to express business logic using Java code.
• does not need to be an expert in system-level programming
• do not need to understand the details of the target environment
The enterprise bean provider produces --- a JAR containing :
(i) The bean’s home interface
(ii) The bean’s remote interface
(iii) The bean’s implementation class
(iv) The bean’s primary key class, in case of an entity bean
(v) A partially-complete deployment descriptor
Example: Banking Account
Entity Bean
members: accountID
customerName
customerType
accountBalance
Bean Name: Account
Home Interface: AccountHome
HI methods: create( );
findbyPrimaryKey( );
Remote Interface: Account
RI methods: withdraw( );
deposit( );
getCustomerType( );
Implementation class: AccountEJB
Session Bean
Bean Name: AccountManager
Home Interface: AccountManagerHome
HI methods: create( );
Remote Interface: AccountManager
RI methods: createAccount( );
withdraw( );
deposit( );
cancel( );
Implementation class: AccountManagerEJB
Security Roles: UnmediatedAccess (Web, ATM) ----- deposit( ), withdraw( )
Teller
----- deposit( ), withdraw( )
Manager
----- All methods
Of
session
bean
(i) The Bean’s Home Interface
(a) Home Interface for the Entity Bean
(b) Home Interface for the Session Bean
(a) Home interface for the entity bean :
package wrox.some_isv ;
import javax.ejb.* ;
import java.rmi.RemoteException ;
public interface AccountHome extends EJBHome {
public Account create (int accountID, String customerName,
String customerType, double initialBalance)
throws CreateException, RemoteException ;
public Account findByPrimaryKey (Integer accountID)
throws FinderException, RemoteException ;
}
(b) Home Interface for the Session Bean :
package wrox.some_isv ;
import javax.ejb.* ;
import java.rmi.RemoteException ;
public interface AccountManagerHome extends EJBHome {
AccountManager create ( )
throws CreateException, RemoteException ;
}
(ii) The Bean’s Remote Interface
(a) Remote Interface for the Entity Bean
(b) Remote Interface for the Session Bean
(a) Remote Interface for the Entity Bean :
package wrox.some_isv ;
import javax.ejb.* ;
import java.rmi.RemoteException ;
public interface Account extends EJBObject {
void withdraw (double amount)
throws InsufficientFundsException, RemoteException ;
void deposit (double amount) throws RemoteException ;
String getCustomerType( ) throws RemoteException ;
}
(b) Remote Interface for the Session Bean :
package wrox.some_isv ;
import javax.ejb.* ;
import java.rmi.RemoteException ;
public interface AccountManager extends EJBObject {
void createAccount (int accountID, String customerName,
String customerType, double initialBalance)
throws NoAccountCreatedException, RemoteException ;
void withdraw (int accountID, double amount)
throws InsufficientFundsException, NoSuchAccountException,
RemoteException ;
void deposit (int accountID, double amount)
throws NoSuchAccountException, RemoteException ;
public void cancel (int accountID) throws RemoteException ;
}
(iii) Bean’s Implementation Class :
Session Bean implementation class :
package wrox.some_isv;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.RemoteException;
public class AccountManagerEJB implements SessionBean {
public SessionContext ctx;
public void createAccount(int accountID, String customerName,
String customerType, double initialBalance)
throws NoAccountCreatedException {
try {
AccountHome accountHome = getAccountHome();
accountHome.create(accountID, customerName, customerType, initialBalance);
} catch (CreateException ce) {
throw new NoAccountCreatedException(ce.getMessage());
} catch (RemoteException re) {
throw new EJBException(re);
}
}
public void withdraw(int accountID, double amount)
throws InsufficientFundsException, NoSuchAccountException {
try {
Account account = getAccount(accountID);
if ((amount > 250)
&& account.getCustomerType().equals(CustomerTypes.INDIVIDUAL)
&& ctx.isCallerInRole("ATM")) {
throw new SecurityException();
}
account.withdraw(amount);
} catch (RemoteException re) {
throw new EJBException(re);
}
}
public void deposit(int accountID,
double amount) throws NoSuchAccountException {
try {
Account account = getAccount(accountID);
account.deposit(amount);
} catch (RemoteException re) {
throw new EJBException(re);
}
}
public void cancel(int accountID) {
try {
Account account = getAccount(accountID);
account.remove();
} catch (NoSuchAccountException nsae) {
} catch (Exception e) {
throw new EJBException(e);
}
}
private Account getAccount(int accountID) throws NoSuchAccountException {
try {
AccountHome home = getAccountHome();
return home.findByPrimaryKey(new Integer(accountID));
} catch (RemoteException re) {
throw new EJBException(re);
} catch (FinderException fe) {
throw new NoSuchAccountException();
}
}
private AccountHome getAccountHome() {
try {
InitialContext initial = new InitialContext();
Object objref = initial.lookup("java:comp/env/ejb/GenericAccount");
AccountHome home =
(AccountHome) javax.rmi.PortableRemoteObject
.narrow(objref, AccountHome.class);
return home;
} catch (NamingException ne) {
throw new EJBException(ne);
}
}
public void ejbCreate() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
}
}
Entity Bean implementation class :
package wrox.some_isv;
import javax.ejb.*;
import javax.naming.*;
public class AccountEJB implements EntityBean {
public Integer accountID;
public String customerName;
public String customerType;
public double accountBalance;
public void withdraw(double amount) throws InsufficientFundsException {
if (accountBalance - amount < 0) {
throw new InsufficientFundsException();
}
accountBalance -= amount;
}
public void deposit(double amount) {
accountBalance += amount;
}
public String getCustomerType() {
return customerType;
}
public Integer ejbCreate(int accountID, String customerName,
String customerType,
double initialBalance) throws CreateException {
if (!customerType.equals(CustomerTypes.CORPORATION)
&&!customerType.equals(CustomerTypes.INDIVIDUAL)) {
throw new CreateException("Unknown customer type.");
}
this.accountID = new Integer(accountID);
this.customerName = customerName;
this.customerType = customerType;
this.accountBalance = initialBalance;
return this.accountID;
}
public void ejbActivate() {}
public void ejbLoad() {}
public void ejbPassivate() {}
public void ejbRemove() {}
}
(v) Deployment Descriptor :
2 types of information contained within a deployment descriptor :
• structural information of the EJBs
(provided by the Bean Developer)
• Application Assembly information
(provided by the Application Assembler)
At this stage, the Deployment Descriptor contains the following
information for each bean :
• Bean’s type: session or entity
• Bean’s name
• Bean’s implementation class
• Bean’s home interface
• Bean’s remote interface
• Whether an entity bean is re-entrant or not
• Whether a session bean is stateful or stateless
• Whether or not a session bean manages its own transactions
• Whether an entity bean uses BMP or CMP
• Bean’s primary key class
• Any references in the code to other EJBs
• Any references in the code to security roles
Indirection :
• to avoid dependencies on a particular security implementation,
a particular database etc.
• is the use of a reference ‘placeholder’ in your Java code or
deployment descriptor, rather than a reference to an actual entity in
the implementation environment
• 3 types of references :
references to other EJBs
references to resources
references to security roles
• At the deployment stage, the deployer creates a map between these
virtual references and the real resources (like database connections)
2. The Application Assembler
• just needs to understand the home and remote interfaces,
and the business logic
• need not understand the implementation of any EJBs
• combines EJBs into a deployable application by
(i) completing the deployment descriptor
(ii) adding other types of application components
(to test the functionality of EJBs)
(i) Completing the deployment descriptor :
Application Assembler
• may define one or more security roles
• may define method permissions
• must link any security role references declared by an EJB to a
security role that they have defined
Final version of our application’s deployment descriptor :
<?xml version="1.0" ?>
<ejb-jar>
// provided by Bean Developer
<enterprise-beans>
<session>
<ejb-name>AccountManager</ejb-name>
<home>wrox.some_isv.AccountManagerHome</home>
<remote>wrox.some_isv.AccountManager</remote>
<ejb-class>wrox.some_isv.AccountManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<ejb-ref>
<ejb-ref-type>Entity</ejb-ref-type>
<ejb-link>Account</ejb-link>
<home>wrox.some_isv.AccountHome</home>
<remote>wrox.some_isv.Account</remote>
</ejb-ref>
<security-role-ref>
<description>This role refers to automated customer withdrawals from the account;
no bank intermediary is involved.</description>
<role-name>ATM</role-name>
<role-link>UnmediatedAccess</role-link>
</security-role-ref>
</session>
<entity>
<ejb-name>Account</ejb-name>
<home>wrox.some_isv.AccountHome</home>
<remote>wrox.some_isv.Account</remote>
<ejb-class>wrox.some_isv.AccountEJB</ejb-class>
<persistence-type>Container</persistence-type>
<reentrant>False</reentrant>
<cmp-field>
<field-name>customerType</field-name>
</cmp-field>
<cmp-field>
<field-name>accountID</field-name>
</cmp-field>
<cmp-field>
<field-name>accountBalance</field-name>
</cmp-field>
<cmp-field>
<field-name>customerName</field-name>
</cmp-field>
<primkey-field>accountID</primkey-field>
</entity>
</enterprise-beans>
// provided by Application Assembler
<assembly-descriptor>
<security-role>
<description>This role is performed by any account access in which a bank employee
is not involved, such as an internet transaction or ATM withdrawal.</description>
<role-name>UnmediatedAccess</role-name>
</security-role>
<security-role>
<description>This role is peformed by any customer-service representative who does not
have account-manager status. They will be able to handle deposits and withdrawals,
but not account management.</description>
<role-name>Teller</role-name>
</security-role>
<security-role>
<description>This role is performed by professionals who are allowed to
manage an account (open, close).</description>
<role-name>Manager</role-name>
</security-role>
<method-permission>
<role-name>Manager</role-name>
<method>
<ejb-name>Account</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>AccountManager</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
<method-permission>
<role-name>Teller</role-name>
<role-name>UnmediatedAccess</role-name>
<method>
<ejb-name>Account</ejb-name>
<method-name>withdraw</method-name>
</method>
<method>
<ejb-name>Account</ejb-name>
<method-name>deposit</method-name>
</method>
<method>
<ejb-name>Account</ejb-name>
<method-name>getCustomerType</method-name>
</method>
<method>
<ejb-name>Account</ejb-name>
<method-name>findByPrimaryKey</method-name>
</method>
<method>
<ejb-name>AccountManager</ejb-name>
<method-name>withdraw</method-name>
</method>
<method>
<ejb-name>AccountManager</ejb-name>
<method-name>deposit</method-name>
</method>
</method-permission>
</assembly-descriptor>
</ejb-jar>
(ii) adding other types of application components :
test client:
Let the security role of this client be “ATM”
package wrox.some_isv;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class TestClient {
public static void main(String[] args) {
try {
InitialContext initial = new InitialContext();
Object objref = initial.lookup("java:comp/env/ejb/AccountAccess");
AccountManagerHome home =
(AccountManagerHome) PortableRemoteObject.narrow(objref,
AccountManagerHome.class);
AccountManager accountManager = home.create();
System.out.println("withdrawing small amount from individual account");
accountManager.withdraw(1, 100.0);
System.out.println("withdrawing large amount from corporate account");
accountManager.withdraw(2, 1000.0);
System.out.println("withdrawing large amount from individual account");
accountManager.withdraw(1, 1000.0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Deployer
• maps the logical references that the bean provider uses onto actual
resources or identities
• need not be a domain expert
• need to be an expert in the environment in which the application
will execute
Mapping the logical references :
(i) map the logical security roles to actual users or groups
(ii) map logical database accesses to an actual database
(iii) map all the beans and references into the JNDI namespace
4. The System Adminstrator
• responsible for configuring the application server, EJB container,
and the environment in which they execute --- including the
database, network, and security systems
• responsible for the ‘well-being’ of the EJBs that are executing
in the container
• must monitor logs for system problems, security problems, etc.
5. Application Server / Container Vendor
• a role that we, as application developers, are unlikely to play
Choosing an Application Server :
• Do you need an ORB-based product to communicate with non-Java
clients or for vendor interoperability ?
• What object/relational mapping capabilities do you require ?
• What kind of development and deployment tools are supported by
a vendor’s application server ?
• What version of EJB specification does the application server
support ?
• What platform(s) does the application server run on ?
Thank You !