Download presentation
Presentation is loading. Please wait.
Published byLuke Hoffman Modified over 10 years ago
1
technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009
2
technische universität dortmund EJB
3
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) {} …
4
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() {} }
5
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; }
6
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; }
7
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));
8
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; }
9
technische universität dortmund 9 Savings Account – ejbRemove public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); }
10
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()); }
11
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:
12
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
13
technische universität dortmund 13 Winter 2009/10 Lifecycle of a Stateful Session Bean
14
technische universität dortmund 14 Winter 2009/10 Lifecycle of a Stateless Session Bean
15
technische universität dortmund 15 Winter 2009/10 Lifecycle of a Message-Driven Bean
16
technische universität dortmund 16 Winter 2009/10 Lifecycle of an Entity Bean
17
technische universität dortmund 17 Winter 2009/10 Java Application Server
18
technische universität dortmund 18 Deployment Descriptor JBoss Hello World Application Hello World EJB HelloWorld com.mastertech.sample.HelloWorldHome com.mastertech.sample.HelloWorld com.mastertech.sample.HelloWorldBean Stateless Bean
19
technische universität dortmund 19 Deployment Descriptor with Query Definition findByName java.lang.String select object(p) From CEntityBean2 as p where p.prof = ?1
20
technische universität dortmund 20 Winter 2009/10 Message Driven-Beans
21
technische universität dortmund 21 Winter 2009/10 JNDI Architecture
22
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); }
23
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); }
24
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); … }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.