technische universität dortmund service computing service computing prof. dr. ramin yahyapour it...

24
technische universität dortmund Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

Upload: luke-hoffman

Post on 28-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

Service Computing Prof. Dr. Ramin Yahyapour

IT & Medien Centrum12. November 2009

Page 2: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

EJB

Page 3: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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) {}

Page 4: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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() {}

}

Page 5: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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;

}

Page 6: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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;}

Page 7: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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));

Page 8: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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;}

Page 9: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

9

Savings Account –ejbRemove

public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } }}

Page 10: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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()); }}

Page 11: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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:

Page 12: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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

Page 13: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

13Winter 2009/10

Lifecycle of a Stateful Session Bean

Page 14: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

14Winter 2009/10

Lifecycle of a Stateless Session Bean

Page 15: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

15Winter 2009/10

Lifecycle of a Message-Driven Bean

Page 16: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

16Winter 2009/10

Lifecycle of an Entity Bean

Page 17: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

17Winter 2009/10

Java Application Server

Page 18: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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>

Page 19: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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>

Page 20: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

20Winter 2009/10

Message Driven-Beans

Page 21: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

technische universität dortmund

21Winter 2009/10

JNDI Architecture

Page 22: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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);}

Page 23: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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);}

Page 24: Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009

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); …}