technische universität dortmund service computing service computing prof. dr. ramin yahyapour it...
TRANSCRIPT
technische universität dortmund
Service Computing Prof. Dr. Ramin Yahyapour
IT & Medien Centrum12. November 2009
technische universität dortmund
EJB
technische universität dortmund
3
EJB CartBean Example (1)import java.util.*;import javax.ejb.*;
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(); }
public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {}
…
technische universität dortmund
4
EJB CartBean Example (2)
… public void addBook(String title) { contents.addElement(title); }
public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + "not in cart."); } }
public Vector getContents() { return contents; }
public CartBean() {}
}
technische universität dortmund
5
CartBean –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;
}
technische universität dortmund
6
CartBean –Remote Interface
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;}
technische universität dortmund
7
Savings Account -Database Definition
CREATE TABLE savingsaccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));
technische universität dortmund
8
Savings Account –ejbCreate
public String ejbCreate(String id, String firstName, String lastName, BigDecimal balance) throws CreateException {
if (balance.signum() == -1) { throw new CreateException ("A negative initial balance is not allowed."); }
try { insertRow(id, firstName, lastName, balance); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); }
this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance;
return id;}
technische universität dortmund
9
Savings Account –ejbRemove
public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } }}
technische universität dortmund
10
Savings Account –ejbLoad/ejbStore
public void ejbLoad() {
try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); }}
public void ejbStore() {
try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); }}
technische universität dortmund
11
Savings Account –Finder Methods
SavingsAccount jones = home.findByPrimaryKey("836");...Collection c = home.findByLastName("Smith");...Collection c = home.findInRange(20.00, 99.00);
public Collection ejbFindByLastName(String lastName) throws FinderException {
Collection result;
try { result = selectByLastName(lastName); } catch (Exception ex) { throw new EJBException("ejbFindByLastName " + ex.getMessage()); } return result;}
For every finder method available to a client, the entity bean class must implement a corresponding method that begins with the prefix ejbFind. The SavingsAccountBean class, for example, implements the ejbFindByLastName method as follows:
technische universität dortmund
12
Savings Account –Finder Methods (2)
public String ejbFindByPrimaryKey(String primaryKey) throws FinderException {
boolean result;
try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); }
if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); }}
the ejbFindByPrimaryKey method is required other Finder methods are optional
technische universität dortmund
13Winter 2009/10
Lifecycle of a Stateful Session Bean
technische universität dortmund
14Winter 2009/10
Lifecycle of a Stateless Session Bean
technische universität dortmund
15Winter 2009/10
Lifecycle of a Message-Driven Bean
technische universität dortmund
16Winter 2009/10
Lifecycle of an Entity Bean
technische universität dortmund
17Winter 2009/10
Java Application Server
technische universität dortmund
18
Deployment Descriptor
<?xml version="1.0" encoding="UTF-8"?><ejb-jar>
<description>JBoss Hello World Application</description><display-name>Hello World EJB</display-name><enterprise-beans> <session>
<ejb-name>HelloWorld</ejb-name><home>com.mastertech.sample.HelloWorldHome</home><remote>com.mastertech.sample.HelloWorld</remote><ejb-class>com.mastertech.sample.HelloWorldBean</ejb-
class><session-type>Stateless</session-type><transaction-type>Bean</transaction-type>
</session></enterprise-beans>
</ejb-jar>
technische universität dortmund
19
Deployment Descriptorwith Query Definition
<query> <query-method> <method-name>findByName</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </query-method> <ejb-ql> select object(p) From CEntityBean2 as p where p.prof = ?1 </ejb-ql></query>
technische universität dortmund
20Winter 2009/10
Message Driven-Beans
technische universität dortmund
21Winter 2009/10
JNDI Architecture
technische universität dortmund
22
JNDI Name Service Example
try { // Create the initial context Context ctx = new InitialContext(env);
// Look up an object Object obj = ctx.lookup(name);
// Print it System.out.println(name + " is bound to: " + obj);
} catch (NamingException e) { System.err.println("Problem looking up " + name + ": " + e);}
technische universität dortmund
23
JNDI Directory Service Example
try {
// Create the initial directory context DirContext ctx = new InitialDirContext(env);
// Ask for all attributes of the object Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People");
// Find the surname attribute ("sn") and print it System.out.println("sn: " + attrs.get("sn").get());
} catch (NamingException e) { System.err.println("Problem getting attribute:" + e);}
technische universität dortmund
24
JNDI Create Context
{ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
Context ctx = new InitialContext(env); …}
{ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL,
"ldap://localhost:389/o=JNDITutorial"); DirContext ctx = new InitialDirContext(env); …}